diff --git a/.eslintrc.json b/.eslintrc.json index a189c44..1d11159 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -41,6 +41,7 @@ "dist/", "node_modules/", "test*.ts", + "build-wp.ts", "packages/networks/boilerplate/" ] } diff --git a/.gitignore b/.gitignore index d074068..57b34ff 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,7 @@ index.html *.sln *.sw? -.npmrc \ No newline at end of file +.npmrc + +# Local WordPress deploy script (machine-specific paths) +build-wp.ts \ No newline at end of file diff --git a/packages/networks/bitcoin/package.json b/packages/networks/bitcoin/package.json index ea0f129..15139b9 100644 --- a/packages/networks/bitcoin/package.json +++ b/packages/networks/bitcoin/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/bitcoin", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/bitcoin/src/models/Transaction.ts b/packages/networks/bitcoin/src/models/Transaction.ts index f44a2d9..6c2159e 100644 --- a/packages/networks/bitcoin/src/models/Transaction.ts +++ b/packages/networks/bitcoin/src/models/Transaction.ts @@ -55,8 +55,6 @@ export interface TransactionData { } } -let counter = 0 - export class Transaction implements TransactionInterface { /** * Each transaction has its own unique ID defined by the user @@ -73,6 +71,8 @@ export class Transaction implements TransactionInterface { */ data: TransactionData | null = null + private notFoundRetries = 0 + /** * @param id Transaction id * @param provider Blockchain network provider @@ -82,30 +82,41 @@ export class Transaction implements TransactionInterface { this.provider = provider ?? Provider.instance } + private isFinalized(data: TransactionData | null): boolean { + if (data === null) { + return false + } + return data.status?.confirmed || data.status?.block_height !== undefined + } + /** * @returns Transaction data */ async getData(): Promise { - if (this.data !== null) { + if (this.data !== null && this.isFinalized(this.data)) { return this.data } try { const data = (await axios.get(this.provider.createEndpoint('tx/' + this.id))).data if (data?.txid !== this.id) { - return (this.data = null) + return null } - return (this.data = data as TransactionData) + const txData = data as TransactionData + if (this.isFinalized(txData)) { + this.data = txData + } + return txData } catch (error) { console.error('MC Bitcoin TX getData', error) const axiosError = error as AxiosError // Returns empty data when the transaction is first created. For this reason, it would be better to check it intermittently and give an error if it still does not exist. Average 10 seconds. if (String(axiosError?.response?.data).includes('Transaction not found')) { - if (counter > 5) { + if (this.notFoundRetries > 5) { throw new Error(ErrorTypeEnum.TRANSACTION_NOT_FOUND) } - counter++ + this.notFoundRetries++ await sleep(2000) return await this.getData() } diff --git a/packages/networks/boilerplate/package.json b/packages/networks/boilerplate/package.json index 3d4ebfa..94780e4 100644 --- a/packages/networks/boilerplate/package.json +++ b/packages/networks/boilerplate/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/boilerplate", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/evm-chains/package.json b/packages/networks/evm-chains/package.json index 2a52f4f..c00ccb6 100644 --- a/packages/networks/evm-chains/package.json +++ b/packages/networks/evm-chains/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/evm-chains", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/evm-chains/src/models/Transaction.ts b/packages/networks/evm-chains/src/models/Transaction.ts index cbde168..8f3a49f 100644 --- a/packages/networks/evm-chains/src/models/Transaction.ts +++ b/packages/networks/evm-chains/src/models/Transaction.ts @@ -72,11 +72,15 @@ export class Transaction implements TransactionInterface { this.ethers = this.provider.ethers } + private isFinalized(data: TransactionData | undefined): boolean { + return data?.receipt !== null && data?.receipt !== undefined + } + /** * @returns Transaction data */ async getData(): Promise { - if (this.data?.response !== undefined && this.data?.receipt !== null) { + if (this.data !== undefined && this.isFinalized(this.data)) { return this.data } try { @@ -85,7 +89,11 @@ export class Transaction implements TransactionInterface { return null } const receipt = await this.ethers.getTransactionReceipt(this.id) - return (this.data = { response, receipt }) + const txData = { response, receipt } + if (this.isFinalized(txData)) { + this.data = txData + } + return txData } catch (error) { console.error('MC EVM TX getData', error) if (error instanceof Error && String(error.message).includes('timeout')) { diff --git a/packages/networks/solana/package.json b/packages/networks/solana/package.json index 7830412..36baa3d 100644 --- a/packages/networks/solana/package.json +++ b/packages/networks/solana/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/solana", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/solana/src/models/Transaction.ts b/packages/networks/solana/src/models/Transaction.ts index a941a95..f505bab 100644 --- a/packages/networks/solana/src/models/Transaction.ts +++ b/packages/networks/solana/src/models/Transaction.ts @@ -43,11 +43,15 @@ export class Transaction implements TransactionInterface { - if (this.data !== null) { + if (this.data !== null && this.isFinalized(this.data)) { return this.data } try { @@ -59,7 +63,8 @@ export class Transaction implements TransactionInterface { this.provider = provider ?? Provider.instance } + private isFinalized(data: TxData | null): boolean { + const status = data?.effects?.status.status + return status === 'success' || status === 'failure' + } + /** * @returns Transaction data */ async getData(): Promise { - if (this.data) { + if (this.data !== null && this.isFinalized(this.data)) { return this.data } try { @@ -76,7 +81,10 @@ export class Transaction implements TransactionInterface { if (response.transaction === null) { return null } - return (this.data = response) + if (this.isFinalized(response)) { + this.data = response + } + return response } catch (error) { console.error('MC SUI TX getData', error) if (error instanceof Error && String(error.message).includes('timeout')) { diff --git a/packages/networks/ton/package.json b/packages/networks/ton/package.json index 3788667..076691a 100644 --- a/packages/networks/ton/package.json +++ b/packages/networks/ton/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/ton", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/ton/src/models/Transaction.ts b/packages/networks/ton/src/models/Transaction.ts index fbba893..5c0123f 100644 --- a/packages/networks/ton/src/models/Transaction.ts +++ b/packages/networks/ton/src/models/Transaction.ts @@ -45,12 +45,16 @@ export class Transaction implements TransactionInterface { this.provider = provider ?? Provider.instance } + private isFinalized(data: TransactionData | null): boolean { + return Boolean(data?.transaction.prev_trans_hash) + } + /** * @returns Transaction data */ async getData(): Promise { try { - if (this.data !== null) { + if (this.data !== null && this.isFinalized(this.data)) { return this.data } @@ -77,7 +81,11 @@ export class Transaction implements TransactionInterface { return null } - return (this.data = { transaction, action }) + const txData = { transaction, action } + if (this.isFinalized(txData)) { + this.data = txData + } + return txData } catch (error) { console.error('MC TON TX getData', error) throw new Error(ErrorTypeEnum.RPC_REQUEST_ERROR) diff --git a/packages/networks/tron/package.json b/packages/networks/tron/package.json index f643970..3a0831f 100644 --- a/packages/networks/tron/package.json +++ b/packages/networks/tron/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/tron", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/tron/src/models/Transaction.ts b/packages/networks/tron/src/models/Transaction.ts index bf79624..eecd4f4 100644 --- a/packages/networks/tron/src/models/Transaction.ts +++ b/packages/networks/tron/src/models/Transaction.ts @@ -118,12 +118,16 @@ export class Transaction implements TransactionInterface { this.provider = provider ?? Provider.instance } + private isFinalized(data: TransactionData | undefined): boolean { + return data?.info?.blockNumber !== undefined + } + /** * @returns Transaction data */ async getData(): Promise { try { - if (this.data?.info !== undefined) { + if (this.data !== undefined && this.isFinalized(this.data)) { return this.data } this.data = (await this.provider.tronWeb.trx.getTransaction(this.id)) ?? undefined diff --git a/packages/networks/xrpl/package.json b/packages/networks/xrpl/package.json index 5b57db1..56d0651 100644 --- a/packages/networks/xrpl/package.json +++ b/packages/networks/xrpl/package.json @@ -1,6 +1,6 @@ { "name": "@multiplechain/xrpl", - "version": "0.4.27", + "version": "0.4.28", "type": "module", "main": "dist/index.cjs", "module": "dist/index.es.js", diff --git a/packages/networks/xrpl/src/models/Transaction.ts b/packages/networks/xrpl/src/models/Transaction.ts index ec31f98..bbb8d3b 100644 --- a/packages/networks/xrpl/src/models/Transaction.ts +++ b/packages/networks/xrpl/src/models/Transaction.ts @@ -21,8 +21,6 @@ export type TransactionData = BaseTransactionData & { date?: number } -let counter = 0 - export class Transaction implements TransactionInterface { /** * Each transaction has its own unique ID defined by the user @@ -39,6 +37,8 @@ export class Transaction implements TransactionInterface { */ data: TransactionData | null = null + private notFoundRetries = 0 + /** * @param id Transaction id * @param provider Blockchain network provider @@ -48,23 +48,31 @@ export class Transaction implements TransactionInterface { this.provider = provider ?? Provider.instance } + private isFinalized(data: TransactionData | null): boolean { + return data?.meta !== undefined + } + /** * @returns Transaction data */ async getData(): Promise { - if (this.data?.meta) { + if (this.data !== null && this.isFinalized(this.data)) { return this.data } try { - return (this.data = await this.provider.rpc.getTransaction(this.id)) + const data = await this.provider.rpc.getTransaction(this.id) + if (this.isFinalized(data)) { + this.data = data + } + return data } catch (error) { console.error('MC XRPl TX getData', error) // Returns empty data when the transaction is first created. For this reason, it would be better to check it intermittently and give an error if it still does not exist. Average 10 seconds. if (String((error as any).message).includes('Transaction not found')) { - if (counter > 5) { + if (this.notFoundRetries > 5) { throw new Error(ErrorTypeEnum.TRANSACTION_NOT_FOUND) } - counter++ + this.notFoundRetries++ await sleep(2000) return await this.getData() } @@ -83,6 +91,7 @@ export class Transaction implements TransactionInterface { const status = await this.getStatus() if (status !== TransactionStatusEnum.PENDING) { resolve(status) + return } setTimeout(check, ms) } catch (error) {