Skip to content

Commit

Permalink
feat: port makeAuction action (#131)
Browse files Browse the repository at this point in the history
* feat: makeAuction action has been added

* test: run test against localhost and move vault creation inside makeAuction action

* test: refactored utility actions

* refactor: move utility actions to separate folder with test migration to localhost

* refactor: move test localhost connection and wallet generation to separate function

* refactor: test/shared/index import ordering

* test: fix addTokensToVault test

Co-authored-by: Tymur Biedukhin <[email protected]>
  • Loading branch information
tbiedukhin and Tymur Biedukhin authored Jan 5, 2022
1 parent d5c8983 commit 715d1d4
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 96 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@
"@commitlint/config-conventional"
]
}
}
}
3 changes: 0 additions & 3 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ export * from './initStore';
export * from './initStoreV2';
export * from './mintNFT';
export * from './mintEditionFromMaster';
export * from './closeVault';
export * from './createVault';
export * from './createExternalPriceAccount';
export * from './createMetadata';
export * from './createMasterEdition';
export * from './signMetadata';
Expand Down
22 changes: 10 additions & 12 deletions src/actions/closeVault.ts → src/actions/utility/closeVault.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Connection } from '../Connection';
import { Wallet } from '../wallet';

import { ActivateVault, CombineVault, Vault } from '@metaplex-foundation/mpl-token-vault';
import { Keypair, PublicKey, TransactionSignature } from '@solana/web3.js';
import { AccountLayout, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { CreateTokenAccount } from '../programs';
import { sendTransaction } from '../actions/transactions';
import BN from 'bn.js';
import { TransactionsBatch } from '../utils/transactions-batch';
import { Transaction } from '@metaplex-foundation/mpl-core';
import { Keypair, PublicKey, TransactionSignature } from '@solana/web3.js';
import { AccountLayout, Token, TOKEN_PROGRAM_ID } from '@solana/spl-token';
import { ActivateVault, CombineVault, Vault } from '@metaplex-foundation/mpl-token-vault';

import { Wallet } from '../../wallet';
import { Connection } from '../../Connection';
import { sendTransaction } from '../transactions';
import { CreateTokenAccount } from '../../programs';
import { TransactionsBatch } from '../../utils/transactions-batch';

interface CloseVaultParams {
connection: Connection;
Expand Down Expand Up @@ -116,7 +116,5 @@ export const closeVault = async ({
wallet,
});

return {
txId,
};
return { txId };
};
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { Connection } from '../Connection';
import { Wallet } from '../wallet';

import BN from 'bn.js';
import {
ExternalPriceAccountData,
Vault,
Expand All @@ -16,9 +14,11 @@ import {
} from '@solana/web3.js';
import { NATIVE_MINT } from '@solana/spl-token';
import { Transaction } from '@metaplex-foundation/mpl-core';
import { sendTransaction } from '../actions/transactions';
import { TransactionsBatch } from '../utils/transactions-batch';
import BN from 'bn.js';

import { Wallet } from '../../wallet';
import { Connection } from '../../Connection';
import { sendTransaction } from '../transactions';
import { TransactionsBatch } from '../../utils/transactions-batch';

interface CreateExternalPriceAccountParams {
connection: Connection;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Connection } from '../Connection';
import { Wallet } from '../wallet';

import { Transaction } from '@metaplex-foundation/mpl-core';
import { AccountLayout, MintLayout, NATIVE_MINT } from '@solana/spl-token';
import { InitVault, Vault, VaultProgram } from '@metaplex-foundation/mpl-token-vault';
import { Keypair, PublicKey, SystemProgram, TransactionSignature } from '@solana/web3.js';
import { AccountLayout, MintLayout, NATIVE_MINT } from '@solana/spl-token';
import { CreateMint, CreateTokenAccount } from '../programs';
import { sendTransaction } from '../actions/transactions';
import { TransactionsBatch } from '../utils/transactions-batch';

import { Wallet } from '../../wallet';
import { Connection } from '../../Connection';
import { sendTransaction } from '../transactions';
import { CreateMint, CreateTokenAccount } from '../../programs';
import { TransactionsBatch } from '../../utils/transactions-batch';

interface CreateVaultParams {
connection: Connection;
Expand Down
4 changes: 4 additions & 0 deletions src/actions/utility/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * from './closeVault';
export * from './createExternalPriceAccount';
export * from './createVault';
export * from './initAuction';
60 changes: 60 additions & 0 deletions src/actions/utility/initAuction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Transaction } from '@metaplex-foundation/mpl-core';
import {
Auction,
AuctionExtended,
CreateAuction,
CreateAuctionArgs,
} from '@metaplex-foundation/mpl-auction';
import { PublicKey, TransactionSignature } from '@solana/web3.js';

import { Wallet } from '../../wallet';
import { Connection } from '../../Connection';
import { sendTransaction } from '../transactions';

interface MakeAuctionParams {
connection: Connection;
wallet: Wallet;
vault: PublicKey;
auctionSettings: Omit<CreateAuctionArgs, 'resource' | 'authority'>;
}

interface MakeAuctionResponse {
txId: TransactionSignature;
auction: PublicKey;
}

export const initAuction = async ({
connection,
wallet,
vault,
auctionSettings,
}: MakeAuctionParams): Promise<MakeAuctionResponse> => {
const txOptions = { feePayer: wallet.publicKey };

const [auctionKey, auctionExtended] = await Promise.all([
Auction.getPDA(vault),
AuctionExtended.getPDA(vault),
]);

const fullSettings = new CreateAuctionArgs({
...auctionSettings,
authority: wallet.publicKey.toBase58(),
resource: vault.toBase58(),
});

const auctionTx: Transaction = new CreateAuction(txOptions, {
args: fullSettings,
auction: auctionKey,
creator: wallet.publicKey,
auctionExtended,
});

const txId = await sendTransaction({
connection,
signers: [],
txs: [auctionTx],
wallet,
});

return { txId, auction: auctionKey };
};
21 changes: 7 additions & 14 deletions test/actions/addTokensToVault.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
import BN from 'bn.js';
import { Transaction } from '@metaplex-foundation/mpl-core';
import { airdrop, LOCALHOST } from '@metaplex-foundation/amman';
import { Keypair, sendAndConfirmTransaction } from '@solana/web3.js';
import { sendAndConfirmTransaction } from '@solana/web3.js';

import { Connection, NodeWallet } from '../../src';
import {
addTokensToVault,
createExternalPriceAccount,
createVault,
prepareTokenAccountAndMintTxs,
} from '../../src/actions';
import { generateConnectionAndWallet } from './shared';
import { addTokensToVault, prepareTokenAccountAndMintTxs } from '../../src/actions';
import { createExternalPriceAccount, createVault } from '../../src/actions/utility';

describe('addTokensToVault action', () => {
test('creation and adding of multiple mint tokens to newly created vault', async () => {
const payer = Keypair.generate();
const wallet = new NodeWallet(payer);
const connection = new Connection(LOCALHOST, 'confirmed');
await airdrop(connection, payer.publicKey, 10);

const TOKEN_AMOUNT = 2;

const { connection, wallet, payer } = await generateConnectionAndWallet();

const externalPriceAccountData = await createExternalPriceAccount({ connection, wallet });

const { vault } = await createVault({
Expand Down
24 changes: 0 additions & 24 deletions test/actions/createExternalPriceAccountLocal.test.ts

This file was deleted.

15 changes: 13 additions & 2 deletions test/actions/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios, { AxiosResponse } from 'axios';
import { Wallet } from '../../../src';
import { Keypair } from '@solana/web3.js';
import axios, { AxiosResponse } from 'axios';
import { airdrop, LOCALHOST } from '@metaplex-foundation/amman';

import { Connection, NodeWallet, Wallet } from '../../../src';

export const uri =
'https://bafkreibj4hjlhf3ehpugvfy6bzhhu2c7frvyhrykjqmoocsvdw24omfqga.ipfs.dweb.link';
Expand Down Expand Up @@ -52,3 +54,12 @@ export const mockAxios404 = () => {
};
mockedAxiosGet.mockRejectedValue(mockedResponse);
};

export const generateConnectionAndWallet = async () => {
const payer = Keypair.generate();
const connection = new Connection(LOCALHOST, 'confirmed');
await airdrop(connection, payer.publicKey, 10);
const wallet = new NodeWallet(payer);

return { connection, wallet, payer };
};
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { NATIVE_MINT } from '@solana/spl-token';
import { Connection, NodeWallet } from '../../src';
import { createVault, closeVault, createExternalPriceAccount } from '../../src/actions';
import { FEE_PAYER, pause } from '../utils';
import { Vault, VaultState } from '@metaplex-foundation/mpl-token-vault';

describe('closing a Vault', () => {
const connection = new Connection('devnet');
const wallet = new NodeWallet(FEE_PAYER);
import { pause } from '../../utils';
import { generateConnectionAndWallet } from '../shared';
import { closeVault, createVault, createExternalPriceAccount } from '../../../src/actions/utility';

describe('closing a Vault', () => {
describe('success', () => {
test('closes vault', async () => {
const { connection, wallet } = await generateConnectionAndWallet();
let vault;

const externalPriceAccountData = await createExternalPriceAccount({ connection, wallet });

await pause(20000);

const vaultResponse = await createVault({
connection,
wallet,
...externalPriceAccountData,
});

await pause(20000);
await pause(1000);

vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Inactive);
Expand All @@ -34,11 +32,11 @@ describe('closing a Vault', () => {
priceMint: NATIVE_MINT,
});

await pause(20000);
await pause(1000);

vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Combined);
}, 70000);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Connection, NodeWallet } from '../../src';
import { createExternalPriceAccount } from '../../src/actions';
import { FEE_PAYER } from '../utils';
import { generateConnectionAndWallet } from '../shared';
import { createExternalPriceAccount } from '../../../src/actions/utility';

describe('creating an external price account', () => {
const connection = new Connection('devnet');
const wallet = new NodeWallet(FEE_PAYER);

describe('success', () => {
test('creates EPA', async () => {
const { connection, wallet } = await generateConnectionAndWallet();

const externalPriceAccount = await createExternalPriceAccount({
connection,
wallet,
});

expect(externalPriceAccount).toHaveProperty('externalPriceAccount');
expect(externalPriceAccount).toHaveProperty('priceMint');
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { Connection, NodeWallet } from '../../src';
import { createVault, createExternalPriceAccount } from '../../src/actions';
import { FEE_PAYER, pause } from '../utils';
import { Vault, VaultState } from '@metaplex-foundation/mpl-token-vault';

describe('creating a Vault', () => {
const connection = new Connection('devnet');
const wallet = new NodeWallet(FEE_PAYER);
import { pause } from '../../utils';
import { createVault, createExternalPriceAccount } from '../../../src/actions/utility';
import { generateConnectionAndWallet } from '../shared';

describe('creating a Vault', () => {
describe('success', () => {
test('generates vault', async () => {
const externalPriceAccountData = await createExternalPriceAccount({ connection, wallet });
const { connection, wallet } = await generateConnectionAndWallet();

await pause(20000);
const externalPriceAccountData = await createExternalPriceAccount({ connection, wallet });

const vaultResponse = await createVault({
connection,
wallet,
...externalPriceAccountData,
});

await pause(20000);
await pause(1000);
const vault = await Vault.load(connection, vaultResponse.vault);
expect(vault).toHaveProperty('data');
expect(vault.data.state).toEqual(VaultState.Inactive);
}, 50000);
});
});
});
Loading

0 comments on commit 715d1d4

Please sign in to comment.