Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
split send to send to + type
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Jul 19, 2024
1 parent 2b7f4e9 commit a3e369c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
3 changes: 2 additions & 1 deletion examples/create_account_and_send_near.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
},
};

#[derive(Debug, Clone)]
pub struct Tokens {
account_id: AccountId,
}
Expand Down Expand Up @@ -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<ConstructTransaction> {
pub fn ft(self, ft_contract: AccountId, amount: u128) -> anyhow::Result<ConstructTransaction> {
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<ConstructTransaction> {
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))
}
}

0 comments on commit a3e369c

Please sign in to comment.