solana.js: fixed getOrCreate SbState params

This commit is contained in:
Conner Gallagher 2023-01-08 18:08:09 -07:00
parent e524a882d5
commit 8c18799daf
13 changed files with 73 additions and 17 deletions

View File

@ -99,13 +99,16 @@ export class ProgramStateAccount extends Account<types.SbState> {
*/ */
static async getOrCreate( static async getOrCreate(
program: SwitchboardProgram, program: SwitchboardProgram,
mint: PublicKey = Mint.native params: ProgramInitParams = {
mint: Mint.native,
daoMint: Mint.native,
}
): Promise<[ProgramStateAccount, number, TransactionSignature | undefined]> { ): Promise<[ProgramStateAccount, number, TransactionSignature | undefined]> {
const [account, bump, txn] = const [account, bump, txn] =
await ProgramStateAccount.getOrCreateInstructions( await ProgramStateAccount.getOrCreateInstructions(
program, program,
program.walletPubkey, program.walletPubkey,
mint params
); );
if (txn) { if (txn) {
@ -119,16 +122,23 @@ export class ProgramStateAccount extends Account<types.SbState> {
static async getOrCreateInstructions( static async getOrCreateInstructions(
program: SwitchboardProgram, program: SwitchboardProgram,
payer: PublicKey, payer: PublicKey,
mint: PublicKey = Mint.native params: ProgramInitParams = {
mint: Mint.native,
daoMint: Mint.native,
}
): Promise<[ProgramStateAccount, number, TransactionObject | undefined]> { ): Promise<[ProgramStateAccount, number, TransactionObject | undefined]> {
const [account, bump] = ProgramStateAccount.fromSeed(program); const [account, bump] = ProgramStateAccount.fromSeed(program);
try { try {
await account.loadData(); await account.loadData();
return [account, bump, undefined];
} catch (e) { } catch (e) {
const vaultKeypair = Keypair.generate();
const ixns: TransactionInstruction[] = []; 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 // load the mint
let splMint: spl.Mint; let splMint: spl.Mint;
try { 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 // create the vault
const vaultInitIxn = spl.createInitializeAccountInstruction( const vaultInitIxn = spl.createInitializeAccountInstruction(
vaultKeypair.publicKey, vaultKeypair.publicKey,
@ -162,6 +197,7 @@ export class ProgramStateAccount extends Account<types.SbState> {
); );
ixns.push(vaultInitIxn); ixns.push(vaultInitIxn);
// if authorized, mint tokens to vault
if (splMint.mintAuthority?.equals(payer)) { if (splMint.mintAuthority?.equals(payer)) {
ixns.push( ixns.push(
spl.createMintToInstruction( 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( ixns.push(
types.programInit( types.programInit(
program, program,
@ -185,7 +236,7 @@ export class ProgramStateAccount extends Account<types.SbState> {
vault: vaultKeypair.publicKey, vault: vaultKeypair.publicKey,
systemProgram: SystemProgram.programId, systemProgram: SystemProgram.programId,
tokenProgram: spl.TOKEN_PROGRAM_ID, 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, programInit];
} }
return [account, bump, undefined];
} }
/** /**
@ -248,3 +298,9 @@ export class ProgramStateAccount extends Account<types.SbState> {
return txnSignature; return txnSignature;
} }
} }
export interface ProgramInitParams {
mint?: PublicKey;
daoMint?: PublicKey;
vaultKeypair?: Keypair;
}

View File

@ -3,7 +3,7 @@ import 'mocha';
import assert from 'assert'; import assert from 'assert';
import * as sbv2 from '../src'; import * as sbv2 from '../src';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js';
import { import {
AggregatorAccount, AggregatorAccount,

View File

@ -1,7 +1,7 @@
import 'mocha'; import 'mocha';
import assert from 'assert'; import assert from 'assert';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair, PublicKey } from '@solana/web3.js'; import { Keypair, PublicKey } from '@solana/web3.js';
import { import {
BufferRelayerAccount, BufferRelayerAccount,

View File

@ -1,7 +1,7 @@
import 'mocha'; import 'mocha';
import assert from 'assert'; 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 { Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
import { import {
AggregatorAccount, AggregatorAccount,

View File

@ -1,7 +1,7 @@
import 'mocha'; import 'mocha';
import assert from 'assert'; import assert from 'assert';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { OracleJob } from '@switchboard-xyz/common'; import { OracleJob } from '@switchboard-xyz/common';
import { JobAccount } from '../src'; import { JobAccount } from '../src';

View File

@ -2,7 +2,7 @@ import 'mocha';
import assert from 'assert'; import assert from 'assert';
import * as sbv2 from '../src'; import * as sbv2 from '../src';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair, PublicKey } from '@solana/web3.js'; import { Keypair, PublicKey } from '@solana/web3.js';
import { import {
AggregatorAccount, AggregatorAccount,

View File

@ -2,7 +2,7 @@ import 'mocha';
import assert from 'assert'; import assert from 'assert';
import * as anchor from '@project-serum/anchor'; 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 { Keypair, PublicKey } from '@solana/web3.js';
import Big from 'big.js'; import Big from 'big.js';

View File

@ -2,7 +2,7 @@ import 'mocha';
import assert from 'assert'; import assert from 'assert';
import * as sbv2 from '../src'; import * as sbv2 from '../src';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js';
import { import {
AggregatorAccount, AggregatorAccount,

View File

@ -2,7 +2,7 @@ import 'mocha';
import assert from 'assert'; import assert from 'assert';
import * as sbv2 from '../src'; import * as sbv2 from '../src';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js';
import { PermissionAccount, TransactionMissingSignerError } from '../src'; import { PermissionAccount, TransactionMissingSignerError } from '../src';

View File

@ -1,7 +1,7 @@
import 'mocha'; import 'mocha';
import assert from 'assert'; import assert from 'assert';
import { DEFAULT_KEYPAIR_PATH } from './utilts'; import { DEFAULT_KEYPAIR_PATH } from './utils';
import { import {
camelToUpperCaseWithUnderscores, camelToUpperCaseWithUnderscores,
SwitchboardTestContext, SwitchboardTestContext,

View File

@ -13,7 +13,7 @@ import {
SystemProgram, SystemProgram,
TransactionInstruction, TransactionInstruction,
} from '@solana/web3.js'; } from '@solana/web3.js';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { import {
ixnsDeepEqual, ixnsDeepEqual,
ixnsEqual, ixnsEqual,

View File

@ -2,7 +2,7 @@
import 'mocha'; import 'mocha';
import assert from 'assert'; import assert from 'assert';
import { setupTest, TestContext } from './utilts'; import { setupTest, TestContext } from './utils';
import { Keypair } from '@solana/web3.js'; import { Keypair } from '@solana/web3.js';
import { import {
AggregatorAccount, AggregatorAccount,