-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(LRAPI): add 1inch classic swap rpc #2222
base: dev
Are you sure you want to change the base?
Changes from 17 commits
14e695d
aad3739
5df753b
57192ca
1c31a7a
3610147
23d8f1d
18466fc
116fb0c
5ec045f
7a1282d
28a645d
ef93d1a
e68cfc2
5ab3b19
e040bca
a19063a
d457bee
6a150a5
c175191
257d4b9
ce96d87
65c4853
df5f045
57efd04
7b5bed3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ | |
#[macro_use] extern crate serde_json; | ||
#[macro_use] extern crate ser_error_derive; | ||
|
||
use crate::eth::Web3RpcError; | ||
shamardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use async_trait::async_trait; | ||
use base58::FromBase58Error; | ||
use bip32::ExtendedPrivateKey; | ||
|
@@ -218,10 +219,9 @@ pub mod coins_tests; | |
|
||
pub mod eth; | ||
use eth::eth_swap_v2::{PaymentStatusErr, PrepareTxDataError, ValidatePaymentV2Err}; | ||
use eth::GetValidEthWithdrawAddError; | ||
use eth::{eth_coin_from_conf_and_request, get_eth_address, EthCoin, EthGasDetailsErr, EthTxFeeDetails, | ||
GetEthAddressError, SignedEthTx}; | ||
use ethereum_types::U256; | ||
use eth::{eth_coin_from_conf_and_request, get_eth_address, u256_to_big_decimal, wei_from_big_decimal, EthCoin, | ||
EthGasDetailsErr, EthTxFeeDetails, GetEthAddressError, GetValidEthWithdrawAddError, SignedEthTx}; | ||
use ethereum_types::{Address as EthAddress, U256}; | ||
shamardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
pub mod hd_wallet; | ||
use hd_wallet::{AccountUpdatingError, AddressDerivingError, HDAccountOps, HDAddressId, HDAddressOps, HDCoinAddress, | ||
|
@@ -660,6 +660,10 @@ impl TransactionErr { | |
} | ||
} | ||
|
||
impl std::fmt::Display for TransactionErr { | ||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "{}", self.get_plain_text_format()) } | ||
} | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub enum FoundSwapTxSpend { | ||
Spent(TransactionEnum), | ||
|
@@ -5599,3 +5603,74 @@ pub mod for_tests { | |
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
shamardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct Erc20ApproveRequest { | ||
coin: String, | ||
spender: EthAddress, | ||
amount: BigDecimal, | ||
} | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub struct Erc20AllowanceRequest { | ||
coin: String, | ||
spender: EthAddress, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Display, EnumFromStringify, Serialize, SerializeErrorType)] | ||
#[serde(tag = "error_type", content = "error_data")] | ||
pub enum Erc20CallError { | ||
#[display(fmt = "No such coin {}", coin)] | ||
NoSuchCoin { coin: String }, | ||
#[display(fmt = "Coin not supported {}", coin)] | ||
CoinNotSupported { coin: String }, | ||
#[from_stringify("NumConversError")] | ||
#[display(fmt = "Invalid param: {}", _0)] | ||
InvalidParam(String), | ||
onur-ozkan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[from_stringify("TransactionErr")] | ||
#[display(fmt = "Transaction error {}", _0)] | ||
TransactionError(String), | ||
#[from_stringify("Web3RpcError")] | ||
#[display(fmt = "Web3 RPC error {}", _0)] | ||
Web3RpcError(String), | ||
} | ||
|
||
impl HttpStatusCode for Erc20CallError { | ||
fn status_code(&self) -> StatusCode { | ||
match self { | ||
Erc20CallError::NoSuchCoin { .. } | ||
| Erc20CallError::CoinNotSupported { .. } | ||
| Erc20CallError::InvalidParam(_) | ||
| Erc20CallError::TransactionError(_) | ||
| Erc20CallError::Web3RpcError(_) => StatusCode::BAD_REQUEST, | ||
shamardy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
/// Call allowance method for ERC20 tokens (see https://eips.ethereum.org/EIPS/eip-20#approve). | ||
/// Returns BigDecimal allowance value. | ||
pub async fn allowance_rpc(ctx: MmArc, req: Erc20AllowanceRequest) -> MmResult<BigDecimal, Erc20CallError> { | ||
let eth_coin = find_erc20_eth_coin(&ctx, &req.coin).await?; | ||
let wei = eth_coin.allowance(req.spender).compat().await?; | ||
let amount = u256_to_big_decimal(wei, eth_coin.decimals())?; | ||
Ok(amount) | ||
} | ||
|
||
/// Call approve method for ERC20 coins (see https://eips.ethereum.org/EIPS/eip-20#allowance). | ||
/// Returns approval transaction hash. | ||
pub async fn approve_rpc(ctx: MmArc, req: Erc20ApproveRequest) -> MmResult<BytesJson, Erc20CallError> { | ||
let eth_coin = find_erc20_eth_coin(&ctx, &req.coin).await?; | ||
let amount = wei_from_big_decimal(&req.amount, eth_coin.decimals())?; | ||
let tx = eth_coin.approve(req.spender, amount).compat().await?; | ||
Ok(tx.tx_hash_as_bytes()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would return a result like that
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should include the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not fixed yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed in 65c4853 (But is it convenient for GUI when we return tx hash in different formats for different coins?) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, we should fix those cases (in a different PR of course). tx hashes and addresses should always be displayed/sent to GUIs with |
||
} | ||
|
||
async fn find_erc20_eth_coin(ctx: &MmArc, coin: &str) -> Result<EthCoin, MmError<Erc20CallError>> { | ||
match lp_coinfind_or_err(ctx, coin).await { | ||
Ok(MmCoinEnum::EthCoin(eth_coin)) => Ok(eth_coin), | ||
Ok(_) => Err(MmError::new(Erc20CallError::CoinNotSupported { | ||
coin: coin.to_string(), | ||
})), | ||
Err(_) => Err(MmError::new(Erc20CallError::NoSuchCoin { coin: coin.to_string() })), | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.