Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
added try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
0xBeycan committed Jun 23, 2023
1 parent d2e35f4 commit 40b97b2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"namespace": "multiplechain",
"name": "@multiplechain/solana",
"version": "0.1.7",
"version": "0.1.8",
"description": "Solana has ready-made methods to connect to wallets and perform many transactions.",
"scripts": {
"serve": "parcel test.html --no-cache --dist-dir test",
Expand Down
80 changes: 44 additions & 36 deletions src/entity/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,20 @@ class Transaction {
* @returns {Boolean}
*/
async validate() {
let data = await this.getData();
if (data === null) {
await this.provider.confirmTransaction(this.hash);
data = await this.getData();
}
try {
let data = await this.getData();
if (data === null) {
await this.provider.confirmTransaction(this.hash);
data = await this.getData();
}

if (data.meta && data.meta.err === null) {
return true;
} else {
return false;
if (data.meta && data.meta.err === null) {
return true;
} else {
return false;
}
} catch (error) {
return this.validate();
}
}

Expand All @@ -79,40 +83,44 @@ class Transaction {
* @returns {Number}
*/
async getTransferAmount(options) {
await this.getData();
let beforeBalance, afterBalance, diff, decimals;
if (options && options.tokenAddress) {
decimals = this.data.meta.preTokenBalances[0].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[0].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[0].uiTokenAmount.uiAmount;
diff = (beforeBalance - afterBalance);

if (typeof this.data.meta.preTokenBalances[1] === 'undefined') {
try {
await this.getData();
let beforeBalance, afterBalance, diff, decimals;
if (options && options.tokenAddress) {
decimals = this.data.meta.preTokenBalances[0].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[0].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[1].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[0].uiTokenAmount.uiAmount;
diff = (beforeBalance - afterBalance);
} else if (diff < 0) {
decimals = this.data.meta.preTokenBalances[1].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[1].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[1].uiTokenAmount.uiAmount;
diff = (beforeBalance - afterBalance);
}

diff = Math.abs(diff).toFixed(decimals);
} else {
beforeBalance = this.data.meta.preBalances[0];
afterBalance = this.data.meta.postBalances[0];
diff = utils.toDec((afterBalance - beforeBalance), 9);

if (diff < 0) {
beforeBalance = this.data.meta.preBalances[1];
afterBalance = this.data.meta.postBalances[1];
if (typeof this.data.meta.preTokenBalances[1] === 'undefined') {
decimals = this.data.meta.preTokenBalances[0].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[0].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[1].uiTokenAmount.uiAmount;
diff = (beforeBalance - afterBalance);
} else if (diff < 0) {
decimals = this.data.meta.preTokenBalances[1].uiTokenAmount.decimals;
beforeBalance = this.data.meta.preTokenBalances[1].uiTokenAmount.uiAmount;
afterBalance = this.data.meta.postTokenBalances[1].uiTokenAmount.uiAmount;
diff = (beforeBalance - afterBalance);
}

diff = Math.abs(diff).toFixed(decimals);
} else {
beforeBalance = this.data.meta.preBalances[0];
afterBalance = this.data.meta.postBalances[0];
diff = utils.toDec((afterBalance - beforeBalance), 9);

if (diff < 0) {
beforeBalance = this.data.meta.preBalances[1];
afterBalance = this.data.meta.postBalances[1];
diff = utils.toDec((afterBalance - beforeBalance), 9);
}
}
}

return parseFloat(diff);
return parseFloat(diff);
} catch (error) {
return this.getTransferAmount(options);
}
}

/**
Expand Down

0 comments on commit 40b97b2

Please sign in to comment.