solana.js: fixed getOrCreate SbState params
This commit is contained in:
parent
e524a882d5
commit
8c18799daf
|
@ -99,13 +99,16 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
*/
|
||||
static async getOrCreate(
|
||||
program: SwitchboardProgram,
|
||||
mint: PublicKey = Mint.native
|
||||
params: ProgramInitParams = {
|
||||
mint: Mint.native,
|
||||
daoMint: Mint.native,
|
||||
}
|
||||
): Promise<[ProgramStateAccount, number, TransactionSignature | undefined]> {
|
||||
const [account, bump, txn] =
|
||||
await ProgramStateAccount.getOrCreateInstructions(
|
||||
program,
|
||||
program.walletPubkey,
|
||||
mint
|
||||
params
|
||||
);
|
||||
|
||||
if (txn) {
|
||||
|
@ -119,16 +122,23 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
static async getOrCreateInstructions(
|
||||
program: SwitchboardProgram,
|
||||
payer: PublicKey,
|
||||
mint: PublicKey = Mint.native
|
||||
params: ProgramInitParams = {
|
||||
mint: Mint.native,
|
||||
daoMint: Mint.native,
|
||||
}
|
||||
): Promise<[ProgramStateAccount, number, TransactionObject | undefined]> {
|
||||
const [account, bump] = ProgramStateAccount.fromSeed(program);
|
||||
|
||||
try {
|
||||
await account.loadData();
|
||||
return [account, bump, undefined];
|
||||
} catch (e) {
|
||||
const vaultKeypair = Keypair.generate();
|
||||
const ixns: TransactionInstruction[] = [];
|
||||
|
||||
const mint = params.mint ?? Keypair.generate().publicKey;
|
||||
const daoMint = params.daoMint ?? Keypair.generate().publicKey;
|
||||
const vaultKeypair = params.vaultKeypair ?? Keypair.generate();
|
||||
|
||||
// load the mint
|
||||
let splMint: spl.Mint;
|
||||
try {
|
||||
|
@ -154,6 +164,31 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
};
|
||||
}
|
||||
|
||||
// load the daoMint
|
||||
let daoSplMint: spl.Mint;
|
||||
try {
|
||||
// try to load mint if it exists
|
||||
daoSplMint = await spl.getMint(program.connection, daoMint);
|
||||
} catch {
|
||||
// create new mint
|
||||
const mintIxn = spl.createInitializeMintInstruction(
|
||||
daoMint,
|
||||
9,
|
||||
payer,
|
||||
payer
|
||||
);
|
||||
ixns.push(mintIxn);
|
||||
daoSplMint = {
|
||||
address: daoMint,
|
||||
mintAuthority: payer,
|
||||
supply: BigInt('100000000000000000'),
|
||||
decimals: 9,
|
||||
isInitialized: true,
|
||||
freezeAuthority: payer,
|
||||
tlvData: Buffer.from(''),
|
||||
};
|
||||
}
|
||||
|
||||
// create the vault
|
||||
const vaultInitIxn = spl.createInitializeAccountInstruction(
|
||||
vaultKeypair.publicKey,
|
||||
|
@ -162,6 +197,7 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
);
|
||||
ixns.push(vaultInitIxn);
|
||||
|
||||
// if authorized, mint tokens to vault
|
||||
if (splMint.mintAuthority?.equals(payer)) {
|
||||
ixns.push(
|
||||
spl.createMintToInstruction(
|
||||
|
@ -173,6 +209,21 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
);
|
||||
}
|
||||
|
||||
// if authorized and daoMint != mint, mint tokens to vault
|
||||
if (
|
||||
!splMint.address.equals(daoSplMint.address) &&
|
||||
daoSplMint.mintAuthority?.equals(payer)
|
||||
) {
|
||||
ixns.push(
|
||||
spl.createMintToInstruction(
|
||||
daoSplMint.address,
|
||||
vaultKeypair.publicKey,
|
||||
payer,
|
||||
BigInt('100000000000000000')
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
ixns.push(
|
||||
types.programInit(
|
||||
program,
|
||||
|
@ -185,7 +236,7 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
vault: vaultKeypair.publicKey,
|
||||
systemProgram: SystemProgram.programId,
|
||||
tokenProgram: spl.TOKEN_PROGRAM_ID,
|
||||
daoMint: splMint.address,
|
||||
daoMint: daoSplMint.address,
|
||||
}
|
||||
)
|
||||
);
|
||||
|
@ -194,7 +245,6 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
|
||||
return [account, bump, programInit];
|
||||
}
|
||||
return [account, bump, undefined];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -248,3 +298,9 @@ export class ProgramStateAccount extends Account<types.SbState> {
|
|||
return txnSignature;
|
||||
}
|
||||
}
|
||||
|
||||
export interface ProgramInitParams {
|
||||
mint?: PublicKey;
|
||||
daoMint?: PublicKey;
|
||||
vaultKeypair?: Keypair;
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
|
||||
import * as sbv2 from '../src';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair } from '@solana/web3.js';
|
||||
import {
|
||||
AggregatorAccount,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'mocha';
|
||||
import assert from 'assert';
|
||||
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair, PublicKey } from '@solana/web3.js';
|
||||
import {
|
||||
BufferRelayerAccount,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'mocha';
|
||||
import assert from 'assert';
|
||||
|
||||
import { createFeed, createFeeds, setupTest, TestContext } from './utilts';
|
||||
import { createFeed, createFeeds, setupTest, TestContext } from './utils';
|
||||
import { Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
|
||||
import {
|
||||
AggregatorAccount,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'mocha';
|
||||
import assert from 'assert';
|
||||
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { OracleJob } from '@switchboard-xyz/common';
|
||||
import { JobAccount } from '../src';
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
|
||||
import * as sbv2 from '../src';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair, PublicKey } from '@solana/web3.js';
|
||||
import {
|
||||
AggregatorAccount,
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
|
||||
import * as anchor from '@project-serum/anchor';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair, PublicKey } from '@solana/web3.js';
|
||||
import Big from 'big.js';
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
|
||||
import * as sbv2 from '../src';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair } from '@solana/web3.js';
|
||||
import {
|
||||
AggregatorAccount,
|
||||
|
|
|
@ -2,7 +2,7 @@ import 'mocha';
|
|||
import assert from 'assert';
|
||||
|
||||
import * as sbv2 from '../src';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair } from '@solana/web3.js';
|
||||
import { PermissionAccount, TransactionMissingSignerError } from '../src';
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'mocha';
|
||||
import assert from 'assert';
|
||||
|
||||
import { DEFAULT_KEYPAIR_PATH } from './utilts';
|
||||
import { DEFAULT_KEYPAIR_PATH } from './utils';
|
||||
import {
|
||||
camelToUpperCaseWithUnderscores,
|
||||
SwitchboardTestContext,
|
||||
|
|
|
@ -13,7 +13,7 @@ import {
|
|||
SystemProgram,
|
||||
TransactionInstruction,
|
||||
} from '@solana/web3.js';
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import {
|
||||
ixnsDeepEqual,
|
||||
ixnsEqual,
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
import 'mocha';
|
||||
import assert from 'assert';
|
||||
|
||||
import { setupTest, TestContext } from './utilts';
|
||||
import { setupTest, TestContext } from './utils';
|
||||
import { Keypair } from '@solana/web3.js';
|
||||
import {
|
||||
AggregatorAccount,
|
||||
|
|
Loading…
Reference in New Issue