hadeswap-sdk
Version:
HadeSwap SDK for interacting with protocol
81 lines (80 loc) • 3.61 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createAssociatedTokenAccountInstruction = exports.getTokenBalance = exports.findAssociatedTokenAddress = exports.createFakeWallet = void 0;
const anchor_1 = require("@project-serum/anchor");
const nodewallet_1 = require("./classes/nodewallet");
//when we only want to view vaults, no need to connect a real wallet.
const createFakeWallet = () => {
const leakedKp = anchor_1.web3.Keypair.fromSecretKey(Uint8Array.from([
208, 175, 150, 242, 88, 34, 108, 88, 177, 16, 168, 75, 115, 181, 199, 242, 120, 4, 78, 75, 19, 227, 13, 215, 184,
108, 226, 53, 111, 149, 179, 84, 137, 121, 79, 1, 160, 223, 124, 241, 202, 203, 220, 237, 50, 242, 57, 158, 226,
207, 203, 188, 43, 28, 70, 110, 214, 234, 251, 15, 249, 157, 62, 80,
]));
return new nodewallet_1.NodeWallet(leakedKp);
};
exports.createFakeWallet = createFakeWallet;
const findAssociatedTokenAddress = (walletAddress, tokenMintAddress) => __awaiter(void 0, void 0, void 0, function* () {
return (yield anchor_1.web3.PublicKey.findProgramAddress([walletAddress.toBuffer(), anchor_1.utils.token.TOKEN_PROGRAM_ID.toBuffer(), tokenMintAddress.toBuffer()], anchor_1.utils.token.ASSOCIATED_PROGRAM_ID))[0];
});
exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
const getTokenBalance = (pubkey, connection) => __awaiter(void 0, void 0, void 0, function* () {
const balance = yield connection.getTokenAccountBalance(pubkey);
return parseInt(balance.value.amount);
});
exports.getTokenBalance = getTokenBalance;
const createAssociatedTokenAccountInstruction = (associatedTokenAddress, payer, walletAddress, splTokenMintAddress) => {
const keys = [
{
pubkey: payer,
isSigner: true,
isWritable: true,
},
{
pubkey: associatedTokenAddress,
isSigner: false,
isWritable: true,
},
{
pubkey: walletAddress,
isSigner: false,
isWritable: false,
},
{
pubkey: splTokenMintAddress,
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,
},
];
return [
new anchor_1.web3.TransactionInstruction({
keys,
programId: anchor_1.utils.token.ASSOCIATED_PROGRAM_ID,
data: Buffer.from([]),
}),
];
};
exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction;