ETH Balnaces implemented

This commit is contained in:
Luis Fernando Molina committed 2020-11-19 13:06:12 +01:00
1 parent 9bf8c2eed6
commit 6784e6bd0b
42 files changed
+979 -669

No files matched your search

@@ -57,47 +57,13 @@ function newEthereumBlockchainSpace() {
for (let j = 0; j < blockchainNetwork.networkClients.length; j++) {
let networkClient = blockchainNetwork.networkClients[j]
let config = JSON.parse(networkClient.config)
if (config.host === undefined) {
networkClient.payload.uiObject.setErrorMessage('Property host not defined at node config.')
continue
}
if (config.httpPort === undefined && config.webSocketsPort === undefined) {
networkClient.payload.uiObject.setErrorMessage('Property httpPort or webSocketsPort must be defined at node config.')
continue
}
let route = UI.projects.ethereum.utilities.routeToClient.buildRouteToClient(networkClient)
/* Web Sockets would be the default protocol */
let clientPort
let clientInterface
if (config.webSocketsPort !== undefined) {
clientInterface = 'ws'
clientPort = config.webSocketsPort
} else {
clientInterface = 'http'
clientPort = config.httpPort
}
if (route === undefined) { continue } // something went wrong building the route.
let params = {
method: 'getNetworkClientStatus',
host: config.host,
port: clientPort,
interface: clientInterface
}
route.params.method = 'getNetworkClientStatus'
/*
Next, we will check which Superalgos Network Node we need as a gateway
to the Ethereum Client. We will use the current node reference parent for that.
*/
if (networkClient.payload.referenceParent === undefined) {
networkClient.payload.uiObject.setErrorMessage('You need to reference a Superalgos Network Node so that it acts as a gateway to the Ethereum Client.')
return
}
let networkNodeConfig = JSON.parse(networkClient.payload.referenceParent.config)
let url = 'http://' + networkNodeConfig.host + ':' + networkNodeConfig.webPort + '/' + 'WEB3'
httpRequest(JSON.stringify(params), url, onResponse)
httpRequest(JSON.stringify(route.params), route.url, onResponse)
function onResponse(err, data) {
/* Lets check the result of the call through the http interface */
@@ -129,13 +95,13 @@ function newEthereumBlockchainSpace() {
if (status.chainId > 0) {
let networkName = UI.projects.ethereum.globals.chainIds.chainNameById(status.chainId)
networkClient.payload.uiObject.setStatus('Connected to ' + networkName + ' via the ' + clientInterface + ' interface, through ' + networkClient.payload.referenceParent.name + ' Network Node.', ANNIMATION_CYCLES_TO_LAST)
networkClient.payload.uiObject.setStatus('Connected to ' + networkName + ' via the ' + route.params.interface + ' interface, through ' + networkClient.payload.referenceParent.name + ' Network Node.', ANNIMATION_CYCLES_TO_LAST)
networkClient.payload.uiObject.valueAtAngle = false
networkClient.payload.uiObject.setValue(
'Current Block Number ' + splitLargeNumber(status.currentBlockNumber)
, 200)
networkClient.payload.uiObject.setPercentage('100.00', ANNIMATION_CYCLES_TO_LAST)
'Current Block Number ' + splitLargeNumber(status.currentBlockNumber)
, 200)
networkClient.payload.uiObject.setPercentage('100.00', ANNIMATION_CYCLES_TO_LAST)
return
}
@@ -193,7 +159,7 @@ function newEthereumBlockchainSpace() {
}
}
} catch (err) {
if (ERROR_LOG === true) { logger.write('[ERROR] checkStatus -> err = ' + err.stack) }
console.log('[ERROR] checkStatus -> err = ' + err.stack)
}
}
}
@@ -1,10 +1,10 @@
function newEthereumWalletSpace() {
const MODULE_NAME = 'Wallet Space'
let thisObject = {
container: undefined,
physics: physics,
draw: draw,
draw: draw,
getContainer: getContainer,
finalize: finalize,
initialize: initialize
@@ -13,6 +13,8 @@ function newEthereumWalletSpace() {
thisObject.container = newContainer()
thisObject.container.initialize(MODULE_NAME)
let lastTryToReconnectDatetime
return thisObject
function finalize() {
@@ -26,7 +28,122 @@ function newEthereumWalletSpace() {
}
function physics() {
walletBalancesPhysics()
}
async function walletBalancesPhysics() {
/* We will query the node only every 3 seconds */
if (lastTryToReconnectDatetime === undefined) {
checkBalances()
lastTryToReconnectDatetime = (new Date()).valueOf()
} else {
let now = (new Date()).valueOf()
if (now - lastTryToReconnectDatetime > 3000) {
checkBalances()
lastTryToReconnectDatetime = now
}
}
async function checkBalances() {
try {
if (UI.projects.superalgos.spaces.designSpace.workspace === undefined) { return }
let wallets = []
let hierarchyHeads = UI.projects.superalgos.spaces.designSpace.workspace.getHierarchyHeads()
for (let i = 0; i < hierarchyHeads.length; i++) {
let hierarchyHead = hierarchyHeads[i]
if (hierarchyHead.type === 'Ethereum Wallet') {
wallets.push(hierarchyHead)
}
}
for (let i = 0; i < wallets.length; i++) {
let wallet = wallets[i]
if (wallet.networkClientReference === undefined) {
wallet.payload.uiObject.setWarningMessage('A Network Client Reference is needed in order to fetch Account Balances from a Network Client.')
continue
}
if (wallet.networkClientReference.payload === undefined) {
// node exists without payload, probably has been deleted.
continue
}
if (wallet.networkClientReference.payload.referenceParent === undefined) {
wallet.networkClientReference.payload.uiObject.setWarningMessage('A reference to a Network Client is needed in order to fetch Account Balances from a Network Client.')
continue
}
let networkClient = wallet.networkClientReference.payload.referenceParent
let route = UI.projects.ethereum.utilities.routeToClient.buildRouteToClient(networkClient)
if (route === undefined) { continue } // something went wrong building the route.
route.params.method = 'getWalletBalances'
let lightingPath = '' +
'Ethereum Wallet->' +
'Wallet Account->' +
'ETH Balance->' +
'Token Balance->' +
'Ethereum Token->' +
'ERC-20 Token Type->ERC-223 Token Type->ERC-721 Token Type->ERC-777 Token Type->'
route.params.walletDefinition = UI.projects.superalgos.functionLibraries.protocolNode.getProtocolNode(wallet, false, true, true, false, false, lightingPath)
httpRequest(JSON.stringify(route.params), route.url, onResponse)
function onResponse(err, data) {
/* Lets check the result of the call through the http interface */
if (err.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) {
networkClient.payload.uiObject.setErrorMessage('Call via HTTP Interface failed.')
return
}
let response = JSON.parse(data)
/* Lets check the result of the method call */
if (response.result !== GLOBAL.DEFAULT_OK_RESPONSE.result) {
networkClient.payload.uiObject.setErrorMessage('Call to WEB3 Server failed. ' + response.error)
return
}
showBalances(response.walletDefinition)
}
function showBalances(walletDefinition) {
for (let i = 0; i < walletDefinition.walletAccounts.length; i++) {
let walletAccount = walletDefinition.walletAccounts[i]
if (walletAccount.ethBalance !== undefined) {
let uiObject = wallet.walletAccounts[i].ethBalance.payload.uiObject
if (walletAccount.error !== undefined) {
uiObject.setErrorMessage(walletAccount.error)
} else {
let value = walletAccount.ethBalance.value / 1000000000000000000
uiObject.valueAtAngle = false
uiObject.setValue(value + ' ETH')
}
}
for (let j = 0; j < walletAccount.tokenBalances.length; j++) {
let tokenBalance = walletAccount.tokenBalances[j]
let uiObject = wallet.walletAccounts[i].tokenBalances[j].payload.uiObject
uiObject.valueAtAngle = false
uiObject.setValue(tokenBalance.value + ' ' + uiObject.payload.node.name)
}
}
}
}
} catch (err) {
console.log('[ERROR] checkBalances -> err = ' + err.stack)
}
}
}
function getContainer(point) {
@@ -40,6 +157,6 @@ function newEthereumWalletSpace() {
}
function draw() {
}
}
}