@pgchain/blockchain-libs
Version:
PGWallet Blockchain Libs
194 lines • 7.44 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Solana = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const json_rpc_1 = require("../../../basic/request/json-rpc");
const provider_1 = require("../../../types/provider");
const abc_1 = require("../../abc");
var RPC_METHODS;
(function (RPC_METHODS) {
RPC_METHODS["SEND_TRANSACTION"] = "sendTransaction";
RPC_METHODS["GET_EPOCH_INFO"] = "getEpochInfo";
RPC_METHODS["GET_HEALTH"] = "getHealth";
RPC_METHODS["GET_ACCOUNT_INFO"] = "getAccountInfo";
RPC_METHODS["GET_TOKEN_ACCOUNTS_BY_OWNER"] = "getTokenAccountsByOwner";
RPC_METHODS["GET_FEES"] = "getFees";
RPC_METHODS["GET_TRANSACTION"] = "getTransaction";
})(RPC_METHODS || (RPC_METHODS = {}));
var PARAMS_ENCODINGS;
(function (PARAMS_ENCODINGS) {
PARAMS_ENCODINGS["BASE64"] = "base64";
PARAMS_ENCODINGS["JSON_PARSED"] = "jsonParsed";
})(PARAMS_ENCODINGS || (PARAMS_ENCODINGS = {}));
class Solana extends abc_1.BaseClient {
constructor(url) {
super();
this.rpc = new json_rpc_1.JsonRPCRequest(url);
}
async broadcastTransaction(rawTx) {
return await this.rpc.call(RPC_METHODS.SEND_TRANSACTION, [
rawTx,
{ encoding: PARAMS_ENCODINGS.BASE64 },
]);
}
async getInfo() {
const [epochInfo, ok] = await this.rpc.batchCall([
[RPC_METHODS.GET_EPOCH_INFO, []],
[RPC_METHODS.GET_HEALTH, []],
]);
const slot = epochInfo.absoluteSlot;
const isReady = ok === 'ok';
return {
bestBlockNumber: slot,
isReady,
};
}
async getAddresses(addresses) {
var _a;
const calls = addresses.map((address) => [
RPC_METHODS.GET_ACCOUNT_INFO,
[address, { encoding: PARAMS_ENCODINGS.JSON_PARSED }],
]);
const resp = await this.rpc.batchCall(calls);
const result = [];
for (const accountInfo of resp) {
if (typeof accountInfo !== 'undefined') {
const balance = new bignumber_js_1.default((_a = accountInfo === null || accountInfo === void 0 ? void 0 : accountInfo.value) === null || _a === void 0 ? void 0 : _a.lamports);
const existing = balance.isFinite() && balance.gte(0);
result.push({
existing,
balance: existing ? balance : new bignumber_js_1.default(0),
});
}
else {
result.push(undefined);
}
}
return result;
}
async getBalances(requests) {
const calls = requests.map((request) => {
var _a;
return ((_a = request.coin) === null || _a === void 0 ? void 0 : _a.tokenAddress)
? [
RPC_METHODS.GET_TOKEN_ACCOUNTS_BY_OWNER,
[
request.address,
{ mint: request.coin.tokenAddress },
{ encoding: PARAMS_ENCODINGS.JSON_PARSED },
],
]
: [
RPC_METHODS.GET_ACCOUNT_INFO,
[request.address, { encoding: PARAMS_ENCODINGS.JSON_PARSED }],
];
});
const resps = await this.rpc.batchCall(calls);
const result = [];
resps.forEach((resp, i) => {
var _a;
if (typeof resp !== 'undefined') {
let balance = new bignumber_js_1.default(0);
if ((_a = requests[i].coin) === null || _a === void 0 ? void 0 : _a.tokenAddress) {
for (const tokenAccount of resp.value) {
const info = tokenAccount.account.data.parsed.info;
if (info.owner === requests[i].address) {
balance = bignumber_js_1.default.sum(balance, info.tokenAmount.amount);
}
else {
//TODO: send sentry warnings
}
}
}
else {
if (resp.value !== null) {
balance = new bignumber_js_1.default(resp.value.lamports);
}
}
result.push(balance);
}
else {
result.push(undefined);
}
});
return result;
}
async getAccountInfo(address) {
const response = await this.rpc.call(RPC_METHODS.GET_ACCOUNT_INFO, [address, { encoding: PARAMS_ENCODINGS.JSON_PARSED }]);
return response.value;
}
async getFeePricePerUnit() {
const [feePerSig] = await this.getFees();
return {
normal: {
price: new bignumber_js_1.default(feePerSig),
},
};
}
async getFees() {
const feeInfo = await this.rpc.call(RPC_METHODS.GET_FEES);
const feePerSig = feeInfo.value.feeCalculator.lamportsPerSignature;
const recentBlockhash = feeInfo.value.blockhash;
return [feePerSig, recentBlockhash];
}
async getTransactionStatuses(txids) {
const calls = txids.map((txid) => [
RPC_METHODS.GET_TRANSACTION,
[txid, PARAMS_ENCODINGS.JSON_PARSED],
]);
const result = [];
const resp = await this.rpc.batchCall(calls);
for (const tx of resp) {
if (typeof tx !== 'undefined') {
if (tx === null) {
result.push(provider_1.TransactionStatus.NOT_FOUND);
}
else {
result.push(tx.meta.err === null
? provider_1.TransactionStatus.CONFIRM_AND_SUCCESS
: provider_1.TransactionStatus.CONFIRM_BUT_FAILED);
}
}
else {
result.push(undefined);
}
}
return result;
}
async getTokenInfos(tokenAddresses) {
const calls = tokenAddresses.map((address) => [
RPC_METHODS.GET_ACCOUNT_INFO,
[address, { encoding: PARAMS_ENCODINGS.JSON_PARSED }],
]);
const resp = await this.rpc.batchCall(calls);
const tokenInfos = [];
resp.forEach((tokenInfo, i) => {
if (typeof tokenInfo !== 'undefined') {
if (tokenInfo.value !== null &&
tokenInfo.value.data.parsed.type === 'mint') {
const accountInfo = tokenInfo.value.data.parsed;
const decimals = accountInfo.info.decimals;
const name = tokenAddresses[i].slice(0, 4);
tokenInfos.push({
name,
symbol: name.toUpperCase(),
decimals,
});
}
else {
console.error('invalid token address');
tokenInfos.push(undefined);
}
}
else {
tokenInfos.push(undefined);
}
});
return tokenInfos;
}
}
exports.Solana = Solana;
//# sourceMappingURL=solana.js.map