Skip to content

Commit

Permalink
chore: export commands functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdaveas committed Mar 26, 2024
1 parent 7b08fb1 commit 6ac1cd8
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 162 deletions.
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,
};
108 changes: 49 additions & 59 deletions examples/amplifier/endpoints/verify.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,54 @@
const commander = require('commander');
const newClient = require('../grpc/client');

commander
.usage(["[OPTIONS] ..."])
.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")
.parse(process.argv);
function verify(id, sourceChain, sourceAddress, destinationChain, destinationAddress, payload) {
console.log("Verifying message with id, sourceChain, sourceAddress, destinationChain, destinationAddress, and payload:", id, sourceChain, sourceAddress, destinationChain, destinationAddress, payload);

const options = commander.opts();

const id = options.id;
if (id.split(':').length != 2) {
console.error("Invalid transaction id. Expected format: txHash:logIndex");
process.exit(1);
}
const sourceChain = options.sourceChain;
const sourceAddress = options.sourceAddress;
const destinationChain = options.destinationChain;
const destinationAddress = options.destinationAddress;
const payload = options.payload.replace('0x', '');

request = {
message: {
id: id,
sourceChain: sourceChain,
sourceAddress: sourceAddress,
destinationChain: destinationChain,
destinationAddress: destinationAddress,
payload: Buffer.from(payload, 'hex'),
},
};

const client = newClient();
const verifyStream = client.Verify();

console.log("Verifying message:", request);

verifyStream.on('data', function (response) {
if (response.error) {
console.error('Error:', response.error);
} else {
console.log('Success verification for', response.message.id);
process.exit(0);
if (id.split(':').length != 2) {
console.error("Invalid transaction id. Expected format: txHash:logIndex");
process.exit(1);
}
});

verifyStream.on('end', function () {
console.log('Server has completed sending responses.');
});

verifyStream.on('error', function (e) {
console.error('Error: ', e);
});

verifyStream.on('status', function (status) {
console.log('Status: ', status);
});
payload = payload.replace('0x', '');

request = {
message: {
id: id,
sourceChain: sourceChain,
sourceAddress: sourceAddress,
destinationChain: destinationChain,
destinationAddress: destinationAddress,
payload: Buffer.from(payload, 'hex'),
},
};

const client = newClient();
const verifyStream = client.Verify();

console.log("Verifying message:", request);

verifyStream.on('data', function (response) {
if (response.error) {
console.error('Error:', response.error);
} else {
console.log('Success verification for', response.message.id);
process.exit(0);
}
});

verifyStream.on('end', function () {
console.log('Server has completed sending responses.');
});

verifyStream.on('error', function (e) {
console.error('Error: ', e);
});

verifyStream.on('status', function (status) {
console.log('Status: ', status);
});

verifyStream.write(request);
}

verifyStream.write(request);
module.exports = {
verify,
};

0 comments on commit 6ac1cd8

Please sign in to comment.