diff --git a/price_feeds/starknet/sdk_js_usage/package-lock.json b/price_feeds/starknet/sdk_js_usage/package-lock.json index 0c70013..45b5560 100644 --- a/price_feeds/starknet/sdk_js_usage/package-lock.json +++ b/price_feeds/starknet/sdk_js_usage/package-lock.json @@ -6,7 +6,7 @@ "": { "dependencies": { "@pythnetwork/price-service-client": "^1.9.0", - "@pythnetwork/pyth-starknet-js": "^0.2.0", + "@pythnetwork/pyth-starknet-js": "^0.2.1", "starknet": "^6.9.0" }, "devDependencies": { @@ -17,6 +17,22 @@ "typescript": "^5.4.5" } }, + "../../../../pyth-crosschain/target_chains/starknet/sdk/js": { + "name": "@pythnetwork/pyth-starknet-js", + "version": "0.2.1", + "license": "Apache-2.0", + "devDependencies": { + "@types/node": "^18.11.18", + "@typescript-eslint/eslint-plugin": "^5.21.0", + "@typescript-eslint/parser": "^5.21.0", + "eslint": "^8.14.0", + "jest": "^29.4.1", + "prettier": "^2.6.2", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.2", + "typescript": "^4.6.3" + } + }, "node_modules/@babel/code-frame": { "version": "7.24.7", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.24.7.tgz", @@ -357,9 +373,8 @@ } }, "node_modules/@pythnetwork/pyth-starknet-js": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-starknet-js/-/pyth-starknet-js-0.2.0.tgz", - "integrity": "sha512-wJocXpuVWcEkTpniN8UYf8Jd/GOaSu/C/H53AMd24jbpu/WAkDLgm2Q4CVtCFn8tpGNJFE+MUkHcoRSYzpnfuA==" + "resolved": "../../../../pyth-crosschain/target_chains/starknet/sdk/js", + "link": true }, "node_modules/@scure/base": { "version": "1.1.7", @@ -3984,9 +3999,18 @@ } }, "@pythnetwork/pyth-starknet-js": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/@pythnetwork/pyth-starknet-js/-/pyth-starknet-js-0.2.0.tgz", - "integrity": "sha512-wJocXpuVWcEkTpniN8UYf8Jd/GOaSu/C/H53AMd24jbpu/WAkDLgm2Q4CVtCFn8tpGNJFE+MUkHcoRSYzpnfuA==" + "version": "file:../../../../pyth-crosschain/target_chains/starknet/sdk/js", + "requires": { + "@types/node": "^18.11.18", + "@typescript-eslint/eslint-plugin": "^5.21.0", + "@typescript-eslint/parser": "^5.21.0", + "eslint": "^8.14.0", + "jest": "^29.4.1", + "prettier": "^2.6.2", + "ts-jest": "^29.0.5", + "ts-node": "^10.9.2", + "typescript": "^4.6.3" + } }, "@scure/base": { "version": "1.1.7", diff --git a/price_feeds/starknet/sdk_js_usage/package.json b/price_feeds/starknet/sdk_js_usage/package.json index 6637238..d9adfbc 100644 --- a/price_feeds/starknet/sdk_js_usage/package.json +++ b/price_feeds/starknet/sdk_js_usage/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@pythnetwork/price-service-client": "^1.9.0", - "@pythnetwork/pyth-starknet-js": "^0.2.0", + "@pythnetwork/pyth-starknet-js": "^0.2.1", "starknet": "^6.9.0" } } diff --git a/price_feeds/starknet/sdk_js_usage/src/index.ts b/price_feeds/starknet/sdk_js_usage/src/index.ts index ee875bf..f0084ba 100644 --- a/price_feeds/starknet/sdk_js_usage/src/index.ts +++ b/price_feeds/starknet/sdk_js_usage/src/index.ts @@ -1,23 +1,34 @@ -import { Account, Contract, RpcProvider, shortString } from "starknet"; -import { PriceServiceConnection } from "@pythnetwork/price-service-client"; +import {Account, Contract, RpcProvider, shortString} from 'starknet'; +import {PriceServiceConnection} from '@pythnetwork/price-service-client'; import { ByteBuffer, ERC20_ABI, STRK_TOKEN_ADDRESS, PYTH_ABI, PYTH_CONTRACT_ADDRESS_SEPOLIA, -} from "@pythnetwork/pyth-starknet-js"; + PYTH_CONTRACT_ADDRESS_MAINNET, +} from '@pythnetwork/pyth-starknet-js'; async function main() { + const chain = process.env.CHAIN || 'sepolia'; + let nodeUrl; + let pythAddress; + if (chain == 'mainnet') { + nodeUrl = 'https://starknet-mainnet.public.blastapi.io/rpc/v0_6'; + pythAddress = PYTH_CONTRACT_ADDRESS_MAINNET; + } else if (chain == 'sepolia') { + nodeUrl = 'https://starknet-sepolia.public.blastapi.io/rpc/v0_6'; + pythAddress = PYTH_CONTRACT_ADDRESS_SEPOLIA; + } else { + throw new Error('unknown chain'); + } // Create a provider for interacting with Starknet RPC. - const provider = new RpcProvider({ - nodeUrl: "https://starknet-sepolia.public.blastapi.io/rpc/v0_6", - }); + const provider = new RpcProvider({nodeUrl}); console.log( - "chain id: ", + 'chain id: ', shortString.decodeShortString(await provider.getChainId()) ); - console.log("rpc version: ", await provider.getSpecVersion()); + console.log('rpc version: ', await provider.getSpecVersion()); // Create a `Contract` instance to interact with a fee token contract on Starknet // (you can use either STRK or ETH to pay fees, but using STRK is recommended). @@ -28,34 +39,30 @@ async function main() { ); // Create a `Contract` instance to interact with the Pyth contract on Starknet. - const pythContract = new Contract( - PYTH_ABI, - PYTH_CONTRACT_ADDRESS_SEPOLIA, - provider - ); + const pythContract = new Contract(PYTH_ABI, pythAddress, provider); const chain_id = await pythContract.chain_id(); - console.log("pyth chain id:", chain_id); + console.log('pyth chain id:', chain_id); const version = await pythContract.version(); - console.log("pyth version:", shortString.decodeShortString(version)); + console.log('pyth version:', shortString.decodeShortString(version)); // Import your account data from environment variables. // You'll need to set them before running the code. const privateKey0 = process.env.ACCOUNT_PRIVATE_KEY; if (privateKey0 === undefined) { - throw new Error("missing ACCOUNT_PRIVATE_KEY"); + throw new Error('missing ACCOUNT_PRIVATE_KEY'); } const account0Address = process.env.ACCOUNT_ADDRESS; if (account0Address === undefined) { - throw new Error("missing ACCOUNT_ADDRESS"); + throw new Error('missing ACCOUNT_ADDRESS'); } const account0 = new Account(provider, account0Address, privateKey0); const balanceInitial = await strkErc0Contract.balanceOf(account0Address); - console.log("account0 balance:", balanceInitial); + console.log('account0 balance:', balanceInitial); // Create a client for pulling price updates from Hermes. - const connection = new PriceServiceConnection("https://hermes.pyth.network", { + const connection = new PriceServiceConnection('https://hermes.pyth.network', { priceFeedRequestConfig: { // Provide this option to retrieve signed price updates for on-chain contracts. // Ignore this option for off-chain use. @@ -64,51 +71,51 @@ async function main() { }); const priceFeedId = - "0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace"; // ETH/USD + '0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace'; // ETH/USD const previousPrice = await pythContract.get_price_unsafe(priceFeedId); - console.log("previous price:", previousPrice); + console.log('previous price:', previousPrice); - console.log("querying pyth update"); + console.log('querying pyth update'); // Get the latest values of the price feeds as json objects. const currentPrices = await connection.getLatestPriceFeeds([priceFeedId]); if (currentPrices === undefined) { - throw new Error("failed to get prices"); + throw new Error('failed to get prices'); } - console.log("current price:", currentPrices[0]); + console.log('current price:', currentPrices[0]); if (!currentPrices[0].vaa) { - throw new Error("missing vaa in response"); + throw new Error('missing vaa in response'); } // Convert the price update to Starknet format. const pythUpdate = ByteBuffer.fromBase64(currentPrices[0].vaa); // Query the amount of fee required by Pyth. - console.log("querying pyth fee"); + console.log('querying pyth fee'); const fee = await pythContract.get_update_fee( pythUpdate, strkErc0Contract.address ); - console.log("pyth fee:", fee); + console.log('pyth fee:', fee); // Approve fee withdrawal. - console.log("approving fee"); + console.log('approving fee'); strkErc0Contract.connect(account0); let tx = await strkErc0Contract.approve(pythContract.address, fee); - console.log("waiting for tx"); + console.log('waiting for tx'); await provider.waitForTransaction(tx.transaction_hash); pythContract.connect(account0); // Create a transaction and submit to your contract using the price update data. - console.log("updating price feeds"); + console.log('updating price feeds'); tx = await pythContract.update_price_feeds(pythUpdate); - console.log("waiting for tx"); + console.log('waiting for tx'); await provider.waitForTransaction(tx.transaction_hash); - console.log("transaction confirmed:", tx.transaction_hash); + console.log('transaction confirmed:', tx.transaction_hash); - const newPrice = await pythContract.get_price_no_older_than(priceFeedId, 60); - console.log("new price:", newPrice); + const newPrice = await pythContract.get_price_no_older_than(priceFeedId, 120); + console.log('new price:', newPrice); } main(); diff --git a/price_feeds/starknet/send_usd/client/package.json b/price_feeds/starknet/send_usd/client/package.json index 6637238..d9adfbc 100644 --- a/price_feeds/starknet/send_usd/client/package.json +++ b/price_feeds/starknet/send_usd/client/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@pythnetwork/price-service-client": "^1.9.0", - "@pythnetwork/pyth-starknet-js": "^0.2.0", + "@pythnetwork/pyth-starknet-js": "^0.2.1", "starknet": "^6.9.0" } } diff --git a/price_feeds/starknet/send_usd/client/src/index.ts b/price_feeds/starknet/send_usd/client/src/index.ts index 1eb2c02..30b729b 100644 --- a/price_feeds/starknet/send_usd/client/src/index.ts +++ b/price_feeds/starknet/send_usd/client/src/index.ts @@ -44,8 +44,9 @@ async function main() { const balanceInitial = await ethErc0Contract.balanceOf(account0Address); console.log('account0 balance:', balanceInitial); - const initialDestinationBalance = - await ethErc0Contract.balanceOf(destination); + const initialDestinationBalance = await ethErc0Contract.balanceOf( + destination + ); console.log('destination balance:', initialDestinationBalance); // Create a client for pulling price updates from Hermes.