Skip to content

Commit

Permalink
chore(amplifier): tidy up commands and update commander lib (#174)
Browse files Browse the repository at this point in the history
* chore: update commander version

* chore: create separate index.js as an entrypoint for all commands

* chore: export commands functions

* chore: rename index.js -> amplifier.js

* chore: update docs
  • Loading branch information
sdaveas authored and benjamin852 committed Mar 27, 2024
1 parent 16f0278 commit 952412f
Show file tree
Hide file tree
Showing 9 changed files with 200 additions and 176 deletions.
18 changes: 9 additions & 9 deletions examples/amplifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ There are two endpoints that can be used for generic commands and events:
To broadcast a command, use the following:

```bash
node ./endpoints/broadcast.js \
$ node amplifier broadcast \
--address <cestination contract> \
--payload <execute message>
```
For example, call `distribute_rewards()` on the `Rewards` contract to distribute rewards:

```bash
$ node ./endpoints/broadcast.js \
$ node ampifier broadcast \
--address axelar1wkwy0xh89ksdgj9hr347dyd2dw7zesmtrue6kfzyml4vdtz6e5ws2pvc5e \
--payload '{"distribute_rewards":{"pool_id":{"chain_name":"fantom","contract":"axelar1ufs3tlq4umljk0qfe8k5ya0x6hpavn897u2cnf9k0en9jr7qarqqa9263g"},"epoch_count":1000}}'
Broadcasting message:
Expand All @@ -48,13 +48,13 @@ Message sent for broadcast
To get all wasm events emitted on the Axelar network, run:

```bash
node ./endpoints/subscribe-to-wasm-events.js
node amplifier subscribe-to-wasm-events
```

You can optionally specify a `start-height` to catch events that were emitted at a previous time with the `--start-height` flag. It is set to `0` by default, which means that subscription starts from the current tip of the chain:

```
$ node ./endpoints/subscribe-to-wasm-events.js --start-height 221645
$ node amplifier subscribe-to-wasm-events --start-height 221645
Subscribing to events starting from block: 221645
Connecting to server at localhost:50051
Event: {
Expand Down Expand Up @@ -89,7 +89,7 @@ The following endpoints are available to facilitate GMP calls:
Given a transaction relayed on the source chain, the `verify` command is called as follows:

``` bash
node ./endpoints/verify.js \
node amplifier verify \
--id 0x02293467b9d6e1ce51d8ac0fa24e9a30fb95b5e1e1e18c26c8fd737f904b564c:4 \
--source-chain avalanche \
--source-address 0x90AD61b0FaC683b23543Ed39B8E3Bd418D6CcBfe \
Expand Down Expand Up @@ -127,7 +127,7 @@ where
After a few seconds, the `verify` command will exit displaying the `id`, and or an error if any:

```bash
node ./endpoints/verify.js --id 0x02293467b9d6e1ce51d8ac0fa24e9a30fb95b5e1e1e18c26c8fd737f904b564c:4 --source-chain avalanche --source-address 0x90AD61b0FaC683b23543Ed39B8E3Bd418D6CcBfe --destination-chain fantom --destination-address 0x9B35d37a8ebCb1d744ADdEC47CA2a939e811B638 --payload 00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000f68656c6c6f206176616c616e63686500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
node amplifier verify --id 0x02293467b9d6e1ce51d8ac0fa24e9a30fb95b5e1e1e18c26c8fd737f904b564c:4 --source-chain avalanche --source-address 0x90AD61b0FaC683b23543Ed39B8E3Bd418D6CcBfe --destination-chain fantom --destination-address 0x9B35d37a8ebCb1d744ADdEC47CA2a939e811B638 --payload 00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000f68656c6c6f206176616c616e63686500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Connecting to server at localhost:50051
Verifying message: {
message: {
Expand All @@ -147,7 +147,7 @@ Success verification for 0x02293467b9d6e1ce51d8ac0fa24e9a30fb95b5e1e1e18c26c8fd7
After a verification is initiated and once all internal processes (verifying, routing messages to the destination gateway, and constructing proof) are done on the Axelar network, a `signing-completed` event is emitted which contains a `session-id`. This `session-id` can be used to query the proof from the Axelar chain and return the execute data that need to be relayed on the destination chain. Do this by running `subscribe-to-approvals`:

```bash
node ./endpoints/subscribe-to-approvals.js \
node amplifer subscribe-to-approvals \
--chain fantom \
--start-height <start-height> # optional
```
Expand All @@ -158,7 +158,7 @@ node ./endpoints/subscribe-to-approvals.js \
For example:

```bash
$ node ./endpoints/subscribe-to-approvals.js -c fantom -s 221645
$ node amplifier subscribe-to-approvals -c fantom -s 221645
Subscribing to approvals starting from block: 221645 on chain: fantom
Connecting to server at localhost:50051
chain: fantom
Expand All @@ -172,7 +172,7 @@ execute data: 09c5eabe0000000000000000000000000000000000000000000000000000000000
To get the payload that was submitted by the transaction on the source chain, use `get-payload`:

```bash
$ node ./endpoints/get-payload.js --hash 0xa9b070ad799e19f1166fdbf4524b684f8026df510fe6a7770f949ad54047098c
$ node amplifier get-payload --hash 0xa9b070ad799e19f1166fdbf4524b684f8026df510fe6a7770f949ad54047098c
Getting payload for payload hash a9b070ad799e19f1166fdbf4524b684f8026df510fe6a7770f949ad54047098c
Connecting to server at localhost:50051
Payload:
Expand Down
52 changes: 52 additions & 0 deletions examples/amplifier/amplifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const commander = require('commander');
const { broadcast } = require('./endpoints/broadcast.js');
const { getPayload } = require('./endpoints/get-payload.js');
const { subscribe_to_approvals } = require('./endpoints/subscribe-to-approvals.js');
const { subscribe_to_wasm_events } = require('./endpoints/subscribe-to-wasm-events.js');
const { verify } = require('./endpoints/verify.js');

const program = new commander.Command();

program
.command('broadcast')
.requiredOption('-a, --address <contract address>', 'The address of the destination contract')
.requiredOption("-p, --payload <payload>", "The payload of the wasm message")
.action((options) => {
broadcast(options.address, options.payload);
});

program
.command('get-payload')
.requiredOption('--hash, <hash>', 'payload hash')
.action((options) => {
getPayload(options.hash);
});

program
.command('subscribe-to-approvals')
.requiredOption("-c, --chain <chain>", "The chain to subscribe to")
.option("-s, --start-height <start height>", "The block height to start from (0 = latest)", parseInt, 0)
.action((options) => {
subscribe_to_approvals(options.chain, options.startHeight);
});

program
.command('subscribe-to-wasm-events')
.option("-s, --start-height <start height>", "The block height to start from (0 = latest)", parseInt, 0)
.action((startHeight) => {
subscribe_to_wasm_events(startHeight)
});

program
.command('verify')
.requiredOption("-i, --id <transaction id>", "The id of the transaction (txHash:logIndex)")
.requiredOption("--source-chain <source chain>", "The source chain")
.requiredOption("--source-address <source address>", "The source address")
.requiredOption("--destination-chain <destination chain>", "The destination chain")
.requiredOption("--destination-address <destination address>", "The destination address")
.requiredOption("--payload <payload>", "The GMP payload in hex")
.action((options) => {
verify(options.id, options.sourceChain, options.sourceAddress, options.destinationChain, options.destinationAddress, options.payload);
});

program.parse();
54 changes: 21 additions & 33 deletions examples/amplifier/endpoints/broadcast.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
const commander = require('commander');
const newClient = require('../grpc/client');

commander
.usage(["[OPTIONS] ..."])
.requiredOption("-a, --address <contract address>", "The address of the destination contract")
.requiredOption("-p, --payload <payload>", "The payload of the wasm message")
.parse(process.argv);
function broadcast(address, payload) {
console.log("Broadcasting message:\n", address, payload);

const address = commander.opts().address;
if (!address) {
console.error("Address is required");
process.exit(1);
}
try {
JSON.parse(payload);
} catch (e) {
console.error("Payload is not valid JSON");
process.exit(1);
}

const payload = commander.opts().payload;
if (!payload) {
console.error("Payload is required");
process.exit(1);
}
try {
JSON.parse(payload);
} catch (e) {
console.error("Payload is not valid JSON");
process.exit(1);
const client = newClient();
const broadcastRequest = { address, payload: Buffer.from(payload) };
response = client.Broadcast(broadcastRequest, (err, response) => {
if (err) {
console.error("Error", err);
} else {
console.log("Message sent for broadcast");
process.exit(0);
}
});
}

console.log("Broadcasting message:\n", address, payload);

const client = newClient();
const broadcastRequest = { address, payload: Buffer.from(payload) };
response = client.Broadcast(broadcastRequest, (err, response) => {
if (err) {
console.error("Error", err);
} else {
console.log("Message sent for broadcast");
process.exit(0);
}
});
module.exports = {
broadcast,
};
38 changes: 19 additions & 19 deletions examples/amplifier/endpoints/get-payload.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
const commander = require('commander');
const newClient = require('../grpc/client');

commander
.usage(["[OPTIONS] ..."])
.requiredOption("--hash, <payload hash>", "The hash of the payload")
.parse(process.argv);
function getPayload(hash) {
console.log("Getting payload for payload hash", hash);

const hash = commander.opts().hash.replace('0x', '');
hash = hash.replace('0x', '');

console.log("Getting payload for payload hash", hash);
const client = newClient();
const getPayloadHashRequest = { hash: Buffer.from(hash, 'hex') };
response = client.GetPayload(getPayloadHashRequest, (err, response) => {
if (err) {
console.error("Error", err);
process.exit(1)
}

const client = newClient();
const getPayloadHashRequest = { hash: Buffer.from(hash, 'hex') };
response = client.GetPayload(getPayloadHashRequest, (err, response) => {
if (err) {
console.error("Error", err);
process.exit(1)
}
if (response) {
console.log("Payload:\n" + response.payload.toString('hex'));
process.exit(0)
}
});
}

if (response) {
console.log("Payload:\n" + response.payload.toString('hex'));
process.exit(0)
}
});
module.exports = {
getPayload,
};
52 changes: 23 additions & 29 deletions examples/amplifier/endpoints/subscribe-to-approvals.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
const commander = require('commander');
const newClient = require('../grpc/client');

commander
.usage(["[OPTIONS] ..."])
.requiredOption("-c, --chain <chain>", "The chain to subscribe to")
.option("-s, --start-height <start height>", "The block height to start from (0 = latest)", parseInt, 0)
.parse(process.argv);
function subscribe_to_approvals(chain, startHeight) {
console.log("Subscribing to approvals starting from block:", startHeight == 0 ? "latest" : startHeight, "on chain:", chain);

const options = commander.opts();
const client = newClient();

const chain = options.chain;
const startHeight = options.startHeight;
const call = client.SubscribeToApprovals({ startHeight: startHeight, chains: [chain] });
call.on('data', (response) => {
console.log("chain:", response.chain);
console.log("block height:", response.blockHeight.toString());
console.log("execute data:", response.executeData.toString('hex'));
console.log("---");
});
call.on('end', () => {
console.log("End");
});
call.on('error', (e) => {
console.log("Error", e);
});
call.on('status', (status) => {
console.log("Status", status);
});
}

console.log("Subscribing to approvals starting from block:", startHeight == 0 ? "latest" : startHeight, "on chain:", chain);

const client = newClient();

const call = client.SubscribeToApprovals({ startHeight: startHeight, chains: [chain] });
call.on('data', (response) => {
console.log("chain:", response.chain);
console.log("block height:", response.blockHeight.toString());
console.log("execute data:", response.executeData.toString('hex'));
console.log("---");
});
call.on('end', () => {
console.log("End");
});
call.on('error', (e) => {
console.log("Error", e);
});
call.on('status', (status) => {
console.log("Status", status);
});
module.exports = {
subscribe_to_approvals,
};
42 changes: 20 additions & 22 deletions examples/amplifier/endpoints/subscribe-to-wasm-events.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
const commander = require('commander');
const newClient = require('../grpc/client');

commander
.usage(["[OPTIONS] ..."])
.option("-s, --start-height <start height>", "The block height to start from (0 = latest)", parseInt, 0)
.parse(process.argv);
function subscribe_to_wasm_events(startHeight) {
console.log("Subscribing to events starting from block:", startHeight == 0 ? "latest" : startHeight);

startHeight = commander.opts().startHeight;
const client = newClient();
const call = client.SubscribeToWasmEvents({ startHeight: startHeight });
call.on('data', (response) => {
console.log("Event:", response);
});
call.on('end', () => {
console.log("End");
});
call.on('error', (e) => {
console.log("Error", e);
});
call.on('status', (status) => {
console.log("Status", status);
});
}

console.log("Subscribing to events starting from block:", startHeight == 0 ? "latest" : startHeight);

const client = newClient();
const call = client.SubscribeToWasmEvents({ startHeight: startHeight });
call.on('data', (response) => {
console.log("Event:", response);
});
call.on('end', () => {
console.log("End");
});
call.on('error', (e) => {
console.log("Error", e);
});
call.on('status', (status) => {
console.log("Status", status);
});
module.exports = {
subscribe_to_wasm_events,
};
Loading

0 comments on commit 952412f

Please sign in to comment.