Skip to content

Commit

Permalink
v9.0.0 (#4)
Browse files Browse the repository at this point in the history
* v9.0.0: support replaced transaction confirmed and failed (for web3 wallets only for now)

* fix tests

* fix tests: ensurance method is gone
  • Loading branch information
spape authored Jan 28, 2022
1 parent d92e47e commit 115346b
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 205 deletions.
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ let sentTransaction = await wallet.sendTransaction({
value: "0",
sent: function(transaction){},
confirmed: function(transaction){},
ensured: function(transaction){},
failed: function(transaction){}
})

Expand All @@ -232,7 +231,6 @@ let sentTransaction = await wallet.sendTransaction({
value: "1000000000000000",
sent: function(transaction){},
confirmed: function(transaction){},
ensured: function(transaction){},
failed: function(transaction){}
})

Expand All @@ -255,8 +253,6 @@ Arguments for `sendTransaction`:

`confirmed: Function`: Callback to be executed if transaction has been confirmed once by the network.

`ensured: Function`: Callback to be executed if transaction has been reached safe amount of confirmations (successful transaction confirmation can be ensured).

`failed: Function`: Callback to be executed if transaction failed to confirm on the network (aka reverted).

#### value
Expand Down Expand Up @@ -295,7 +291,7 @@ in case wallet is connected to the wrong network and network cant be switched au

#### Transaction

Returned instances of `Transaction` (e.g. via `sendTransaction`, or `sent`, `confirmed`, `ensured` or `failed` callback) have the following format:
Returned instances of `Transaction` (e.g. via `sendTransaction`, or `sent`, `confirmed` or `failed` callback) have the following format:

`blockchain: string`: Blockchain the transaction belongs to.

Expand All @@ -319,8 +315,6 @@ Returned instances of `Transaction` (e.g. via `sendTransaction`, or `sent`, `con

`confirmation: Promise`: Returns a promise that resolves once the transaction confirms.

`ensurance: Promise`: Returns a promise that resolves once the transaction confirms enough times to consider it ensured.

`failure: Promise`: Returns a promise that resolves once the transaction fails.

### Estimations
Expand Down
65 changes: 31 additions & 34 deletions dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17760,7 +17760,7 @@ function parseUnits(value, unitName) {
function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
class Transaction {

constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, ensured, failed }) {
constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, failed }) {

this.blockchain = blockchain;
this.from = from;
Expand All @@ -17772,10 +17772,8 @@ class Transaction {
this.value = _optionalChain$5([Transaction, 'access', _ => _.bigNumberify, 'call', _2 => _2(value, blockchain), 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]);
this.sent = sent;
this.confirmed = confirmed;
this.ensured = ensured;
this.failed = failed;
this._confirmed = false;
this._ensured = false;
this._failed = false;
}

Expand Down Expand Up @@ -17822,19 +17820,6 @@ class Transaction {
})
}

ensurance() {
if (this._ensured) {
return Promise.resolve(this)
}
return new Promise((resolve, reject) => {
let originalEnsured = this.ensured;
this.ensured = () => {
if (originalEnsured) originalEnsured(this);
resolve(this);
};
})
}

failure() {
if (this._failed) {
return Promise.resolve(this)
Expand Down Expand Up @@ -17879,15 +17864,22 @@ const sendTransaction$1 = async ({ transaction, wallet })=> {
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
});
sentTransaction.wait(12).then(() => {
transaction._ensured = true;
if (transaction.ensured) transaction.ensured(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash) {
transaction.id = error.replacement.hash;
transaction.url = Blockchain.findByName(transaction.blockchain).explorerUrlFor({ transaction });
}
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
} else {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
});
} else {
throw('Submitting transaction failed!')
Expand Down Expand Up @@ -18128,15 +18120,20 @@ const sendTransaction = async ({ transaction, wallet })=> {
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
});
sentTransaction.wait(12).then(() => {
transaction._ensured = true;
if (transaction.ensured) transaction.ensured(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction.id = error.replacement.hash;
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction.id = error.replacement.hash;
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
} else {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
});
}
} else {
Expand Down
65 changes: 31 additions & 34 deletions dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17764,7 +17764,7 @@
function _optionalChain$5(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
class Transaction {

constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, ensured, failed }) {
constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, failed }) {

this.blockchain = blockchain;
this.from = from;
Expand All @@ -17776,10 +17776,8 @@
this.value = _optionalChain$5([Transaction, 'access', _ => _.bigNumberify, 'call', _2 => _2(value, blockchain), 'optionalAccess', _3 => _3.toString, 'call', _4 => _4()]);
this.sent = sent;
this.confirmed = confirmed;
this.ensured = ensured;
this.failed = failed;
this._confirmed = false;
this._ensured = false;
this._failed = false;
}

Expand Down Expand Up @@ -17826,19 +17824,6 @@
})
}

ensurance() {
if (this._ensured) {
return Promise.resolve(this)
}
return new Promise((resolve, reject) => {
let originalEnsured = this.ensured;
this.ensured = () => {
if (originalEnsured) originalEnsured(this);
resolve(this);
};
})
}

failure() {
if (this._failed) {
return Promise.resolve(this)
Expand Down Expand Up @@ -17883,15 +17868,22 @@
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
});
sentTransaction.wait(12).then(() => {
transaction._ensured = true;
if (transaction.ensured) transaction.ensured(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash) {
transaction.id = error.replacement.hash;
transaction.url = web3Blockchains.Blockchain.findByName(transaction.blockchain).explorerUrlFor({ transaction });
}
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
} else {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
});
} else {
throw('Submitting transaction failed!')
Expand Down Expand Up @@ -18132,15 +18124,20 @@
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
});
sentTransaction.wait(12).then(() => {
transaction._ensured = true;
if (transaction.ensured) transaction.ensured(transaction);
}).catch((error)=>{
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction.id = error.replacement.hash;
transaction._confirmed = true;
if (transaction.confirmed) transaction.confirmed(transaction);
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction.id = error.replacement.hash;
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
} else {
transaction._failed = true;
if(transaction.failed) transaction.failed(transaction, error);
}
});
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@depay/web3-wallets",
"moduleName": "Web3Wallets",
"version": "8.1.0",
"version": "9.0.0",
"description": "One-Stop-Shop JavaScript library to integrate various web3 crypto wallets and multiple blockchains at once with a single interface.",
"main": "dist/umd/index.js",
"module": "dist/esm/index.js",
Expand Down Expand Up @@ -50,7 +50,7 @@
"@depay/web3-blockchains": "^4.2.0",
"@depay/web3-client": "^8.0.0",
"@depay/web3-constants": "^5.0.0",
"@depay/web3-mock": "^11.2.1",
"@depay/web3-mock": "^11.6.0",
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-replace": "^2.4.2",
Expand Down
17 changes: 1 addition & 16 deletions src/Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CONSTANTS } from '@depay/web3-constants'

class Transaction {

constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, ensured, failed }) {
constructor({ blockchain, from, nonce, to, api, method, params, value, sent, confirmed, failed }) {

this.blockchain = blockchain
this.from = from
Expand All @@ -15,10 +15,8 @@ class Transaction {
this.value = Transaction.bigNumberify(value, blockchain)?.toString()
this.sent = sent
this.confirmed = confirmed
this.ensured = ensured
this.failed = failed
this._confirmed = false
this._ensured = false
this._failed = false
}

Expand Down Expand Up @@ -65,19 +63,6 @@ class Transaction {
})
}

ensurance() {
if (this._ensured) {
return Promise.resolve(this)
}
return new Promise((resolve, reject) => {
let originalEnsured = this.ensured
this.ensured = () => {
if (originalEnsured) originalEnsured(this)
resolve(this)
}
})
}

failure() {
if (this._failed) {
return Promise.resolve(this)
Expand Down
23 changes: 14 additions & 9 deletions src/wallets/WalletConnect/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ const sendTransaction = async ({ transaction, wallet })=> {
transaction._confirmed = true
if (transaction.confirmed) transaction.confirmed(transaction)
}).catch((error)=>{
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
})
sentTransaction.wait(12).then(() => {
transaction._ensured = true
if (transaction.ensured) transaction.ensured(transaction)
}).catch((error)=>{
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction.id = error.replacement.hash
transaction._confirmed = true
if (transaction.confirmed) transaction.confirmed(transaction)
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction.id = error.replacement.hash
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
}
} else {
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
}
})
}
} else {
Expand Down
25 changes: 16 additions & 9 deletions src/wallets/Web3Wallet/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ const sendTransaction = async ({ transaction, wallet })=> {
transaction._confirmed = true
if (transaction.confirmed) transaction.confirmed(transaction)
}).catch((error)=>{
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
})
sentTransaction.wait(12).then(() => {
transaction._ensured = true
if (transaction.ensured) transaction.ensured(transaction)
}).catch((error)=>{
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
if(error && error.code && error.code == 'TRANSACTION_REPLACED') {
if(error.replacement && error.replacement.hash) {
transaction.id = error.replacement.hash
transaction.url = Blockchain.findByName(transaction.blockchain).explorerUrlFor({ transaction })
}
if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 1) {
transaction._confirmed = true
if (transaction.confirmed) transaction.confirmed(transaction)
} else if(error.replacement && error.replacement.hash && error.receipt && error.receipt.status == 0) {
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
}
} else {
transaction._failed = true
if(transaction.failed) transaction.failed(transaction, error)
}
})
} else {
throw('Submitting transaction failed!')
Expand Down
Loading

0 comments on commit 115346b

Please sign in to comment.