NEP-141 implementation

Viewed 389

While trying to implement NEP-141 fungible token, I am using trait

impl FungibleTokenCore for FungibleToken {

fn ft_transfer(&mut self, receiver_id: ValidAccountId, amount: U128, memo: Option<String>) {
        assert_one_yocto();
        let sender_id = env::predecessor_account_id();
        let amount: Balance = amount.into();
        self.internal_transfer(&sender_id, receiver_id.as_ref(), amount, memo);
    }

}

But the problem is the function ft_transfer is inaccessible from the contract. It gives error: "Contract method is not found".

export TOKEN=dev-1618119753426-1904392
near call $TOKEN ft_transfer '{"receiver_id":"avrit.testnet", "amount": 10, "memo":""}' --accountId=amiyatulu.testnet
3 Answers

As you mentioned in a comment, the problem is that you need to:

  • Use #[near_bindgen] for the impl definition:
#[near_bindgen]
impl FungibleTokenCore for FungibleToken { ... }
  • Use a public method:
pub fn ft_transfer(&mut self, ...)

There is already an implementation of this token in near-contract-standards

I am unable to add custom logic into the token using the library, so not using it.

Regarding how to extend the behaviour of the toke I suggest you to take a look at how the Rainbow Bridge does it for ERC20 tokens bridged to Near: BridgeToken. We also needed to extend its functionalities, and for this end, we used the Token as an internal field, and then changed a bit the public functions exposed.

There is also a useful macro to derive base implementation for common functions.

Like burning some tokens during transfer.

To this, you can follow the previous approach, without using the macro to expose all functions, and instead implement ft_transfer properly for your use case, but still making calls to the inner field: token: FungibleToken.

Related