From 3422fd7e4710bbf293fbb3de142f5f1ce140ab47 Mon Sep 17 00:00:00 2001 From: Pedro Camboim Date: Thu, 28 Dec 2023 14:03:48 -0300 Subject: [PATCH] fix: contract_address parse & trc20 transfer abi --- packages/kos-sdk/src/chains/tron/mod.rs | 46 +++++++++++++++----- packages/kos-sdk/src/chains/tron/requests.rs | 6 +++ packages/kos-sdk/src/chains/tron/trc20.rs | 2 +- 3 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/kos-sdk/src/chains/tron/mod.rs b/packages/kos-sdk/src/chains/tron/mod.rs index 776ff2b..6d7a826 100644 --- a/packages/kos-sdk/src/chains/tron/mod.rs +++ b/packages/kos-sdk/src/chains/tron/mod.rs @@ -224,29 +224,30 @@ impl TRX { })?; let to_address = *ETHAddress::from_bytes(addr_receiver.as_tvm_bytes()); - + let encoded = func - .encode_input(&[ - ethabi::Token::Address(to_address.into()), - ethabi::Token::Uint( - U256::from_dec_str(&amount.to_string()) - .map_err(|e| Error::InvalidNumberParse(e.to_string()))?, - ), + .encode_input(&[ + ethabi::Token::Address(to_address.into()), + ethabi::Token::Uint( + U256::from_dec_str(&amount.to_string()) + .map_err(|e| Error::InvalidNumberParse(e.to_string()))?, + ), ]) .map_err(|e| Error::InvalidTransaction(e.to_string()))?; + + let contract_address = address::Address::from_str(&token)?; let contract = kos_proto::tron::TriggerSmartContract { owner_address: addr_sender.as_bytes().to_vec(), - contract_address: token.as_bytes().to_vec(), + contract_address: contract_address.as_bytes().to_vec(), data: encoded, call_token_value: 0, call_value: 0, - token_id: 0, - + token_id: 0, }; - requests::create_trigger_contract(&node, contract).await? + requests::create_trigger_contract(&node, contract).await.unwrap() } else { let contract = kos_proto::tron::TransferAssetContract { @@ -354,6 +355,8 @@ impl TRX { mod tests { use std::assert_eq; + use crate::models::SendOptions; + use super::*; use hex::FromHex; use kos_types::Bytes32; @@ -423,6 +426,27 @@ mod tests { } } + #[test] + fn test_send_trc20() { + // create TRX send options + let trx_options = kos_proto::options::TRXOptions { + token: Some("TKk6DLX1xWRKHjDhHfdyQKefnP1WUppEXB".to_string()), + ..Default::default() + }; + + let options = SendOptions::new_tron_send_options(trx_options); + + let result = tokio_test::block_on(TRX::send( + "TCwwZeH6so1X4R5kcdbKqa4GWuzF53xPqG".to_string(), + DEFAULT_ADDRESS.to_string(), + BigNumber::from(1000000), + Some(options), + Some("https://api.trongrid.io".to_string()), + )); + + assert!(result.is_ok()); + } + #[test] fn test_validate_bip44() { let default_mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"; diff --git a/packages/kos-sdk/src/chains/tron/requests.rs b/packages/kos-sdk/src/chains/tron/requests.rs index 5a9563c..7b2866c 100644 --- a/packages/kos-sdk/src/chains/tron/requests.rs +++ b/packages/kos-sdk/src/chains/tron/requests.rs @@ -104,6 +104,12 @@ fn unpack_result(value: serde_json::Value) -> Result { return Ok(v.to_string()); } + if let Some(transaction) = value.get("transaction") { + if let Some(v) = transaction.get("raw_data_hex").and_then(|v| v.as_str()) { + return Ok(v.to_string()); + } + } + match value.get("Error") { Some(err) => Err(Error::ReqwestError(err.to_string())), None => Err(Error::ReqwestError("Unknown error".to_string())), diff --git a/packages/kos-sdk/src/chains/tron/trc20.rs b/packages/kos-sdk/src/chains/tron/trc20.rs index 23c4825..a9bc9fd 100644 --- a/packages/kos-sdk/src/chains/tron/trc20.rs +++ b/packages/kos-sdk/src/chains/tron/trc20.rs @@ -52,7 +52,7 @@ const TRC20_CONTRACT_ABI: &str = r#" "payable":false, "stateMutability":"nonpayable", "type":"function" - }, + } ] "#;