multipool-staking-sdk
Version:
This is an sdk for solmask staking pool program
64 lines • 2.98 kB
JavaScript
;
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.getWalletTokenAccount = exports.assert = exports.getDecimals = exports.getAnchorProvider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const anchor_1 = require("@project-serum/anchor");
const spl_token_1 = require("@solana/spl-token");
function getAnchorProvider(connection, wallet, opts) {
return new anchor_1.AnchorProvider(connection, wallet, opts);
}
exports.getAnchorProvider = getAnchorProvider;
function getDecimals(connection, mint) {
return __awaiter(this, void 0, void 0, function* () {
const { value: tokenAmount } = yield connection.getTokenSupply(mint, "confirmed");
return tokenAmount.decimals;
});
}
exports.getDecimals = getDecimals;
function assert(condition, message) {
if (!condition) {
throw new Error(message);
}
}
exports.assert = assert;
function getWalletTokenAccount(connection, wallet) {
return __awaiter(this, void 0, void 0, function* () {
const walletTokenAccount = yield connection.getTokenAccountsByOwner(wallet, {
programId: spl_token_1.TOKEN_PROGRAM_ID,
});
const promises = walletTokenAccount.value.map((i) => __awaiter(this, void 0, void 0, function* () {
const pubkey = i.pubkey;
const programId = i.account.owner;
const accountInfo = spl_token_1.AccountLayout.decode(i.account.data);
const decimals = yield getDecimals(connection, accountInfo.mint);
const amount = (0, bignumber_js_1.default)(accountInfo.amount.toString())
.div((0, bignumber_js_1.default)(10).pow(decimals))
.toFixed();
return {
pubkey,
programId,
info: {
mint: accountInfo.mint,
owner: accountInfo.owner,
amount,
},
};
}));
const tokenAccounts = yield Promise.all(promises);
return tokenAccounts;
});
}
exports.getWalletTokenAccount = getWalletTokenAccount;
//# sourceMappingURL=utils.js.map