Skip to content

Commit

Permalink
Replace createMintWithSingleToken with more generic helper
Browse files Browse the repository at this point in the history
  • Loading branch information
lorisleiva committed Mar 13, 2023
1 parent 1eb4988 commit 5f04d11
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,35 @@ import { createAssociatedToken, mintTokensTo } from './generated';
import { findAssociatedTokenPda } from './hooked';

// Inputs.
export type CreateMintWithSingleTokenArgs = CreateMintArgs & {
export type CreateMintWithAssociatedTokenArgs = CreateMintArgs & {
owner: PublicKey;
amount?: number | bigint;
};

// Instruction.
export function createMintWithSingleToken(
export function createMintWithAssociatedToken(
context: Pick<
Context,
'serializer' | 'programs' | 'identity' | 'payer' | 'eddsa'
>,
input: CreateMintWithSingleTokenArgs
input: CreateMintWithAssociatedTokenArgs
): WrappedInstruction[] {
const mintAndOwner = { mint: input.mint.publicKey, owner: input.owner };
return [
const amount = input.amount ?? 0;
const instructions: WrappedInstruction[] = [
...createMint(context, input),
createAssociatedToken(context, mintAndOwner),
mintTokensTo(context, {
amount: 1,
mint: input.mint.publicKey,
token: findAssociatedTokenPda(context, mintAndOwner),
}),
];

if (amount > 0) {
instructions.push(
mintTokensTo(context, {
amount,
mint: input.mint.publicKey,
token: findAssociatedTokenPda(context, mintAndOwner),
})
);
}

return instructions;
}
2 changes: 1 addition & 1 deletion clients/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from './instructions';

export * from './createLutForTransactionBuilder';
export * from './createMint';
export * from './createMintWithSingleToken';
export * from './createMintWithAssociatedToken';
export * from './createToken';
export * from './fetchTokensByOwner';
export * from './findLargestTokensByMint';
Expand Down
177 changes: 177 additions & 0 deletions clients/js/test/createMintWithAssociatedToken.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
import {
generateSigner,
none,
publicKey,
some,
transactionBuilder,
} from '@metaplex-foundation/umi';
import test from 'ava';
import {
createMintWithAssociatedToken,
fetchMint,
fetchToken,
findAssociatedTokenPda,
Mint,
Token,
TokenState,
} from '../src';
import { createUmi } from './_setup';

test('it can create a new mint and token account with no tokens', async (t) => {
// Given a mint address and an owner address.
const umi = await createUmi();
const mint = generateSigner(umi);
const owner = generateSigner(umi).publicKey;

// When we create a new mint and token account without specifying an amount.
await transactionBuilder(umi)
.add(createMintWithAssociatedToken(umi, { mint, owner }))
.sendAndConfirm();

// Then it created a new mint account with a supply of 0.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
mintAuthority: some(publicKey(umi.identity)),
supply: 0n,
decimals: 0,
isInitialized: true,
freezeAuthority: some(publicKey(umi.identity)),
});

// And a new associated token account with no tokens.
const ata = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner });
const tokenAccount = await fetchToken(umi, ata);
t.like(tokenAccount, <Token>{
publicKey: publicKey(ata),
mint: publicKey(mint),
owner: publicKey(owner),
amount: 0n,
delegate: none(),
state: TokenState.Initialized,
isNative: none(),
delegatedAmount: 0n,
closeAuthority: none(),
});
});

test('it can create a new mint and token account with a single token', async (t) => {
// Given a mint address and an owner address.
const umi = await createUmi();
const mint = generateSigner(umi);
const owner = generateSigner(umi).publicKey;

// When we create a new mint and token account with a single token.
await transactionBuilder(umi)
.add(createMintWithAssociatedToken(umi, { mint, owner, amount: 1 }))
.sendAndConfirm();

// Then it created a new mint account with a supply of 1.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
mintAuthority: some(publicKey(umi.identity)),
supply: 1n,
decimals: 0,
isInitialized: true,
freezeAuthority: some(publicKey(umi.identity)),
});

// And a new associated token account with one token.
const ata = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner });
const tokenAccount = await fetchToken(umi, ata);
t.like(tokenAccount, <Token>{
publicKey: publicKey(ata),
mint: publicKey(mint),
owner: publicKey(owner),
amount: 1n,
delegate: none(),
state: TokenState.Initialized,
isNative: none(),
delegatedAmount: 0n,
closeAuthority: none(),
});
});

test('it can create a new mint and token account with many tokens', async (t) => {
// Given a mint address and an owner address.
const umi = await createUmi();
const mint = generateSigner(umi);
const owner = generateSigner(umi).publicKey;

// When we create a new mint and token account with 42 tokens.
await transactionBuilder(umi)
.add(createMintWithAssociatedToken(umi, { mint, owner, amount: 42 }))
.sendAndConfirm();

// Then it created a new mint account with a supply of 42.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
mintAuthority: some(publicKey(umi.identity)),
supply: 42n,
decimals: 0,
isInitialized: true,
freezeAuthority: some(publicKey(umi.identity)),
});

// And a new associated token account with 42 tokens.
const ata = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner });
const tokenAccount = await fetchToken(umi, ata);
t.like(tokenAccount, <Token>{
publicKey: publicKey(ata),
mint: publicKey(mint),
owner: publicKey(owner),
amount: 42n,
delegate: none(),
state: TokenState.Initialized,
isNative: none(),
delegatedAmount: 0n,
closeAuthority: none(),
});
});

test('it can create a new mint and token account with decimals', async (t) => {
// Given a mint address and an owner address.
const umi = await createUmi();
const mint = generateSigner(umi);
const owner = generateSigner(umi).publicKey;

// When we create a new mint and token account with 42 tokens and one decimal.
await transactionBuilder(umi)
.add(
createMintWithAssociatedToken(umi, {
mint,
owner,
amount: 42,
decimals: 1,
})
)
.sendAndConfirm();

// Then it created a new mint account with a supply of 42 and one decimal.
const mintAccount = await fetchMint(umi, mint.publicKey);
t.like(mintAccount, <Mint>{
publicKey: publicKey(mint),
mintAuthority: some(publicKey(umi.identity)),
supply: 42n,
decimals: 1,
isInitialized: true,
freezeAuthority: some(publicKey(umi.identity)),
});

// And a new associated token account with 42 tokens.
const ata = findAssociatedTokenPda(umi, { mint: mint.publicKey, owner });
const tokenAccount = await fetchToken(umi, ata);
t.like(tokenAccount, <Token>{
publicKey: publicKey(ata),
mint: publicKey(mint),
owner: publicKey(owner),
amount: 42n,
delegate: none(),
state: TokenState.Initialized,
isNative: none(),
delegatedAmount: 0n,
closeAuthority: none(),
});
});
56 changes: 0 additions & 56 deletions clients/js/test/createMintWithSingleToken.test.ts

This file was deleted.

0 comments on commit 5f04d11

Please sign in to comment.