Skip to content

feat: support mainnet in starknet sdk example, fix formatting #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions price_feeds/starknet/sdk_js_usage/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion price_feeds/starknet/sdk_js_usage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
75 changes: 41 additions & 34 deletions price_feeds/starknet/sdk_js_usage/src/index.ts
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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.
Expand All @@ -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();
2 changes: 1 addition & 1 deletion price_feeds/starknet/send_usd/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
5 changes: 3 additions & 2 deletions price_feeds/starknet/send_usd/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading