Skip to content

Commit

Permalink
fix: contract_address parse & trc20 transfer abi
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrxlz committed Dec 28, 2023
1 parent d4ab350 commit 3422fd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
46 changes: 35 additions & 11 deletions packages/kos-sdk/src/chains/tron/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand Down
6 changes: 6 additions & 0 deletions packages/kos-sdk/src/chains/tron/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ fn unpack_result(value: serde_json::Value) -> Result<String, Error> {
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())),
Expand Down
2 changes: 1 addition & 1 deletion packages/kos-sdk/src/chains/tron/trc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const TRC20_CONTRACT_ABI: &str = r#"
"payable":false,
"stateMutability":"nonpayable",
"type":"function"
},
}
]
"#;

Expand Down

0 comments on commit 3422fd7

Please sign in to comment.