Skip to content

Commit

Permalink
version 0.9.3 (#328)
Browse files Browse the repository at this point in the history
* 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)
  • Loading branch information
cmeisl authored Jul 16, 2019
1 parent 8ec3c30 commit 0dd0991
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
18 changes: 12 additions & 6 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.2.
The current version is 0.9.3.
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-2/assist.js"></script>
<script src="https://assist.blocknative.com/0-9-3/assist.js"></script>

<!-- OR... -->

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

### Initialize the Library
Expand Down Expand Up @@ -281,6 +281,8 @@ The function that is defined on the `handleNotificationEvent` property of the co
}
```

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.

#### `eventCode`

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:
Expand Down Expand Up @@ -388,6 +390,7 @@ The available ids for the `networkId` property of the config object:
- `1`: mainnet
- `3`: ropsten testnet
- `4`: rinkeby testnet
- `5`: goerli testnet

*The kovan testnet is not supported.*

Expand Down Expand Up @@ -534,19 +537,19 @@ var myContract = assistInstance.Contract(address, abi)
myContract.myMethod().call()
```

### `Transaction(txObject [, callback] [, inlineCustomMsgs])`
### `Transaction(txObjectOrHash [, callback] [, inlineCustomMsgs])`

#### Parameters

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

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

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

#### Returns

`Promise` or `PromiEvent` (`web3.js 1.0`)
`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

- resolves with transaction hash
- rejects with an error message
Expand All @@ -561,6 +564,9 @@ assistInstance.Transaction(txObject)
.catch(error => {
console.log(error.message) // => 'User has insufficient funds'
})

// you can alternatively pass in a transaction hash to get Assist's notifications for a transaction that has already been sent to the network
assistInstance.Transaction(hash)
```

### `getState()`
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.2",
"version": "0.9.3",
"description": "Blocknative Assist js library for Dapp developers",
"main": "lib/assist.min.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/js/helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ export function networkName(id) {
return 'ropsten'
case 4:
return 'rinkeby'
case 5:
return 'goerli'
case 42:
return 'kovan'
case 'localhost':
Expand Down
37 changes: 32 additions & 5 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import {
modernCall,
truffleSend
} from './logic/contract-methods'
import { addTransactionToQueue } from './helpers/transaction-queue'
import { openWebsocketConnection } from './helpers/websockets'
import { getUserAgent } from './helpers/browser'
import { getUncheckedSigner } from './helpers/ethers-provider'
import { checkUserEnvironment, prepareForTransaction } from './logic/user'
import sendTransaction from './logic/send-transaction'
import { sendTransaction, onTxHash } from './logic/send-transaction'
import { configureWeb3 } from './helpers/web3'
import {
getOverloadedMethodKeys,
truffleContractUsesWeb3v1
truffleContractUsesWeb3v1,
createTransactionId
} from './helpers/utilities'
import { createIframe, updateStyle } from './helpers/iframe'
import { validateConfig } from './helpers/validation'
Expand Down Expand Up @@ -510,7 +512,7 @@ function init(config) {

// TRANSACTION FUNCTION //

function Transaction(txOptions, callback, inlineCustomMsgs = {}) {
function Transaction(txOptionsOrHash, callback, inlineCustomMsgs = {}) {
const {
validApiKey,
supportedNetwork,
Expand All @@ -533,6 +535,31 @@ function init(config) {
throw errorObj
}

// if we are passed a transaction hash instead of an options object
// then we just track the already sent transaction
if (typeof txOptionsOrHash === 'string') {
if (!/^0x([A-Fa-f0-9]{64})$/.test(txOptionsOrHash)) {
return false
}

// create id for transaction
const id = createTransactionId()

// add transaction to queue
addTransactionToQueue({
transaction: {
id,
status: 'signedTransaction'
},
inlineCustomMsgs
})

// handle txhash
onTxHash(id, txOptionsOrHash, 'activeTransaction')

return true
}

// Check if we have an instance of web3
if (!web3Instance && !ethers) {
configureWeb3()
Expand All @@ -548,7 +575,7 @@ function init(config) {
const promiEvent = new PromiEventLib.PromiEvent()
sendTransaction({
categoryCode: 'activeTransaction',
txOptions,
txOptionsOrHash,
sendMethod,
callback,
inlineCustomMsgs: inlineCustomMsgs.messages,
Expand All @@ -560,7 +587,7 @@ function init(config) {

return sendTransaction({
categoryCode: 'activeTransaction',
txOptions,
txOptionsOrHash,
sendMethod,
callback,
inlineCustomMsgs: inlineCustomMsgs.messages
Expand Down
2 changes: 1 addition & 1 deletion src/js/logic/contract-methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { separateArgs, handleError } from '~/js/helpers/utilities'
import { getContractMethod } from '~/js/helpers/web3'
import { checkNetwork, getCorrectNetwork } from '~/js/logic/user'

import sendTransaction from './send-transaction'
import { sendTransaction } from './send-transaction'

export function modernCall({ contractObj, methodName, args, truffleContract }) {
const originalReturnObject = getContractMethod({
Expand Down
8 changes: 3 additions & 5 deletions src/js/logic/send-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {

import { prepareForTransaction } from './user'

function sendTransaction({
export function sendTransaction({
categoryCode,
txOptions = {},
sendMethod,
Expand Down Expand Up @@ -55,7 +55,7 @@ function sendTransaction({
truffleContract
})

const contractEventObj = {
const contractEventObj = methodName && {
methodName,
parameters: methodArgs
}
Expand Down Expand Up @@ -265,7 +265,7 @@ function sendTransaction({
})
}

function onTxHash(id, hash, categoryCode) {
export function onTxHash(id, hash, categoryCode) {
const txObj = updateTransactionInQueue(id, {
status: 'approved',
hash,
Expand Down Expand Up @@ -378,5 +378,3 @@ function onTxError(id, error, categoryCode) {

removeTransactionFromQueue(id)
}

export default sendTransaction

0 comments on commit 0dd0991

Please sign in to comment.