Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"dist/",
"node_modules/",
"test*.ts",
"build-wp.ts",
"packages/networks/boilerplate/"
]
}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ index.html
*.sln
*.sw?

.npmrc
.npmrc

# Local WordPress deploy script (machine-specific paths)
build-wp.ts
2 changes: 1 addition & 1 deletion packages/networks/bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 18 additions & 7 deletions packages/networks/bitcoin/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ export interface TransactionData {
}
}

let counter = 0

export class Transaction implements TransactionInterface<TransactionData> {
/**
* Each transaction has its own unique ID defined by the user
Expand All @@ -73,6 +71,8 @@ export class Transaction implements TransactionInterface<TransactionData> {
*/
data: TransactionData | null = null

private notFoundRetries = 0

/**
* @param id Transaction id
* @param provider Blockchain network provider
Expand All @@ -82,30 +82,41 @@ export class Transaction implements TransactionInterface<TransactionData> {
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<TransactionData | null> {
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()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/boilerplate/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/evm-chains/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/networks/evm-chains/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ export class Transaction implements TransactionInterface<TransactionData> {
this.ethers = this.provider.ethers
}

private isFinalized(data: TransactionData | undefined): boolean {
return data?.receipt !== null && data?.receipt !== undefined
}

/**
* @returns Transaction data
*/
async getData(): Promise<TransactionData | null> {
if (this.data?.response !== undefined && this.data?.receipt !== null) {
if (this.data !== undefined && this.isFinalized(this.data)) {
return this.data
}
try {
Expand All @@ -85,7 +89,11 @@ export class Transaction implements TransactionInterface<TransactionData> {
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')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/solana/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
9 changes: 7 additions & 2 deletions packages/networks/solana/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ export class Transaction implements TransactionInterface<ParsedTransactionWithMe
this.provider = provider ?? Provider.instance
}

private isFinalized(data: ParsedTransactionWithMeta | null): boolean {
return data !== null
}

/**
* @returns Transaction data
*/
async getData(): Promise<ParsedTransactionWithMeta | null> {
if (this.data !== null) {
if (this.data !== null && this.isFinalized(this.data)) {
return this.data
}
try {
Expand All @@ -59,7 +63,8 @@ export class Transaction implements TransactionInterface<ParsedTransactionWithMe
return null
}

return (this.data = data)
this.data = data
return data
} catch (error) {
console.error('MC Solana TX getData', error)
throw new Error(ErrorTypeEnum.RPC_REQUEST_ERROR)
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/sui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@multiplechain/sui",
"version": "0.4.27",
"version": "0.4.28",
"type": "module",
"main": "dist/index.cjs",
"module": "dist/index.es.js",
Expand Down
12 changes: 10 additions & 2 deletions packages/networks/sui/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,16 @@ export class Transaction implements TransactionInterface<TxData> {
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<TxData | null> {
if (this.data) {
if (this.data !== null && this.isFinalized(this.data)) {
return this.data
}
try {
Expand All @@ -76,7 +81,10 @@ export class Transaction implements TransactionInterface<TxData> {
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')) {
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/ton/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
12 changes: 10 additions & 2 deletions packages/networks/ton/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ export class Transaction implements TransactionInterface<TransactionData> {
this.provider = provider ?? Provider.instance
}

private isFinalized(data: TransactionData | null): boolean {
return Boolean(data?.transaction.prev_trans_hash)
}

/**
* @returns Transaction data
*/
async getData(): Promise<TransactionData | null> {
try {
if (this.data !== null) {
if (this.data !== null && this.isFinalized(this.data)) {
return this.data
}

Expand All @@ -77,7 +81,11 @@ export class Transaction implements TransactionInterface<TransactionData> {
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)
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/tron/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
6 changes: 5 additions & 1 deletion packages/networks/tron/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ export class Transaction implements TransactionInterface<TransactionData> {
this.provider = provider ?? Provider.instance
}

private isFinalized(data: TransactionData | undefined): boolean {
return data?.info?.blockNumber !== undefined
}

/**
* @returns Transaction data
*/
async getData(): Promise<TransactionData | null> {
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
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/xrpl/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
21 changes: 15 additions & 6 deletions packages/networks/xrpl/src/models/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export type TransactionData = BaseTransactionData & {
date?: number
}

let counter = 0

export class Transaction implements TransactionInterface<TransactionData> {
/**
* Each transaction has its own unique ID defined by the user
Expand All @@ -39,6 +37,8 @@ export class Transaction implements TransactionInterface<TransactionData> {
*/
data: TransactionData | null = null

private notFoundRetries = 0

/**
* @param id Transaction id
* @param provider Blockchain network provider
Expand All @@ -48,23 +48,31 @@ export class Transaction implements TransactionInterface<TransactionData> {
this.provider = provider ?? Provider.instance
}

private isFinalized(data: TransactionData | null): boolean {
return data?.meta !== undefined
}

/**
* @returns Transaction data
*/
async getData(): Promise<TransactionData | null> {
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()
}
Expand All @@ -83,6 +91,7 @@ export class Transaction implements TransactionInterface<TransactionData> {
const status = await this.getStatus()
if (status !== TransactionStatusEnum.PENDING) {
resolve(status)
return
}
setTimeout(check, ms)
} catch (error) {
Expand Down
Loading