Execute commands
Now we complete the task - sending some tokens that have been delegated to a key

Running commands

As mentioned in the introduction, now we want to:
  1. 1.
    As the admin key (A)†, set an allowance for a key (B)
  2. 2.
    As the key with an allowance, send tokens from key (B) to key (C)
  3. 3.
    See tokens arrive at key (C)
  4. 4.
    See allowance decrease for key (B)
† this is the key that you used to instantiate, and set as an admin. If you are running a validator in the testnet, then it is probably your self-delegate key.

1. Add allowance for key B

Using node, you can again encode the following arguments:
1
{
2
increase_allowance: {
3
spender: '<key-B>',
4
amount: {
5
denom: "ujuno",
6
amount: "2000000"
7
}
8
}
9
}
Copied!
Then, as the admin key (A), increase key (B)'s allowance:
1
junod tx wasm execute <contract-addr> \
2
'{"increase_allowance":{"spender":"<key-B>","amount":{"denom":"ujuno","amount":"2000000"}}}' \
3
--from <admin-key-A> \
4
--chain-id <chain-id>
Copied!
If you query its allowance, you should see a value of 2000000:
1
junod query wasm contract-state smart <contract-addr> '{"allowance":{"spender":"<key-B>"}}' --chain-id <chain-id>
Copied!
Should return:
1
data:
2
balance:
3
- amount: "2000000"
4
denom: ujuno
5
expires:
6
never: {}
Copied!

2. Send tokens from key B to key C

First, query the balance of another key, that we did not allocate any tokens to (C):
1
junod q bank balances <key-C>
Copied!
Let's say it already has a balance of 500ujuno - the command will return:
1
balances:
2
- amount: "500"
3
denom: ujuno
4
pagination:
5
next_key: null
6
total: "0"
Copied!
Then, we again need to encode some arguments to JSON for the send:
1
{
2
execute: {
3
msgs: [{
4
bank: {
5
send: {
6
to_address: "<key-C>",
7
amount: [{
8
denom: "ujuno",
9
amount: "500"
10
}]
11
}
12
}
13
}]
14
}
15
};
Copied!
Once we have the JSON, we can shape an execute command:
1
junod tx wasm execute <contract-addr> \
2
'{"execute":{"msgs":[{"bank":{"send":{"to_address":"<key-C>","amount":[{"denom":"ujuno","amount":"500"}]}}}]}}' \
3
--from <key-B> \
4
--chain-id <chain-id>
Copied!
Note that the --from flag is now signing this from the key (B) that the admin key (A) gave a token balance to. This CW1 Subkeys contract will only work with the native token of the chain, in this case ujuno.

3. Check balance of key C

If we query balance again:
1
junod q bank balances <key-C>
Copied!
We expect to see the balance incremented by 500ujuno:
1
balances:
2
- amount: "1000"
3
denom: ujuno
4
pagination:
5
next_key: null
6
total: "0"
Copied!

4. See allowance decrease for key B

Now, if we query the allowance for key B, we should see it has decreased by 500ujuno:
1
junod query wasm contract-state smart <contract-addr> '{"allowance":{"spender":"<key-B>"}}' --chain-id <chain-id>
Copied!
1
data:
2
balance:
3
- amount: "1999500"
4
denom: ujuno
5
expires:
6
never: {}
Copied!
We're done!
Play around some more with increasing and decreasing allowances, or even adding expiries to allowances, to get a better feel for how this works.
Last modified 5d ago