@ledgerhq/coin-ton
Version:
115 lines • 4.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fetchAdjacentTransactions = exports.broadcastTx = exports.estimateFee = exports.fetchJettonWallets = exports.fetchJettonTransactions = exports.fetchAccountInfo = exports.fetchTransactions = exports.fetchLastBlockNumber = void 0;
const live_network_1 = __importDefault(require("@ledgerhq/live-network"));
const ton_1 = require("@ton/ton");
const config_1 = require("../../config");
const getTonUrl = (path) => {
const currencyConfig = (0, config_1.getCoinConfig)();
return `${currencyConfig.infra.API_TON_ENDPOINT}${path ?? ""}`;
};
const fetch = async (path) => {
const url = getTonUrl(path);
const { data } = await (0, live_network_1.default)({
method: "GET",
url,
});
return data;
};
const send = async (path, data) => {
const url = getTonUrl(path);
const { data: dataResponse } = await (0, live_network_1.default)({
method: "POST",
url,
data: JSON.stringify(data),
headers: { "Content-Type": "application/json" },
});
return dataResponse;
};
async function fetchLastBlockNumber() {
const data = await fetch("/masterchainInfo");
return data.last.seqno;
}
exports.fetchLastBlockNumber = fetchLastBlockNumber;
async function fetchTransactions(addr, opts) {
const address = ton_1.Address.parse(addr);
const urlAddr = address.toString({ bounceable: false, urlSafe: true });
let url = `/transactions?account=${urlAddr}&limit=256`;
if (opts?.startLt)
url += `&start_lt=${opts.startLt}`;
if (opts?.endLt)
url += `&end_lt=${opts.endLt}`;
return await fetch(url);
}
exports.fetchTransactions = fetchTransactions;
async function fetchAccountInfo(addr) {
const address = ton_1.Address.parse(addr);
const urlAddr = address.toString({ bounceable: false, urlSafe: true });
const data = await fetch(`/account?address=${urlAddr}`);
if (data.status === "uninit" || data.status === "nonexist") {
return {
balance: data.balance,
last_transaction_lt: data.last_transaction_lt,
last_transaction_hash: data.last_transaction_hash,
status: data.status,
seqno: 0,
};
}
const { seqno } = await fetch(`/wallet?address=${urlAddr}`);
return {
balance: data.balance,
last_transaction_lt: data.last_transaction_lt,
last_transaction_hash: data.last_transaction_hash,
status: data.status,
seqno: seqno || 0,
};
}
exports.fetchAccountInfo = fetchAccountInfo;
async function fetchJettonTransactions(addr, opts) {
const address = ton_1.Address.parse(addr);
const urlAddr = address.toString({ bounceable: false, urlSafe: true });
let url = `/jetton/transfers?address=${urlAddr}&limit=256`;
if (opts?.jettonMaster)
url += `&jetton_master=${opts.jettonMaster}`;
if (opts?.startLt)
url += `&start_lt=${opts.startLt}`;
if (opts?.endLt)
url += `&end_lt=${opts.endLt}`;
return (await fetch(url)).jetton_transfers;
}
exports.fetchJettonTransactions = fetchJettonTransactions;
async function fetchJettonWallets(opts) {
let url = `/jetton/wallets?limit=256`;
if (opts?.jettonMaster)
url += `&jetton_address=${opts.jettonMaster}`;
if (opts?.address) {
const address = ton_1.Address.parse(opts.address);
const urlAddr = address.toString({ bounceable: false, urlSafe: true });
url += `&owner_address=${urlAddr}`;
}
return (await fetch(url)).jetton_wallets;
}
exports.fetchJettonWallets = fetchJettonWallets;
async function estimateFee(address, body, initCode, initData) {
return (await send("/estimateFee", {
address,
body,
init_code: initCode,
init_data: initData,
ignore_chksig: true,
})).source_fees;
}
exports.estimateFee = estimateFee;
async function broadcastTx(bocBase64) {
return (await send("/message", { boc: bocBase64 })).message_hash;
}
exports.broadcastTx = broadcastTx;
async function fetchAdjacentTransactions(txHash, dir = "in") {
const url = `/adjacentTransactions?hash=${encodeURIComponent(txHash)}&direction=${dir}`;
return await fetch(url);
}
exports.fetchAdjacentTransactions = fetchAdjacentTransactions;
//# sourceMappingURL=api.js.map