diff --git a/javascript/solana.js/src/accounts/programStateAccount.ts b/javascript/solana.js/src/accounts/programStateAccount.ts index f426cef..5051245 100644 --- a/javascript/solana.js/src/accounts/programStateAccount.ts +++ b/javascript/solana.js/src/accounts/programStateAccount.ts @@ -99,13 +99,16 @@ export class ProgramStateAccount extends Account { */ 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 { 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 { }; } + // 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 { ); 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 { ); } + // 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 { 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 { return [account, bump, programInit]; } - return [account, bump, undefined]; } /** @@ -248,3 +298,9 @@ export class ProgramStateAccount extends Account { return txnSignature; } } + +export interface ProgramInitParams { + mint?: PublicKey; + daoMint?: PublicKey; + vaultKeypair?: Keypair; +} diff --git a/javascript/solana.js/test/aggregator.spec.ts b/javascript/solana.js/test/aggregator.spec.ts index 03a9350..a96c564 100644 --- a/javascript/solana.js/test/aggregator.spec.ts +++ b/javascript/solana.js/test/aggregator.spec.ts @@ -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, diff --git a/javascript/solana.js/test/buffer-relayer.spec.ts b/javascript/solana.js/test/buffer-relayer.spec.ts index 7949c2e..3103bbd 100644 --- a/javascript/solana.js/test/buffer-relayer.spec.ts +++ b/javascript/solana.js/test/buffer-relayer.spec.ts @@ -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, diff --git a/javascript/solana.js/test/crank.spec.ts b/javascript/solana.js/test/crank.spec.ts index ccd0f7f..4f8e3ed 100644 --- a/javascript/solana.js/test/crank.spec.ts +++ b/javascript/solana.js/test/crank.spec.ts @@ -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, diff --git a/javascript/solana.js/test/job.spec.ts b/javascript/solana.js/test/job.spec.ts index c3568c1..553e615 100644 --- a/javascript/solana.js/test/job.spec.ts +++ b/javascript/solana.js/test/job.spec.ts @@ -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'; diff --git a/javascript/solana.js/test/lease.spec.ts b/javascript/solana.js/test/lease.spec.ts index c6084cf..e5792be 100644 --- a/javascript/solana.js/test/lease.spec.ts +++ b/javascript/solana.js/test/lease.spec.ts @@ -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, diff --git a/javascript/solana.js/test/mint.spec.ts b/javascript/solana.js/test/mint.spec.ts index d2c4d68..defcc35 100644 --- a/javascript/solana.js/test/mint.spec.ts +++ b/javascript/solana.js/test/mint.spec.ts @@ -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'; diff --git a/javascript/solana.js/test/open-round.spec.ts b/javascript/solana.js/test/open-round.spec.ts index ee85fda..55eb78a 100644 --- a/javascript/solana.js/test/open-round.spec.ts +++ b/javascript/solana.js/test/open-round.spec.ts @@ -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, diff --git a/javascript/solana.js/test/queue.spec.ts b/javascript/solana.js/test/queue.spec.ts index 3eeb93d..a5b391f 100644 --- a/javascript/solana.js/test/queue.spec.ts +++ b/javascript/solana.js/test/queue.spec.ts @@ -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'; diff --git a/javascript/solana.js/test/test.spec.ts b/javascript/solana.js/test/test.spec.ts index 6a2c1bd..82aa809 100644 --- a/javascript/solana.js/test/test.spec.ts +++ b/javascript/solana.js/test/test.spec.ts @@ -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, diff --git a/javascript/solana.js/test/transaction-object.spec.ts b/javascript/solana.js/test/transaction-object.spec.ts index ce43e84..04a1303 100644 --- a/javascript/solana.js/test/transaction-object.spec.ts +++ b/javascript/solana.js/test/transaction-object.spec.ts @@ -13,7 +13,7 @@ import { SystemProgram, TransactionInstruction, } from '@solana/web3.js'; -import { setupTest, TestContext } from './utilts'; +import { setupTest, TestContext } from './utils'; import { ixnsDeepEqual, ixnsEqual, diff --git a/javascript/solana.js/test/transfer.spec.ts b/javascript/solana.js/test/transfer.spec.ts index 16f1beb..3105e41 100644 --- a/javascript/solana.js/test/transfer.spec.ts +++ b/javascript/solana.js/test/transfer.spec.ts @@ -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, diff --git a/javascript/solana.js/test/utilts.ts b/javascript/solana.js/test/utils.ts similarity index 100% rename from javascript/solana.js/test/utilts.ts rename to javascript/solana.js/test/utils.ts