-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
22,717 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './oracleClient'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
] | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
Oops, something went wrong.