Skip to content
Open
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
85 changes: 45 additions & 40 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
// Initialize Anchor and provider
const anchor = require("@coral-xyz/anchor")
const anchor = require("@coral-xyz/anchor");
const args = require("minimist")(process.argv.slice(2));

// Set up provider and program
anchor.setProvider(anchor.AnchorProvider.env());
const provider = anchor.getProvider();
const program = anchor.workspace.ChainlinkSolanaDemo;

const CHAINLINK_PROGRAM_ID = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny";
const DIVISOR = 100000000;

// Data feed account address
// Default is SOL / USD
const args = require('minimist')(process.argv.slice(2));
default_feed = "99B2bTijsU6f1GCT73HmdR7HCFFjGMBcPZY6jZ96ynrR"
const CHAINLINK_FEED = args['feed'] || default_feed
const DEFAULT_FEED = "99B2bTijsU6f1GCT73HmdR7HCFFjGMBcPZY6jZ96ynrR"; // SOL/USD
const CHAINLINK_FEED = args["feed"] || DEFAULT_FEED;

async function main() {
// Create program client
const program = anchor.workspace.ChainlinkSolanaDemo
console.log(`trying the interact with program: ${program.programId}`)
console.log(`Interacting with program: ${program.programId}`);

//create an account to store the price data
const priceFeedAccount = anchor.web3.Keypair.generate();
console.log('priceFeedAccount public key: ' + priceFeedAccount.publicKey);

// Execute the RPC.
let transactionSignature = await program.methods
.execute()
.accounts({
decimal: priceFeedAccount.publicKey,
chainlinkFeed: CHAINLINK_FEED,
chainlinkProgram: CHAINLINK_PROGRAM_ID,
})
.signers([priceFeedAccount])
.rpc()

console.log(`Transaction Signature: ${transactionSignature}`)

// show logs for transaction
console.log("Fetching transaction logs...");
const txDetails = await program.provider
.connection.getConfirmedTransaction(transactionSignature, "confirmed");

const txLogs = txDetails?.meta?.logMessages || null;
console.log(txLogs)


// Fetch the account details of the account containing the price data
const latestPrice = await program.account.decimal.fetch(priceFeedAccount.publicKey);
console.log('Price Is: ' + latestPrice.value / DIVISOR)
console.log(`Generated account: ${priceFeedAccount.publicKey}`);

try {
// Execute the RPC
const txSignature = await program.methods
.execute()
.accounts({
decimal: priceFeedAccount.publicKey,
chainlinkFeed: CHAINLINK_FEED,
chainlinkProgram: CHAINLINK_PROGRAM_ID,
})
.signers([priceFeedAccount])
.rpc();

console.log(`Transaction Signature: ${txSignature}`);

// Fetch and log transaction details
const txDetails = await provider.connection.getTransaction(txSignature, {
commitment: "confirmed",
});

const txLogs = txDetails?.meta?.logMessages ?? [];
console.log("Transaction Logs: ", txLogs);

// Fetch and log price data
const latestPrice = await program.account.decimal.fetch(
priceFeedAccount.publicKey
);

console.log(`Price: ${latestPrice.value / DIVISOR}`);
} catch (error) {
console.error("Error running the program:", error);
}
}

console.log("Running client...");
main().then(() => console.log("Success"));
main()
.then(() => console.log("Success"))
.catch(console.error);