-1

So I am trying to transfer a FT, saved as a file ft.rs, which is generated in the standard way, like so:

impl Contract {
    /// Initializes the contract with the given total supply owned by the given `owner_id` with
    /// default metadata (for example purposes only).
    #[init]
    pub fn new_default_meta(owner_id: AccountId, total_supply: U128) -> Self {
        Self::new(
            owner_id,
            total_supply,
            FungibleTokenMetadata {
                spec: FT_METADATA_SPEC.to_string(),
                name: "Example NEAR fungible token".to_string(),
                symbol: "EXAMPLE".to_string(),
                icon: None,
                reference: None,
                reference_hash: None,
                decimals: 24,
            },
        )
    }

    /// Initializes the contract with the given total supply owned by the given `owner_id` with
    /// the given fungible token metadata.
    #[init]
    pub fn new(
        owner_id: AccountId,
        total_supply: U128,
        metadata: FungibleTokenMetadata,
    ) -> Self {
        assert!(!env::state_exists(), "Already initialized");
        metadata.assert_valid();
        let mut this = Self {
            token: FungibleToken::new(b"a".to_vec()),
            metadata: LazyOption::new(b"m".to_vec(), Some(&metadata)),
        };
        this.token.internal_register_account(&owner_id);
        this.token.internal_deposit(&owner_id, total_supply.into());
        near_contract_standards::fungible_token::events::FtMint {
            owner_id: &owner_id,
            amount: &total_supply,
            memo: Some("Initial tokens supply is minted"),
        }
        .emit();
        this
    }
}

And I want to make another contract which can do two things, send the ft, and receive an ft:

pub use crate::ft::*;
mod ft; 

impl Contract {

    #[payable]
    fn receive_ft(&self) -> bool {
        // user sends ft and this contract registers and stores it
    }

    #[payable]
    fn send_ft(&self, receiver : AccountId, amount : U128) -> bool {
        // user sends ft to external address
    }
}

Anyone here to point me in the right direction?

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
AKRA
  • 300
  • 2
  • 12

1 Answers1

1

your going to want to import the FungibleTokenReceiver from the near-contract-standards crate and then implement it in it's own impl block in something what would look like this:

use near_contract_standards::fungible_token::receiver::FungibleTokenReceiver;
use near_sdk::{near_bindgen, PromiseOrValue};
use near_sdk::json_types::{ValidAccountId, U128};

#[near_bindgen]
impl FungibleTokenReceiver for Contract {
      
    pub fn ft_on_transfer(&mut self, sender_id: ValidAccountId, amount: U128, msg: String) -> PromiseOrValue<U128> {
      // Your logic goes here make sure you return a promise that would
     // be the value of the number of tokens you want to send back to
     // the account [if any]
     PromiseOrValue::Value(U128::from(0))
   }
}

As for the sending out of tokens, I don't know the exact implementation, but you would have to implement a cross contract call on the fungible_token contract to transfer the tokens from the contract to the contract caller

Hope this helps :)

Sonny Vesali
  • 99
  • 1
  • 11
  • This is great, thanks ... So if I understand correctly, this will allow for an external user to send tokens to the contract? Do you mind expanding on the second part a little? – AKRA May 25 '22 at 09:31