Skip to content

Commit bab6ccc

Browse files
authored
add getPrice call to example (#666)
1 parent 60b1a36 commit bab6ccc

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

target_chains/ethereum/sdk/js/src/examples/EvmRelay.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,41 @@ async function run() {
8080
.call();
8181
console.log(`Update fee: ${updateFee}`);
8282

83-
pythContract.methods
83+
let txHash = undefined;
84+
await pythContract.methods
8485
.updatePriceFeeds(priceFeedUpdateData)
8586
.send({ value: updateFee })
8687
.on("transactionHash", (hash: string) => {
87-
console.log(`Tx hash: ${hash}`);
88+
txHash = hash;
8889
})
8990
.on("error", (err: any, receipt: any) => {
9091
console.error(receipt);
9192
throw err;
9293
});
9394

95+
console.log(`Tx hash: ${txHash}`);
96+
if (txHash === undefined) {
97+
console.error("Something went wrong. Could not send price update tx.");
98+
} else {
99+
console.log("Awaiting tx confirmation...");
100+
let receipt = undefined;
101+
while (!receipt) {
102+
receipt = await web3.eth.getTransactionReceipt(txHash);
103+
}
104+
105+
// For on-chain use, you will typically perform the getPrice call within the same transaction as updatePriceFeeds.
106+
// The call to getPrice below simply demonstrates that the on-chain price was in fact updated.
107+
// Note that the code above for waiting for tx confirmation is a little flaky -- if so, you may see an old price printed here.
108+
for (const priceId of priceIds) {
109+
const [price, conf, expo, publishTime] = await pythContract.methods
110+
.getPrice(priceId)
111+
.call();
112+
console.log(
113+
`Updated ${priceId} to (${price} +- ${conf}) * 10^${expo} at unix timestamp ${publishTime}`
114+
);
115+
}
116+
}
117+
94118
provider.engine.stop();
95119
}
96120

0 commit comments

Comments
 (0)