-
Notifications
You must be signed in to change notification settings - Fork 117
feat(tron): initial groundwork for full TRON integration #2425
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
Changes from 13 commits
7b94d43
83049c1
af4867b
d26b0c6
7b74fb6
bfce81d
e9ba8d0
3676b1f
b7806a7
59deab6
bf2dd93
fbaca78
f904359
2b44429
7b02f35
7e61403
6ce97d2
e8fd60a
33f6996
dec3d1d
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.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,6 +158,9 @@ use fee_estimation::eip1559::{block_native::BlocknativeGasApiCaller, infura::Inf | |
|
|
||
| pub mod erc20; | ||
| use erc20::get_token_decimals; | ||
|
|
||
| pub mod tron; | ||
|
|
||
|
borngraced marked this conversation as resolved.
Outdated
|
||
| pub(crate) mod eth_swap_v2; | ||
| use eth_swap_v2::{extract_id_from_tx_data, EthPaymentType, PaymentMethod, SpendTxSearchParams}; | ||
|
|
||
|
|
@@ -762,6 +765,30 @@ struct SavedErc20Events { | |
| latest_block: U64, | ||
| } | ||
|
|
||
| /// Specifies which blockchain the EthCoin operates on: EVM-compatible or TRON. | ||
| /// This distinction allows unified logic for EVM & TRON coins. | ||
| #[derive(Clone, Debug)] | ||
| pub enum ChainSpec { | ||
| Evm { chain_id: u64 }, | ||
| Tron { network: tron::Network }, | ||
|
borngraced marked this conversation as resolved.
|
||
| } | ||
|
|
||
| impl ChainSpec { | ||
| pub fn chain_id(&self) -> Option<u64> { | ||
| match self { | ||
| ChainSpec::Evm { chain_id } => Some(*chain_id), | ||
| ChainSpec::Tron { .. } => None, | ||
| } | ||
| } | ||
|
|
||
| pub fn kind(&self) -> &'static str { | ||
| match self { | ||
| ChainSpec::Evm { .. } => "EVM", | ||
| ChainSpec::Tron { .. } => "TRON", | ||
| } | ||
| } | ||
| } | ||
|
borngraced marked this conversation as resolved.
|
||
|
|
||
|
borngraced marked this conversation as resolved.
|
||
| #[derive(Clone, Debug, PartialEq, Eq)] | ||
| pub enum EthCoinType { | ||
| /// Ethereum itself or it's forks: ETC/others | ||
|
|
@@ -816,6 +843,8 @@ impl From<PrivKeyBuildPolicy> for EthPrivKeyBuildPolicy { | |
| pub struct EthCoinImpl { | ||
| ticker: String, | ||
| pub coin_type: EthCoinType, | ||
| /// Specifies the underlying blockchain (EVM or TRON). | ||
| pub chain_spec: ChainSpec, | ||
|
Comment on lines
+845
to
+846
Collaborator
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. nitty nit: feels like this field could be integrated into
Collaborator
Author
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 was the first solution I considered ofcourse, I opted for What is different between EVMs and Tron are actually the chain specs not coin types, so Tron platform coin and it's tokens have the same characteristics as EVM coins and tokens. If we were to use your suggested
Collaborator
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. aha i see ur point. if (p.s. same for
Collaborator
Author
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. After this PR I will look into if just adding 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. unresolved it, as we sometimes come back to this thread to re-read |
||
| pub(crate) priv_key_policy: EthPrivKeyPolicy, | ||
| /// Either an Iguana address or a 'EthHDWallet' instance. | ||
| /// Arc is used to use the same hd wallet from platform coin if we need to. | ||
|
|
@@ -836,7 +865,6 @@ pub struct EthCoinImpl { | |
| /// Coin needs access to the context in order to reuse the logging and shutdown facilities. | ||
| /// Using a weak reference by default in order to avoid circular references and leaks. | ||
| pub ctx: MmWeak, | ||
| chain_id: u64, | ||
| /// The name of the coin with which Trezor wallet associates this asset. | ||
| trezor_coin: Option<String>, | ||
| /// the block range used for eth_getLogs | ||
|
|
@@ -1043,7 +1071,7 @@ impl EthCoinImpl { | |
| } | ||
|
|
||
| #[inline(always)] | ||
| pub fn chain_id(&self) -> u64 { self.chain_id } | ||
| pub fn chain_id(&self) -> Option<u64> { self.chain_spec.chain_id() } | ||
| } | ||
|
|
||
| async fn get_raw_transaction_impl(coin: EthCoin, req: RawTransactionRequest) -> RawTransactionResult { | ||
|
|
@@ -1161,7 +1189,16 @@ pub async fn withdraw_erc1155(ctx: MmArc, withdraw_type: WithdrawErc1155) -> Wit | |
| .build() | ||
| .map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?; | ||
| let secret = eth_coin.priv_key_policy.activated_key_or_err()?.secret(); | ||
| let signed = tx.sign(secret, Some(eth_coin.chain_id))?; | ||
| let chain_id = match eth_coin.chain_spec { | ||
| ChainSpec::Evm { chain_id } => chain_id, | ||
| // Todo: Add support for Tron NFTs | ||
| ChainSpec::Tron { .. } => { | ||
| return MmError::err(WithdrawError::InternalError( | ||
| "Tron is not supported for withdraw_erc1155 yet".to_owned(), | ||
| )) | ||
| }, | ||
| }; | ||
| let signed = tx.sign(secret, Some(chain_id))?; | ||
| let signed_bytes = rlp::encode(&signed); | ||
| let fee_details = EthTxFeeDetails::new(gas, pay_for_gas_option, fee_coin)?; | ||
|
|
||
|
|
@@ -1252,7 +1289,16 @@ pub async fn withdraw_erc721(ctx: MmArc, withdraw_type: WithdrawErc721) -> Withd | |
| .build() | ||
| .map_to_mm(|e| WithdrawError::InternalError(e.to_string()))?; | ||
| let secret = eth_coin.priv_key_policy.activated_key_or_err()?.secret(); | ||
| let signed = tx.sign(secret, Some(eth_coin.chain_id))?; | ||
| let chain_id = match eth_coin.chain_spec { | ||
| ChainSpec::Evm { chain_id } => chain_id, | ||
| // Todo: Add support for Tron NFTs | ||
| ChainSpec::Tron { .. } => { | ||
| return MmError::err(WithdrawError::InternalError( | ||
| "Tron is not supported for withdraw_erc721 yet".to_owned(), | ||
| )) | ||
| }, | ||
| }; | ||
| let signed = tx.sign(secret, Some(chain_id))?; | ||
| let signed_bytes = rlp::encode(&signed); | ||
| let fee_details = EthTxFeeDetails::new(gas, pay_for_gas_option, fee_coin)?; | ||
|
|
||
|
|
@@ -2639,9 +2685,18 @@ async fn sign_transaction_with_keypair<'a>( | |
| let tx_builder = tx_builder_with_pay_for_gas_option(coin, tx_builder, pay_for_gas_option) | ||
| .map_err(|e| TransactionErr::Plain(e.get_inner().to_string()))?; | ||
| let tx = tx_builder.build()?; | ||
| let chain_id = match coin.chain_spec { | ||
| ChainSpec::Evm { chain_id } => chain_id, | ||
| // Todo: Add Tron signing logic | ||
| ChainSpec::Tron { .. } => { | ||
| return Err(TransactionErr::Plain( | ||
| "Tron is not supported for sign_transaction_with_keypair yet".into(), | ||
| )) | ||
| }, | ||
| }; | ||
|
|
||
| Ok(( | ||
| tx.sign(key_pair.secret(), Some(coin.chain_id))?, | ||
| tx.sign(key_pair.secret(), Some(chain_id))?, | ||
| web3_instances_with_latest_nonce, | ||
| )) | ||
| } | ||
|
|
@@ -6403,7 +6458,7 @@ pub async fn eth_coin_from_conf_and_request( | |
| } | ||
|
|
||
| let (coin_type, decimals) = match protocol { | ||
| CoinProtocol::ETH => (EthCoinType::Eth, ETH_DECIMALS), | ||
| CoinProtocol::ETH { .. } => (EthCoinType::Eth, ETH_DECIMALS), | ||
| CoinProtocol::ERC20 { | ||
| platform, | ||
| contract_address, | ||
|
|
@@ -6444,6 +6499,8 @@ pub async fn eth_coin_from_conf_and_request( | |
|
|
||
| let sign_message_prefix: Option<String> = json::from_value(conf["sign_message_prefix"].clone()).unwrap_or(None); | ||
|
|
||
| // Todo: write documentation note about this | ||
| // Kept only for v1 activation but should be removed in time, needed for both ETH and ERC20 | ||
| let chain_id = try_s!(conf["chain_id"] | ||
| .as_u64() | ||
| .ok_or_else(|| format!("chain_id is not set for {}", ticker))); | ||
|
|
@@ -6480,6 +6537,8 @@ pub async fn eth_coin_from_conf_and_request( | |
| priv_key_policy: key_pair, | ||
| derivation_method: Arc::new(derivation_method), | ||
| coin_type, | ||
| // Tron is not supported for v1 activation | ||
| chain_spec: ChainSpec::Evm { chain_id }, | ||
| sign_message_prefix, | ||
| swap_contract_address, | ||
| swap_v2_contracts: None, | ||
|
|
@@ -6493,7 +6552,6 @@ pub async fn eth_coin_from_conf_and_request( | |
| max_eth_tx_type, | ||
| ctx: ctx.weak(), | ||
| required_confirmations, | ||
| chain_id, | ||
| trezor_coin, | ||
| logs_block_range: conf["logs_block_range"].as_u64().unwrap_or(DEFAULT_LOGS_BLOCK_RANGE), | ||
| address_nonce_locks, | ||
|
|
@@ -6782,6 +6840,8 @@ fn calc_total_fee(gas: U256, pay_for_gas_option: &PayForGasOption) -> NumConvers | |
| } | ||
| } | ||
|
|
||
| // Todo: Tron have a different concept from gas (Energy, Bandwidth and Free Transaction), it should be added as a different function | ||
| // and this should be part of a trait abstracted over both types | ||
| #[allow(clippy::result_large_err)] | ||
| fn tx_builder_with_pay_for_gas_option( | ||
| eth_coin: &EthCoin, | ||
|
|
@@ -6793,9 +6853,14 @@ fn tx_builder_with_pay_for_gas_option( | |
| PayForGasOption::Eip1559(Eip1559FeePerGas { | ||
| max_priority_fee_per_gas, | ||
| max_fee_per_gas, | ||
| }) => tx_builder | ||
| .with_priority_fee_per_gas(max_fee_per_gas, max_priority_fee_per_gas) | ||
| .with_chain_id(eth_coin.chain_id), | ||
| }) => { | ||
| let chain_id = eth_coin | ||
| .chain_id() | ||
| .ok_or_else(|| WithdrawError::InternalError("chain_id should be set for an EVM coin".to_string()))?; | ||
| tx_builder | ||
| .with_priority_fee_per_gas(max_fee_per_gas, max_priority_fee_per_gas) | ||
| .with_chain_id(chain_id) | ||
| }, | ||
| }; | ||
| Ok(tx_builder) | ||
| } | ||
|
|
@@ -7299,6 +7364,7 @@ impl EthCoin { | |
| let coin = EthCoinImpl { | ||
| ticker: self.ticker.clone(), | ||
| coin_type: new_coin_type, | ||
| chain_spec: self.chain_spec.clone(), | ||
| priv_key_policy: self.priv_key_policy.clone(), | ||
| derivation_method: Arc::clone(&self.derivation_method), | ||
| sign_message_prefix: self.sign_message_prefix.clone(), | ||
|
|
@@ -7315,7 +7381,6 @@ impl EthCoin { | |
| swap_txfee_policy: Mutex::new(self.swap_txfee_policy.lock().unwrap().clone()), | ||
| max_eth_tx_type: self.max_eth_tx_type, | ||
| ctx: self.ctx.clone(), | ||
| chain_id: self.chain_id, | ||
| trezor_coin: self.trezor_coin.clone(), | ||
| logs_block_range: self.logs_block_range, | ||
| address_nonce_locks: Arc::clone(&self.address_nonce_locks), | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.