Skip to content

Commit

Permalink
version 0.9.6 (#349)
Browse files Browse the repository at this point in the history
* Make promievent errors and events match web3 js

* Add transaction receipt to transaction obj in queue

* update to version 0.9.6
  • Loading branch information
cmeisl authored Jul 24, 2019
1 parent 0c68b80 commit 8e0b89a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 43 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ yarn add bnc-assist
#### Script Tag

The library uses [semantic versioning](https://semver.org/spec/v2.0.0.html).
The current version is 0.9.5.
The current version is 0.9.6.
There are minified and non-minified versions.
Put this script at the top of your `<head>`

```html
<script src="https://assist.blocknative.com/0-9-5/assist.js"></script>
<script src="https://assist.blocknative.com/0-9-6/assist.js"></script>

<!-- OR... -->

<script src="https://assist.blocknative.com/0-9-5/assist.min.js"></script>
<script src="https://assist.blocknative.com/0-9-6/assist.min.js"></script>
```

### Initialize the Library
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bnc-assist",
"version": "0.9.5",
"version": "0.9.6",
"description": "Blocknative Assist js library for Dapp developers",
"main": "lib/assist.min.js",
"scripts": {
Expand Down
11 changes: 7 additions & 4 deletions src/js/helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ export function separateArgs(allArgs, argsLength) {

const notificationOptions =
typeof last(allArgsCopy) === 'object' &&
(last(allArgsCopy).messages || last(allArgsCopy).clickHandlers) &&
takeLast(allArgsCopy)
(last(allArgsCopy).messages || last(allArgsCopy).clickHandlers)
? takeLast(allArgsCopy)
: {}

const callback =
typeof last(allArgsCopy) === 'function' && takeLast(allArgsCopy)
Expand Down Expand Up @@ -243,12 +244,14 @@ export function stepToImageKey(step) {
}

export function handleError(handlers = {}) {
return errorObj => {
return (errorObj, receipt) => {
const { callback, reject, resolve, promiEvent } = handlers

if (promiEvent) {
promiEvent.emit('error', errorObj)
promiEvent.emit('error', errorObj, receipt)
promiEvent.reject(errorObj)
resolve()

return
}

Expand Down
64 changes: 29 additions & 35 deletions src/js/logic/send-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ export function sendTransaction({
resolve(hash)
callback && callback(null, hash)

return waitForTransactionReceipt(hash).then(() => {
onTxReceipt(transactionId, categoryCode)
return waitForTransactionReceipt(hash).then(receipt => {
onTxReceipt(transactionId, categoryCode, receipt)
})
})
.catch(async errorObj => {
Expand All @@ -235,42 +235,36 @@ export function sendTransaction({
resolve(tx)
callback && callback(null, tx)

await tx.wait()
onTxReceipt(transactionId, categoryCode)
const receipt = await tx.wait()
onTxReceipt(transactionId, categoryCode, receipt)
})
.catch(errorObj => {
onTxError(transactionId, errorObj, categoryCode)
handleError({ resolve, reject, callback })(errorObj)
})
} else {
new Promise(confirmed => {
/* In web3 v1 instead of resolving the promise returned by sendTransaction
* we need to setup the promiEvent argument to mirror the behavior of the
* promiEvent returned by web3 when we call .send on the contract method.
*/

txPromise
.on('transactionHash', hash => {
promiEvent.emit('transactionHash', hash)
onTxHash(transactionId, hash, categoryCode)
callback && callback(null, hash)
})
.on('receipt', receipt => {
promiEvent.emit('receipt', receipt)
confirmed(receipt)
})
.once('confirmation', confirmed)
.on('confirmation', (confirmation, receipt) => {
promiEvent.emit('confirmation', confirmation, receipt)
})
.on('error', errorObj => {
promiEvent.emit('error', errorObj)
onTxError(transactionId, errorObj, categoryCode)
handleError({ resolve, reject, callback })(errorObj)
})
.then(promiEvent.resolve)
.catch(promiEvent.reject)
}).then(() => onTxReceipt(transactionId, categoryCode))
txPromise
.on('transactionHash', hash => {
promiEvent.emit('transactionHash', hash)
onTxHash(transactionId, hash, categoryCode)
callback && callback(null, hash)
})
.on('receipt', receipt => {
promiEvent.emit('receipt', receipt)
promiEvent.resolve(receipt)
resolve()
onTxReceipt(transactionId, categoryCode, receipt)
})
.on('confirmation', (confirmation, receipt) => {
promiEvent.emit('confirmation', confirmation, receipt)
})
.on('error', (errorObj, receipt) => {
onTxError(transactionId, errorObj, categoryCode)
handleError({ resolve, reject, callback, promiEvent })(
errorObj,
receipt
)
})
}
})
}
Expand Down Expand Up @@ -333,13 +327,13 @@ export function onTxHash(id, hash, categoryCode) {
}, customStallTimeout || timeouts.txStall)
}

async function onTxReceipt(id, categoryCode) {
async function onTxReceipt(id, categoryCode, receipt) {
let txObj = getTxObjFromQueue(id)

if (txObj.transaction.status === 'confirmed') {
txObj = updateTransactionInQueue(id, { status: 'completed' })
txObj = updateTransactionInQueue(id, { status: 'completed', receipt })
} else {
txObj = updateTransactionInQueue(id, { status: 'confirmed' })
txObj = updateTransactionInQueue(id, { status: 'confirmed', receipt })
}

handleEvent({
Expand Down

0 comments on commit 8e0b89a

Please sign in to comment.