Skip to content

Commit

Permalink
add sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
tkkinn committed May 16, 2024
1 parent 5787691 commit 585eded
Show file tree
Hide file tree
Showing 11 changed files with 22,717 additions and 2 deletions.
21,076 changes: 21,076 additions & 0 deletions sdk/dist/index.js

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@tkkinn/mock-pyth-sdk",
"version": "1.0.0",
"description": "This is a SDK for Mock Pyth Oracle",
"main": "./dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsup ./src/index.ts"
},
"author": "",
"license": "ISC",
"devDependencies": {
"tsup": "^8.0.2",
"typescript": "^5.4.5"
},
"dependencies": {
"@solana/web3.js": "^1.91.8"
}
}
73 changes: 73 additions & 0 deletions sdk/src/idl/mock_pyth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"address": "FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH",
"metadata": {
"name": "mock_pyth",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "initialize",
"discriminator": [
175,
175,
109,
31,
13,
152,
155,
237
],
"accounts": [
{
"name": "price",
"writable": true
}
],
"args": [
{
"name": "price",
"type": "i64"
},
{
"name": "expo",
"type": "i32"
},
{
"name": "conf",
"type": "u64"
}
]
},
{
"name": "set_price",
"discriminator": [
16,
19,
182,
8,
149,
83,
72,
181
],
"accounts": [
{
"name": "price",
"writable": true
}
],
"args": [
{
"name": "price",
"type": "i64"
},
{
"name": "conf",
"type": "u64"
}
]
}
]
}
1 change: 1 addition & 0 deletions sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './oracleClient';
84 changes: 84 additions & 0 deletions sdk/src/oracleClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { AnchorProvider, BN, Program } from "@coral-xyz/anchor";
import { ConfirmOptions, Keypair, PublicKey, SystemProgram, TransactionInstruction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import { MockPyth } from "./types/mock_pyth";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import { OracleClientConfig } from "./oracleClientConfig";
import idl from "./idl/mock_pyth.json";

export class oracleClient {
provider: AnchorProvider;
wallet: NodeWallet;
public program: Program<MockPyth>;
opts?: ConfirmOptions;

public constructor(config: OracleClientConfig) {
this.provider = config.provider;
this.wallet = config.wallet;
if (!config.program) {
const a = JSON.stringify(idl)
const token_faucet_idl = JSON.parse(a)
this.program = new Program(token_faucet_idl);
} else {
this.program = config.program;
}
this.opts = config.opts;
}

public async initOracle(uiPrice: number, expo: number, conf?: number) {
const priceFeed = Keypair.generate();
if (!conf) conf = (uiPrice / 10) * 10 ** -expo ;

const createPriceAccount = SystemProgram.createAccount({
fromPubkey: this.wallet.publicKey,
newAccountPubkey: priceFeed.publicKey,
space: 3312,
lamports: await this.provider.connection.getMinimumBalanceForRentExemption(3312),
programId: this.program.programId,
});

const initializePriceAccount = await this.program.methods.initialize(
new BN(uiPrice * 10 ** -expo),
expo,
new BN(conf * 10 ** 9)
).accounts({
price: priceFeed.publicKey,
}).instruction();

const instructions = [createPriceAccount, initializePriceAccount];
const txId = await this.provider.connection.sendTransaction(await this.v0_pack(instructions, priceFeed), this.opts);

return [txId, priceFeed.publicKey];
}

public async setPrice(priceFeed: PublicKey, uiPrice: number, expo: number, conf?: number) {
if (!conf) conf = (uiPrice / 10) * 10 ** -expo ;
const setPrice = await this.program.methods.setPrice(
new BN(uiPrice * 10 ** -expo),
new BN(conf * 10 ** 9)
).accounts({
price: priceFeed,
}).instruction();

const tx = await this.provider.connection.sendTransaction(await this.v0_pack([setPrice]), this.opts);
return tx;
}

async v0_pack(instructions: TransactionInstruction[], signer?: Keypair) {
const blockhash = await this.provider.connection
.getLatestBlockhash()
.then(res => res.blockhash);

const messageV0 = new TransactionMessage({
payerKey: this.wallet.publicKey,
recentBlockhash: blockhash,
instructions,
}).compileToV0Message();

const transaction = new VersionedTransaction(messageV0);
transaction.sign([this.wallet.payer]);
if (signer!=null) transaction.sign([signer]);

return transaction;
}

}
12 changes: 12 additions & 0 deletions sdk/src/oracleClientConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { AnchorProvider, Program } from "@coral-xyz/anchor";
import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet";
import { ConfirmOptions, PublicKey } from "@solana/web3.js";
import { MockPyth } from "./types/mock_pyth";

export type OracleClientConfig = {
wallet: NodeWallet;
provider: AnchorProvider;
program?: Program<MockPyth>;
programId?: PublicKey;
opts?: ConfirmOptions;
};
79 changes: 79 additions & 0 deletions sdk/src/types/mock_pyth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Program IDL in camelCase format in order to be used in JS/TS.
*
* Note that this is only a type helper and is not the actual IDL. The original
* IDL can be found at `target/idl/mock_pyth.json`.
*/
export type MockPyth = {
"address": "FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH",
"metadata": {
"name": "mockPyth",
"version": "0.1.0",
"spec": "0.1.0",
"description": "Created with Anchor"
},
"instructions": [
{
"name": "initialize",
"discriminator": [
175,
175,
109,
31,
13,
152,
155,
237
],
"accounts": [
{
"name": "price",
"writable": true
}
],
"args": [
{
"name": "price",
"type": "i64"
},
{
"name": "expo",
"type": "i32"
},
{
"name": "conf",
"type": "u64"
}
]
},
{
"name": "setPrice",
"discriminator": [
16,
19,
182,
8,
149,
83,
72,
181
],
"accounts": [
{
"name": "price",
"writable": true
}
],
"args": [
{
"name": "price",
"type": "i64"
},
{
"name": "conf",
"type": "u64"
}
]
}
]
};
13 changes: 13 additions & 0 deletions sdk/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"target": "ES2019",
"esModuleInterop": true,
"declaration": true,
"outDir": "./lib",
"resolveJsonModule": true,
"skipLibCheck": true
},
"include": ["src"],
"exclude": ["node_modules"]
}
Loading

0 comments on commit 585eded

Please sign in to comment.