@accessprotocol/distributor
Version:
Access Protocol Distributor Library
129 lines (128 loc) • 5.64 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClaimStatus = void 0;
const web3_js_1 = require("@solana/web3.js");
const bn_js_1 = __importDefault(require("bn.js")); // eslint-disable-line @typescript-eslint/no-unused-vars
const borsh = __importStar(require("@coral-xyz/borsh")); // eslint-disable-line @typescript-eslint/no-unused-vars
const programId_1 = require("../programId");
/** Holds whether or not a claimant has claimed tokens. */
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 = programId_1.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 = programId_1.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 web3_js_1.PublicKey(obj.claimant),
lockedAmount: new bn_js_1.default(obj.lockedAmount),
lockedAmountWithdrawn: new bn_js_1.default(obj.lockedAmountWithdrawn),
unlockedAmount: new bn_js_1.default(obj.unlockedAmount),
});
}
}
exports.ClaimStatus = ClaimStatus;
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 = programId_1.PROGRAM_ID) => {
return web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("ClaimStatus"), claimant.toBuffer(), distributor.toBuffer()], programId)[0];
};