Skip to content

Commit

Permalink
Add a e2e test that tests hello-world with the store contract
Browse files Browse the repository at this point in the history
  • Loading branch information
archseer committed Jan 19, 2022
1 parent c22b509 commit eee3033
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 4 deletions.
11 changes: 10 additions & 1 deletion contracts/examples/hello-world/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ url = "https://anchor.projectserum.com"

[provider]
cluster = "localnet"
wallet = "/home/speed/.config/solana/id.json"
# wallet = "~/.config/solana/id.json"
wallet = "../../id.json"

[scripts]
test = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/**/*.ts"

[[test.genesis]]
address = "A7Jh2nb1hZHwqEofm4N8SXbKTj82rx7KUfjParQXUyMQ"
program = "../../target/deploy/store.so"

[[test.genesis]]
address = "2F5NEkMnCRkmahEAcQfTQcZv1xtGgrWFfjENtTwHLuKg"
program = "../../target/deploy/access_controller.so"
105 changes: 102 additions & 3 deletions contracts/examples/hello-world/tests/hello-world.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,116 @@
import * as anchor from '@project-serum/anchor';
import { Program } from '@project-serum/anchor';
import * as fs from 'fs';
import { Program, BN } from '@project-serum/anchor';
import { HelloWorld } from '../target/types/hello_world';

const CHAINLINK_PROGRAM_ID = "A7Jh2nb1hZHwqEofm4N8SXbKTj82rx7KUfjParQXUyMQ";

describe('hello-world', () => {
const provider = anchor.Provider.env();

// Configure the client to use the local cluster.
anchor.setProvider(anchor.Provider.env());
anchor.setProvider(provider);

const program = anchor.workspace.HelloWorld as Program<HelloWorld>;

it('Is initialized!', async () => {
const owner = provider.wallet;
const store = anchor.web3.Keypair.generate();
const feed = anchor.web3.Keypair.generate();
const accessController = anchor.web3.Keypair.generate();

let storeIdl = JSON.parse(fs.readFileSync('../../target/idl/store.json'));
const storeProgram = new Program(storeIdl, CHAINLINK_PROGRAM_ID, provider);

let acIdl = JSON.parse(fs.readFileSync('../../target/idl/access_controller.json'));
const accessControllerProgram = new Program(acIdl, "2F5NEkMnCRkmahEAcQfTQcZv1xtGgrWFfjENtTwHLuKg", provider);

await accessControllerProgram.rpc.initialize({
accounts: {
state: accessController.publicKey,
payer: provider.wallet.publicKey,
owner: owner.publicKey,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
systemProgram: anchor.web3.SystemProgram.programId,
},
signers: [accessController],
preInstructions: [
await accessControllerProgram.account.accessController.createInstruction(accessController),
],
});

// Initialize a new store
await storeProgram.rpc.initialize({
accounts: {
store: store.publicKey,
owner: owner.publicKey,
loweringAccessController: accessController.publicKey,
},
signers: [store],
preInstructions: [
await storeProgram.account.store.createInstruction(store),
],
});

// Create a feed
const description = "FOO/BAR";
const decimals = 18;
const granularity = 30;
const liveLength = 3;
await storeProgram.rpc.createFeed(
description,
decimals,
granularity,
liveLength,
{
accounts: {
store: store.publicKey,
feed: feed.publicKey,
authority: owner.publicKey,
},
signers: [feed],
preInstructions: [
await storeProgram.account.transmissions.createInstruction(feed, 8+128+6*24),
],
});

await storeProgram.rpc.setWriter(
owner.publicKey,
{
accounts: {
store: store.publicKey,
feed: feed.publicKey,
authority: owner.publicKey,
},
});


const scale = (new BN(10)).pow(new BN(decimals));
// Scale answer to enough decimals
let answer = (new BN(1)).mul(scale);
let round = { timestamp: new BN(1), answer };

let tx = await storeProgram.rpc.submit(
round,
{
accounts: {
store: store.publicKey,
feed: feed.publicKey,
authority: owner.publicKey,
},
});
await provider.connection.confirmTransaction(tx);

// Add your test here.
const tx = await program.rpc.initialize({});
tx = await program.rpc.execute({
accounts: {
chainlinkFeed: feed.publicKey,
chainlinkProgram: CHAINLINK_PROGRAM_ID
},
options: { commitment: "confirmed" },
});
console.log("Your transaction signature", tx);
let t = await provider.connection.getConfirmedTransaction(tx, "confirmed");
console.log(t.logMessages)
});
});

0 comments on commit eee3033

Please sign in to comment.