UNPKG

@accessprotocol/distributor

Version:

Access Protocol Distributor Library

139 lines (138 loc) 5.77 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { PublicKey } from "@solana/web3.js"; import BN from "bn.js"; // eslint-disable-line @typescript-eslint/no-unused-vars import * as borsh from "@coral-xyz/borsh"; // eslint-disable-line @typescript-eslint/no-unused-vars import { PROGRAM_ID } from "../programId"; /** State for the account which distributes tokens. */ export class MerkleDistributor { constructor(fields) { this.bump = fields.bump; this.version = fields.version; this.root = fields.root; this.mint = fields.mint; this.tokenVault = fields.tokenVault; this.maxTotalClaim = fields.maxTotalClaim; this.maxNumNodes = fields.maxNumNodes; this.totalAmountClaimed = fields.totalAmountClaimed; this.numNodesClaimed = fields.numNodesClaimed; this.startTs = fields.startTs; this.endTs = fields.endTs; this.creator = fields.creator; this.admin = fields.admin; this.clawedBack = fields.clawedBack; } static fetch(c_1, address_1) { return __awaiter(this, arguments, void 0, function* (c, address, programId = PROGRAM_ID) { const info = yield c.getAccountInfo(address); if (info === null) { return null; } if (!info.owner.equals(programId)) { throw new Error("account doesn't belong to this program"); } return this.decode(info.data); }); } static fetchMultiple(c_1, addresses_1) { return __awaiter(this, arguments, void 0, function* (c, addresses, programId = PROGRAM_ID) { const infos = yield c.getMultipleAccountsInfo(addresses); return infos.map((info) => { if (info === null) { return null; } if (!info.owner.equals(programId)) { throw new Error("account doesn't belong to this program"); } return this.decode(info.data); }); }); } static decode(data) { if (!data.slice(0, 8).equals(MerkleDistributor.discriminator)) { throw new Error("invalid account discriminator"); } const dec = MerkleDistributor.layout.decode(data.slice(8)); return new MerkleDistributor({ bump: dec.bump, version: dec.version, root: dec.root, mint: dec.mint, tokenVault: dec.tokenVault, maxTotalClaim: dec.maxTotalClaim, maxNumNodes: dec.maxNumNodes, totalAmountClaimed: dec.totalAmountClaimed, numNodesClaimed: dec.numNodesClaimed, startTs: dec.startTs, endTs: dec.endTs, creator: dec.creator, admin: dec.admin, clawedBack: dec.clawedBack, }); } toJSON() { return { bump: this.bump, version: this.version.toString(), root: this.root, mint: this.mint.toString(), tokenVault: this.tokenVault.toString(), maxTotalClaim: this.maxTotalClaim.toString(), maxNumNodes: this.maxNumNodes.toString(), totalAmountClaimed: this.totalAmountClaimed.toString(), numNodesClaimed: this.numNodesClaimed.toString(), startTs: this.startTs.toString(), endTs: this.endTs.toString(), creator: this.creator.toString(), admin: this.admin.toString(), clawedBack: this.clawedBack, }; } static fromJSON(obj) { return new MerkleDistributor({ bump: obj.bump, version: new BN(obj.version), root: obj.root, mint: new PublicKey(obj.mint), tokenVault: new PublicKey(obj.tokenVault), maxTotalClaim: new BN(obj.maxTotalClaim), maxNumNodes: new BN(obj.maxNumNodes), totalAmountClaimed: new BN(obj.totalAmountClaimed), numNodesClaimed: new BN(obj.numNodesClaimed), startTs: new BN(obj.startTs), endTs: new BN(obj.endTs), creator: new PublicKey(obj.creator), admin: new PublicKey(obj.admin), clawedBack: obj.clawedBack, }); } } MerkleDistributor.discriminator = Buffer.from([ 77, 119, 139, 70, 84, 247, 12, 26, ]); MerkleDistributor.layout = borsh.struct([ borsh.u8("bump"), borsh.u64("version"), borsh.array(borsh.u8(), 32, "root"), borsh.publicKey("mint"), borsh.publicKey("tokenVault"), borsh.u64("maxTotalClaim"), borsh.u64("maxNumNodes"), borsh.u64("totalAmountClaimed"), borsh.u64("numNodesClaimed"), borsh.i64("startTs"), borsh.i64("endTs"), borsh.publicKey("creator"), borsh.publicKey("admin"), borsh.bool("clawedBack"), ]); MerkleDistributor.getAddress = (creator, mint, version, programId = PROGRAM_ID) => { return PublicKey.findProgramAddressSync([Buffer.from("MerkleDistributor"), mint.toBuffer(), creator.toBuffer(), version.toArrayLike(Buffer, "le", 8)], programId)[0]; };