Skip to content

Commit

Permalink
Merge pull request #9 from blocknative/develop
Browse files Browse the repository at this point in the history
Release 0.1.1

- Documentation updates
- Watch account feature
- Add typescript definitions
  • Loading branch information
lnbc1QWFyb24 authored Oct 3, 2019
2 parents 902a21d + ebc5269 commit 8644845
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 59 deletions.
44 changes: 27 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ const options = {
// initialize notify
const notify = Notify(options)

// send a transaction and get the hash
const transactionHash = await web3.eth.sendTransaction({
to: "0xa1C5E103Dfd56CBC3f6B6a526d2044598fD1cf1F",
value: 100000000000000,
from: "0x54c43790da9F8bd5d9bef06f56f798Eb16c53A91"
})
// get users' account address
const accounts = await window.ethereum.enable()

// pass the hash in to notify to receive "post-flight" notifications
const { emitter } = notify.hash(transactionHash)
// pass the account address in to notify to receive "post-flight" notifications for every incoming and outgoing transaction that happens on the users' account
const { emitter } = notify.account(accounts[0])

// listen to transaction events
emitter.on("txSent", console.log)
Expand All @@ -42,9 +38,7 @@ emitter.on("txFailed", console.log)
emitter.on("all", console.log)
```

### API

### Initialization
## Initialization

```javascript
import Notify from "bn-notify"
Expand All @@ -59,7 +53,7 @@ const options = {
const notify = Notify(options)
```

#### Options
### Options

```javascript
const options = {
Expand All @@ -69,11 +63,11 @@ const options = {
}
```

##### `dappId` - [REQUIRED]
#### `dappId` - [REQUIRED]

Your unique apiKey that identifies your application. You can generate a dappId by visiting the [Blocknative account page](https://account.blocknative.com/) and create a free account.

##### `networkId` - [REQUIRED]
#### `networkId` - [REQUIRED]

The Ethereum network id that your application runs on. The following values are valid:

Expand All @@ -83,12 +77,14 @@ The Ethereum network id that your application runs on. The following values are
- `5` Goerli Test Network
- `42` Kovan Test Network

##### `transactionEvents` - [OPTIONAL]
#### `transactionEvents` - [OPTIONAL]

The function defined for the `transactionEvents` parameter will be called once for every status update for _every_ transaction that is associated with a watched address _or_ a watched transaction. This is useful as a global handler for all transactions and status updates. The callback is called with the following object:

See the [Transaction Object](#transaction-object) section for more info on what is included in the `transaction` parameter.

## API

### `hash`

To get notifications for every status that occurs after sending a transaction ("post-flight"), use the `hash` function:
Expand All @@ -103,6 +99,20 @@ const { emitter } = notify.hash(hash)

Check out the [Emitter Section](#emitter) for details on the `emitter` object

### `account`

To get notifications for every "post-flight" status update for every transaction that occurs on a particular address, use the `account` function:

```javascript
// get the users' account address
const accounts = await window.ethereum.enable()

// pash the hash in to notify.hash
const { emitter } = notify.account(accounts[0])
```

Check out the [Emitter Section](#emitter) for details on the `emitter` object

### `transaction`

To get notifications for every status that occurs for the full transaction lifecycle ("pre-flight" and "post-flight"), use the `transaction` function:
Expand Down Expand Up @@ -159,7 +169,7 @@ notify.config({
})
```

### Emitter
## Emitter

The `emitter` object returned is used to listen for transaction events:

Expand All @@ -182,7 +192,7 @@ emitter.on("all", transaction => {
})
```

### Event Codes
## Event Codes

The following event codes are valid events to listen to on the transaction emitter:

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "bnc-notify",
"version": "0.1.0",
"version": "0.1.1",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
"files": [
"dist/esm/*",
"dist/cjs/*"
],
"types": "./types.d.ts",
"devDependencies": {
"rollup": "^1.12.0",
"rollup-plugin-commonjs": "^10.0.0",
Expand Down
17 changes: 9 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,19 @@ function init(initialize) {
return {
hash,
transaction,
account,
notification,
config
}

// function account(address) {
// try {
// const { emitter } = blocknative.account(address)
// return emitter
// } catch (error) {
// throw new Error(error)
// }
// }
function account(address) {
try {
const result = blocknative.account(address)
return result
} catch (error) {
throw new Error(error)
}
}

function hash(hash, id) {
try {
Expand Down
44 changes: 21 additions & 23 deletions src/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,27 @@ export function createNotification(details, customization = {}) {
"..." +
counterparty.substring(counterparty.length - 4)

// const formatterOptions = counterparty
// ? [
// `watched.${eventCode}`,
// {
// verb:
// eventCode === "txConfirmed"
// ? direction === "incoming"
// ? "received"
// : "sent"
// : direction === "incoming"
// ? "receiving"
// : "sending",
// formattedValue: BigNumber(value)
// .div(BigNumber("1000000000000000000"))
// .toString(),
// preposition: direction === "incoming" ? "from" : "to",
// counterpartyShortened,
// asset
// }
// ]
// : [`transaction.${eventCode}`]

const formatterOptions = [`transaction.${eventCode}`]
const formatterOptions = counterparty
? [
`watched.${eventCode}`,
{
verb:
eventCode === "txConfirmed"
? direction === "incoming"
? "received"
: "sent"
: direction === "incoming"
? "receiving"
: "sending",
formattedValue: BigNumber(value)
.div(BigNumber("1000000000000000000"))
.toString(),
preposition: direction === "incoming" ? "from" : "to",
counterpartyShortened,
asset
}
]
: [`transaction.${eventCode}`]

const notificationObject = {
id: id || hash,
Expand Down
10 changes: 0 additions & 10 deletions src/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ export function handlePreFlightEvent({
}

export function handleTransactionEvent({ transaction, emitterResult }) {
// transaction queue alread has tx with same id and same eventCode then don't update
// this is to allow for the fact that the server mirrors events sent to it
if (
transactionQueue.find(
tx => tx.id === transaction.id && tx.eventCode === transaction.eventCode
)
) {
return
}

transactions.updateQueue(transaction)

// create notification if dev hasn't opted out
Expand Down
58 changes: 58 additions & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
export as namespace notify

export = init

interface transactionCallback {
(transactionEvent: object): void
}

interface initializationObject {
networkId: number
dappId: string
transactionListeners?: transactionCallback[]
}

interface transactionOptions {
sendTransaction: () => any
estimateGas: () => any
gasPrice: () => any
balance: string
contract: {
methodName: string
parameters: any[]
}
txDetails: {
to: string
value: any
from: string
}
}

interface notificationObject {
type: string
message: string
autoDismiss: number
onclick: () => any
}

interface configOptions {
mobilePosition: string
desktopPosition: string
darkMode: boolean
txApproveReminderTimeout: number
txStallPendingTimeout: number
txStallConfirmedTimeout: number
}

interface notifyApi {
hash: (hash: string, id?: string) => any
transaction: (options: transactionOptions) => any
account: (address: string) => any
notification: (
eventCode: string,
notificationObject: notificationObject
) => any
config: (options: configOptions) => void
}

declare function init(initialize: initializationObject): notifyApi

0 comments on commit 8644845

Please sign in to comment.