Skip to content

Commit cfd9c6d

Browse files
authored
feat: support mainnet in starknet sdk example, fix formatting (#19)
1 parent a252035 commit cfd9c6d

File tree

5 files changed

+77
-45
lines changed

5 files changed

+77
-45
lines changed

price_feeds/starknet/sdk_js_usage/package-lock.json

Lines changed: 31 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

price_feeds/starknet/sdk_js_usage/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@pythnetwork/price-service-client": "^1.9.0",
22-
"@pythnetwork/pyth-starknet-js": "^0.2.0",
22+
"@pythnetwork/pyth-starknet-js": "^0.2.1",
2323
"starknet": "^6.9.0"
2424
}
2525
}
Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1-
import { Account, Contract, RpcProvider, shortString } from "starknet";
2-
import { PriceServiceConnection } from "@pythnetwork/price-service-client";
1+
import {Account, Contract, RpcProvider, shortString} from 'starknet';
2+
import {PriceServiceConnection} from '@pythnetwork/price-service-client';
33
import {
44
ByteBuffer,
55
ERC20_ABI,
66
STRK_TOKEN_ADDRESS,
77
PYTH_ABI,
88
PYTH_CONTRACT_ADDRESS_SEPOLIA,
9-
} from "@pythnetwork/pyth-starknet-js";
9+
PYTH_CONTRACT_ADDRESS_MAINNET,
10+
} from '@pythnetwork/pyth-starknet-js';
1011

