added AnchorWallet to ts api instead of relying on cjs NodeWallet import

This commit is contained in:
Conner Gallagher 2022-05-26 17:48:28 -06:00
parent 6e3e23a2b6
commit f26ccb9bae
16 changed files with 7488 additions and 47 deletions

File diff suppressed because it is too large Load Diff

View File

@ -99,7 +99,7 @@
"@solana/spl-token": "^0.1.8",
"@solana/web3.js": "^1.42.0",
"@switchboard-xyz/sbv2-utils": "^0.1.14",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"assert": "^2.0.0",
"big.js": "^6.1.1",
"bs58": "^5.0.0",

View File

@ -36,7 +36,7 @@
"@saberhq/token-utils": "^1.12.68",
"@solana/spl-token": "^0.1.8",
"@solana/web3.js": "^1.42.0",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"big.js": "^6.1.1",
"chalk": "4",
"decimal.js": "^10.3.1",

View File

@ -1,6 +1,6 @@
{
"name": "@switchboard-xyz/switchboard-v2",
"version": "0.0.99",
"version": "0.0.102",
"license": "MIT",
"author": "mitch@switchboard.xyz",
"description": "API wrapper for intergating with the Switchboardv2 program",
@ -33,7 +33,7 @@
"build:esm": "shx rm -rf lib/esm && tsc -p tsconfig.esm.json && shx echo '{\"type\": \"module\"}' > lib/esm/package.json",
"build": "shx rm -rf lib && yarn build:cjs && yarn build:esm && shx rm lib/*.tsbuildinfo",
"test": "mocha --extension ts --require ts-node/register -t 1000000 tests/",
"prepublishOnly": "yarn build"
"prepublish": "yarn build"
},
"peerDependencies": {
"@solana/spl-governance": "^0.0.34",
@ -52,6 +52,7 @@
"chalk": "^4.1.2",
"chan": "^0.6.1",
"crypto-js": "^4.0.0",
"mocha": "^9.1.1",
"dotenv": "^16.0.0",
"long": "^4.0.0",
"node-fetch": "^3.2.3",
@ -61,7 +62,6 @@
"@types/big.js": "^6.0.2",
"@types/long": "^4.0.1",
"@types/mocha": "^9.0.0",
"mocha": "^9.1.1",
"npm-run-all": "^4.1.5",
"shx": "^0.3.4",
"typedoc": "^0.22.13",

View File

@ -1,5 +1,4 @@
import * as anchor from "@project-serum/anchor";
import NodeWallet from "@project-serum/anchor/dist/cjs/nodewallet";
import { getGovernance } from "@solana/spl-governance";
import * as spl from "@solana/spl-token";
import {
@ -22,6 +21,7 @@ import {
import { OracleJob } from "@switchboard-xyz/switchboard-api";
import Big from "big.js";
import * as crypto from "crypto";
var assert = require("assert");
export { IOracleJob, OracleJob } from "@switchboard-xyz/switchboard-api";
@ -82,9 +82,9 @@ export async function loadSwitchboardProgram(
): Promise<anchor.Program> {
const DEFAULT_KEYPAIR = Keypair.fromSeed(new Uint8Array(32).fill(1));
const programId = getSwitchboardPid(cluster);
const wallet: NodeWallet = payerKeypair
? new NodeWallet(payerKeypair)
: new NodeWallet(DEFAULT_KEYPAIR);
const wallet: AnchorWallet = payerKeypair
? new AnchorWallet(payerKeypair)
: new AnchorWallet(DEFAULT_KEYPAIR);
const provider = new anchor.AnchorProvider(
connection,
wallet,
@ -4258,7 +4258,7 @@ export async function createMint(
}
export function programWallet(program: anchor.Program): Keypair {
return ((program.provider as anchor.AnchorProvider).wallet as NodeWallet)
return ((program.provider as anchor.AnchorProvider).wallet as AnchorWallet)
.payer;
}
@ -4269,3 +4269,25 @@ function safeDiv(number_: Big, denominator: Big, decimals = 20): Big {
Big.DP = oldDp;
return result;
}
export class AnchorWallet implements anchor.Wallet {
constructor(readonly payer: Keypair) {
this.payer = payer;
}
async signTransaction(tx: Transaction): Promise<Transaction> {
tx.partialSign(this.payer);
return tx;
}
async signAllTransactions(txs: Transaction[]): Promise<Transaction[]> {
return txs.map((t) => {
t.partialSign(this.payer);
return t;
});
}
get publicKey(): PublicKey {
return this.payer.publicKey;
}
}

View File

@ -1,8 +1,8 @@
import "mocha";
import { strict as assert } from "assert";
import * as sbv2 from "../src";
import Big from "big.js";
import * as anchor from "@project-serum/anchor";
import { strict as assert } from "assert";
import Big from "big.js";
import "mocha";
import * as sbv2 from "../src";
describe("Decimal tests", () => {
it("Converts a SwitchboardDecimal to a Big", async () => {
@ -44,7 +44,7 @@ describe("Decimal tests", () => {
});
it("Converts a SwitchboardDecimal back and forth", async () => {
let big = new Big(4.847);
const big = new Big(4.847);
let sbd = sbv2.SwitchboardDecimal.fromBig(big);
assert(sbd.toBig().toNumber() === 4.847);

View File

@ -0,0 +1,18 @@
import { Keypair } from "@solana/web3.js";
import { assert } from "console";
import "mocha";
import { loadSwitchboardProgram, programWallet } from "../src";
describe("Wallet tests", () => {
it("Get program wallet", async () => {
const defaultKeypair = Keypair.fromSeed(new Uint8Array(32).fill(1));
const keypair = Keypair.generate();
const program = await loadSwitchboardProgram("devnet", undefined, keypair);
const getKeypair = programWallet(program);
assert(
keypair.publicKey.equals(getKeypair.publicKey),
"Program Wallet does not match generated keypair"
);
});
});

View File

@ -28,7 +28,7 @@
"dependencies": {
"@project-serum/anchor": "^0.24.2",
"@solana/web3.js": "^1.33.0",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"big.js": "^6.1.1"
}
}

View File

@ -21,7 +21,7 @@
"@project-serum/anchor": "^0.24.2",
"@solana/spl-token": "^0.2.0",
"@solana/web3.js": "^1.37.1",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"@switchboard-xyz/v2-task-library": "^0.1.1",
"chalk": "^4.1.2",
"dotenv": "^16.0.0",

View File

@ -17,7 +17,7 @@
"@project-serum/anchor": "^0.24.2",
"@solana/web3.js": "1.33.0",
"@switchboard-xyz/sbv2-utils": "^0.1.14",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"dotenv": "^16.0.0",
"node-pagerduty": "^1.3.6"
},

View File

@ -1,16 +1,16 @@
import { PublicKey, TransactionInstruction } from "@solana/web3.js" // eslint-disable-line @typescript-eslint/no-unused-vars
import { PROGRAM_ID } from "../programId"
import { PublicKey, TransactionInstruction } from "@solana/web3.js"; // eslint-disable-line @typescript-eslint/no-unused-vars
import { PROGRAM_ID } from "../programId";
export interface ReadResultAccounts {
aggregator: PublicKey
aggregator: PublicKey;
}
export function readResult(accounts: ReadResultAccounts) {
const keys = [
{ pubkey: accounts.aggregator, isSigner: false, isWritable: false },
]
const identifier = Buffer.from([130, 229, 115, 203, 180, 191, 240, 90])
const data = identifier
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data })
return ix
];
const identifier = Buffer.from([130, 229, 115, 203, 180, 191, 240, 90]);
const data = identifier;
const ix = new TransactionInstruction({ keys, programId: PROGRAM_ID, data });
return ix;
}

View File

@ -15,7 +15,7 @@
"@project-serum/anchor": "^0.24.2",
"@solana/web3.js": "^1.42.0",
"@switchboard-xyz/sbv2-utils": "^0.1.14",
"@switchboard-xyz/switchboard-v2": "^0.0.99"
"@switchboard-xyz/switchboard-v2": "^0.0.102"
},
"devDependencies": {
"@types/chai": "^4.3.0",

View File

@ -16,7 +16,7 @@
"@solana/spl-token": "^0.1.8",
"@solana/web3.js": "^1.42.0",
"@switchboard-xyz/sbv2-utils": "^0.1.14",
"@switchboard-xyz/switchboard-v2": "^0.0.99",
"@switchboard-xyz/switchboard-v2": "^0.0.102",
"chalk": "^4.1.2",
"child_process": "^1.0.2",
"dotenv": "^16.0.0"

View File

@ -17,7 +17,7 @@
"@project-serum/anchor": "^0.24.2",
"@solana/web3.js": "^1.42.0",
"@switchboard-xyz/sbv2-utils": "^0.1.14",
"@switchboard-xyz/switchboard-v2": "^0.0.99"
"@switchboard-xyz/switchboard-v2": "^0.0.102"
},
"devDependencies": {
"@types/chai": "^4.3.0",

4766
switchboard_v2.json Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4690,6 +4690,26 @@
typedoc "^0.22.15"
ws "^7.4.6"
"@switchboard-xyz/switchboard-v2@^0.0.99":
version "0.0.99"
resolved "https://registry.npmjs.org/@switchboard-xyz/switchboard-v2/-/switchboard-v2-0.0.99.tgz#4e03c812bf4a4a90d2ea1bcc554e96cb6b18c63f"
integrity sha512-zZfZbi7KODZI617n9ndlRsvqOtM1ksuJBEWvhKzp9Ct//BtN2cij0yQNMtEse05y4fODRDfA0HHtS9ygS2mxjQ==
dependencies:
"@project-serum/anchor" "^0.24.2"
"@solana/spl-governance" "^0.0.34"
"@switchboard-xyz/switchboard-api" "^0.2.200"
assert "^2.0.0"
big.js "^6.1.1"
bs58 "^4.0.1"
buffer-layout "^1.2.0"
chalk "^4.1.2"
chan "^0.6.1"
crypto-js "^4.0.0"
dotenv "^16.0.0"
long "^4.0.0"
node-fetch "^3.2.3"
protobufjs "^6.10.2"
"@switchboard-xyz/v2-task-library@^0.1.1":
version "0.1.5"
resolved "https://registry.npmjs.org/@switchboard-xyz/v2-task-library/-/v2-task-library-0.1.5.tgz"