ton-watcher
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -14,6 +14,6 @@ export * from './modules/txs/tonTxWatcher'; | ||
declare const _default: { | ||
parse_tx: (tx: import("@ton/ton").Transaction) => any; | ||
parse_tx: (tx: import("@ton/ton").Transaction, is_testnet?: boolean) => any; | ||
TonTxWatcher: typeof TonTxWatcher; | ||
getJettonInfo: typeof getJettonInfo; | ||
getJettonsInfo: (client: TonClient, deposit_address: string, jettons_config: Record<string, import("./modules/Jetons/JetonInfo").JetonSettings>) => Promise<Record<string, JettonsDepo>>; | ||
getJettonsInfo: (client: TonClient, deposit_address: string, jettons_config: Record<string, import("./modules/Jetons/JetonInfo").JetonSettings>, is_testnet?: boolean) => Promise<Record<string, JettonsDepo>>; | ||
PriorityQueue: typeof PriorityQueue; | ||
@@ -20,0 +20,0 @@ memorySore: { |
@@ -45,3 +45,3 @@ import { Address, JettonMaster, JettonWallet, OpenedContract, TonClient } from "@ton/ton"; | ||
}; | ||
export declare const getJettonsInfo: (client: TonClient, deposit_address: string, jettons_config: Record<string, JetonSettings>) => Promise<Record<string, JettonsDepo>>; | ||
export declare function getJettonInfo(jetton_address: string, client: TonClient): Promise<JettonInfoMain>; | ||
export declare const getJettonsInfo: (client: TonClient, deposit_address: string, jettons_config: Record<string, JetonSettings>, is_testnet?: boolean) => Promise<Record<string, JettonsDepo>>; | ||
export declare function getJettonInfo(jetton_address: string, client: TonClient, is_testnet?: boolean): Promise<JettonInfoMain>; |
@@ -13,5 +13,5 @@ "use strict"; | ||
const jettons_cache = {}; | ||
const getJettonsInfo = async (client, deposit_address, jettons_config) => { | ||
const getJettonsInfo = async (client, deposit_address, jettons_config, is_testnet) => { | ||
const jettons = Object.create(null); | ||
const known_jettons = { ...jettons_config, ...JETTONS_INFO }; | ||
const known_jettons = { ...JETTONS_INFO, ...jettons_config }; | ||
for (const name in known_jettons) { | ||
@@ -31,4 +31,4 @@ const info = known_jettons[name]; | ||
}; | ||
jettons[jettonMasterAddress.toString()] = jettons[name]; | ||
jettons[jettonUserWalletAddress.toString()] = jettons[name]; | ||
jettons[jettonMasterAddress.toString({ testOnly: is_testnet })] = jettons[name]; | ||
jettons[jettonUserWalletAddress.toString({ testOnly: is_testnet })] = jettons[name]; | ||
} | ||
@@ -38,6 +38,6 @@ return jettons; | ||
exports.getJettonsInfo = getJettonsInfo; | ||
async function getJettonInfo(jetton_address, client) { | ||
async function getJettonInfo(jetton_address, client, is_testnet) { | ||
const master_jeton_address = ton_1.Address.parse(jetton_address); | ||
if (jettons_cache[master_jeton_address.toString()]) { | ||
return jettons_cache[master_jeton_address.toString()]; | ||
if (jettons_cache[master_jeton_address.toString({ testOnly: is_testnet })]) { | ||
return jettons_cache[master_jeton_address.toString({ testOnly: is_testnet })]; | ||
} | ||
@@ -64,5 +64,5 @@ const result = Object.create(null); | ||
result.getDepositData = getDepositData; | ||
jettons_cache[master_jeton_address.toString()] = result; | ||
jettons_cache[master_jeton_address.toString({ testOnly: is_testnet })] = result; | ||
return result; | ||
} | ||
//# sourceMappingURL=JetonInfo.js.map |
@@ -20,2 +20,3 @@ "use strict"; | ||
} | ||
const isTestnet = init.network !== 'mainnet'; | ||
const key_pair = (0, crypto_1.keyPairFromSecretKey)(Buffer.from(init.deposit_secret, 'hex')); | ||
@@ -25,17 +26,21 @@ const wallet = ton_1.WalletContractV5R1.create({ publicKey: key_pair.publicKey }); | ||
try { | ||
axios_request_throttle_1.default.use(axios_1.default, { requestsPerSecond: 5 }); | ||
const endpoint = await (0, ton_access_1.getHttpEndpoint)({ network: init.network }); | ||
axios_request_throttle_1.default.use(axios_1.default, { requestsPerSecond: isTestnet ? 0.8 : 5 }); | ||
const endpoint = isTestnet ? 'https://testnet.toncenter.com/api/v2/jsonRPC' : await (0, ton_access_1.getHttpEndpoint)({ network: init.network }); | ||
const endpoint2 = isTestnet ? 'https://testnet.toncenter.com/api/v2/jsonRPC' : await (0, ton_access_1.getHttpEndpoint)({ network: init.network }); | ||
const core = await Core_1.TonInteraction.fabric({ | ||
endpoint, | ||
key_pair, | ||
jetton_settings: init.jettons_config, | ||
isTestnet, | ||
}); | ||
const client = new ton_1.TonClient({ | ||
endpoint, | ||
endpoint: endpoint2, | ||
}); | ||
const txSender = new sendTx_1.SendTx(core); | ||
const txWatcher = new tonTxWatcher_1.TonTxWatcher(client, address.toString(), init.last_lt, init.last_hash, undefined, { | ||
const txWatcher = new tonTxWatcher_1.TonTxWatcher(client, address.toString({ testOnly: isTestnet }), init.last_lt, init.last_hash, undefined, { | ||
tonBalance: (data) => { process.send && process.send((0, safeJson_1.safeJSONStringify)({ data, event: 'tonBalance' })); }, | ||
jettonBalance: (data) => { process.send && process.send((0, safeJson_1.safeJSONStringify)({ data, event: 'jettonBalance' })); }, | ||
jettonDeposit: (data) => { process.send && process.send((0, safeJson_1.safeJSONStringify)({ data, event: 'jettonDeposit' })); }, | ||
}, init.jettons_config); | ||
logger: (level, data) => { process.send && process.send((0, safeJson_1.safeJSONStringify)({ data: { level, data: data }, event: 'logger' })); }, | ||
}, init.jettons_config, isTestnet); | ||
txWatcher.start(); | ||
@@ -42,0 +47,0 @@ process.send && process.send(`{ "event": "address", "data": { "address" : "${address.toString()}" }}`); |
@@ -36,2 +36,3 @@ import { TonClient, Transaction } from "@ton/ton"; | ||
transferTx?: (data?: any) => void | Promise<void>; | ||
logger?: (level: 'error' | 'info' | 'warning' | 'debug', data: unknown) => void | Promise<void>; | ||
onTx?: (tx: Transaction) => void | Promise<void>; | ||
@@ -48,2 +49,3 @@ }; | ||
private is_started; | ||
private is_testnet; | ||
private callbacks; | ||
@@ -53,5 +55,6 @@ private lastTonbalance; | ||
private jettonsConfig; | ||
constructor(client: TonClient, address: string, default_lt?: string, default_hash?: string, store?: PersistentStory, callbacks?: EventMap, jetonsConfig?: any, timeDelayMs?: number); | ||
constructor(client: TonClient, address: string, default_lt?: string, default_hash?: string, store?: PersistentStory, callbacks?: EventMap, jetonsConfig?: any, is_testnet?: boolean, timeDelayMs?: number); | ||
start(): void; | ||
private scan; | ||
private log; | ||
private fetchTxs; | ||
@@ -61,3 +64,3 @@ private parseTx; | ||
private getJettonWalletBalances; | ||
private getJetonConfig; | ||
private getJettonConfig; | ||
private checkJetonTransferTx; | ||
@@ -64,0 +67,0 @@ private processing_jetton; |
@@ -18,2 +18,3 @@ "use strict"; | ||
is_started = false; | ||
is_testnet = false; | ||
callbacks; | ||
@@ -23,4 +24,5 @@ lastTonbalance = -1n; | ||
jettonsConfig = Object.create(null); | ||
constructor(client, address, default_lt, default_hash, store, callbacks = Object.create(null), jetonsConfig = Object.create(null), timeDelayMs = 20000) { | ||
this.jetons_config = jetonsConfig; | ||
constructor(client, address, default_lt, default_hash, store, callbacks = Object.create(null), jetonsConfig = Object.create(null), is_testnet = false, timeDelayMs = 20000) { | ||
this.is_testnet = is_testnet; | ||
this.jettonsConfig = jetonsConfig; | ||
this.store = store ? store : persistant_1.memorySore; | ||
@@ -42,10 +44,14 @@ this.address = ton_1.Address.parse(address); | ||
async scan() { | ||
await this.getJetonConfig(); | ||
await this.getlastData(); | ||
await (0, common_helpers_1.retry)(async () => { | ||
await this.getJettonConfig(); | ||
}, { delay: 2000, retries: 5 }); | ||
await (0, common_helpers_1.retry)(async () => { | ||
await this.getlastData(); | ||
}, { delay: 2000, retries: 5 }); | ||
while (true) { | ||
try { | ||
const { balance, lastTransaction } = await this.client.getContractState(this.address); | ||
const { balance, lastTransaction } = await (0, common_helpers_1.retry)(() => this.client.getContractState(this.address), { delay: 1500, retries: 3 }); | ||
this.updateTonBalance(balance); | ||
if (lastTransaction && (lastTransaction.hash !== this.last_hash || (this.last_lt && (BigInt(this.last_lt) !== BigInt(lastTransaction.lt))))) { | ||
const jeton_balances = await this.getJettonWalletBalances(); | ||
const jeton_balances = await (0, common_helpers_1.retry)(() => this.getJettonWalletBalances(), { delay: 1500, retries: 3 }); | ||
this.updateJettonBalances(jeton_balances); | ||
@@ -56,3 +62,3 @@ await this.fetchTxs(lastTransaction); | ||
catch (e) { | ||
console.error(e); | ||
this.log('warning', `Can't fetch txs for ${this.address.toString({ testOnly: this.is_started })}`); | ||
} | ||
@@ -62,2 +68,5 @@ await new Promise(res => setTimeout(res, this.time_delay_ms)); | ||
} | ||
log(level, data) { | ||
this.callbacks.logger && this.callbacks.logger(level, data); | ||
} | ||
async fetchTxs(lastTransaction) { | ||
@@ -70,5 +79,7 @@ const params = Object.create(null); | ||
} | ||
else { | ||
return; | ||
} | ||
let all_txs = []; | ||
while (true) { | ||
console.log(params); | ||
const txs = await (0, common_helpers_1.retry)(() => this.client.getTransactions(this.address, params), { retries: 5, delay: 5000 }); | ||
@@ -102,6 +113,6 @@ for (let i = 0; i < txs.length; i++) { | ||
parseTx(tx) { | ||
const parsed_tx = (0, Transaction_1.parse_tx)(tx); | ||
const parsed_tx = (0, Transaction_1.parse_tx)(tx, this.is_testnet); | ||
if (parsed_tx.inMessage.jetton_notify) { | ||
const source = parsed_tx.inMessage.sender; | ||
const jeton_data = this.checkJetonTransferTx(source); | ||
const jeton_data = this.checkJetonTransferTx(ton_1.Address.parse(source)); | ||
const { jettonAmount, jettonSender, message } = parsed_tx.inMessage.jetton_notify; | ||
@@ -111,4 +122,4 @@ if (jeton_data) { | ||
hash: parsed_tx.hash, | ||
from: jettonSender.toString(), | ||
to: this.address.toString(), | ||
from: jettonSender.toString({ testOnly: this.is_testnet }), | ||
to: this.address.toString({ testOnly: this.is_testnet }), | ||
fee: parsed_tx?.totalFees?.toString(), | ||
@@ -129,7 +140,6 @@ time: tx.now * 1000, | ||
} | ||
else if ((parsed_tx.inMessage.type === 'external-in' && parsed_tx.inMessage.recipient === this.address.toString()) || parsed_tx.inMessage.recipient === this.address.toString()) { | ||
else if (parsed_tx.inMessage.type === 'external-in' || parsed_tx.inMessage.recipient === this.address.toString()) { | ||
this.processing_out_tx(parsed_tx); | ||
} | ||
else { | ||
debugger; | ||
} | ||
@@ -139,7 +149,7 @@ } | ||
const last_lt = await this.store.get('last_time'); | ||
if (last_lt) { | ||
if (!this.last_lt && last_lt) { | ||
this.last_lt = last_lt; | ||
} | ||
const last_hash = await this.store.get('last_hash'); | ||
if (last_hash) { | ||
if (!this.last_hash && last_hash) { | ||
this.last_hash = last_hash; | ||
@@ -170,7 +180,7 @@ } | ||
} | ||
async getJetonConfig() { | ||
this.jetons_config = await (0, JetonInfo_1.getJettonsInfo)(this.client, this.address.toString(), this.jettonsConfig); | ||
async getJettonConfig() { | ||
this.jetons_config = await (0, JetonInfo_1.getJettonsInfo)(this.client, this.address.toString({ testOnly: this.is_testnet }), this.jettonsConfig, this.is_testnet); | ||
} | ||
checkJetonTransferTx(sender_address) { | ||
const jettonConfig = this.jetons_config?.[sender_address.toString()]; | ||
const jettonConfig = this.jetons_config?.[sender_address.toString({ testOnly: this.is_testnet })]; | ||
if (!jettonConfig) { | ||
@@ -177,0 +187,0 @@ return null; |
import { Transaction } from "@ton/ton"; | ||
export declare const parse_tx: (tx: Transaction) => any; | ||
export declare const parse_tx: (tx: Transaction, is_testnet?: boolean) => any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.parse_tx = void 0; | ||
const parse_tx = (tx) => { | ||
const parse_tx = (tx, is_testnet = false) => { | ||
const result = Object.create(null); | ||
result.originalTx = tx; | ||
Object.assign(result, parseMainData(tx)); | ||
Object.assign(result, parseInMessage(tx.inMessage)); | ||
Object.assign(result, parseOutMessage(tx.outMessages)); | ||
Object.assign(result, parseMainData(tx, is_testnet)); | ||
Object.assign(result, parseInMessage(tx.inMessage, is_testnet)); | ||
Object.assign(result, parseOutMessage(tx.outMessages, is_testnet)); | ||
if (tx.description.type === 'generic') { | ||
@@ -19,3 +19,3 @@ Object.assign(result, parseGenericDescription(tx)); | ||
exports.parse_tx = parse_tx; | ||
function parseMainData(tx) { | ||
function parseMainData(tx, is_testnet = false) { | ||
return { | ||
@@ -31,6 +31,7 @@ accStatus: tx.endStatus, | ||
address: tx.address, | ||
...parseTxRaw(tx), | ||
is_testnet: is_testnet, | ||
...parseTxRaw(tx, is_testnet), | ||
}; | ||
} | ||
function parseTxRaw(tx) { | ||
function parseTxRaw(tx, is_testnet = false) { | ||
const slice = tx.raw.beginParse().clone(); | ||
@@ -67,6 +68,6 @@ const wallet_id = slice.loadUint(32); | ||
} | ||
function parseOutMessage(out_message) { | ||
function parseOutMessage(out_message, is_testnet = false) { | ||
const messages = out_message.values(); | ||
return { outMessages: messages.map(item => { | ||
const message = parseInMessage(item); | ||
const message = parseInMessage(item, is_testnet); | ||
return { | ||
@@ -77,3 +78,3 @@ ...message.inMessage, | ||
} | ||
function parseInMessage(in_message) { | ||
function parseInMessage(in_message, is_testnet = false) { | ||
const result = Object.create(null); | ||
@@ -96,4 +97,4 @@ if (!in_message) { | ||
result.msgType = in_message.info.type; | ||
result.sender = in_message.info.src ? in_message.info.src.toString() : null; | ||
result.recipient = in_message.info.dest ? in_message.info.dest.toString() : null; | ||
result.sender = in_message.info.src ? in_message.info.src.toString({ testOnly: is_testnet }) : null; | ||
result.recipient = in_message.info.dest ? in_message.info.dest.toString({ testOnly: is_testnet }) : null; | ||
result.hash = in_message.body.hash().toString('hex'); | ||
@@ -100,0 +101,0 @@ return { inMessage: result }; |
@@ -11,2 +11,3 @@ import { TonClient, Address, Cell } from "@ton/ton"; | ||
private jetton_settings; | ||
private is_testnet; | ||
private constructor(); | ||
@@ -61,2 +62,3 @@ getSeqNo(): Promise<number>; | ||
jetton_settings?: Record<string, JetonSettings>; | ||
isTestnet?: boolean; | ||
}; |
@@ -14,3 +14,4 @@ "use strict"; | ||
jetton_settings; | ||
constructor(client, key_pair, contract, jetton_settings) { | ||
is_testnet = false; | ||
constructor(client, key_pair, contract, jetton_settings, is_testnet) { | ||
if (!client) { | ||
@@ -24,2 +25,3 @@ throw new Error('Invalid ton client'); | ||
this.address = contract?.address.toString(); | ||
this.is_testnet = !!is_testnet; | ||
} | ||
@@ -87,3 +89,3 @@ getSeqNo() { | ||
const message = (0, ton_1.internal)({ | ||
to: parsed_address.toString(), | ||
to: parsed_address.toString({ testOnly: this.is_testnet }), | ||
value: amount, | ||
@@ -114,3 +116,3 @@ body: msg, | ||
} | ||
const jetton_info = await this.getJettonInfo(parsed_jetton_master_address.toString()); | ||
const jetton_info = await this.getJettonInfo(parsed_jetton_master_address.toString({ testOnly: this.is_testnet })); | ||
if (!jetton_info) { | ||
@@ -195,3 +197,3 @@ throw new Error(`invalid jetton master address ${jetton_master_address}`); | ||
static async fabric(params) { | ||
const endpoint = await (0, ton_access_1.getHttpEndpoint)({ network: 'mainnet' }); | ||
const endpoint = params.endpoint ? params.endpoint : await (0, ton_access_1.getHttpEndpoint)({ network: params.isTestnet ? 'testnet' : 'mainnet' }); | ||
const client = new ton_1.TonClient({ | ||
@@ -209,3 +211,3 @@ apiKey: params.apiKey, | ||
const connected_wallet = client.open(wallet); | ||
return new TonInteraction(client, params.key_pair, connected_wallet); | ||
return new TonInteraction(client, params.key_pair, connected_wallet, params.jetton_settings, params.isTestnet); | ||
} | ||
@@ -239,3 +241,3 @@ async fetchJettonWalletBalance(jettonMasterAddress, userWalletAddress) { | ||
const jettonWalletAddress = await jettonMasterContract.getWalletAddress(userAddress); | ||
return jettonWalletAddress.toString(); | ||
return jettonWalletAddress.toString({ testOnly: this.is_testnet }); | ||
} | ||
@@ -254,3 +256,3 @@ async sendJetton(ton_address, jetton_master_address, amount, msg) { | ||
} | ||
const jetton_info = await this.getJettonInfo(parsed_jetton_master_address.toString()); | ||
const jetton_info = await this.getJettonInfo(parsed_jetton_master_address.toString({ testOnly: this.is_testnet })); | ||
if (!jetton_info) { | ||
@@ -325,4 +327,4 @@ throw new Error(`invalid jetton master address ${jetton_master_address}`); | ||
} | ||
const { getDepositData } = await (0, JetonInfo_1.getJettonInfo)(jetton_master_address, this.ton_client); | ||
this.jettons_info[jetton_master_address] = await getDepositData(this.contract?.address.toString()); | ||
const { getDepositData } = await (0, JetonInfo_1.getJettonInfo)(jetton_master_address, this.ton_client, this.is_testnet); | ||
this.jettons_info[jetton_master_address] = await getDepositData(this.contract?.address.toString({ testOnly: this.is_testnet })); | ||
} | ||
@@ -329,0 +331,0 @@ return this.jettons_info[jetton_master_address]; |
@@ -13,6 +13,6 @@ import { TonTxWatcher } from './modules/txs/tonTxWatcher'; | ||
declare const _default: { | ||
parse_tx: (tx: import("@ton/core").Transaction) => any; | ||
parse_tx: (tx: import("@ton/core").Transaction, is_testnet?: boolean) => any; | ||
TonTxWatcher: typeof TonTxWatcher; | ||
getJettonInfo: typeof getJettonInfo; | ||
getJettonsInfo: (client: import("@ton/ton").TonClient, deposit_address: string, jettons_config: Record<string, import("./modules/Jetons/JetonInfo").JetonSettings>) => Promise<Record<string, JettonsDepo>>; | ||
getJettonsInfo: (client: import("@ton/ton").TonClient, deposit_address: string, jettons_config: Record<string, import("./modules/Jetons/JetonInfo").JetonSettings>, is_testnet?: boolean) => Promise<Record<string, JettonsDepo>>; | ||
PriorityQueue: typeof PriorityQueue; | ||
@@ -19,0 +19,0 @@ memorySore: { |
{ | ||
"name": "ton-watcher", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
216441
3618