Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add instant sale action #57

Merged
merged 6 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@solana/spl-token": "^0.1.8",
"@solana/web3.js": "^1.30.2",
"@types/bs58": "^4.0.1",
"async-retry": "^1.3.3",
"axios": "^0.21.4",
"bn.js": "^5.2.0",
"borsh": "^0.4.0",
Expand All @@ -59,6 +60,7 @@
"@rollup/plugin-replace": "^3.0.0",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"@types/async-retry": "^1.4.3",
"@types/jest": "^27.0.1",
"@types/node": "^16.9.2",
"@typescript-eslint/eslint-plugin": "^4.30.0",
Expand Down
1 change: 1 addition & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './cancelBid';
export * from './placeBid';
export * from './redeemBid';
export * from './claimBid';
export * from './instantSale';
124 changes: 124 additions & 0 deletions src/actions/instantSale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { PublicKey } from '@solana/web3.js';
import { AccountLayout } from '@solana/spl-token';
import retry from 'async-retry';
import { Wallet } from '../wallet';
import { Connection } from '../Connection';
import { sendTransaction } from './transactions';
import { Auction, AuctionExtended, BidderPot } from '../programs/auction';
import { AuctionManager, SafetyDepositConfig } from '../programs/metaplex';
import { placeBid } from './placeBid';
import { getClaimBidTransactions } from './claimBid';
import { getRedeemBidTransactions } from './redeemBid';
import { Vault } from '../programs/vault/accounts/Vault';
import { Metadata } from '../programs/metadata';
import { getBidRedemptionPDA } from './redeemBid';
import { Account } from '../Account';

interface IInstantSaleParams {
connection: Connection;
wallet: Wallet;
auction: PublicKey;
store: PublicKey;
}

interface IInstantSaleResponse {
txId: string;
}

