added test

This commit is contained in:
Conner Gallagher 2022-08-10 12:23:38 -06:00
parent 191f196204
commit 1a50462516
8 changed files with 187 additions and 17 deletions

View File

@ -27,7 +27,7 @@
"types": "lib/cjs/index.d.ts", "types": "lib/cjs/index.d.ts",
"scripts": { "scripts": {
"docgen": "typedoc --entryPoints src/index.ts --out ../../website/static/api/sbv2-utils", "docgen": "typedoc --entryPoints src/index.ts --out ../../website/static/api/sbv2-utils",
"test": "echo \"No test script for @switchboard-xyz/sbv2-utils\" && exit 0", "test": "ts-mocha -p ./tsconfig.cjs.json -t 1000000 ./tests/*.tests.ts",
"build": "shx rm -rf lib && tsc && tsc -p tsconfig.cjs.json", "build": "shx rm -rf lib && tsc && tsc -p tsconfig.cjs.json",
"watch": "tsc -p tsconfig.cjs.json --watch", "watch": "tsc -p tsconfig.cjs.json --watch",
"prepublishOnly": "shx rm -rf lib && yarn build" "prepublishOnly": "shx rm -rf lib && yarn build"
@ -44,12 +44,15 @@
"chalk": "4", "chalk": "4",
"decimal.js": "^10.3.1", "decimal.js": "^10.3.1",
"dotenv": "^16.0.1", "dotenv": "^16.0.1",
"mocha": "^9.1.1",
"toml": "^3.0.0" "toml": "^3.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/big.js": "^6.1.3", "@types/big.js": "^6.1.3",
"@types/mocha": "^9.0.0",
"@types/node": "^17.0.45", "@types/node": "^17.0.45",
"shx": "^0.3.4", "shx": "^0.3.4",
"ts-mocha": "^9.0.2",
"typedoc": "^0.23.8", "typedoc": "^0.23.8",
"typescript": "^4.6.3" "typescript": "^4.6.3"
} }

View File

