var img = document.createElement('img'); img.src = "https://terradocs.matomo.cloud//piwik.php?idsite=1&rec=1&url=https://docs.terra.money" + location.pathname; img.style = "border:0"; img.alt = "tracker"; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(img,s);
Skip to main content

MsgAuthorization

An authorization message is used to authorize an address to perform certain actions or operations. For more information on authorizations, visit the Authz module spec.

Example

You can use LocalTerra to run the MsgAuthorization example.

In this code, the following actions are performed:

  1. The test1 (granter) account creates the MsgGrantAuthorization message to grant MsgSend authorization to test2 (the grantee).

  2. test2 creates the MsgExecAuthorized message to send 2000000000000uluna from the test1 account to the test3 account.

msgauth.ts
Copy

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