-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrelay-submit-data.mjs
More file actions
73 lines (64 loc) · 1.85 KB
/
Copy pathrelay-submit-data.mjs
File metadata and controls
73 lines (64 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env node
/**
* Relay submit_data to citizen-science-registry on Osmosis.
* Reads JSON from stdin, writes JSON to stdout.
*
* Input: { rpc, denom, contract, mnemonic, sensor_id, data }
* Output: { ok: true, tx_hash } | { ok: false, error }
*/
import { readFileSync } from "node:fs";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
const require = createRequire(
resolve(dirname(fileURLToPath(import.meta.url)), "package.json"),
);
const { SigningCosmWasmClient } = require("@cosmjs/cosmwasm-stargate");
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing");
async function main() {
const raw = readFileSync(0, "utf8");
const input = JSON.parse(raw);
const {
rpc,
denom = "uosmo",
contract,
mnemonic,
sensor_id: sensorId,
data,
} = input;
if (!rpc || !contract || !mnemonic || sensorId == null || !data) {
process.stdout.write(
JSON.stringify({ ok: false, error: "missing required relay fields" }),
);
process.exit(1);
}
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic.trim(), {
prefix: "osmo",
});
const [account] = await wallet.getAccounts();
const client = await SigningCosmWasmClient.connectWithSigner(rpc, wallet);
const fee = {
amount: [{ denom, amount: "80000" }],
gas: "1500000",
};
const msg = { submit_data: { sensor_id: Number(sensorId), data } };
const res = await client.execute(
account.address,
contract,
msg,
fee,
"agent-gateway drone measurement",
);
process.stdout.write(
JSON.stringify({ ok: true, tx_hash: res.transactionHash }),
);
}
main().catch((err) => {
process.stdout.write(
JSON.stringify({
ok: false,
error: err instanceof Error ? err.message : String(err),
}),
);
process.exit(1);
});