solmash-whitelist-sdk-v2
Version:
A sdk for interacting Solmash Whitelist program
331 lines • 17.3 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SolmashWhitelistService = void 0;
const assert_1 = __importDefault(require("assert"));
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const anchor_1 = require("@project-serum/anchor");
const spl_token_1 = require("@solana/spl-token");
const web3_js_1 = require("@solana/web3.js");
const constants_1 = require("./constants");
const payload_1 = require("./payload");
const utils_1 = require("./utils");
class SolmashWhitelistService {
static deriveAuctionAddress(auctionName, programId) {
const _programId = new web3_js_1.PublicKey(programId);
const [auction] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(anchor_1.utils.bytes.utf8.encode(constants_1.SOLMASH_SEEDS.auctionSeed)),
Buffer.from(anchor_1.utils.bytes.utf8.encode(auctionName)),
], _programId);
return auction;
}
static deriveAuctionVaultAddress(auction, programId) {
const _auction = new web3_js_1.PublicKey(auction);
const _programId = new web3_js_1.PublicKey(programId);
const [auctionVault] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(anchor_1.utils.bytes.utf8.encode(constants_1.SOLMASH_SEEDS.auctionVaultSeed)), _auction.toBuffer()], _programId);
return auctionVault;
}
static deriveBuyerPdaAddress(auction, buyer, programId) {
const _auction = new web3_js_1.PublicKey(auction);
const _buyer = new web3_js_1.PublicKey(buyer);
const _programId = new web3_js_1.PublicKey(programId);
const [buyerPda] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(anchor_1.utils.bytes.utf8.encode(constants_1.SOLMASH_SEEDS.buyerSeed)),
_buyer.toBuffer(),
_auction.toBuffer(),
], _programId);
return buyerPda;
}
constructor(instructions, _connection, _signTransaction) {
this.instructions = instructions;
this._connection = _connection;
this._signTransaction = _signTransaction;
this._programId = this.instructions.program.programId;
}
_createPayload(tx, signers) {
const errorMap = new Map();
for (let i = 0; i < this.instructions.program.idl.errors.length; i++) {
const error = this.instructions.program.idl.errors[i];
errorMap.set(error.code, error.msg);
}
return new payload_1.SolmashWhitelistPayload(this._connection, this._signTransaction, errorMap, tx, signers);
}
async initAuction(params) {
const creator = new web3_js_1.PublicKey(params.creatorAddress);
const auctionToken = new web3_js_1.PublicKey(params.auctionTokenAddress);
const bidToken = new web3_js_1.PublicKey(params.bidTokenAddress);
const auction = SolmashWhitelistService.deriveAuctionAddress(params.name, this._programId);
console.debug("auction:", auction.toString());
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
console.debug("auction vault:", auctionVault.toString());
const auctionVaultTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, auctionVault, true);
const bidTokenDecimals = await (0, utils_1.getDecimals)(this._connection, bidToken);
const auctionTokenDecimals = await (0, utils_1.getDecimals)(this._connection, auctionToken);
const ticketPriceInSol = (0, bignumber_js_1.default)(params.ticketPriceInSol).times(web3_js_1.LAMPORTS_PER_SOL).toFixed(0);
const ticketPriceInBid = (0, bignumber_js_1.default)(params.ticketPriceInBid)
.times(constants_1.TEN_BIGNUM.pow(bidTokenDecimals))
.toFixed(0);
const tokenQuantityPerTicket = (0, bignumber_js_1.default)(params.tokenQuantityPerTicket)
.times(constants_1.TEN_BIGNUM.pow(auctionTokenDecimals))
.toFixed(0);
const ix = await this.instructions.getInitAuctionInstruction(auction, auctionVault, auctionVaultTokenAccount, creator, {
auctionToken,
bidToken,
claimTime: new anchor_1.BN(params.claimTime),
enabled: params.enabled,
name: params.name,
preSaleEndTime: new anchor_1.BN(params.preSaleEndTime),
preSaleStartTime: new anchor_1.BN(params.preSaleStartTime),
ticketPriceInBid: new anchor_1.BN(ticketPriceInBid),
ticketPriceInSol: new anchor_1.BN(ticketPriceInSol),
ticketsInPool: new anchor_1.BN(params.ticketsInPool),
tokenQuantityPerTicket: new anchor_1.BN(tokenQuantityPerTicket),
});
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async addToken(params) {
const creator = new web3_js_1.PublicKey(params.creatorAddress);
const auction = new web3_js_1.PublicKey(params.auctionAddress);
const auctionData = await this.instructions.program.account.auction.fetch(auction, "confirmed");
const auctionToken = auctionData.auctionToken;
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const auctionVaultTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, auctionVault, true);
const creatorAuctionTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, creator);
const ix = await this.instructions.getAddTokenInstruction(auction, auctionToken, auctionVault, auctionVaultTokenAccount, creator, creatorAuctionTokenAccount);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async withdrawFunds(params) {
const { auctionAddress, creatorAddress, withdrawSol } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const auctionData = await this.instructions.program.account.auction.fetch(auction, "confirmed");
const auctionToken = auctionData.auctionToken;
const bidToken = auctionData.bidToken;
const creator = new web3_js_1.PublicKey(creatorAddress);
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const auctionBidTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(bidToken, auctionVault, true);
const auctionVaultTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, auctionVault, true);
const createAuctionTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, creator);
const creatorBidTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(bidToken, creator);
const ix = await this.instructions.getWithdrawFundsInstruction(auction, auctionBidTokenAccount, auctionToken, auctionVault, auctionVaultTokenAccount, bidToken, creator, createAuctionTokenAccount, creatorBidTokenAccount, withdrawSol);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
});
tx.add(ix);
return this._createPayload(tx, []);
}
async whitelist(params) {
const { auctionAddress, creatorAddress, whitelistUserAddressList } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const creator = new web3_js_1.PublicKey(creatorAddress);
(0, assert_1.default)(whitelistUserAddressList.length, "Empty whitelist user list");
const ixs = await Promise.all(whitelistUserAddressList.map(async (user) => {
const whitelistUser = new web3_js_1.PublicKey(user);
const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, whitelistUser, this._programId);
const ix = await this.instructions.getWhitelistInstruction(auction, buyerPda, creator, whitelistUser);
return ix;
}));
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
}).add(...ixs);
return this._createPayload(tx, []);
}
// async preSaleBuyUsingSpl(params: {
// auctionAddress: string;
// bidTokenAddress: string;
// buyerAddress: string;
// }) {
// const { auctionAddress, bidTokenAddress, buyerAddress } = params;
// const auction = new PublicKey(auctionAddress);
// const bidToken = new PublicKey(bidTokenAddress);
// const buyer = new PublicKey(buyerAddress);
// const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(
// auction,
// this._programId,
// );
// const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, buyer, this._programId);
// const auctionVaultBidTokenAccount = getAssociatedTokenAddressSync(bidToken, auctionVault, true);
// const buyerBidTokenAccount = getAssociatedTokenAddressSync(bidToken, buyer);
// const auctionVaultBidTokenAccountInfo = await this._connection.getAccountInfo(
// auctionVaultBidTokenAccount,
// "confirmed",
// );
// const ix = await this.instructions.getPreSaleBuyUsingSplInstruction(
// auction,
// auctionVault,
// auctionVaultBidTokenAccount,
// bidToken,
// buyer,
// buyerBidTokenAccount,
// buyerPda,
// );
// const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
// const tx = new Transaction({
// feePayer: buyer,
// blockhash,
// lastValidBlockHeight,
// });
// if (!auctionVaultBidTokenAccountInfo) {
// tx.add(
// createAssociatedTokenAccountInstruction(
// buyer,
// auctionVaultBidTokenAccount,
// auctionVault,
// bidToken,
// ),
// );
// }
// tx.add(ix);
// return this._createPayload(tx, []);
// }
async preSaleBuyUsingSol(params) {
const { auctionAddress, buyerAddress } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const buyer = new web3_js_1.PublicKey(buyerAddress);
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, buyer, this._programId);
const ix = await this.instructions.getPreSaleBuyUsingSolInstruction(auction, auctionVault, buyer, buyerPda);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: buyer,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async claimTokens(params) {
const { auctionAddress, buyerAddress } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const auctionData = await this.instructions.program.account.auction.fetch(auction, "confirmed");
const auctionToken = auctionData.auctionToken;
const buyer = new web3_js_1.PublicKey(buyerAddress);
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, buyer, this._programId);
const auctionVaultTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, auctionVault, true);
const buyerAuctionTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, buyer);
const ix = await this.instructions.getClaimTokensInstruction(auction, auctionToken, auctionVault, auctionVaultTokenAccount, buyer, buyerAuctionTokenAccount, buyerPda);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: buyer,
blockhash,
lastValidBlockHeight,
});
tx.add(ix);
return this._createPayload(tx, []);
}
async changeTime(params) {
const auction = new web3_js_1.PublicKey(params.auctionAddress);
const creator = new web3_js_1.PublicKey(params.creatorAddress);
const newStartTime = new anchor_1.BN(params.newStartTime);
const newEndTime = new anchor_1.BN(params.newEndTime);
const ix = await this.instructions.getChangeTimeInstruction(auction, creator, newStartTime, newEndTime);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async getAuctionInfo(auctionAddress) {
const auction = new web3_js_1.PublicKey(auctionAddress);
const decoded = await this.instructions.program.account.auction.fetch(auction);
const bidTokenDecimals = await (0, utils_1.getDecimals)(this._connection, decoded.bidToken);
const auctionTokenDecimals = await (0, utils_1.getDecimals)(this._connection, decoded.auctionToken);
const accumulatedBidToken = (0, bignumber_js_1.default)(decoded.accumulatedBid.toString())
.div(constants_1.TEN_BIGNUM.pow(bidTokenDecimals))
.toFixed();
const accumulatedSol = (0, bignumber_js_1.default)(decoded.accumulatedSol.toString())
.div(web3_js_1.LAMPORTS_PER_SOL)
.toFixed();
const ticketPriceInBid = (0, bignumber_js_1.default)(decoded.ticketPriceInBid.toString())
.div(constants_1.TEN_BIGNUM.pow(bidTokenDecimals))
.toFixed();
const ticketPriceInSol = (0, bignumber_js_1.default)(decoded.ticketPriceInSol.toString())
.div(web3_js_1.LAMPORTS_PER_SOL)
.toFixed();
const tokenQuantityPerTicket = (0, bignumber_js_1.default)(decoded.tokenQuantityPerTicket.toString())
.div(constants_1.TEN_BIGNUM.pow(auctionTokenDecimals))
.toFixed();
return {
accumulatedBidToken,
accumulatedSol,
auctionToken: decoded.auctionToken.toString(),
bidToken: decoded.bidToken.toString(),
enabled: decoded.enabled,
name: decoded.name,
owner: decoded.owner.toString(),
preSaleEndTime: decoded.preSaleEndTime.toNumber(),
preSaleStartTime: decoded.preSaleStartTime.toNumber(),
ticketPriceInBid,
ticketPriceInSol,
ticketsInPool: decoded.ticketsInPool.toString(),
tokenQuantityPerTicket,
claimTime: decoded.claimTime.toNumber(),
remainingTickets: decoded.remainingTickets.toString(),
ticketsClaimed: decoded.ticketsClaimed.toString(),
};
}
async getBuyerInfo(auctionAddress, buyerAddress) {
const auction = new web3_js_1.PublicKey(auctionAddress);
const buyer = new web3_js_1.PublicKey(buyerAddress);
const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, buyer, this._programId);
const decoded = await this.instructions.program.account.buyer.fetch(buyerPda);
return {
auctionAddress: decoded.auctionAddress.toString(),
buyerAddress: decoded.buyerAddress.toString(),
claimed: decoded.claimed,
participated: decoded.participated,
whitelisted: decoded.whitelisted,
blacklisted: decoded.blacklisted,
};
}
async isBuyerWhitelisted(auctionAddress, buyerAddress) {
try {
const buyerPdaData = await this.getBuyerInfo(auctionAddress, buyerAddress);
return buyerPdaData.whitelisted;
}
catch (err) {
return false;
}
}
async buyerParticipated(auctionAddress, buyerAddress) {
try {
const info = await this.getBuyerInfo(auctionAddress, buyerAddress);
return info.participated;
}
catch (err) {
return false;
}
}
async buyerClaimed(auctionAddress, buyerAddress) {
try {
const info = await this.getBuyerInfo(auctionAddress, buyerAddress);
return info.claimed;
}
catch (err) {
return false;
}
}
}
exports.SolmashWhitelistService = SolmashWhitelistService;
//# sourceMappingURL=service.js.map