solana.js: published 2.0.97

This commit is contained in:
Conner Gallagher 2023-01-17 16:42:27 -07:00
parent 75bbd243d5
commit 6d60e1c5c1
5 changed files with 59 additions and 25 deletions

View File

@ -1,19 +1,19 @@
{ {
"name": "@switchboard-xyz/solana.js", "name": "@switchboard-xyz/solana.js",
"version": "2.0.96", "version": "2.0.97",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@switchboard-xyz/solana.js", "name": "@switchboard-xyz/solana.js",
"version": "2.0.96", "version": "2.0.97",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@project-serum/anchor": "^0.26.0", "@project-serum/anchor": "^0.26.0",
"@project-serum/borsh": "^0.2.5", "@project-serum/borsh": "^0.2.5",
"@solana/spl-token": "^0.3.6", "@solana/spl-token": "^0.3.6",
"@solana/web3.js": "^1.73.0", "@solana/web3.js": "^1.73.0",
"@switchboard-xyz/common": "^2.1.8", "@switchboard-xyz/common": "^2.1.12",
"big.js": "^6.2.1", "big.js": "^6.2.1",
"bn.js": "^5.2.1", "bn.js": "^5.2.1",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
@ -589,10 +589,11 @@
} }
}, },
"node_modules/@switchboard-xyz/common": { "node_modules/@switchboard-xyz/common": {
"version": "2.1.8", "version": "2.1.12",
"resolved": "https://registry.npmjs.org/@switchboard-xyz/common/-/common-2.1.8.tgz", "resolved": "https://registry.npmjs.org/@switchboard-xyz/common/-/common-2.1.12.tgz",
"integrity": "sha512-mqCDwCtBf3tY2Bvce0USR4m6DsXSqw8rn4luc2sjLL4PPFKCuCZVgzysfkwzwBHoipbnx6odRQ+BIfm35nhWDA==", "integrity": "sha512-eQ3JSybbmNG0AqHsSUXS1hKZvE21JZ9HV6s9wRNXtQhMVw9sy92NyIze5RDfyxs9mbPsrFhkqaJh+nl6TnU3rQ==",
"dependencies": { "dependencies": {
"@solana/web3.js": "^1.66.2",
"big.js": "^6.2.1", "big.js": "^6.2.1",
"bn.js": "^5.2.1", "bn.js": "^5.2.1",
"decimal.js": "^10.4.3", "decimal.js": "^10.4.3",
@ -5701,10 +5702,11 @@
} }
}, },
"@switchboard-xyz/common": { "@switchboard-xyz/common": {
"version": "2.1.8", "version": "2.1.12",
"resolved": "https://registry.npmjs.org/@switchboard-xyz/common/-/common-2.1.8.tgz", "resolved": "https://registry.npmjs.org/@switchboard-xyz/common/-/common-2.1.12.tgz",
"integrity": "sha512-mqCDwCtBf3tY2Bvce0USR4m6DsXSqw8rn4luc2sjLL4PPFKCuCZVgzysfkwzwBHoipbnx6odRQ+BIfm35nhWDA==", "integrity": "sha512-eQ3JSybbmNG0AqHsSUXS1hKZvE21JZ9HV6s9wRNXtQhMVw9sy92NyIze5RDfyxs9mbPsrFhkqaJh+nl6TnU3rQ==",
"requires": { "requires": {
"@solana/web3.js": "^1.66.2",
"big.js": "^6.2.1", "big.js": "^6.2.1",
"bn.js": "^5.2.1", "bn.js": "^5.2.1",
"decimal.js": "^10.4.3", "decimal.js": "^10.4.3",

View File

@ -1,6 +1,6 @@
{ {
"name": "@switchboard-xyz/solana.js", "name": "@switchboard-xyz/solana.js",
"version": "2.0.96", "version": "2.0.97",
"author": "", "author": "",
"license": "MIT", "license": "MIT",
"description": "API wrapper for integrating with the Switchboard V2 program on Solana", "description": "API wrapper for integrating with the Switchboard V2 program on Solana",
@ -51,7 +51,7 @@
"@project-serum/borsh": "^0.2.5", "@project-serum/borsh": "^0.2.5",
"@solana/spl-token": "^0.3.6", "@solana/spl-token": "^0.3.6",
"@solana/web3.js": "^1.73.0", "@solana/web3.js": "^1.73.0",
"@switchboard-xyz/common": "^2.1.8", "@switchboard-xyz/common": "^2.1.12",
"big.js": "^6.2.1", "big.js": "^6.2.1",
"bn.js": "^5.2.1", "bn.js": "^5.2.1",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",

View File

@ -0,0 +1,25 @@
import {
DockerOracle,
IOracleBaseConfig,
ISolanaOracleConfig,
} from '@switchboard-xyz/common';
import path from 'path';
export type SolanaOracleConfig = Omit<IOracleBaseConfig, 'chain'> &
ISolanaOracleConfig;
export class SolanaDockerOracle extends DockerOracle {
constructor(
config: SolanaOracleConfig,
readonly nodeImage: string,
readonly switchboardDirectory = path.join(process.cwd(), '.switchboard'),
readonly silent = false
) {
super(
{ ...config, chain: 'solana' },
nodeImage,
switchboardDirectory,
silent
);
}
}

View File

@ -1,4 +1,5 @@
export * from './accounts'; export * from './accounts';
export * from './SolanaDockerOracle';
export * from './const'; export * from './const';
export * from './errors'; export * from './errors';
export * as types from './generated'; export * as types from './generated';

View File

@ -71,7 +71,7 @@ export class Mint {
public async getAssociatedAccount( public async getAssociatedAccount(
owner: PublicKey owner: PublicKey
): Promise<spl.Account | null> { ): Promise<spl.Account | null> {
const ownerTokenAddress = Mint.getAssociatedAddress(owner); const ownerTokenAddress = this.getAssociatedAddress(owner);
const ownerTokenAccountInfo = await this.provider.connection.getAccountInfo( const ownerTokenAccountInfo = await this.provider.connection.getAccountInfo(
ownerTokenAddress ownerTokenAddress
); );
@ -110,16 +110,15 @@ export class Mint {
} }
public getAssociatedAddress(user: PublicKey): PublicKey { public getAssociatedAddress(user: PublicKey): PublicKey {
return Mint.getAssociatedAddress(user); return Mint.getAssociatedAddress(user, this.mint.address);
} }
public static getAssociatedAddress(owner: PublicKey): PublicKey { public static getAssociatedAddress(
owner: PublicKey,
mint: PublicKey
): PublicKey {
const [associatedToken] = anchor.utils.publicKey.findProgramAddressSync( const [associatedToken] = anchor.utils.publicKey.findProgramAddressSync(
[ [owner.toBuffer(), spl.TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
owner.toBuffer(),
spl.TOKEN_PROGRAM_ID.toBuffer(),
Mint.native.toBuffer(),
],
spl.ASSOCIATED_TOKEN_PROGRAM_ID spl.ASSOCIATED_TOKEN_PROGRAM_ID
); );
return associatedToken; return associatedToken;
@ -130,7 +129,7 @@ export class Mint {
user?: PublicKey user?: PublicKey
): Promise<PublicKey> { ): Promise<PublicKey> {
const owner = user ?? payer; const owner = user ?? payer;
const associatedToken = Mint.getAssociatedAddress(owner); const associatedToken = this.getAssociatedAddress(owner);
const accountInfo = await this.connection.getAccountInfo(associatedToken); const accountInfo = await this.connection.getAccountInfo(associatedToken);
if (accountInfo === null) { if (accountInfo === null) {
await this.createAssocatedUser(payer, user); await this.createAssocatedUser(payer, user);
@ -155,10 +154,11 @@ export class Mint {
public static createAssocatedUserInstruction( public static createAssocatedUserInstruction(
payer: PublicKey, payer: PublicKey,
mint: PublicKey,
user?: PublicKey user?: PublicKey
): [TransactionObject, PublicKey] { ): [TransactionObject, PublicKey] {
const owner = user ?? payer; const owner = user ?? payer;
const associatedToken = Mint.getAssociatedAddress(owner); const associatedToken = Mint.getAssociatedAddress(owner, mint);
const ixn = spl.createAssociatedTokenAccountInstruction( const ixn = spl.createAssociatedTokenAccountInstruction(
payer, payer,
associatedToken, associatedToken,
@ -172,15 +172,16 @@ export class Mint {
payer: PublicKey, payer: PublicKey,
user?: PublicKey user?: PublicKey
): [TransactionObject, PublicKey] { ): [TransactionObject, PublicKey] {
return Mint.createAssocatedUserInstruction(payer, user); return Mint.createAssocatedUserInstruction(payer, this.mint.address, user);
} }
public static createUserInstruction( public static createUserInstruction(
payer: PublicKey, payer: PublicKey,
mint: PublicKey,
user?: Keypair user?: Keypair
): [PublicKey, TransactionObject] { ): [PublicKey, TransactionObject] {
const owner = user ? user.publicKey : payer; const owner = user ? user.publicKey : payer;
const account = Mint.getAssociatedAddress(owner); const account = Mint.getAssociatedAddress(owner, mint);
const ixn = spl.createInitializeAccountInstruction( const ixn = spl.createInitializeAccountInstruction(
account, account,
Mint.native, Mint.native,
@ -193,7 +194,7 @@ export class Mint {
payer: PublicKey, payer: PublicKey,
user?: Keypair user?: Keypair
): [PublicKey, TransactionObject] { ): [PublicKey, TransactionObject] {
return Mint.createUserInstruction(payer, user); return Mint.createUserInstruction(payer, this.mint.address, user);
} }
public async createUser( public async createUser(
@ -223,6 +224,8 @@ export class Mint {
} }
export class NativeMint extends Mint { export class NativeMint extends Mint {
public static address = Mint.native;
public static async load( public static async load(
provider: anchor.AnchorProvider provider: anchor.AnchorProvider
): Promise<NativeMint> { ): Promise<NativeMint> {
@ -259,7 +262,10 @@ export class NativeMint extends Mint {
user?: Keypair user?: Keypair
): Promise<[PublicKey, TransactionObject | undefined]> { ): Promise<[PublicKey, TransactionObject | undefined]> {
const owner = user ? user.publicKey : payer; const owner = user ? user.publicKey : payer;
const associatedToken = Mint.getAssociatedAddress(owner); const associatedToken = Mint.getAssociatedAddress(
owner,
NativeMint.address
);
const accountInfo = await this.connection.getAccountInfo(associatedToken); const accountInfo = await this.connection.getAccountInfo(associatedToken);
if (accountInfo === null) { if (accountInfo === null) {