@ -258,7 +258,7 @@ export async function createAggregatorReq(
aggregator: aggregatorKeypair.publicKey, aggregator: aggregatorKeypair.publicKey,
authority, authority,
queue: params.queueAccount.publicKey, queue: params.queueAccount.publicKey,
authorWallet: params.authorWallet ?? state.tokenVault, // authorWallet: params.authorWallet ?? state.tokenVault,
programState: programStateAccount.publicKey, programState: programStateAccount.publicKey,
}) })
.instruction(), .instruction(),
@ -266,7 +266,7 @@ export async function createAggregatorReq(
.permissionInit({}) .permissionInit({})
.accounts({ .accounts({
permission: permissionAccount.publicKey, permission: permissionAccount.publicKey,
authority: params.authority, authority: queue.authority,
granter: queueAccount.publicKey, granter: queueAccount.publicKey,
grantee: aggregatorKeypair.publicKey, grantee: aggregatorKeypair.publicKey,
payer: payerPubkey, payer: payerPubkey,
@ -289,9 +289,7 @@ export async function createAggregatorReq(
payerPubkey, payerPubkey,
leaseEscrow, leaseEscrow,
leaseAccount.publicKey, leaseAccount.publicKey,
mint.address, mint.address
spl.TOKEN_PROGRAM_ID,
spl.ASSOCIATED_TOKEN_PROGRAM_ID
), ),
await program.methods await program.methods
.leaseInit({ .leaseInit({
@ -328,7 +326,7 @@ export async function createAggregatorReq(
}) })
.accounts({ .accounts({
aggregator: aggregatorKeypair.publicKey, aggregator: aggregatorKeypair.publicKey,
authority: payerPubkey, authority,
job: jobAccount.publicKey, job: jobAccount.publicKey,
}) })
.instruction(); .instruction();

View File

@ -228,10 +228,11 @@ export class SwitchboardTestContext implements ISwitchboardTestContext {
); );
if (!balance) { if (!balance) {
try { try {
await provider.connection.requestAirdrop( const airdropSignature = await provider.connection.requestAirdrop(
payerKeypair.publicKey, payerKeypair.publicKey,
1_000_000_000 1_000_000_000
); );
await provider.connection.confirmTransaction(airdropSignature);
} catch {} } catch {}
} }

View File

@ -0,0 +1,169 @@
import * as anchor from "@project-serum/anchor";
import {
AnchorWallet,
JobAccount,
OracleJob,
} from "@switchboard-xyz/switchboard-v2";
import fs from "fs";
import "mocha";
import {
awaitOpenRound,
createAggregator,
SwitchboardTestContext,
} from "../lib/cjs";
describe("Feed tests", () => {
// const payer = anchor.web3.Keypair.generate();
const payer = anchor.web3.Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(fs.readFileSync("../../../payer-keypair.json", "utf8"))
)
);
let switchboard: SwitchboardTestContext;
before(async () => {
try {
const localnetConnection = new anchor.web3.Connection(
"http://localhost:8899"
);
const provider = new anchor.AnchorProvider(
localnetConnection,
new AnchorWallet(payer),
{ commitment: "confirmed" }
);
switchboard = await SwitchboardTestContext.loadFromEnv(provider);
console.log("local env detected");
return;
} catch (error: any) {}
try {
const devnetConnection = new anchor.web3.Connection(
"https://devnet.genesysgo.net/"
);
const provider = new anchor.AnchorProvider(
devnetConnection,
new AnchorWallet(payer),
{ commitment: "confirmed" }
);
// const airdropSignature = await devnetConnection.requestAirdrop(
// payer.publicKey,
// anchor.web3.LAMPORTS_PER_SOL
// );
// await devnetConnection.confirmTransaction(airdropSignature);
switchboard = await SwitchboardTestContext.loadDevnetQueue(
provider,
"F8ce7MsckeZAbAGmxjJNetxYXQa9mKr9nnrC3qKubyYy"
);
console.log("devnet detected");
return;
} catch (error: any) {
// console.log(`Error: SBV2 Devnet - ${error.message}`);
console.error(error);
}
if (!switchboard) {
// If fails, throw error
throw new Error(
`Failed to load the SwitchboardTestContext from devnet or from a switchboard.env file`
);
}
const airdropSignature =
await switchboard.program.provider.connection.requestAirdrop(
payer.publicKey,
anchor.web3.LAMPORTS_PER_SOL
);
await switchboard.program.provider.connection.confirmTransaction(
airdropSignature
);
});
it("Creates a switchboard feed", async () => {
const job1 = await JobAccount.create(switchboard.program, {
name: Buffer.from("Job1"),
authority: payer.publicKey,
data: Buffer.from(
OracleJob.encodeDelimited(
OracleJob.create({
tasks: [
OracleJob.Task.create({
httpTask: OracleJob.HttpTask.create({
url: `https://ftx.us/api/markets/SOL_USD`,
}),
}),
OracleJob.Task.create({
jsonParseTask: OracleJob.JsonParseTask.create({
path: "$.result.price",
}),
}),
],
})
).finish()
),
});
const job2 = await JobAccount.create(switchboard.program, {
name: Buffer.from("Job1"),
authority: payer.publicKey,
data: Buffer.from(
OracleJob.encodeDelimited(
OracleJob.create({
tasks: [
OracleJob.Task.create({
httpTask: OracleJob.HttpTask.create({
url: "https://www.binance.com/api/v3/ticker/price?symbol=SOLUSDT",
}),
}),
OracleJob.Task.create({
jsonParseTask: OracleJob.JsonParseTask.create({
path: "$.price",
}),
}),
OracleJob.Task.create({
multiplyTask: OracleJob.MultiplyTask.create({
aggregatorPubkey:
"ETAaeeuQBwsh9mC2gCov9WdhJENZuffRMXY2HgjCcSL9",
}),
}),
],
})
).finish()
),
});
try {
const newAggregator = await createAggregator(
switchboard.program,
switchboard.queue,
{
name: Buffer.from("Test Feed"),
batchSize: 1,
minRequiredOracleResults: 1,
minRequiredJobResults: 1,
minUpdateDelaySeconds: 10,
queueAccount: switchboard.queue,
},
[
[job1, 1],
[job2, 1],
]
);
console.log(`Created Aggregator: ${newAggregator.publicKey}`);
// call openRound
const value = await awaitOpenRound(
newAggregator,
switchboard.queue,
switchboard.payerTokenWallet,
undefined,
45
);
console.log(`Aggregator Value: ${value.toString()}`);
} catch (error) {
console.error(error);
throw error;
}
});
});

View File

@ -1,9 +1,12 @@
{ {
"include": ["./src/**/*"], "include": ["./src/**/*"],
"exclude": ["tests/**/*", "lib", "node_modules"],
"files": ["./src/index.ts"], "files": ["./src/index.ts"],
"compilerOptions": { "compilerOptions": {
"target": "es2019", // "target": "es2019",
"composite": true,
"sourceMap": true, "sourceMap": true,
"inlineSources": true,
"declaration": true, "declaration": true,
"declarationMap": true, "declarationMap": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
@ -11,8 +14,8 @@
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,
"esModuleInterop": true, "esModuleInterop": true,
"resolveJsonModule": true, "resolveJsonModule": true,
"composite": true,
"skipLibCheck": true, "skipLibCheck": true,
"types": ["mocha", "node", "long"],
// strict // strict
"strict": true, "strict": true,
"noImplicitThis": true, "noImplicitThis": true,
@ -23,11 +26,7 @@
"noImplicitReturns": true, "noImplicitReturns": true,
"strictPropertyInitialization": true, "strictPropertyInitialization": true,
"paths": { "paths": {
"@switchboard-xyz/switchboard-v2": ["../ts"], "@switchboard-xyz/switchboard-v2": ["../ts"]
"@solana/spl-token": [
"../../node_modules/@solana/spl-token",
"./node_modules/@solana/spl-token"
]
} }
}, },
"references": [{ "path": "../ts" }] "references": [{ "path": "../ts" }]

View File

@ -39,7 +39,7 @@
"build:esm": "tsc && yarn build:esm:protos", "build:esm": "tsc && yarn build:esm:protos",
"build": "yarn build:protos && yarn build:cjs && yarn build:esm", "build": "yarn build:protos && yarn build:cjs && yarn build:esm",
"watch": "tsc -p tsconfig.cjs.json --watch", "watch": "tsc -p tsconfig.cjs.json --watch",
"test": "mocha --extension ts --require ts-node/register -t 1000000 tests/", "test": "ts-mocha -p ./tsconfig.cjs.json --require ts-node/register -t 1000000 ./tests/*.tests.ts",
"lint": "eslint --fix-dry-run --ext .ts src/**/*.ts", "lint": "eslint --fix-dry-run --ext .ts src/**/*.ts",
"prepublish": "shx rm -rf lib && yarn build" "prepublish": "shx rm -rf lib && yarn build"
}, },
@ -52,7 +52,6 @@
"@solana/spl-governance": "^0.0.34", "@solana/spl-governance": "^0.0.34",
"@solana/spl-token-v2": "npm:@solana/spl-token@^0.2.0", "@solana/spl-token-v2": "npm:@solana/spl-token@^0.2.0",
"@solana/web3.js": "^1.44.3", "@solana/web3.js": "^1.44.3",
"@types/big.js": "^6.1.4",
"assert": "^2.0.0", "assert": "^2.0.0",
"big.js": "^6.2.0", "big.js": "^6.2.0",
"bs58": "^5.0.0", "bs58": "^5.0.0",
@ -70,6 +69,7 @@
"@types/mocha": "^9.0.0", "@types/mocha": "^9.0.0",
"@types/node": "^17.0.45", "@types/node": "^17.0.45",
"shx": "^0.3.4", "shx": "^0.3.4",
"ts-mocha": "^9.0.2",
"typedoc": "^0.23.8", "typedoc": "^0.23.8",
"typescript": "^4.7" "typescript": "^4.7"
} }