@sen-use/web3
Version:
The library for Sentre
124 lines (123 loc) • 5.4 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.initTxCreateMultiTokenAccount = exports.initTxCreateTokenAccount = exports.initTxCreateMint = exports.initTxMintTo = void 0;
const anchor_1 = require("@project-serum/anchor");
const rpc_1 = require("../rpc");
const utils_1 = require("../utils");
const initTxMintTo = (provider, { mintAddress, amount, dstAddress = provider.wallet.publicKey }) => __awaiter(void 0, void 0, void 0, function* () {
const splProgram = anchor_1.Spl.token(provider);
const mintPublicKey = (0, utils_1.toPublicKey)(mintAddress);
const associatedAddress = yield anchor_1.utils.token.associatedAddress({
mint: mintPublicKey,
owner: (0, utils_1.toPublicKey)(dstAddress),
});
const ixMintTo = yield splProgram.methods
.mintTo(amount)
.accounts({
mint: mintPublicKey,
to: associatedAddress,
authority: provider.wallet.publicKey,
})
.instruction();
return ixMintTo;
});
exports.initTxMintTo = initTxMintTo;
const initTxCreateMint = (provider, { mint, decimals = 9, mintAuthority = provider.wallet.publicKey, freezeAuthority = provider.wallet.publicKey, }) => __awaiter(void 0, void 0, void 0, function* () {
const splProgram = anchor_1.Spl.token(provider);
const ixCreate = yield splProgram.account.mint.createInstruction(mint);
const ixRent = yield splProgram.methods
.initializeMint(decimals, (0, utils_1.toPublicKey)(mintAuthority), (0, utils_1.toPublicKey)(freezeAuthority))
.accounts({
mint: mint.publicKey,
rent: anchor_1.web3.SYSVAR_RENT_PUBKEY,
})
.instruction();
return new anchor_1.web3.Transaction().add(ixCreate).add(ixRent);
});
exports.initTxCreateMint = initTxCreateMint;
const initTxCreateTokenAccount = (provider, { mintAddress, owner = provider.wallet.publicKey }) => __awaiter(void 0, void 0, void 0, function* () {
const mintPublicKey = (0, utils_1.toPublicKey)(mintAddress);
const payerPublicKey = (0, utils_1.toPublicKey)(provider.wallet.publicKey);
const ownerPublicKey = (0, utils_1.toPublicKey)(owner);
const associatedTokenAccount = yield anchor_1.utils.token.associatedAddress({
mint: mintPublicKey,
owner: ownerPublicKey,
});
const ix = new anchor_1.web3.TransactionInstruction({
keys: [
{
pubkey: payerPublicKey,
isSigner: true,
isWritable: true,
},
{
pubkey: associatedTokenAccount,
isSigner: false,
isWritable: true,
},
{
pubkey: ownerPublicKey,
isSigner: false,
isWritable: false,
},
{
pubkey: mintPublicKey,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.web3.SystemProgram.programId,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.utils.token.TOKEN_PROGRAM_ID,
isSigner: false,
isWritable: false,
},
{
pubkey: anchor_1.web3.SYSVAR_RENT_PUBKEY,
isSigner: false,
isWritable: false,
},
],
programId: anchor_1.utils.token.ASSOCIATED_PROGRAM_ID,
data: Buffer.from([]),
});
return new anchor_1.web3.Transaction().add(ix);
});
exports.initTxCreateTokenAccount = initTxCreateTokenAccount;
const initTxCreateMultiTokenAccount = (provider, { mints, owner = provider.wallet.publicKey }) => __awaiter(void 0, void 0, void 0, function* () {
const ownerPublicKey = (0, utils_1.toPublicKey)(owner);
const transactions = [];
const tokenAccounts = [];
for (const mint of mints) {
const mintPublicKey = (0, utils_1.toPublicKey)(mint);
const associatedTokenAccount = yield anchor_1.utils.token.associatedAddress({
mint: mintPublicKey,
owner: ownerPublicKey,
});
tokenAccounts.push(associatedTokenAccount);
}
const data = yield (0, rpc_1.getMultipleAccounts)(provider.connection, tokenAccounts);
data.forEach((value, index) => __awaiter(void 0, void 0, void 0, function* () {
if (value !== null)
return;
const tx = yield (0, exports.initTxCreateTokenAccount)(provider, {
mintAddress: mints[index],
owner,
});
transactions.push(tx);
}));
return transactions;
});
exports.initTxCreateMultiTokenAccount = initTxCreateMultiTokenAccount;