Skip to content

Commit

Permalink
compute price scaling in multisig creation
Browse files Browse the repository at this point in the history
  • Loading branch information
DanL0 committed Jan 9, 2025
1 parent 30c4bae commit eed5acc
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 13 deletions.
9 changes: 6 additions & 3 deletions examples/oft-solana/tasks/solana/createOFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
publicKey,
transactionBuilder,
} from '@metaplex-foundation/umi'
import { fromWeb3JsPublicKey, toWeb3JsKeypair, toWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters'
import { fromWeb3JsPublicKey, toWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'
import bs58 from 'bs58'
Expand Down Expand Up @@ -187,10 +187,13 @@ task('lz:oft:solana:create', 'Mints new SPL Token and creates new OFT Store acco
const additionalMinters = additionalMintersAsStrings?.map((minter) => new PublicKey(minter)) ?? []
const mintAuthorityPublicKey = await createMintAuthorityMultisig(
connection,
toWeb3JsKeypair(umiWalletKeyPair),
umi,
eid,
umiWalletSigner,
toWeb3JsPublicKey(oftStorePda),
toWeb3JsPublicKey(tokenProgramId), // Only configurable for MABA
additionalMinters
additionalMinters,
computeUnitPriceScaleFactor
)
console.log(`created SPL multisig @ ${mintAuthorityPublicKey.toBase58()}`)
await checkMultisigSigners(connection, mintAuthorityPublicKey, [
Expand Down
91 changes: 81 additions & 10 deletions examples/oft-solana/tasks/solana/multisig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,77 @@
import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, createMultisig } from '@solana/spl-token'
import { Connection, PublicKey, Signer } from '@solana/web3.js'
import { createAccount, initializeMultisig } from '@metaplex-foundation/mpl-toolbox'
import {
KeypairSigner,
Umi,
createSignerFromKeypair,
transactionBuilder,
publicKey as umiPublicKey,
} from '@metaplex-foundation/umi'
import { toWeb3JsPublicKey } from '@metaplex-foundation/umi-web3js-adapters'
import { MULTISIG_SIZE, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { Connection, PublicKey } from '@solana/web3.js'
import bs58 from 'bs58'

import { EndpointId } from '@layerzerolabs/lz-definitions'

import { assertAccountInitialized } from './utils'

import { addComputeUnitInstructions, getExplorerTxLink } from '.'

export async function createMultisig(
connection: Connection,
umi: Umi,
eid: EndpointId,
umiWalletSigner: KeypairSigner,
signers: PublicKey[],
m: number,
keypair = umi.eddsa.generateKeypair(),
programId = TOKEN_PROGRAM_ID,
computeUnitPriceScaleFactor?: number
): Promise<PublicKey> {
let txBuilder = transactionBuilder()
.add(
createAccount(umi, {
newAccount: createSignerFromKeypair(umi, keypair),
lamports: await umi.rpc.getRent(MULTISIG_SIZE),
space: MULTISIG_SIZE,
programId: umiPublicKey(programId.toBase58()),
})
)
.add(
initializeMultisig(umi, {
multisig: keypair.publicKey,
rent: undefined,
m,
}).addRemainingAccounts(
signers.map((signer) => ({
pubkey: umiPublicKey(signer.toBase58()),
isWritable: false,
isSigner: false,
}))
)
)

if (computeUnitPriceScaleFactor) {
txBuilder = await addComputeUnitInstructions(
connection,
umi,
eid,
txBuilder,
umiWalletSigner,
computeUnitPriceScaleFactor
)
}

const multisigPublicKey = toWeb3JsPublicKey(keypair.publicKey)

const tx = await txBuilder.sendAndConfirm(umi)
await assertAccountInitialized(connection, multisigPublicKey)
const isTestnet = eid == EndpointId.SOLANA_V2_TESTNET
console.log(`createMultisigTx: ${getExplorerTxLink(bs58.encode(tx.signature), isTestnet)}`)

return multisigPublicKey
}

/**
* Creates a (1/N) multisig account for use as the mint authority.
* @param connection {Connection}
Expand All @@ -13,22 +82,24 @@ import { assertAccountInitialized } from './utils'
*/
export const createMintAuthorityMultisig = async (
connection: Connection,
payer: Signer,
umi: Umi,
eid: EndpointId,
umiWalletSigner: KeypairSigner,
oftStorePda: PublicKey,
tokenProgramId: PublicKey = TOKEN_PROGRAM_ID,
additionalSigners: PublicKey[]
additionalSigners: PublicKey[],
computeUnitPriceScaleFactor: number
) => {
return createMultisig(
connection,
payer,
umi,
eid,
umiWalletSigner,
[oftStorePda, ...additionalSigners],
1, // quorum 1/N
undefined,
{
commitment: 'confirmed',
preflightCommitment: 'confirmed',
},
tokenProgramId
tokenProgramId,
computeUnitPriceScaleFactor
)
}

Expand Down

0 comments on commit eed5acc

Please sign in to comment.