-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(actions): add redeem and claim bid actions (#55)
* feat: add redeem bid action * feat: add claim bid action
- Loading branch information
Showing
5 changed files
with
314 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { PublicKey } from '@solana/web3.js'; | ||
import { Wallet } from '../wallet'; | ||
import { Connection } from '../Connection'; | ||
import { sendTransaction } from './transactions'; | ||
import { Auction, AuctionExtended, BidderPot } from '../programs/auction'; | ||
import { TransactionsBatch } from '../utils/transactions-batch'; | ||
import { AuctionManager, ClaimBid } from '../programs/metaplex'; | ||
|
||
interface IClaimBidParams { | ||
connection: Connection; | ||
wallet: Wallet; | ||
auction: PublicKey; | ||
store: PublicKey; | ||
bidderPotToken: PublicKey; | ||
} | ||
|
||
interface IClaimBidResponse { | ||
txId: string; | ||
} | ||
|
||
export const claimBid = async ({ | ||
connection, | ||
wallet, | ||
store, | ||
auction, | ||
bidderPotToken, | ||
}: IClaimBidParams): Promise<IClaimBidResponse> => { | ||
// get data for transactions | ||
const bidder = wallet.publicKey; | ||
const auctionManager = await AuctionManager.getPDA(auction); | ||
const manager = await AuctionManager.load(connection, auctionManager); | ||
const vault = new PublicKey(manager.data.vault); | ||
const { | ||
data: { tokenMint }, | ||
} = await Auction.load(connection, auction); | ||
const acceptPayment = new PublicKey(manager.data.acceptPayment); | ||
const auctionExtended = await AuctionExtended.getPDA(vault); | ||
const auctionTokenMint = new PublicKey(tokenMint); | ||
const bidderPot = await BidderPot.getPDA(auction, bidder); | ||
//// | ||
|
||
const txBatch = await getClaimBidTransactions({ | ||
auctionTokenMint, | ||
bidder, | ||
store, | ||
vault, | ||
auction, | ||
auctionExtended, | ||
auctionManager, | ||
acceptPayment, | ||
bidderPot, | ||
bidderPotToken, | ||
}); | ||
|
||
const txId = await sendTransaction({ | ||
connection, | ||
wallet, | ||
txs: txBatch.toTransactions(), | ||
signers: txBatch.signers, | ||
}); | ||
|
||
return { txId }; | ||
}; | ||
|
||
interface IClaimBidTransactionsParams { | ||
bidder: PublicKey; | ||
bidderPotToken?: PublicKey; | ||
bidderPot: PublicKey; | ||
auction: PublicKey; | ||
auctionExtended: PublicKey; | ||
auctionTokenMint: PublicKey; | ||
vault: PublicKey; | ||
store: PublicKey; | ||
auctionManager: PublicKey; | ||
acceptPayment: PublicKey; | ||
} | ||
|
||
export const getClaimBidTransactions = async ({ | ||
bidder, | ||
auctionTokenMint, | ||
store, | ||
vault, | ||
auction, | ||
auctionManager, | ||
auctionExtended, | ||
acceptPayment, | ||
bidderPot, | ||
bidderPotToken, | ||
}: IClaimBidTransactionsParams) => { | ||
const txBatch = new TransactionsBatch({ transactions: [] }); | ||
|
||
// create claim bid | ||
const claimBidTransaction = new ClaimBid( | ||
{ feePayer: bidder }, | ||
{ | ||
store, | ||
vault, | ||
auction, | ||
auctionExtended, | ||
auctionManager, | ||
bidder, | ||
tokenMint: auctionTokenMint, | ||
acceptPayment, | ||
bidderPot, | ||
bidderPotToken, | ||
}, | ||
); | ||
txBatch.addTransaction(claimBidTransaction); | ||
//// | ||
|
||
return txBatch; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
import { Keypair, PublicKey } from '@solana/web3.js'; | ||
import { AccountLayout } from '@solana/spl-token'; | ||
import { Wallet } from '../wallet'; | ||
import { Connection } from '../Connection'; | ||
import { sendTransaction } from './transactions'; | ||
import { AuctionExtended, BidderMetadata } from '../programs/auction'; | ||
import { TransactionsBatch } from '../utils/transactions-batch'; | ||
import { | ||
AuctionManager, | ||
MetaplexProgram, | ||
RedeemBid, | ||
SafetyDepositConfig, | ||
} from '../programs/metaplex'; | ||
import { CreateTokenAccount } from '../programs'; | ||
import { Vault } from '../programs/vault'; | ||
import { Metadata, UpdatePrimarySaleHappenedViaToken } from '../programs/metadata'; | ||
|
||
interface IRedeemBidParams { | ||
connection: Connection; | ||
wallet: Wallet; | ||
auction: PublicKey; | ||
store: PublicKey; | ||
} | ||
|
||
interface IRedeemBidResponse { | ||
txId: string; | ||
} | ||
|
||
export const redeemBid = async ({ | ||
connection, | ||
wallet, | ||
store, | ||
auction, | ||
}: IRedeemBidParams): Promise<IRedeemBidResponse> => { | ||
// 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 fractionMint = new PublicKey(vault.data.fractionMint); | ||
const auctionExtended = await AuctionExtended.getPDA(vault.pubkey); | ||
// assuming we have 1 item | ||
const [safetyDepositBox] = await vault.getSafetyDepositBoxes(connection); | ||
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 safetyDepositConfig = await SafetyDepositConfig.getPDA( | ||
auctionManager, | ||
safetyDepositBox.pubkey, | ||
); | ||
const transferAuthority = await Vault.getPDA(vault.pubkey); | ||
const metadata = await Metadata.getPDA(tokenMint); | ||
//// | ||
|
||
const txBatch = await getRedeemBidTransactions({ | ||
accountRentExempt, | ||
tokenMint, | ||
bidder, | ||
bidderMeta, | ||
store, | ||
vault: vault.pubkey, | ||
auction, | ||
auctionExtended, | ||
auctionManager, | ||
fractionMint, | ||
safetyDepositTokenStore, | ||
safetyDeposit: safetyDepositBox.pubkey, | ||
bidRedemption, | ||
safetyDepositConfig, | ||
transferAuthority, | ||
metadata, | ||
}); | ||
|
||
const txId = await sendTransaction({ | ||
connection, | ||
wallet, | ||
txs: txBatch.toTransactions(), | ||
signers: txBatch.signers, | ||
}); | ||
|
||
return { txId }; | ||
}; | ||
|
||
interface IRedeemBidTransactionsParams { | ||
bidder: PublicKey; | ||
accountRentExempt: number; | ||
bidderPotToken?: PublicKey; | ||
bidderMeta: PublicKey; | ||
auction: PublicKey; | ||
auctionExtended: PublicKey; | ||
tokenMint: PublicKey; | ||
vault: PublicKey; | ||
store: PublicKey; | ||
auctionManager: PublicKey; | ||
bidRedemption: PublicKey; | ||
safetyDepositTokenStore: PublicKey; | ||
safetyDeposit: PublicKey; | ||
fractionMint: PublicKey; | ||
safetyDepositConfig: PublicKey; | ||
transferAuthority: PublicKey; | ||
metadata: PublicKey; | ||
} | ||
|
||
export const getRedeemBidTransactions = async ({ | ||
accountRentExempt, | ||
bidder, | ||
tokenMint, | ||
store, | ||
vault, | ||
auction, | ||
auctionManager, | ||
auctionExtended, | ||
bidRedemption, | ||
bidderMeta: bidMetadata, | ||
safetyDepositTokenStore, | ||
safetyDeposit, | ||
fractionMint, | ||
safetyDepositConfig, | ||
transferAuthority, | ||
metadata, | ||
}: IRedeemBidTransactionsParams) => { | ||
const txBatch = new TransactionsBatch({ transactions: [] }); | ||
|
||
// create a new account for redeeming | ||
const account = Keypair.generate(); | ||
const createDestinationTransaction = new CreateTokenAccount( | ||
{ feePayer: bidder }, | ||
{ | ||
newAccountPubkey: account.publicKey, | ||
lamports: accountRentExempt, | ||
mint: tokenMint, | ||
}, | ||
); | ||
txBatch.addSigner(account); | ||
txBatch.addTransaction(createDestinationTransaction); | ||
//// | ||
|
||
// create redeem bid | ||
const redeemBidTransaction = new RedeemBid( | ||
{ feePayer: bidder }, | ||
{ | ||
store, | ||
vault, | ||
auction, | ||
auctionManager, | ||
bidRedemption, | ||
bidderMeta: bidMetadata, | ||
safetyDepositTokenStore, | ||
destination: account.publicKey, | ||
safetyDeposit, | ||
fractionMint, | ||
bidder, | ||
// set to false for now to setup basic flow | ||
isPrintingType: false, | ||
safetyDepositConfig, | ||
auctionExtended, | ||
transferAuthority, | ||
}, | ||
); | ||
txBatch.addTransaction(redeemBidTransaction); | ||
//// | ||
|
||
// update primary sale happened via token | ||
const updatePrimarySaleHappenedViaTokenTransaction = new UpdatePrimarySaleHappenedViaToken( | ||
{ feePayer: bidder }, | ||
{ | ||
metadata, | ||
owner: bidder, | ||
tokenAccount: account.publicKey, | ||
}, | ||
); | ||
txBatch.addTransaction(updatePrimarySaleHappenedViaTokenTransaction); | ||
//// | ||
|
||
return txBatch; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters