Search code examples
nearprotocol

NEAR cross-contract calls (allow an account to release funds from escrow to a different account)


I'm trying to learn how to allow 1 account (a "donation matcher") to deposit funds into a 2nd account (this contract, which serves as an "escrow" account), which a 3rd account (a regular "donor") then is allowed to trigger to send to a 4th account (the "recipient", such as a charity).

The function I've written as a placeholder probably doesn't make any sense because I assume that (as currently written) it would probably transfer funds from the caller/signer rather than the escrow/self/contract:

function transferFromEscrow(destinationAccount: AccountId, amount: u128): ContractPromiseBatch {
  // TODO: Fix this function!
  const toDestinationAccount = ContractPromiseBatch.create(destinationAccount);
  return toDestinationAccount.transfer(amount);
}

Thanks for any help!

This official NEAR repo led me to expect to see working examples in index.ts and 02.using-multiple-calls, but I don't see any.

I also posted this question to https://github.com/near-examples/cross-contract-calls/issues/13


Solution

  • As mentioned here, there is no way to withdraw/transfer tokens from some other account, so you should design your contract in such a way that it requires the user to deposit some tokens into your contract (attach tokens with a deposit-like function call). There are nice examples of this pattern in the core contracts:

    • lockup contract locks funds according to the schedule
    • staking-pool locks the tokens for delegated stake
    • multisig contract uses two calls to enable 2FA by using add_request function with all the parameters of a future call and confirm function that takes the previous request and executes it