Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Watch Account Feature #6

Merged
merged 5 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 { emitter } = blocknative.account(address)
return emitter
} 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