Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amplifier): add save payload #184

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/amplifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ The following endpoints are available to facilitate GMP calls:
1. `verify` -- triggers a verification on the source chain (routing is handled automatically)
2. `subscribe-to-approvals` -- creates a channel to return all calls that are approved on the destination chain
3. `get-payload` -- queries the payload of the initial source-chain transaction by its hash
4. `save-payload` -- stores a payload of the initial source-chain transaction and returns its hash

### `verify`

Expand Down Expand Up @@ -219,6 +220,20 @@ execute data: 09c5eabe0000000000000000000000000000000000000000000000000000000000
---
```

### `save-payload`

To save a payload that was submitted by the transaction on the source chain, use `save-payload`:

```bash
$ node amplifier save-payload --payload 00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000f68656c6c6f206176616c616e63686500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Saving payload 00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000f68656c6c6f206176616c616e63686500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Connecting to server at localhost:50051
Payload hash:
0xa9b070ad799e19f1166fdbf4524b684f8026df510fe6a7770f949ad54047098c
```

- `payload` -- the payload

### `get-payload`

To get the payload that was submitted by the transaction on the source chain, use `get-payload`:
Expand Down
10 changes: 9 additions & 1 deletion examples/amplifier/amplifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const commander = require('commander');
const { broadcast } = require('./endpoints/broadcast.js');
const { getReceipt } = require('./endpoints/get-receipt.js');
const { getPayload } = require('./endpoints/get-payload.js');
const { savePayload } = require('./endpoints/save-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');
Expand Down Expand Up @@ -30,6 +31,13 @@ program
getPayload(options.hash);
});

program
.command('save-payload')
.requiredOption('--payload, <payload>', 'payload')
.action((options) => {
savePayload(options.payload);
});

program
.command('subscribe-to-approvals')
.requiredOption("-c, --chain <chain>", "The chain to subscribe to")
Expand All @@ -47,7 +55,7 @@ program

program
.command('verify')
.requiredOption("-i, --id <transaction id>", "The id of the transaction (txHash:logIndex)")
.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")
Expand Down
10 changes: 10 additions & 0 deletions examples/amplifier/amplifier.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ service Amplifier {
get : "/v1beta1/receipt/{receipt_id}"
};
}
rpc SavePayload(SavePayloadRequest) returns (SavePayloadResponse) {
option (google.api.http) = {
post : "/v1beta1/payload"
body : "*"
};
}
}

message Message {
Expand All @@ -43,6 +49,10 @@ message GetPayloadRequest { bytes hash = 1; }

message GetPayloadResponse { bytes payload = 1; }

message SavePayloadRequest { bytes payload = 1; }

message SavePayloadResponse { bytes hash = 1; }

message SubscribeToApprovalsRequest {
repeated string chains = 1;
optional uint64 start_height = 2; // can be used to replay events
Expand Down
23 changes: 23 additions & 0 deletions examples/amplifier/endpoints/save-payload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const newClient = require('../grpc/client');

function savePayload(payload) {
console.log("Saving payload", payload);

const client = newClient();
const savePayloadRequest = { payload: Buffer.from(payload, 'hex') };
response = client.SavePayload(savePayloadRequest, (err, response) => {
if (err) {
console.error("Error", err);
process.exit(1)
}

if (response) {
console.log("Payload hash:\n" + response.hash);
process.exit(0)
}
});
}

module.exports = {
savePayload,
};
2 changes: 1 addition & 1 deletion examples/amplifier/endpoints/subscribe-to-wasm-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function subscribe_to_wasm_events(startHeight) {
console.log("Subscribing to events starting from block:", startHeight == 0 ? "latest" : startHeight);

const client = newClient();
const call = client.SubscribeToWasmEvents({ startHeight: startHeight });
const call = client.SubscribeToWasmEvents(startHeight);
call.on('data', (response) => {
console.log("Event:", response);
});
Expand Down
4 changes: 2 additions & 2 deletions examples/amplifier/endpoints/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const newClient = require('../grpc/client');
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);

if (id.split(':').length != 2) {
console.error("Invalid transaction id. Expected format: txHash:logIndex");
if (id.split('-').length != 2) {
console.error("Invalid transaction id. Expected format: txHash-logIndex");
process.exit(1);
}
payload = payload.replace('0x', '');
Expand Down
Loading