Skip to content

Instantly share code, notes, and snippets.

@cmeisl
Last active April 25, 2019 22:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmeisl/012d7f73ba9b5e05ee606b50f2b949af to your computer and use it in GitHub Desktop.
Save cmeisl/012d7f73ba9b5e05ee606b50f2b949af to your computer and use it in GitHub Desktop.
Example file consolidating Assist calls
import bnc from 'bnc-assist'calls
let initializedAssist
// Assist methods
export const initializeAssist = web3 => getAssist(web3) // Call this function as soon as web3 is initialized
export const onboardUser = () => getAssist().onboard()
export const decorateContract = contract => getAssist().Contract(contract)
export const decorateTransaction = txObject => getAssist().Transaction(txObject)
export const getUserState = () => getAssist().getState()
// Returns initialized assist object if previously initialized.
// Otherwise will initialize assist with the config object
export function getAssist(web3) {
if (!initializedAssist) {
initializedAssist = bnc.init(
{
networkId: process.env.ETHEREUM_NETWORK_ID || 1,
dappId: 'YOUR_API_KEY', // from https://accounts.blocknative.com
web3,
messages: { // optional custom notification text handlers, see documentation
txSent: txSentMsg,
txPending: txPendingMsg,
txConfirmed: txConfirmedMsg,
txFailed: txFailedMsg
},
style: {
darkMode: true // optional style settings, see documentation
}
}
)
}
return initializedAssist
}
function txSentMsg(data) {
// return a global message for transaction sent event
// data contains transaction/contract context, see documention for details
// e.g.
let highFiveContract = data.contract && data.contract.methodName === 'highFive'
return highFiveContract ? 'Sending a High Five... 👋' : 'Sending some ETH...'
}
function txPendingMsg(data) {
// return a global message for transaction pending event
// data contains transaction/contract context, see documention for details
// e.g.
let highFiveContract = data.contract && data.contract.methodName === 'highFive'
return highFiveContract ? 'Your High Five is pending... 👋' : 'Your sending of ETH is pending...'
}
function txConfirmedMsg(data) {
// return a global message for transaction confirmed event
// data contains transaction/contract context, see documention for details
// e.g.
let highFiveContract = data.contract && data.contract.methodName === 'highFive'
return highFiveContract ? 'High Five successful! 👋' : 'Your ETH was sent!'
}
function txFailedMsg(data) {
// return a global message for transaction failed event
// data contains transaction/contract context, see documention for details
let highFiveContract = data.contract && data.contract.methodName === 'highFive'
return highFiveContract ? 'Your High Five failed! 😞' : 'Your sending of ETH failed!'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment