Skip to content
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 send trc20 token #31

Merged
merged 14 commits into from
Jan 24, 2024
35 changes: 17 additions & 18 deletions packages/kos-sdk/src/chains/tron/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
pub mod address;
pub mod requests;

use std::str::FromStr;

use wasm_bindgen::prelude::*;
use web3::{ethabi, types::U256};

use kos_crypto::{keypair::KeyPair, secp256k1::Secp256k1KeyPair};
use kos_types::error::Error;
use kos_types::hash::Hash;
use kos_types::number::BigNumber;

use crate::{
chain::{self, BaseChain},
chains::ethereum::address::Address as ETHAddress,
chains::evm20,
models::{BroadcastResult, PathOptions, Transaction, TransactionRaw},
};

use kos_crypto::{keypair::KeyPair, secp256k1::Secp256k1KeyPair};
use kos_types::error::Error;
use kos_types::hash::Hash;
use kos_types::number::BigNumber;

use wasm_bindgen::prelude::*;
use web3::{ethabi, types::U256};
pub mod address;
pub mod requests;

#[derive(Debug, Copy, Clone)]
#[wasm_bindgen]
Expand Down Expand Up @@ -225,12 +225,9 @@ impl TRX {
node: &str,
fee_limit: &i64,
) -> Result<kos_proto::tron::Transaction, Error> {
use ethabi;
use requests;

let contract = evm20::get_contract_evm20();
let func = contract.function("transfer").map_err(|e| {
Error::InvalidMessage(format!("failed to get transferFrom function: {}", e))
Error::InvalidMessage(format!("failed to get transfer function: {}", e))
})?;

let to_address = *ETHAddress::from_bytes(addr_receiver.as_tvm_bytes());
Expand All @@ -254,12 +251,12 @@ impl TRX {
token_id: 0,
};

let extended = requests::TransferOptions {
let extended = requests::ContractOptions {
contract,
// TODO: estimate fee limit, for now use 100 TRX
fee_limit: fee_limit | 100000000,
};
let transaction = requests::create_trc20_transfer(node, extended).await?;
let transaction = requests::trigger_smartcontract(node, extended).await?;
Ok(transaction)
}

Expand Down Expand Up @@ -397,11 +394,13 @@ impl TRX {
mod tests {
use std::assert_eq;

use hex::FromHex;

use kos_types::Bytes32;

use crate::models::SendOptions;

use super::*;
use hex::FromHex;
use kos_types::Bytes32;

const DEFAULT_PRIVATE_KEY: &str =
"b5a4cea271ff424d7c31dc12a3e43e401df7a40d7412a15750f3f0b6b5449a28";
Expand Down
6 changes: 3 additions & 3 deletions packages/kos-sdk/src/chains/tron/requests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::Serialize;
use serde_json::json;

#[derive(Serialize)]
pub struct TransferOptions {
pub struct ContractOptions {
#[serde(flatten)]
pub contract: kos_proto::tron::TriggerSmartContract,
pub fee_limit: i64,
Expand Down Expand Up @@ -74,9 +74,9 @@ pub async fn create_asset_transfer(
create_transaction(url, contract).await
}

pub async fn create_trc20_transfer(
pub async fn trigger_smartcontract(
node_url: &str,
contract: TransferOptions,
contract: ContractOptions,
) -> Result<kos_proto::tron::Transaction, Error> {
let url = format!("{}/wallet/triggersmartcontract", node_url);

Expand Down
Loading