Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach value (deposit) to transaction with Nearlib?

Tags:

nearprotocol

Let's say I have a contract function that expects a certain amount of near to be send with a certain transaction, the function is called create_order, create_order takes a couple arguments.

I have my contract setup in the frontend under the name myContract.

I want to call myContract.create_order({...}) but the transaction fails because this method call doesn't have the right amount of NEAR tokens attached.

How do I assign a certain value of deposit to a transaction?

like image 969
jasper Avatar asked Jan 26 '23 20:01

jasper


2 Answers

Nearlib supports it using account.functionCall(..., amount). But it might not work, because of the design of the access keys with function calls. Default authorized access keys towards applications only allows function calls without attached token deposits (only prepaid gas). It's done this way to prevent apps from automatically using your balance without your explicit approval. Details on access keys are here: https://github.com/nearprotocol/NEPs/blob/master/text/0005-access-keys.md

The way to attach a deposit for the transaction should be done with the explicit approval from the wallet. The app should create a request for the wallet, redirect to the wallet for the approval (or through the popup). Once user approves the transaction, it's signed with full access key from the wallet directly and broadcasted. But I'm afraid we don't have this API on the wallet yet. Issue for this: https://github.com/nearprotocol/near-wallet/issues/56

like image 22
Evgeny Kuzyakov Avatar answered Jan 28 '23 09:01

Evgeny Kuzyakov


It's possible to use account.functionCall directly (without sugar for RPCs) to either attach amount or specify gas allowance for the call.

See Account#functionCall in nearlib.

like image 183
Vlad Grichina Avatar answered Jan 28 '23 10:01

Vlad Grichina