This commit is contained in:
2025-01-13 01:02:32 +08:00
parent e749aa387a
commit b4bb91e4fd
16 changed files with 2324 additions and 52 deletions

104
src/sol/reader.js Normal file
View File

@@ -0,0 +1,104 @@
"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.GetSolBalance = GetSolBalance;
exports.GetTokenAccount = GetTokenAccount;
exports.GetTokenBalanceByOwner = GetTokenBalanceByOwner;
exports.GetTokenBalanceByTA = GetTokenBalanceByTA;
exports.GetTicketsAccount = GetTicketsAccount;
exports.GetTicketsBalanceByOwner = GetTicketsBalanceByOwner;
exports.GetTicketsBalanceByTA = GetTicketsBalanceByTA;
const web3_js_1 = require("@solana/web3.js");
const shared_1 = require("./shared");
function GetSolBalance(wallet) {
return __awaiter(this, void 0, void 0, function* () {
const balance = yield shared_1.connection.getBalance(new web3_js_1.PublicKey(wallet));
return balance;
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function GetTokenAccount(ownerAddress) {
return __awaiter(this, void 0, void 0, function* () {
const accountPublicKey = new web3_js_1.PublicKey(ownerAddress);
const account = yield shared_1.connection.getTokenAccountsByOwner(accountPublicKey, { mint: shared_1.TOKENS_MINT });
try {
const tokenAddress = account.value[0].pubkey.toString();
return tokenAddress;
}
catch (_a) {
return null;
}
});
}
function GetTokenBalanceByOwner(owner) {
return __awaiter(this, void 0, void 0, function* () {
const tokenAccount = yield GetTokenAccount(owner);
if (tokenAccount == null) {
return 0;
}
console.log(tokenAccount);
return yield GetTokenBalanceByTA(tokenAccount);
});
}
function GetTokenBalanceByTA(tokenAccountAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (tokenAccountAddress == null) {
return 0;
}
const tokenAccount = new web3_js_1.PublicKey(tokenAccountAddress);
const info = yield shared_1.connection.getTokenAccountBalance(tokenAccount);
if (info.value.uiAmount == null) {
return -1;
}
console.log('Token Balance (using connection-Web3.js): ', info.value.uiAmount);
return info.value.uiAmount;
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function GetTicketsAccount(ownerAddress) {
return __awaiter(this, void 0, void 0, function* () {
const accountPublicKey = new web3_js_1.PublicKey(ownerAddress);
const account = yield shared_1.connection.getTokenAccountsByOwner(accountPublicKey, { mint: shared_1.TICKETS_MINT });
console.log(JSON.stringify(account));
try {
const tokenAddress = account.value[0].pubkey.toString();
return tokenAddress;
}
catch (_a) {
return null;
}
});
}
function GetTicketsBalanceByOwner(owner) {
return __awaiter(this, void 0, void 0, function* () {
const tokenAccount = yield GetTicketsAccount(owner);
if (tokenAccount == null) {
return 0;
}
console.log(tokenAccount);
return yield GetTicketsBalanceByTA(tokenAccount);
});
}
function GetTicketsBalanceByTA(tokenAccountAddress) {
return __awaiter(this, void 0, void 0, function* () {
if (tokenAccountAddress == null) {
return 0;
}
const tokenAccount = new web3_js_1.PublicKey(tokenAccountAddress);
const info = yield shared_1.connection.getTokenAccountBalance(tokenAccount);
if (info.value.uiAmount == null) {
return -1;
}
console.log('Ticket Balance (using Solana-Web3.js): ', info.value.uiAmount);
return info.value.uiAmount;
});
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------