MsgAuthorization
The following code snippet requires LocalTerra.
test1creates MsgGrantAuthorization message to grant MsgSend authorization to granteetest2.test2creates MsgExecAuthorized message to send2000000000000ulunafrom thetest1account to thetest3account.
_112import {_112 LCDClient,_112 MnemonicKey,_112 Wallet,_112 MsgGrantAuthorization,_112 SendAuthorization,_112 MsgSend,_112 Int,_112 MsgExecAuthorized,_112 Coins,_112} from '@terra-money/feather.js';_112_112function grant(_112 granter: Wallet,_112 grantee: string,_112 spendLimit: Coins.Input,_112 duration: Int,_112) {_112 const msgs = [_112 new MsgGrantAuthorization(_112 granter.key.accAddress('terra'),_112 grantee,_112 new SendAuthorization(spendLimit),_112 duration,_112 ),_112 ];_112_112 return granter.createAndSignTx({ msgs, chainID: 'pisco-1' });_112}_112_112function sendAuthorized(_112 granter: Wallet,_112 grantee: Wallet,_112 to: string,_112 amount: Coins.Input,_112) {_112 const msgs = [_112 new MsgExecAuthorized(grantee.key.accAddress, [_112 new MsgSend(_112 granter.key.accAddress('terra'), // From test1_112 to,_112 amount,_112 ),_112 ]),_112 ];_112_112 return grantee.createAndSignTx({ msgs, chainID: 'pisco-1' });_112}_112_112async function main() {_112 const client = new LCDClient(...);_112_112 // Granter (terra1x46rqay4d3cssq8gxxvqz8xt6nwlz4td20k38v)_112 const granter = client.wallet(_112 new MnemonicKey({_112 mnemonic:_112 'notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius',_112 }),_112 );_112_112 // Grantee (terra17lmam6zguazs5q5u6z5mmx76uj63gldnse2pdp)_112 const grantee = client.wallet(_112 new MnemonicKey({_112 mnemonic:_112 'quality vacuum heart guard buzz spike sight swarm shove special gym robust assume sudden deposit grid alcohol choice devote leader tilt noodle tide penalty',_112 }),_112 );_112_112 // MsgGrantAuthorization_112 await grant(_112 granter,_112 grantee.key.accAddress('terra'),_112 // Set enough spend limit since it will be decreased upon every MsgSend transactions_112 '1000000000000uluna',_112 // expire after 100 years_112 new Int(86400 * 365 * 100 * 1000000000),_112 )_112 .then((tx) => client.tx.broadcast(tx, 'pisco-1'))_112 .then(console.info)_112 .catch((err) => {_112 if (err.response) {_112 console.error(err.response.data);_112 } else {_112 console.error(err.message);_112 }_112 });_112_112 // MsgExecAuthorized of MsgSend_112 await sendAuthorized(_112 granter,_112 grantee,_112 // Test3_112 'terra1757tkx08n0cqrw7p86ny9lnxsqeth0wgp0em95',_112 '2000000000000uluna',_112 )_112 .then((tx) => client.tx.broadcast(tx, 'piscp-1'))_112 .then(console.info)_112 .catch((err) => {_112 if (err.response) {_112 // unauthorized: authorization not found: failed to execute message; message index: 0: failed to simulate tx_112 // happens when the granted amount of tokens is exceeded._112_112 // insufficient funds: insufficient account funds; ..._112 // happens when granter does not have enough tokens_112 console.error(err.response.data);_112 } else {_112 console.error(err.message);_112 }_112 });_112}_112_112main().catch(console.error);