Skip to content

Commit 96f5f88

Browse files
committed
fix: add redstone.ts script
1 parent 1bf1f40 commit 96f5f88

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

script/redstone.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { DataServiceWrapper } from "@redstone-finance/evm-connector/dist/src/wrappers/DataServiceWrapper";
2+
import { ethers } from "ethers";
3+
import { arrayify } from "ethers/lib/utils";
4+
import { RedstonePayloadParser } from "redstone-protocol/dist/src/redstone-payload/RedstonePayloadParser";
5+
6+
async function getRedstonePayloadForManualUsage(
7+
dataServiceId: string,
8+
dataFeed: string,
9+
signersCount: number,
10+
): Promise<string> {
11+
// REDSTONE_GATEWAYS env variable can be used to provide local caching proxies that prevent rate limiting
12+
// it's passed when attach tests are executed on testnets
13+
const gateways = process.env.REDSTONE_GATEWAYS
14+
? process.env.REDSTONE_GATEWAYS.split(",")
15+
: undefined;
16+
const dataPayload = await new DataServiceWrapper({
17+
dataServiceId,
18+
dataFeeds: [dataFeed === "STETH" ? "stETH" : dataFeed],
19+
uniqueSignersCount: signersCount,
20+
urls: gateways,
21+
}).prepareRedstonePayload(true);
22+
23+
const parser = new RedstonePayloadParser(arrayify(`0x${dataPayload}`));
24+
const { signedDataPackages } = parser.parse();
25+
26+
let dataPackageIndex = 0;
27+
let ts = 0;
28+
for (const signedDataPackage of signedDataPackages) {
29+
const newTimestamp =
30+
signedDataPackage.dataPackage.timestampMilliseconds / 1000;
31+
32+
if (dataPackageIndex === 0) {
33+
ts = newTimestamp;
34+
} else if (ts !== newTimestamp) {
35+
throw new Error("Timestamps are not equal");
36+
}
37+
38+
++dataPackageIndex;
39+
}
40+
41+
const result = ethers.utils.defaultAbiCoder.encode(
42+
["uint256", "bytes"],
43+
[ts, arrayify(`0x${dataPayload}`)],
44+
);
45+
46+
return result;
47+
}
48+
49+
if (process.argv.length !== 5) {
50+
console.error(
51+
"Usage: npx node redstone.ts <data-service-id> <data-feed-id> <num-signers>",
52+
);
53+
process.exit(1);
54+
}
55+
56+
getRedstonePayloadForManualUsage(
57+
process.argv[2],
58+
process.argv[3],
59+
Number(process.argv[4]),
60+
)
61+
.then(payload => {
62+
console.log(`${payload}`);
63+
})
64+
.catch(error => {
65+
console.error(error);
66+
});

0 commit comments

Comments
 (0)