Skip to content

Commit

Permalink
Add instructions + example client for devnet
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jan 24, 2022
1 parent c488b7d commit 19a737e
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 6 deletions.
6 changes: 4 additions & 2 deletions contracts/examples/hello-world/Cargo.lock

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

61 changes: 61 additions & 0 deletions contracts/examples/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# chainlink-solana

## Running the example on devnet

Generate a new wallet:

```
solana-keygen new -o id.json
solana-keygen pubkey id.json
```

Airdrop some tokens:

```
solana airdrop 4 <RECIPIENT_ACCOUNT_ADDRESS> --url https://api.devnet.solana.com
```

Build and deploy the contract:

```
anchor build
anchor deploy --provider.cluster devnet
```

Extract the `program_id` and insert it into `client.js`

```
solana-keygen pubkey target/deploy/hello_world-keypair.json
```

Now run the Node.JS client:

```
ANCHOR_PROVIDER_URL=https://api.devnet.solana.com ANCHOR_WALLET=id.json node client.js
Running client...
Fetching transaction logs...
[
'Program <PROGRAM_ID> invoke [1]',
'Program log: Instruction: Execute',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g invoke [2]',
'Program log: Instruction: Query',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g consumed 2916 of 196135 compute units',
'Program return: DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g 6NQDALZQ7mEAAAAAQH32BQAAAAAAAAAAAAAAAA==',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g success',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g invoke [2]',
'Program log: Instruction: Query',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g consumed 2943 of 189793 compute units',
'Program return: DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g CgAAAFVTRFQgLyBVU0Q=',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g success',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g invoke [2]',
'Program log: Instruction: Query',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g consumed 2332 of 183111 compute units',
'Program return: DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g CA==',
'Program DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g success',
'Program log: USDT / USD price is 1.00040000',
'Program ENzTWES9dJhCDh2cBdxysQyzBUTxUqokPcojSj5phykw consumed 21585 of 200000 compute units',
'Program return: DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g CA==',
'Program ENzTWES9dJhCDh2cBdxysQyzBUTxUqokPcojSj5phykw success'
]
Success
```
42 changes: 42 additions & 0 deletions contracts/examples/hello-world/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const anchor = require("@project-serum/anchor");

// devnet IDs
const CHAINLINK_PROGRAM_ID = "DWqYEinRbZWtuq1DiDYvmexAKFoyjSyazZZUvdgPHT5g";
// USDT/USD
const CHAINLINK_FEED = "CwBg8pxL73LvuJ781cWBGF1e64G2z7AbZ22J2g8Lp35a";

const provider = anchor.Provider.env();

// Configure the cluster.
anchor.setProvider(provider);

async function main() {
// #region main
// Read the generated IDL.
const idl = JSON.parse(
require("fs").readFileSync("./target/idl/hello_world.json", "utf8")
);

// Address of the deployed program.
const programId = new anchor.web3.PublicKey("<YOUR-PROGRAM-ID>");

// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);

// Execute the RPC.
let tx = await program.rpc.execute({
accounts: {
chainlinkFeed: CHAINLINK_FEED,
chainlinkProgram: CHAINLINK_PROGRAM_ID
},
options: { commitment: "confirmed" },
});

console.log("Fetching transaction logs...");
let t = await provider.connection.getConfirmedTransaction(tx, "confirmed");
console.log(t.meta.logMessages);
// #endregion main
}

console.log("Running client...");
main().then(() => console.log("Success"));
16 changes: 12 additions & 4 deletions contracts/examples/hello-world/programs/hello-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ use chainlink_solana as chainlink;

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

const DECIMALS: u32 = 18;

struct Decimal {
pub value: i128,
pub decimals: u32,
Expand Down Expand Up @@ -42,8 +40,18 @@ pub mod hello_world {
ctx.accounts.chainlink_feed.to_account_info(),
)?;

let decimal = Decimal::new(round.answer, DECIMALS);
msg!("Price is {}", decimal);
let description = chainlink::description(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;

let decimals = chainlink::decimals(
ctx.accounts.chainlink_program.to_account_info(),
ctx.accounts.chainlink_feed.to_account_info(),
)?;

let decimal = Decimal::new(round.answer, u32::from(decimals));
msg!("{} price is {}", description, decimal);
Ok(())
}
}
Expand Down

0 comments on commit 19a737e

Please sign in to comment.