diff --git a/README.md b/README.md index 2801cf9..b5ad6ea 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) @@ -42,9 +38,7 @@ emitter.on("txFailed", console.log) emitter.on("all", console.log) ``` -### API - -### Initialization +## Initialization ```javascript import Notify from "bn-notify" @@ -59,7 +53,7 @@ const options = { const notify = Notify(options) ``` -#### Options +### Options ```javascript const options = { @@ -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: @@ -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: @@ -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: @@ -159,7 +169,7 @@ notify.config({ }) ``` -### Emitter +## Emitter The `emitter` object returned is used to listen for transaction events: @@ -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: diff --git a/package.json b/package.json index 0bd1be5..77e14f0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.js b/src/index.js index 6a91dc5..eec0687 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 result = blocknative.account(address) + return result + } 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, 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 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