@ -1,48 +1,29 @@
// eslint-disable-next-line no-unused-vars
import React from 'react'
import React from 'react'
import { shortenAddress } from '@remix-ui/helper'
import * as ethJSUtil from 'ethereumjs-util'
import * as ethJSUtil from 'ethereumjs-util'
import Web3 from 'web3'
import Web3 from 'web3'
import { fetchAccountsListFailed , fetchAccountsListRequest , fetchAccountsListSuccess } from './payload'
import { shortenAddress } from '@remix-ui/helper'
import { addProvider , fetchAccountsListFailed , fetchAccountsListRequest , fetchAccountsListSuccess , removeProvider , setExecutionEnvironment , setExternalEndpoint , setGasLimit , setNetworkName , setSelectedAccount , setSendUnit , setSendValue } from './payload'
import { Udapp } from '../types'
let plugin , dispatch : React.Dispatch < any >
let plugin : Udapp , dispatch : React.Dispatch < any >
export const initSettingsTab = ( udapp ) = > async ( reducerDispatch : React.Dispatch < any > ) = > {
export const initRunTab = ( udapp : Udapp ) = > async ( reducerDispatch : React.Dispatch < any > ) = > {
if ( udapp ) {
plugin = udapp
plugin = udapp
dispatch = reducerDispatch
dispatch = reducerDispatch
setupEvents ( )
setupEvents ( )
setInterval ( ( ) = > {
fillAccountsList ( )
} , 1000 )
}
}
}
const setupEvents = ( ) = > {
const setupEvents = ( ) = > {
plugin . blockchain . events . on ( 'newTransaction' , ( tx , receipt ) = > {
plugin . emit ( 'newTransaction' , tx , receipt )
} )
plugin . blockchain . event . register ( 'transactionExecuted' , ( error , from , to , data , lookupOnly , txResult ) = > {
// if (!lookupOnly) this.el.querySelector('#value').value = 0
if ( error ) return
updateAccountBalances ( )
} )
plugin . blockchain . resetAndInit ( plugin . config , {
plugin . blockchain . resetAndInit ( plugin . config , {
getAddress : ( cb ) = > {
getAddress : ( cb ) = > {
cb ( null , $ ( '#txorigin' ) . val ( ) )
cb ( null , plugin . REACT_API . accounts . selectedAccount )
} ,
} ,
getValue : ( cb ) = > {
getValue : ( cb ) = > {
try {
try {
const number = document . querySelector ( '#value' ) . value
const number = plugin . REACT_API . sendValue
const select = document . getElementById ( 'unit' )
const unit = plugin . REACT_API . sendUnit
const index = select . selectedIndex
const selectedUnit = select . querySelectorAll ( 'option' ) [ index ] . dataset . unit
let unit = 'ether' // default
if ( [ 'ether' , 'finney' , 'gwei' , 'wei' ] . indexOf ( selectedUnit ) >= 0 ) {
unit = selectedUnit
}
cb ( null , Web3 . utils . toWei ( number , unit ) )
cb ( null , Web3 . utils . toWei ( number , unit ) )
} catch ( e ) {
} catch ( e ) {
cb ( e )
cb ( e )
@ -50,27 +31,66 @@ const setupEvents = () => {
} ,
} ,
getGasLimit : ( cb ) = > {
getGasLimit : ( cb ) = > {
try {
try {
cb ( null , '0x' + new ethJSUtil . BN ( $ ( '#gasLimit' ) . val ( ) , 10 ) . toString ( 16 ) )
cb ( null , '0x' + new ethJSUtil . BN ( plugin . REACT_API . gasLimit , 10 ) . toString ( 16 ) )
} catch ( e ) {
} catch ( e ) {
cb ( e . message )
cb ( e . message )
}
}
}
}
} )
} )
plugin . blockchain . events . on ( 'newTransaction' , ( tx , receipt ) = > {
plugin . emit ( 'newTransaction' , tx , receipt )
} )
plugin . blockchain . event . register ( 'transactionExecuted' , ( error , from , to , data , lookupOnly , txResult ) = > {
if ( ! lookupOnly ) dispatch ( setSendValue ( 0 ) )
if ( error ) return
updateAccountBalances ( )
} )
plugin . blockchain . event . register ( 'contextChanged' , ( context , silent ) = > {
setFinalContext ( )
} )
plugin . blockchain . event . register ( 'networkStatus' , ( { error , network } ) = > {
if ( error ) {
const netUI = 'can\'t detect network '
setNetworkNameFromProvider ( netUI )
return
}
const networkProvider = plugin . networkModule . getNetworkProvider . bind ( plugin . networkModule )
const netUI = ( networkProvider ( ) !== 'vm' ) ? ` ${ network . name } ( ${ network . id || '-' } ) network ` : ''
setNetworkNameFromProvider ( netUI )
} )
plugin . blockchain . event . register ( 'addProvider' , provider = > addExternalProvider ( provider ) )
plugin . blockchain . event . register ( 'removeProvider' , name = > removeExternalProvider ( name ) )
plugin . on ( 'manager' , 'pluginActivated' , addPluginProvider . bind ( plugin ) )
plugin . on ( 'manager' , 'pluginDeactivated' , removePluginProvider . bind ( plugin ) )
// setInterval(() => {
// fillAccountsList()
// }, 1000)
// fillAccountsList()
setTimeout ( ( ) = > {
fillAccountsList ( )
} , 0 )
}
}
const updateAccountBalances = ( ) = > {
const updateAccountBalances = ( ) = > {
// const accounts = $(this.el.querySelector('#txorigin')).children('option')
const accounts = runTab . accounts . loadedAccounts
// accounts.each((index, account) => {
Object . keys ( accounts ) . map ( ( value ) = > {
// plugin.blockchain.getBalanceInEther(account.value, (err, balance) => {
plugin . blockchain . getBalanceInEther ( value , ( err , balance ) = > {
// if (err) return
if ( err ) return
// const updated = shortenAddress(account.value, balance)
const updated = shortenAddress ( value , balance )
// if (updated !== account.innerText) { // check if the balance has been updated and update UI accordingly.
accounts [ value ] = updated
// account.innerText = updated
} )
// }
} )
// })
dispatch ( fetchAccountsListSuccess ( accounts ) )
// })
}
}
const fillAccountsList = async ( ) = > {
const fillAccountsList = async ( ) = > {
@ -79,7 +99,18 @@ const fillAccountsList = async () => {
const promise = plugin . blockchain . getAccounts ( )
const promise = plugin . blockchain . getAccounts ( )
promise . then ( ( accounts : string [ ] ) = > {
promise . then ( ( accounts : string [ ] ) = > {
dispatch ( fetchAccountsListSuccess ( accounts ) )
const loadedAccounts = { }
if ( ! accounts ) accounts = [ ]
accounts . forEach ( ( account ) = > {
plugin . blockchain . getBalanceInEther ( account , ( err , balance ) = > {
if ( err ) return
const updated = shortenAddress ( account , balance )
loadedAccounts [ account ] = updated
} )
} )
dispatch ( fetchAccountsListSuccess ( loadedAccounts ) )
} ) . catch ( ( e ) = > {
} ) . catch ( ( e ) = > {
dispatch ( fetchAccountsListFailed ( e . message ) )
dispatch ( fetchAccountsListFailed ( e . message ) )
} )
} )
@ -87,3 +118,88 @@ const fillAccountsList = async () => {
// addTooltip(`Cannot get account list: ${e}`)
// addTooltip(`Cannot get account list: ${e}`)
}
}
}
}
const setAccount = ( account : string ) = > {
dispatch ( setSelectedAccount ( account ) )
}
const setUnit = ( unit : 'ether' | 'finney' | 'gwei' | 'wei' ) = > {
dispatch ( setSendUnit ( unit ) )
}
const setGasFee = ( value : number ) = > {
dispatch ( setGasLimit ( value ) )
}
const addPluginProvider = ( profile ) = > {
if ( profile . kind === 'provider' ) {
( ( profile , app ) = > {
const web3Provider = {
async sendAsync ( payload , callback ) {
try {
const result = await app . call ( profile . name , 'sendAsync' , payload )
callback ( null , result )
} catch ( e ) {
callback ( e )
}
}
}
app . blockchain . addProvider ( { name : profile.displayName , provider : web3Provider } )
} ) ( profile , plugin )
}
}
const removePluginProvider = ( profile ) = > {
if ( profile . kind === 'provider' ) plugin . blockchain . removeProvider ( profile . displayName )
}
const setFinalContext = ( ) = > {
// set the final context. Cause it is possible that this is not the one we've originaly selected
const value = _getProviderDropdownValue ( )
setExecEnv ( value )
// this.event.trigger('clearInstance', [])
}
const _getProviderDropdownValue = ( ) : string = > {
const provider = plugin . blockchain . getProvider ( )
const fork = plugin . blockchain . getCurrentFork ( )
return provider === 'vm' ? provider + '-' + fork : provider
}
const setExecEnv = ( env : string ) = > {
dispatch ( setExecutionEnvironment ( env ) )
}
const setNetworkNameFromProvider = ( networkName : string ) = > {
dispatch ( setNetworkName ( networkName ) )
}
const addExternalProvider = ( network ) = > {
dispatch ( addProvider ( network ) )
// addTooltip(yo`<span><b>${network.name}</b> provider added</span>`)
}
const removeExternalProvider = ( name ) = > {
dispatch ( removeProvider ( name ) )
}
const setProviderFromEndpoint = ( executionContext : { context : string , fork : string } ) = > {
plugin . blockchain . setProviderFromEndpoint ( runTab . externalEndpoint , executionContext , ( alertMsg ) = > {
// if (alertMsg) addTooltip(alertMsg)
setFinalContext ( )
} )
}
const setExecutionContext = ( executionContext : { context : string , fork : string } ) = > {
plugin . blockchain . changeExecutionContext ( executionContext , ( ) = > {
executionContextModal ( executionContext )
} , ( alertMsg ) = > {
// addTooltip(alertMsg)
} , setFinalContext ( ) )
}
const setWeb3Endpoint = ( endpoint : string ) = > {
dispatch ( setExternalEndpoint ( endpoint ) )
}