forked from smartcontractkit/solana-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
62 lines (50 loc) · 2.12 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Parse arguments
// --program - [Required] The account address for your deployed program.
// --feed - The account address for the Chainlink data feed to retrieve
const args = require('minimist')(process.argv.slice(2));
// Initialize Anchor and provider
const anchor = require("@project-serum/anchor");
const provider = anchor.AnchorProvider.env();
// Configure the cluster.
anchor.setProvider(provider);
const CHAINLINK_PROGRAM_ID = "HEvSKofvBgfaexv23kMabbYqxasxU3mQ4ibBMEmJWHny";
const DIVISOR = 100000000;
// Data feed account address
// Default is SOL / USD
const default_feed = "99B2bTijsU6f1GCT73HmdR7HCFFjGMBcPZY6jZ96ynrR";
const CHAINLINK_FEED = args['feed'] || default_feed;
async function main() {
// Read the generated IDL.
const idl = JSON.parse(
require("fs").readFileSync("./target/idl/chainlink_solana_demo.json", "utf8")
);
// Address of the deployed program.
const programId = new anchor.web3.PublicKey(args['program']);
// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);
//create an account to store the price data
const priceFeedAccount = anchor.web3.Keypair.generate();
console.log('priceFeedAccount public key: ' + priceFeedAccount.publicKey);
console.log('user public key: ' + provider.wallet.publicKey);
// Execute the RPC.
let tx = await program.rpc.execute({
accounts: {
decimal: priceFeedAccount.publicKey,
user: provider.wallet.publicKey,
chainlinkFeed: CHAINLINK_FEED,
chainlinkProgram: CHAINLINK_PROGRAM_ID,
systemProgram: anchor.web3.SystemProgram.programId
},
options: { commitment: "confirmed" },
signers: [priceFeedAccount],
});
console.log("Fetching transaction logs...");
let t = await provider.connection.getConfirmedTransaction(tx, "confirmed");
console.log(t.meta.logMessages);
// #endregion main
// 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("Running client...");
main().then(() => console.log("Success"));