solmash-whitelist-sdk
Version:
A sdk for interacting Solmash Whitelist program
270 lines • 15.7 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 [auctionAddress] = 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 auctionAddress;
}
static deriveAuctionVaultAddress(auctionAddress, programId) {
const [auctionVaultAddress] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(anchor_1.utils.bytes.utf8.encode(constants_1.SOLMASH_SEEDS.auctionVaultSeed)),
auctionAddress.toBuffer(),
], programId);
return auctionVaultAddress;
}
static deriveBuyerPdaAddress(auctionAddress, buyer, programId) {
const [auctionVaultAddress] = web3_js_1.PublicKey.findProgramAddressSync([
Buffer.from(anchor_1.utils.bytes.utf8.encode(constants_1.SOLMASH_SEEDS.buyerSeed)),
buyer.toBuffer(),
auctionAddress.toBuffer(),
], programId);
return auctionVaultAddress;
}
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();
this.instructions.program.idl.errors.forEach((error) => errorMap.set(error.code, error.msg));
return new payload_1.SolmashWhitelistPayload(this._connection, this._signTransaction, errorMap, tx, signers);
}
async initAuction(params) {
const owner = new web3_js_1.PublicKey(params.ownerAddress);
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 address:", auction.toString());
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const ticketPriceInSol = (0, bignumber_js_1.default)(params.ticketPriceInSol).times(web3_js_1.LAMPORTS_PER_SOL).toFixed(0);
const bidTokenDecimals = await (0, utils_1.getDecimals)(this._connection, bidToken);
const ticketPriceInSobb = (0, bignumber_js_1.default)(params.ticketPriceInSobb)
.times((0, bignumber_js_1.default)(10).pow(bidTokenDecimals))
.toFixed(0);
const ix = await this.instructions.getInitAuctionInstruction(auction, auctionVault, owner, {
enabled: params.enabled,
name: params.name,
preSaleEndTime: new anchor_1.BN(params.preSaleEndTime),
preSaleStartTime: new anchor_1.BN(params.preSaleStartTime),
ticketPriceInSobb: new anchor_1.BN(ticketPriceInSobb),
ticketPriceInSol: new anchor_1.BN(ticketPriceInSol),
ticketsInPool: new anchor_1.BN(params.ticketsInPool),
tokenQuantityPerTicket: new anchor_1.BN(params.tokenQuantityPerTicket),
auctionToken,
bidToken,
});
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: owner,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async addToken(params) {
const owner = new web3_js_1.PublicKey(params.ownerAddress);
const auction = new web3_js_1.PublicKey(params.auctionAddress);
const auctionToken = new web3_js_1.PublicKey(params.auctionTokenAddress);
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const auctionVaultTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, auctionVault, true);
const ownerAuctionTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(auctionToken, owner);
const ix = await this.instructions.getAddTokenInstruction(auction, auctionToken, auctionVault, auctionVaultTokenAccount, owner, ownerAuctionTokenAccount);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: owner,
blockhash,
lastValidBlockHeight,
}).add(ix);
return this._createPayload(tx, []);
}
async withdrawFunds(params) {
const { auctionAddress, auctionTokenAddress, bidTokenAddress, creatorAddress } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const auctionToken = new web3_js_1.PublicKey(auctionTokenAddress);
const bidToken = new web3_js_1.PublicKey(bidTokenAddress);
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 creatorBidTokenAccountInfo = await this._connection.getAccountInfo(creatorBidTokenAccount, "confirmed");
const auctionVaultTokenAccountInfo = await this._connection.getAccountInfo(auctionVaultTokenAccount, "confirmed");
const ix = await this.instructions.getWithdrawFundsInstruction(auction, auctionBidTokenAccount, auctionToken, auctionVault, auctionVaultTokenAccount, bidToken, creator, createAuctionTokenAccount, creatorBidTokenAccount);
const { blockhash, lastValidBlockHeight } = await this._connection.getLatestBlockhash();
const tx = new web3_js_1.Transaction({
feePayer: creator,
blockhash,
lastValidBlockHeight,
});
if (!auctionVaultTokenAccountInfo) {
tx.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(creator, auctionVaultTokenAccount, auctionVault, auctionToken));
}
if (!creatorBidTokenAccountInfo) {
console.info("Creator doesn't have bid token account");
tx.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(creator, creatorBidTokenAccount, creator, bidToken));
}
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) {
const { auctionAddress, bidTokenAddress, buyerAddress } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const bidToken = new web3_js_1.PublicKey(bidTokenAddress);
const buyer = new web3_js_1.PublicKey(buyerAddress);
const auctionVault = SolmashWhitelistService.deriveAuctionVaultAddress(auction, this._programId);
const buyerPda = SolmashWhitelistService.deriveBuyerPdaAddress(auction, buyer, this._programId);
const auctionVaultBidTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(bidToken, auctionVault, true);
const buyerBidTokenAccount = (0, spl_token_1.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 web3_js_1.Transaction({
feePayer: buyer,
blockhash,
lastValidBlockHeight,
});
if (!auctionVaultBidTokenAccountInfo) {
tx.add((0, spl_token_1.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, auctionTokenAddress, buyerAddress } = params;
const auction = new web3_js_1.PublicKey(auctionAddress);
const auctionToken = new web3_js_1.PublicKey(auctionTokenAddress);
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 buyerAuctionTokenAccountInfo = await this._connection.getAccountInfo(buyerAuctionTokenAccount);
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,
});
if (!buyerAuctionTokenAccountInfo) {
console.info("Claimer doesn't have auction token account");
tx.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(buyer, buyerAuctionTokenAccount, buyer, auctionToken));
}
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);
return {
accumulatedSobb: decoded.accumulatedSobb.toString(),
accumulatedSol: decoded.accumulatedSol.toString(),
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(),
ticketPriceInSobb: decoded.ticketPriceInSobb.toString(),
ticketPriceInSol: decoded.ticketPriceInSol.toString(),
ticketsInPool: decoded.ticketsInPool.toString(),
tokenQuantityPerTicket: decoded.tokenQuantityPerTicket.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 {
claimed: decoded.claimed,
participated: decoded.participated,
whitelisted: decoded.whitelisted,
blacklisted: decoded.blacklisted,
};
}
async isBuyerWhitelisted(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 accountInfo = await this._connection.getAccountInfo(buyerPda, "confirmed");
return accountInfo !== null;
}
async buyerParticipated(auctionAddress, buyerAddress) {
const info = await this.getBuyerInfo(auctionAddress, buyerAddress);
return info.participated;
}
async buyerClaimed(auctionAddress, buyerAddress) {
const info = await this.getBuyerInfo(auctionAddress, buyerAddress);
return info.claimed;
}
}
exports.SolmashWhitelistService = SolmashWhitelistService;
//# sourceMappingURL=service.js.map