Skip to content

Commit 0dd0991

Browse files
authored
version 0.9.3 (#328)
* Feature/transaction hash (#321) * Change exports, make contract object undefined if not a contract call * change import * Add ability to add txhash to transaction watching * Update docs * Update docs and return boolean from Transaction if passed a hash * Add Goerli as a valid network (#322) * update to version 0.9.3 * Update documentation for handleNotificationEvent (#327)
1 parent 8ec3c30 commit 0dd0991

File tree

6 files changed

+51
-18
lines changed

6 files changed

+51
-18
lines changed

README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ yarn add bnc-assist
4343
#### Script Tag
4444

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

5050
```html
51-
<script src="https://assist.blocknative.com/0-9-2/assist.js"></script>
51+
<script src="https://assist.blocknative.com/0-9-3/assist.js"></script>
5252

5353
<!-- OR... -->
5454

55-
<script src="https://assist.blocknative.com/0-9-2/assist.min.js"></script>
55+
<script src="https://assist.blocknative.com/0-9-3/assist.min.js"></script>
5656
```
5757

5858
### Initialize the Library
@@ -281,6 +281,8 @@ The function that is defined on the `handleNotificationEvent` property of the co
281281
}
282282
```
283283

284+
You can then decide whether you would like a notification to be shown for this event or not. Return `true` to show the notification or `false` to not show the notification.
285+
284286
#### `eventCode`
285287

286288
The list of event codes that are included in the object that `handleNotificationEvent` is called with are the same as the list included in the `messages` object that is passed to the config with one addition:
@@ -388,6 +390,7 @@ The available ids for the `networkId` property of the config object:
388390
- `1`: mainnet
389391
- `3`: ropsten testnet
390392
- `4`: rinkeby testnet
393+
- `5`: goerli testnet
391394

392395
*The kovan testnet is not supported.*
393396

@@ -534,19 +537,19 @@ var myContract = assistInstance.Contract(address, abi)
534537
myContract.myMethod().call()
535538
```
536539

537-
### `Transaction(txObject [, callback] [, inlineCustomMsgs])`
540+
### `Transaction(txObjectOrHash [, callback] [, inlineCustomMsgs])`
538541

539542
#### Parameters
540543

541-
`txObject` - `Object`: Transaction object (**Required**)
544+
`txObjectOrHash` - `Object` || `String`: Transaction object or transaction hash (**Required**)
542545

543546
`callback` - `Function`: Optional error first style callback if you don't want to use promises
544547

545548
`inlineCustomMsgs` - `Object`: Optional notification message overrides
546549

547550
#### Returns
548551

549-
`Promise` or `PromiEvent` (`web3.js 1.0`)
552+
`Promise` or `PromiEvent` (`web3.js 1.0`) if passed a transaction object or `true` if passed a valid transaction hash or `false` if hash is invalid
550553

551554
- resolves with transaction hash
552555
- rejects with an error message
@@ -561,6 +564,9 @@ assistInstance.Transaction(txObject)
561564
.catch(error => {
562565
console.log(error.message) // => 'User has insufficient funds'
563566
})
567+
568+
// you can alternatively pass in a transaction hash to get Assist's notifications for a transaction that has already been sent to the network
569+
assistInstance.Transaction(hash)
564570
```
565571

566572
### `getState()`

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bnc-assist",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"description": "Blocknative Assist js library for Dapp developers",
55
"main": "lib/assist.min.js",
66
"scripts": {

src/js/helpers/utilities.js

+2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ export function networkName(id) {
203203
return 'ropsten'
204204
case 4:
205205
return 'rinkeby'
206+
case 5:
207+
return 'goerli'
206208
case 42:
207209
return 'kovan'
208210
case 'localhost':

src/js/index.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ import {
1717
modernCall,
1818
truffleSend
1919
} from './logic/contract-methods'
20+
import { addTransactionToQueue } from './helpers/transaction-queue'
2021
import { openWebsocketConnection } from './helpers/websockets'
2122
import { getUserAgent } from './helpers/browser'
2223
import { getUncheckedSigner } from './helpers/ethers-provider'
2324
import { checkUserEnvironment, prepareForTransaction } from './logic/user'
24-
import sendTransaction from './logic/send-transaction'
25+
import { sendTransaction, onTxHash } from './logic/send-transaction'
2526
import { configureWeb3 } from './helpers/web3'
2627
import {
2728
getOverloadedMethodKeys,
28-
truffleContractUsesWeb3v1
29+
truffleContractUsesWeb3v1,
30+
createTransactionId
2931
} from './helpers/utilities'
3032
import { createIframe, updateStyle } from './helpers/iframe'
3133
import { validateConfig } from './helpers/validation'
@@ -510,7 +512,7 @@ function init(config) {
510512

511513
// TRANSACTION FUNCTION //
512514

513-
function Transaction(txOptions, callback, inlineCustomMsgs = {}) {
515+
function Transaction(txOptionsOrHash, callback, inlineCustomMsgs = {}) {
514516
const {
515517
validApiKey,
516518
supportedNetwork,
@@ -533,6 +535,31 @@ function init(config) {
533535
throw errorObj
534536
}
535537

538+
// if we are passed a transaction hash instead of an options object
539+
// then we just track the already sent transaction
540+
if (typeof txOptionsOrHash === 'string') {
541+
if (!/^0x([A-Fa-f0-9]{64})$/.test(txOptionsOrHash)) {
542+
return false
543+
}
544+
545+
// create id for transaction
546+
const id = createTransactionId()
547+
548+
// add transaction to queue
549+
addTransactionToQueue({
550+
transaction: {
551+
id,
552+
status: 'signedTransaction'
553+
},
554+
inlineCustomMsgs
555+
})
556+
557+
// handle txhash
558+
onTxHash(id, txOptionsOrHash, 'activeTransaction')
559+
560+
return true
561+
}
562+
536563
// Check if we have an instance of web3
537564
if (!web3Instance && !ethers) {
538565
configureWeb3()
@@ -548,7 +575,7 @@ function init(config) {
548575
const promiEvent = new PromiEventLib.PromiEvent()
549576
sendTransaction({
550577
categoryCode: 'activeTransaction',
551-
txOptions,
578+
txOptionsOrHash,
552579
sendMethod,
553580
callback,
554581
inlineCustomMsgs: inlineCustomMsgs.messages,
@@ -560,7 +587,7 @@ function init(config) {
560587

561588
return sendTransaction({
562589
categoryCode: 'activeTransaction',
563-
txOptions,
590+
txOptionsOrHash,
564591
sendMethod,
565592
callback,
566593
inlineCustomMsgs: inlineCustomMsgs.messages

src/js/logic/contract-methods.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { separateArgs, handleError } from '~/js/helpers/utilities'
66
import { getContractMethod } from '~/js/helpers/web3'
77
import { checkNetwork, getCorrectNetwork } from '~/js/logic/user'
88

9-
import sendTransaction from './send-transaction'
9+
import { sendTransaction } from './send-transaction'
1010

1111
export function modernCall({ contractObj, methodName, args, truffleContract }) {
1212
const originalReturnObject = getContractMethod({

src/js/logic/send-transaction.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323

2424
import { prepareForTransaction } from './user'
2525

26-
function sendTransaction({
26+
export function sendTransaction({
2727
categoryCode,
2828
txOptions = {},
2929
sendMethod,
@@ -55,7 +55,7 @@ function sendTransaction({
5555
truffleContract
5656
})
5757

58-
const contractEventObj = {
58+
const contractEventObj = methodName && {
5959
methodName,
6060
parameters: methodArgs
6161
}
@@ -265,7 +265,7 @@ function sendTransaction({
265265
})
266266
}
267267

268-
function onTxHash(id, hash, categoryCode) {
268+
export function onTxHash(id, hash, categoryCode) {
269269
const txObj = updateTransactionInQueue(id, {
270270
status: 'approved',
271271
hash,
@@ -378,5 +378,3 @@ function onTxError(id, error, categoryCode) {
378378

379379
removeTransactionFromQueue(id)
380380
}
381-
382-
export default sendTransaction

0 commit comments

Comments
 (0)