export const instantSale = async ({
connection,
wallet,
store,
auction,
}: IInstantSaleParams): Promise<IInstantSaleResponse> => {
// get data for transactions
const bidder = wallet.publicKey;
const accountRentExempt = await connection.getMinimumBalanceForRentExemption(AccountLayout.span);
const auctionManager = await AuctionManager.getPDA(auction);
const manager = await AuctionManager.load(connection, auctionManager);
const vault = await Vault.load(connection, manager.data.vault);
const {
data: { tokenMint },
} = await Auction.load(connection, auction);
const auctionExtended = await AuctionExtended.getPDA(vault.pubkey);
const acceptPayment = new PublicKey(manager.data.acceptPayment);
const {
data: { instantSalePrice },
} = await AuctionExtended.load(connection, auctionExtended);
const auctionTokenMint = new PublicKey(tokenMint);
const bidderPot = await BidderPot.getPDA(auction, bidder);
const fractionMint = new PublicKey(vault.data.fractionMint);
// assuming we have 1 item
const [safetyDepositBox] = await vault.getSafetyDepositBoxes(connection);
const metadataTokenMint = new PublicKey(safetyDepositBox.data.tokenMint);
const safetyDepositTokenStore = new PublicKey(safetyDepositBox.data.store);
const safetyDepositConfig = await SafetyDepositConfig.getPDA(
auctionManager,
safetyDepositBox.pubkey,
);
const transferAuthority = await Vault.getPDA(vault.pubkey);
const metadata = await Metadata.getPDA(metadataTokenMint);
////

const { bidderPotToken, bidderMeta } = await placeBid({
connection,
wallet,
amount: instantSalePrice,
auction,
});

// workaround to wait for bidderMeta to be created
await retry(
async (bail) => {
await Account.getInfo(connection, bidderMeta);
},
{
retries: 5,
},
);
const bidRedemption = await getBidRedemptionPDA(auction, bidderMeta);

const redeemBatch = await getRedeemBidTransactions({
accountRentExempt,
tokenMint: metadataTokenMint,
bidder,
bidderMeta,
store,
vault: vault.pubkey,
auction,
auctionExtended,
auctionManager,
fractionMint,
safetyDepositTokenStore,
safetyDeposit: safetyDepositBox.pubkey,
bidRedemption,
safetyDepositConfig,
transferAuthority,
metadata,
});

const claimBatch = await getClaimBidTransactions({
auctionTokenMint,
bidder,
store,
vault: vault.pubkey,
auction,
auctionExtended,
auctionManager,
acceptPayment,
bidderPot,
bidderPotToken,
});

const txs = [...redeemBatch.toTransactions(), ...claimBatch.toTransactions()];
const signers = [...redeemBatch.signers, ...claimBatch.signers];

const txId = await sendTransaction({
connection,
wallet,
txs,
signers,
});

return { txId };
};
59 changes: 10 additions & 49 deletions src/actions/placeBid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ interface IPlaceBidParams {

interface IPlaceBidResponse {
txId: string;
bidderPotToken: PublicKey;
bidderMeta: PublicKey;
}

export const placeBid = async ({
Expand All @@ -45,54 +47,6 @@ export const placeBid = async ({
const bidderMeta = await BidderMetadata.getPDA(auction, bidder);
////

const txBatch = await getPlaceBidTransactions({
accountRentExempt,
bidder,
auctionTokenMint,
vault,
auction,
auctionExtended,
bidderPot,
bidderPotToken,
bidderMeta,
amount,
});

const txId = await sendTransaction({
connection,
wallet,
txs: txBatch.toTransactions(),
signers: txBatch.signers,
});

return { txId };
};

interface IPlaceBidTransactionsParams {
bidder: PublicKey;
accountRentExempt: number;
bidderPot: PublicKey;
bidderPotToken?: PublicKey;
bidderMeta: PublicKey;
auction: PublicKey;
auctionExtended: PublicKey;
auctionTokenMint: PublicKey;
vault: PublicKey;
amount: BN;
}

export const getPlaceBidTransactions = async ({
accountRentExempt,
bidder,
auctionTokenMint,
vault,
auction,
auctionExtended,
bidderPot,
bidderPotToken,
bidderMeta,
amount,
}: IPlaceBidTransactionsParams): Promise<TransactionsBatch> => {
let txBatch = new TransactionsBatch({ transactions: [] });

if (bidderPotToken) {
Expand Down Expand Up @@ -194,5 +148,12 @@ export const getPlaceBidTransactions = async ({
txBatch.addTransaction(placeBidTransaction);
////

return txBatch;
const txId = await sendTransaction({
connection,
wallet,
txs: txBatch.toTransactions(),
signers: txBatch.signers,
});

return { txId, bidderPotToken, bidderMeta };
};
27 changes: 15 additions & 12 deletions src/actions/redeemBid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TransactionsBatch } from '../utils/transactions-batch';
import {
AuctionManager,
MetaplexProgram,
RedeemBid,
RedeemFullRightsTransferBid,
SafetyDepositConfig,
} from '../programs/metaplex';
import { CreateTokenAccount } from '../programs';
Expand Down Expand Up @@ -45,13 +45,7 @@ export const redeemBid = async ({
const tokenMint = new PublicKey(safetyDepositBox.data.tokenMint);
const safetyDepositTokenStore = new PublicKey(safetyDepositBox.data.store);
const bidderMeta = await BidderMetadata.getPDA(auction, bidder);
// TODO: probably should be moved to a class
const bidRedemption = (
await PublicKey.findProgramAddress(
[Buffer.from(MetaplexProgram.PREFIX), auction.toBuffer(), bidderMeta.toBuffer()],
MetaplexProgram.PUBKEY,
)
)[0];
const bidRedemption = await getBidRedemptionPDA(auction, bidderMeta);
const safetyDepositConfig = await SafetyDepositConfig.getPDA(
auctionManager,
safetyDepositBox.pubkey,
Expand Down Expand Up @@ -144,25 +138,25 @@ export const getRedeemBidTransactions = async ({
////

// create redeem bid
const redeemBidTransaction = new RedeemBid(
const redeemBidTransaction = new RedeemFullRightsTransferBid(
{ feePayer: bidder },
{
store,
vault,
auction,
auctionManager,
bidRedemption,
bidderMeta: bidMetadata,
bidMetadata,
safetyDepositTokenStore,
destination: account.publicKey,
safetyDeposit,
fractionMint,
bidder,
// set to false for now to setup basic flow
isPrintingType: false,
safetyDepositConfig,
auctionExtended,
transferAuthority,
newAuthority: bidder,
masterMetadata: metadata,
},
);
txBatch.addTransaction(redeemBidTransaction);
Expand All @@ -182,3 +176,12 @@ export const getRedeemBidTransactions = async ({

return txBatch;
};

export const getBidRedemptionPDA = async (auction: PublicKey, bidderMeta: PublicKey) => {
return (
await PublicKey.findProgramAddress(
[Buffer.from(MetaplexProgram.PREFIX), auction.toBuffer(), bidderMeta.toBuffer()],
MetaplexProgram.PUBKEY,
)
)[0];
};
Loading