UNPKG

@accessprotocol/distributor

Version:

Access Protocol Distributor Library

89 lines (88 loc) 3.86 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"; /** Holds whether or not a claimant has claimed tokens. */ export class ClaimStatus { constructor(fields) { this.claimant = fields.claimant; this.lockedAmount = fields.lockedAmount; this.lockedAmountWithdrawn = fields.lockedAmountWithdrawn; this.unlockedAmount = fields.unlockedAmount; } 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(ClaimStatus.discriminator)) { throw new Error("invalid account discriminator"); } const dec = ClaimStatus.layout.decode(data.slice(8)); return new ClaimStatus({ claimant: dec.claimant, lockedAmount: dec.lockedAmount, lockedAmountWithdrawn: dec.lockedAmountWithdrawn, unlockedAmount: dec.unlockedAmount, }); } toJSON() { return { claimant: this.claimant.toString(), lockedAmount: this.lockedAmount.toString(), lockedAmountWithdrawn: this.lockedAmountWithdrawn.toString(), unlockedAmount: this.unlockedAmount.toString(), }; } static fromJSON(obj) { return new ClaimStatus({ claimant: new PublicKey(obj.claimant), lockedAmount: new BN(obj.lockedAmount), lockedAmountWithdrawn: new BN(obj.lockedAmountWithdrawn), unlockedAmount: new BN(obj.unlockedAmount), }); } } ClaimStatus.discriminator = Buffer.from([ 22, 183, 249, 157, 247, 95, 150, 96, ]); ClaimStatus.layout = borsh.struct([ borsh.publicKey("claimant"), borsh.u64("lockedAmount"), borsh.u64("lockedAmountWithdrawn"), borsh.u64("unlockedAmount"), ]); ClaimStatus.getAddress = (claimant, distributor, programId = PROGRAM_ID) => { return PublicKey.findProgramAddressSync([Buffer.from("ClaimStatus"), claimant.toBuffer(), distributor.toBuffer()], programId)[0]; };