Skip to main content

MsgAuthorization

The following code snippet requires LocalTerra.

  1. test1 creates MsgGrantAuthorization message to grant MsgSend authorization to grantee test2.
  2. test2 creates MsgExecAuthorized message to send 2000000000000ukrw from the test1 account to the test3 account.

_116
import {
_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
_116
function 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
_116
function 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
_116
async 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
_116
main().catch(console.error);