use ethabi::{ethereum_types::U256, ParamType, Token}; use heliosphere::{MethodCall, RpcClient}; use heliosphere_core::Address; use heliosphere_signer::{keypair::Keypair, signer::Signer}; #[tokio::test] async fn test_send_usdt() { let api = "https://api.shasta.trongrid.io"; let keypair = Keypair::from_hex_key( std::fs::read_to_string(".key") .expect("no ./.key found") .trim(), ) .unwrap(); let client = RpcClient::new(api).unwrap(); let from = keypair.address(); let to: Address = "TAJdphSsfiduXs7VmG1oDRqMSsEQhivxka".parse().unwrap(); let usdt: Address = "TGbL7tuCyyHL2XrTrZAyWDR4jqFbmpoLJd".parse().unwrap(); // shasta testnet USDT let amount: u64 = 1; // 0.000001 USDT // Fetch account balance let method_call_balance = MethodCall { caller: &from, contract: &usdt, selector: "balanceOf(address)", parameter: ðabi::encode(&[Token::Address(from.into())]), }; let res = ðabi::decode( &[ParamType::Uint(256)], &client .query_contract(&method_call_balance) .await .unwrap() .constant_result(0) .unwrap(), ) .unwrap()[0]; let current_balance = match res { Token::Uint(x) => x, _ => panic!("Wrong type"), }; println!("Balance: {}", current_balance); // Transfer tokens let method_call = MethodCall { caller: &from, contract: &usdt, selector: "transfer(address,uint256)", parameter: ðabi::encode(&[Token::Address(to.into()), Token::Uint(U256::from(amount))]), }; // Estimate energy usage let estimated = client.estimate_energy(&method_call).await.unwrap(); println!("Estimated energy usage: {}", estimated); // Send tx let mut tx = client .trigger_contract(&method_call, 0, None) .await .unwrap(); keypair.sign_transaction(&mut tx).unwrap(); let txid = client.broadcast_transaction(&tx).await.unwrap(); println!("Txid: {}", txid); println!("Confirming..."); client.await_confirmation(txid).await.unwrap(); }