MsgAuthorization
The following code snippet requires LocalTerra.
test1creates MsgGrantAuthorization message to grant MsgSend authorization to granteetest2.test2creates MsgExecAuthorized message to send2000000000000ukrwfrom thetest1account to thetest3account.
_116import {_116 LCDClient,_116 MnemonicKey,_116 Wallet,_116 MsgGrantAuthorization,_116 SendAuthorization,_116 MsgSend,_116 Int,_116 MsgExecAuthorized,_116 Coins,_116} from "@terra-money/terra.js";_116_116function grant(_116 granter: Wallet,_116 grantee: string,_116 spendLimit: Coins.Input,_116 duration: Int_116) {_116 const msgs = [_116 new MsgGrantAuthorization(_116 granter.key.accAddress,_116 grantee,_116 new SendAuthorization(spendLimit),_116 duration_116 ),_116 ];_116_116 return granter.createAndSignTx({ msgs });_116}_116_116function sendAuthorized(_116 granter: Wallet,_116 grantee: Wallet,_116 to: string,_116 amount: Coins.Input_116) {_116 const msgs = [_116 new MsgExecAuthorized(grantee.key.accAddress, [_116 new MsgSend(_116 granter.key.accAddress, // From test1_116 to,_116 amount_116 ),_116 ]),_116 ];_116_116 return grantee.createAndSignTx({ msgs });_116}_116_116async function main() {_116 const client = new LCDClient({_116 URL: "http://localhost:1317/",_116 chainID: "localterra",_116 gasPrices: "169.77ukrw",_116 });_116_116 // Granter (terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v)_116 const granter = client.wallet(_116 new MnemonicKey({_116 mnemonic:_116 "notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius",_116 })_116 );_116_116 // Grantee (terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp)_116 const grantee = client.wallet(_116 new MnemonicKey({_116 mnemonic:_116 "quality vacuum heart guard buzz spike sight swarm shove special gym robust assume sudden deposit grid alcohol choice devote leader tilt noodle tide penalty",_116 })_116 );_116_116 // MsgGrantAuthorization_116 await grant(_116 granter,_116 grantee.key.accAddress,_116 // Set enough spend limit since it will be decreased upon every MsgSend transactions_116 "1000000000000000ukrw,1000000000000uluna",_116 // expire after 100 year_116 new Int(86400 * 365 * 100 * 1000000000)_116 )_116 .then((tx) => client.tx.broadcast(tx))_116 .then(console.info)_116 .catch((err) => {_116 if (err.response) {_116 console.error(err.response.data);_116 } else {_116 console.error(err.message);_116 }_116 });_116_116 // MsgExecAuthorized of MsgSend_116 await sendAuthorized(_116 granter,_116 grantee,_116 // Test3_116 "terra1757tkx08n0cqrw7p86ny9lnxsqeth0wgp0em95",_116 "2000000000000ukrw"_116 )_116 .then((tx) => client.tx.broadcast(tx))_116 .then(console.info)_116 .catch((err) => {_116 if (err.response) {_116 // unauthorized: authorization not found: failed to execute message; message index: 0: failed to simulate tx_116 // happenes when there's not enough amount of granted amount of token_116_116 // insufficient funds: insufficient account funds; ..._116 // happenes when granter does not have enough amount of token_116 console.error(err.response.data);_116 } else {_116 console.error(err.message);_116 }_116 });_116}_116_116main().catch(console.error);