1112
async function main() {
13+
const chain = process.env.CHAIN || 'sepolia';
14+
let nodeUrl;
15+
let pythAddress;
16+
if (chain == 'mainnet') {
17+
nodeUrl = 'https://starknet-mainnet.public.blastapi.io/rpc/v0_6';
18+
pythAddress = PYTH_CONTRACT_ADDRESS_MAINNET;
19+
} else if (chain == 'sepolia') {
20+
nodeUrl = 'https://starknet-sepolia.public.blastapi.io/rpc/v0_6';
21+
pythAddress = PYTH_CONTRACT_ADDRESS_SEPOLIA;
22+
} else {
23+
throw new Error('unknown chain');
24+
}
1225
// Create a provider for interacting with Starknet RPC.
13-
const provider = new RpcProvider({
14-
nodeUrl: "https://starknet-sepolia.public.blastapi.io/rpc/v0_6",
15-
});
26+
const provider = new RpcProvider({nodeUrl});
1627
console.log(
17-
"chain id: ",
28+
'chain id: ',
1829
shortString.decodeShortString(await provider.getChainId())
1930
);
20-
console.log("rpc version: ", await provider.getSpecVersion());
31+
console.log('rpc version: ', await provider.getSpecVersion());
2132

2233
// Create a `Contract` instance to interact with a fee token contract on Starknet
2334
// (you can use either STRK or ETH to pay fees, but using STRK is recommended).
@@ -28,34 +39,30 @@ async function main() {
2839
);
2940

3041
// Create a `Contract` instance to interact with the Pyth contract on Starknet.
31-
const pythContract = new Contract(
32-
PYTH_ABI,
33-
PYTH_CONTRACT_ADDRESS_SEPOLIA,
34-
provider
35-
);
42+
const pythContract = new Contract(PYTH_ABI, pythAddress, provider);
3643
const chain_id = await pythContract.chain_id();
37-
console.log("pyth chain id:", chain_id);
44+
console.log('pyth chain id:', chain_id);
3845

3946
const version = await pythContract.version();
40-
console.log("pyth version:", shortString.decodeShortString(version));
47+
console.log('pyth version:', shortString.decodeShortString(version));
4148

4249
// Import your account data from environment variables.
4350
// You'll need to set them before running the code.
4451
const privateKey0 = process.env.ACCOUNT_PRIVATE_KEY;
4552
if (privateKey0 === undefined) {
46-
throw new Error("missing ACCOUNT_PRIVATE_KEY");
53+
throw new Error('missing ACCOUNT_PRIVATE_KEY');
4754
}
4855
const account0Address = process.env.ACCOUNT_ADDRESS;
4956
if (account0Address === undefined) {
50-
throw new Error("missing ACCOUNT_ADDRESS");
57+
throw new Error('missing ACCOUNT_ADDRESS');
5158
}
5259
const account0 = new Account(provider, account0Address, privateKey0);
5360

5461
const balanceInitial = await strkErc0Contract.balanceOf(account0Address);
55-
console.log("account0 balance:", balanceInitial);
62+
console.log('account0 balance:', balanceInitial);
5663

5764
// Create a client for pulling price updates from Hermes.
58-
const connection = new PriceServiceConnection("https://hermes.pyth.network", {
65+
const connection = new PriceServiceConnection('https://hermes.pyth.network', {
5966
priceFeedRequestConfig: {
6067
// Provide this option to retrieve signed price updates for on-chain contracts.
6168
// Ignore this option for off-chain use.
@@ -64,51 +71,51 @@ async function main() {
6471
});
6572

6673
const priceFeedId =
67-
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; // ETH/USD
74+
'0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace'; // ETH/USD
6875
const previousPrice = await pythContract.get_price_unsafe(priceFeedId);
69-
console.log("previous price:", previousPrice);
76+
console.log('previous price:', previousPrice);
7077

71-
console.log("querying pyth update");
78+
console.log('querying pyth update');
7279
// Get the latest values of the price feeds as json objects.
7380
const currentPrices = await connection.getLatestPriceFeeds([priceFeedId]);
7481
if (currentPrices === undefined) {
75-
throw new Error("failed to get prices");
82+
throw new Error('failed to get prices');
7683
}
77-
console.log("current price:", currentPrices[0]);
84+
console.log('current price:', currentPrices[0]);
7885

7986
if (!currentPrices[0].vaa) {
80-
throw new Error("missing vaa in response");
87+
throw new Error('missing vaa in response');
8188
}
8289

8390
// Convert the price update to Starknet format.
8491
const pythUpdate = ByteBuffer.fromBase64(currentPrices[0].vaa);
8592

8693
// Query the amount of fee required by Pyth.
87-
console.log("querying pyth fee");
94+
console.log('querying pyth fee');
8895
const fee = await pythContract.get_update_fee(
8996
pythUpdate,
9097
strkErc0Contract.address
9198
);
92-
console.log("pyth fee:", fee);
99+
console.log('pyth fee:', fee);
93100

94101
// Approve fee withdrawal.
95-
console.log("approving fee");
102+
console.log('approving fee');
96103
strkErc0Contract.connect(account0);
97104
let tx = await strkErc0Contract.approve(pythContract.address, fee);
98-
console.log("waiting for tx");
105+
console.log('waiting for tx');
99106
await provider.waitForTransaction(tx.transaction_hash);
100107

101108
pythContract.connect(account0);
102109

103110
// Create a transaction and submit to your contract using the price update data.
104-
console.log("updating price feeds");
111+
console.log('updating price feeds');
105112
tx = await pythContract.update_price_feeds(pythUpdate);
106-
console.log("waiting for tx");
113+
console.log('waiting for tx');
107114
await provider.waitForTransaction(tx.transaction_hash);
108-
console.log("transaction confirmed:", tx.transaction_hash);
115+
console.log('transaction confirmed:', tx.transaction_hash);
109116

110-
const newPrice = await pythContract.get_price_no_older_than(priceFeedId, 60);
111-
console.log("new price:", newPrice);
117+
const newPrice = await pythContract.get_price_no_older_than(priceFeedId, 120);
118+
console.log('new price:', newPrice);
112119
}
113120

114121
main();

price_feeds/starknet/send_usd/client/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
},
2020
"dependencies": {
2121
"@pythnetwork/price-service-client": "^1.9.0",
22-
"@pythnetwork/pyth-starknet-js": "^0.2.0",
22+
"@pythnetwork/pyth-starknet-js": "^0.2.1",
2323
"starknet": "^6.9.0"
2424
}
2525
}

price_feeds/starknet/send_usd/client/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ async function main() {
4444
const balanceInitial = await ethErc0Contract.balanceOf(account0Address);
4545
console.log('account0 balance:', balanceInitial);
4646

47-
const initialDestinationBalance =
48-
await ethErc0Contract.balanceOf(destination);
47+
const initialDestinationBalance = await ethErc0Contract.balanceOf(
48+
destination
49+
);
4950
console.log('destination balance:', initialDestinationBalance);
5051

5152
// Create a client for pulling price updates from Hermes.

0 commit comments

Comments
 (0)