From a072e8f6c908305da12be8d1f9037a00e69ee3f0 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 11:19:41 +1000 Subject: [PATCH 1/8] Add watch account feature back in to library --- src/index.js | 17 +++++++++-------- src/notifications.js | 44 +++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/index.js b/src/index.js index 6a91dc5..fcc94fa 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { diff --git a/src/notifications.js b/src/notifications.js index 3b3affc..274fbae 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -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, From ad66111ab7fd0d08d1efa8a18d34d3ea5d962391 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 11:24:29 +1000 Subject: [PATCH 2/8] Update docs --- README.md | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2801cf9..30fc931 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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: From 478c5d1d7c2756c5cea1da307a191dc2ab97af2b Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 11:47:26 +1000 Subject: [PATCH 3/8] Docs update --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 30fc931..a122fa3 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ emitter.on("txFailed", console.log) emitter.on("all", console.log) ``` -### API +## API ### Initialization From 8d9eebbc08f7b3609d6aa0b7f82ad9e3fe233abc Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 13:15:41 +1000 Subject: [PATCH 4/8] Documentation updates --- README.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a122fa3..b5ad6ea 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,7 @@ emitter.on("txFailed", console.log) emitter.on("all", console.log) ``` -## API - -### Initialization +## Initialization ```javascript import Notify from "bn-notify" @@ -55,7 +53,7 @@ const options = { const notify = Notify(options) ``` -#### Options +### Options ```javascript const options = { @@ -65,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: @@ -79,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: @@ -169,7 +169,7 @@ notify.config({ }) ``` -### Emitter +## Emitter The `emitter` object returned is used to listen for transaction events: @@ -192,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: From 2c6f273d2a4dfcafb99015af58daa9a77bbd4fc5 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Wed, 2 Oct 2019 15:05:43 +1000 Subject: [PATCH 5/8] Remove code that handles server echo messages --- src/transactions.js | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/transactions.js b/src/transactions.js index 6d5270a..ad6a15b 100644 --- a/src/transactions.js +++ b/src/transactions.js @@ -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 From ac649a5d7d3e34dc1eb838f92a51912b3da8e252 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Thu, 3 Oct 2019 14:46:39 +1000 Subject: [PATCH 6/8] Make return value from account the same as the rest of the api functions --- src/index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index fcc94fa..eec0687 100644 --- a/src/index.js +++ b/src/index.js @@ -73,8 +73,8 @@ function init(initialize) { function account(address) { try { - const { emitter } = blocknative.account(address) - return emitter + const result = blocknative.account(address) + return result } catch (error) { throw new Error(error) } From ddaeb8c0c7598373218c1eb6c407a14403ad6366 Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Thu, 3 Oct 2019 14:46:54 +1000 Subject: [PATCH 7/8] Add typescript definitions --- package.json | 1 + types.d.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 types.d.ts diff --git a/package.json b/package.json index 0bd1be5..0fe9d43 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "dist/esm/*", "dist/cjs/*" ], + "types": "./types.d.ts", "devDependencies": { "rollup": "^1.12.0", "rollup-plugin-commonjs": "^10.0.0", diff --git a/types.d.ts b/types.d.ts new file mode 100644 index 0000000..911caa1 --- /dev/null +++ b/types.d.ts @@ -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 From ebc5269dae8b20fbfd965cee8870e2307e7c5e2b Mon Sep 17 00:00:00 2001 From: Aaron Barnard Date: Thu, 3 Oct 2019 14:55:00 +1000 Subject: [PATCH 8/8] Release 0.1.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 0fe9d43..77e14f0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-notify", - "version": "0.1.0", + "version": "0.1.1", "main": "dist/cjs/index.js", "module": "dist/esm/index.js", "files": [