From a3e369c9fe5c49d99cf764b1df811ace12b6f483 Mon Sep 17 00:00:00 2001 From: akorchyn Date: Fri, 19 Jul 2024 18:32:03 +0300 Subject: [PATCH] split send to send to + type --- examples/create_account_and_send_near.rs | 3 +- src/tokens.rs | 38 +++++++++++++++--------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/examples/create_account_and_send_near.rs b/examples/create_account_and_send_near.rs index c524ae2..382a367 100644 --- a/examples/create_account_and_send_near.rs +++ b/examples/create_account_and_send_near.rs @@ -33,7 +33,8 @@ async fn main() { .unwrap(); near::Tokens::of(account.id().clone()) - .send_near(new_account.clone(), NearToken::from_near(1)) + .send_to(new_account.clone()) + .near(NearToken::from_near(1)) .with_signer(Signer::from_workspace(&account)) .send_to(&network) .await diff --git a/src/tokens.rs b/src/tokens.rs index 73c5545..bbeab82 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -25,6 +25,7 @@ use crate::{ }, }; +#[derive(Debug, Clone)] pub struct Tokens { account_id: AccountId, } @@ -131,47 +132,56 @@ impl Tokens { Ok(query_builder) } - pub fn send_near(self, receiver_id: AccountId, amount: NearToken) -> ConstructTransaction { - ConstructTransaction::new(self.account_id, receiver_id).add_action(Action::Transfer( + pub fn send_to(self, receiver_id: AccountId) -> SendTo { + SendTo { + from: self.account_id, + receiver_id, + } + } +} + +#[derive(Debug, Clone)] +pub struct SendTo { + from: AccountId, + receiver_id: AccountId, +} + +impl SendTo { + pub fn near(self, amount: NearToken) -> ConstructTransaction { + ConstructTransaction::new(self.from, self.receiver_id).add_action(Action::Transfer( TransferAction { deposit: amount.as_yoctonear(), }, )) } - pub fn send_ft( - self, - ft_contract: AccountId, - receiver_id: AccountId, - amount: u128, - ) -> anyhow::Result { + pub fn ft(self, ft_contract: AccountId, amount: u128) -> anyhow::Result { Ok(Contract(ft_contract) .call_function( "ft_transfer", json!({ - "receiver_id": receiver_id, + "receiver_id": self.receiver_id, "amount": amount }), )? .transaction() - .with_signer_account(self.account_id)) + .with_signer_account(self.from)) } - pub fn send_nft( + pub fn nft( self, nft_contract: AccountId, - receiver_id: AccountId, token_id: String, ) -> anyhow::Result { Ok(Contract(nft_contract) .call_function( "nft_transfer", json!({ - "receiver_id": receiver_id.to_string(), + "receiver_id": self.receiver_id, "token_id": token_id }), )? .transaction() - .with_signer_account(self.account_id)) + .with_signer_account(self.from)) } }