2022-05-19 13:49:38 -07:00
|
|
|
import * as anchor from "@project-serum/anchor";
|
|
|
|
import {
|
|
|
|
Keypair,
|
|
|
|
PublicKey,
|
|
|
|
Transaction,
|
|
|
|
TransactionInstruction,
|
|
|
|
} from "@solana/web3.js";
|
|
|
|
import { SwitchboardTestContext } from "@switchboard-xyz/sbv2-utils";
|
|
|
|
import fs from "fs";
|
|
|
|
import path from "path";
|
|
|
|
|
|
|
|
function getProgramId(): PublicKey {
|
|
|
|
const programKeypairPath = path.join(
|
|
|
|
__dirname,
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"..",
|
|
|
|
"target",
|
|
|
|
"deploy",
|
2022-07-14 13:40:44 -07:00
|
|
|
"native_feed_parser-keypair.json"
|
2022-05-19 13:49:38 -07:00
|
|
|
);
|
|
|
|
const PROGRAM_ID = Keypair.fromSecretKey(
|
|
|
|
new Uint8Array(JSON.parse(fs.readFileSync(programKeypairPath, "utf8")))
|
|
|
|
).publicKey;
|
|
|
|
|
|
|
|
return PROGRAM_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sleep = (ms: number): Promise<any> =>
|
|
|
|
new Promise((s) => setTimeout(s, ms));
|
|
|
|
|
|
|
|
// Anchor.toml will copy this to localnet when we start our tests
|
|
|
|
const DEFAULT_SOL_USD_FEED = new PublicKey(
|
|
|
|
"GvDMxPzN1sCj7L26YDK2HnMRXEQmQ2aemov8YBtPS7vR"
|
|
|
|
);
|
|
|
|
|
2022-07-14 13:40:44 -07:00
|
|
|
describe("native-feed-parser test", () => {
|
2022-05-19 13:49:38 -07:00
|
|
|
const provider = anchor.AnchorProvider.env();
|
|
|
|
anchor.setProvider(provider);
|
|
|
|
|
|
|
|
let switchboard: SwitchboardTestContext;
|
|
|
|
let aggregatorKey: PublicKey;
|
|
|
|
|
|
|
|
before(async () => {
|
|
|
|
// First, attempt to load the switchboard devnet PID
|
|
|
|
try {
|
2022-07-15 16:26:57 -07:00
|
|
|
switchboard = await SwitchboardTestContext.loadDevnetQueue(
|
|
|
|
provider,
|
|
|
|
"F8ce7MsckeZAbAGmxjJNetxYXQa9mKr9nnrC3qKubyYy"
|
|
|
|
);
|
2022-05-19 13:49:38 -07:00
|
|
|
aggregatorKey = DEFAULT_SOL_USD_FEED;
|
|
|
|
console.log("devnet detected");
|
|
|
|
return;
|
2022-05-20 11:15:50 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
console.log(`Error: SBV2 Devnet - ${error.message}`);
|
2022-05-19 13:49:38 -07:00
|
|
|
}
|
|
|
|
// If fails, fallback to looking for a local env file
|
|
|
|
try {
|
|
|
|
switchboard = await SwitchboardTestContext.loadFromEnv(provider);
|
|
|
|
const aggregatorAccount = await switchboard.createStaticFeed(100);
|
|
|
|
aggregatorKey = aggregatorAccount.publicKey ?? PublicKey.default;
|
2022-07-15 16:26:57 -07:00
|
|
|
console.log("local env detected");
|
2022-05-19 13:49:38 -07:00
|
|
|
return;
|
2022-05-20 11:15:50 -07:00
|
|
|
} catch (error: any) {
|
|
|
|
console.log(`Error: SBV2 Localnet - ${error.message}`);
|
2022-05-19 13:49:38 -07:00
|
|
|
}
|
|
|
|
// If fails, throw error
|
|
|
|
throw new Error(
|
|
|
|
`Failed to load the SwitchboardTestContext from devnet or from a switchboard.env file`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Read SOL/USD Feed", async () => {
|
|
|
|
const PROGRAM_ID = getProgramId();
|
|
|
|
|
|
|
|
const readSwitchboardAggregatorTxn = new Transaction().add(
|
|
|
|
new TransactionInstruction({
|
|
|
|
keys: [
|
|
|
|
{
|
|
|
|
pubkey: aggregatorKey,
|
|
|
|
isSigner: false,
|
|
|
|
isWritable: false,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
programId: new PublicKey(PROGRAM_ID),
|
|
|
|
data: Buffer.from([]),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const signature = await provider.sendAndConfirm(
|
|
|
|
readSwitchboardAggregatorTxn
|
|
|
|
);
|
|
|
|
|
2022-05-20 11:15:50 -07:00
|
|
|
// wait for RPC
|
|
|
|
await sleep(2000);
|
|
|
|
|
2022-05-19 13:49:38 -07:00
|
|
|
const confirmedTxn = await provider.connection.getParsedTransaction(
|
|
|
|
signature,
|
|
|
|
"confirmed"
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log(JSON.stringify(confirmedTxn?.meta?.logMessages, undefined, 2));
|
|
|
|
});
|
|
|
|
});
|