Search code examples
nearprotocol

How can I move NEAR token from one account to other using contract?


Disclaimer: this is a community question from a chat published here for better discoverability.

I want to transfer NEAR token from a.testnet to b.testnet.


Solution

  • You cannot transfer tokens from an arbitrary account. Imagine I have a contract that can transfer tokens from your balance. That does not sound secure, right?

    The approach you want to implement is the following: when account a.testnet calls a contract, it attaches some tokens with the call (create a transaction with non-zero deposit in FunctionCall action), and then, inside the contract, create a Transfer to the account b.testnet.

    In AssemblyScript, you can use ContractPromiseBatch.create('b.testnet').transfer(context.attachedDeposit) to issue the transfer.

    In Rust, Promise::new("b.testnet".to_string()).transfer(near_sdk::env::attached_deposit())