diff --git a/README.md b/README.md index 7c8c5225..bc3eb6b9 100644 --- a/README.md +++ b/README.md @@ -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 `
` ```html - + - + ``` ### Initialize the Library @@ -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: @@ -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.* @@ -534,11 +537,11 @@ 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 @@ -546,7 +549,7 @@ myContract.myMethod().call() #### 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 @@ -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()` diff --git a/package.json b/package.json index 8beae481..d85b1c66 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/js/helpers/utilities.js b/src/js/helpers/utilities.js index a21b18ab..c72fb17b 100644 --- a/src/js/helpers/utilities.js +++ b/src/js/helpers/utilities.js @@ -203,6 +203,8 @@ export function networkName(id) { return 'ropsten' case 4: return 'rinkeby' + case 5: + return 'goerli' case 42: return 'kovan' case 'localhost': diff --git a/src/js/index.js b/src/js/index.js index 19728874..c07652d5 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -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' @@ -510,7 +512,7 @@ function init(config) { // TRANSACTION FUNCTION // - function Transaction(txOptions, callback, inlineCustomMsgs = {}) { + function Transaction(txOptionsOrHash, callback, inlineCustomMsgs = {}) { const { validApiKey, supportedNetwork, @@ -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() @@ -548,7 +575,7 @@ function init(config) { const promiEvent = new PromiEventLib.PromiEvent() sendTransaction({ categoryCode: 'activeTransaction', - txOptions, + txOptionsOrHash, sendMethod, callback, inlineCustomMsgs: inlineCustomMsgs.messages, @@ -560,7 +587,7 @@ function init(config) { return sendTransaction({ categoryCode: 'activeTransaction', - txOptions, + txOptionsOrHash, sendMethod, callback, inlineCustomMsgs: inlineCustomMsgs.messages diff --git a/src/js/logic/contract-methods.js b/src/js/logic/contract-methods.js index 264dbab5..89544f75 100644 --- a/src/js/logic/contract-methods.js +++ b/src/js/logic/contract-methods.js @@ -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({ diff --git a/src/js/logic/send-transaction.js b/src/js/logic/send-transaction.js index 0510b7d3..e0ec3d19 100644 --- a/src/js/logic/send-transaction.js +++ b/src/js/logic/send-transaction.js @@ -23,7 +23,7 @@ import { import { prepareForTransaction } from './user' -function sendTransaction({ +export function sendTransaction({ categoryCode, txOptions = {}, sendMethod, @@ -55,7 +55,7 @@ function sendTransaction({ truffleContract }) - const contractEventObj = { + const contractEventObj = methodName && { methodName, parameters: methodArgs } @@ -265,7 +265,7 @@ function sendTransaction({ }) } -function onTxHash(id, hash, categoryCode) { +export function onTxHash(id, hash, categoryCode) { const txObj = updateTransactionInQueue(id, { status: 'approved', hash, @@ -378,5 +378,3 @@ function onTxError(id, error, categoryCode) { removeTransactionFromQueue(id) } - -export default sendTransaction