Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/remix-ide/src/blockchain/blockchain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const profile = {
name: 'blockchain',
displayName: 'Blockchain',
description: 'Blockchain - Logic',
methods: ['dumpState', 'getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'isSmartAccount', 'getAccounts', 'web3VM', 'web3', 'getProvider', 'getCurrentProvider', 'getCurrentNetworkStatus', 'getAllProviders', 'getPinnedProviders', 'changeExecutionContext', 'getProviderObject'],
methods: ['dumpState', 'getCode', 'getTransactionReceipt', 'addProvider', 'removeProvider', 'getCurrentFork', 'isSmartAccount', 'getAccounts', 'web3VM', 'web3', 'getProvider', 'getCurrentProvider', 'getCurrentNetworkStatus', 'getCurrentNetworkCurrency', 'getAllProviders', 'getPinnedProviders', 'changeExecutionContext', 'getProviderObject'],

version: packageJson.version
}
Expand Down Expand Up @@ -63,6 +63,11 @@ export class Blockchain extends Plugin {
}
error?: string
}
networkNativeCurrency: {
name: string,
symbol: string,
decimals: number
}
providers: {[key: string]: VMProvider | InjectedProvider | NodeProvider }
transactionContextAPI: TransactionContextAPI
registeredPluginEvents: string[]
Expand Down Expand Up @@ -100,6 +105,7 @@ export class Blockchain extends Plugin {
// the first item in the list should be latest fork.
this.defaultPinnedProviders = ['vm-prague', 'vm-cancun', 'vm-mainnet-fork', 'walletconnect', 'injected-MetaMask', 'basic-http-provider', 'hardhat-provider', 'foundry-provider', 'desktopHost']
this.networkStatus = { network: { name: this.defaultPinnedProviders[0], id: ' - ' } }
this.networkNativeCurrency = { name: "Ether", symbol: "ETH", decimals: 18 }
this.pinnedProviders = []
this.setupEvents()
this.setupProviders()
Expand All @@ -120,6 +126,7 @@ export class Blockchain extends Plugin {
if (plugin.name === this.executionContext.executionContext) {
this.detectNetwork((error, network) => {
this.networkStatus = { network, error }
if (network.networkNativeCurrency) this.networkNativeCurrency = network.networkNativeCurrency
this._triggerEvent('networkStatus', [this.networkStatus])
})
}
Expand Down Expand Up @@ -184,6 +191,7 @@ export class Blockchain extends Plugin {
this._triggerEvent('contextChanged', [context])
this.detectNetwork((error, network) => {
this.networkStatus = { network, error }
if (network.networkNativeCurrency) this.networkNativeCurrency = network.networkNativeCurrency
this._triggerEvent('networkStatus', [this.networkStatus])
})
})
Expand All @@ -199,6 +207,7 @@ export class Blockchain extends Plugin {
setInterval(() => {
this.detectNetwork((error, network) => {
this.networkStatus = { network, error }
if (network.networkNativeCurrency) this.networkNativeCurrency = network.networkNativeCurrency
this._triggerEvent('networkStatus', [this.networkStatus])
})
}, 30000)
Expand All @@ -208,6 +217,10 @@ export class Blockchain extends Plugin {
return this.networkStatus
}

getCurrentNetworkCurrency() {
return this.networkNativeCurrency
}

isSmartAccount(address) {
return this.transactionContextAPI.isSmartAccount(address)
}
Expand Down
47 changes: 32 additions & 15 deletions apps/remix-ide/src/blockchain/execution-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,34 +78,51 @@ export class ExecutionContext {
callback && callback('No provider set')
return reject('No provider set')
}
const cb = (err, id) => {
let name = null
const cb = async (err, id) => {
let name = 'Custom'
let networkNativeCurrency = { name: "Ether", symbol: "ETH", decimals: 18 }
if (err) name = 'Unknown'
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
else if (id === 1) name = 'Main'
else if (id === 3) name = 'Ropsten'
else if (id === 4) name = 'Rinkeby'
else if (id === 5) name = 'Goerli'
else if (id === 42) name = 'Kovan'
else if (id === 11155111) name = 'Sepolia'
else name = 'Custom'

else {
let networkDetails = localStorage.getItem('networkDetails')
if (!networkDetails) networkDetails = '{}'
networkDetails = JSON.parse(networkDetails)
if (networkDetails[id]) {
name = networkDetails[id].name
networkNativeCurrency = networkDetails[id].nativeCurrency
} else {
const response = await fetch('https://chainid.network/chains.json')
if (response.ok) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the case where the nativeCurrency can't be resolved is handled? like defaulting to eth or something?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you want, I can do the same in this file too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am saying that because line 83 the local variable is set to null.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I will update that here too

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

const networks = await response.json()
const connectedNetwork = networks.find((n) => n.chainId === id)
if (connectedNetwork) {
name = connectedNetwork.name
networkNativeCurrency = connectedNetwork.nativeCurrency
networkDetails[id] = { name, nativeCurrency: networkNativeCurrency}
localStorage.setItem('networkDetails', JSON.stringify(networkDetails))
}
}
}
}

if (id === 1) {
web3.eth.getBlock(0).then((block) => {
if (block && block.hash !== this.mainNetGenesisHash) name = 'Custom'
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
}).catch((error) => {
// Rabby wallet throws an error at this point. We are in that case unable to check the genesis hash.
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
})
} else {
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork })
callback && callback(err, { id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
return resolve({ id, name, lastBlock: this.lastBlock, currentFork: this.currentFork, networkNativeCurrency })
}
}
web3.eth.net.getId().then(id=>cb(null,parseInt(id))).catch(err=>cb(err))
web3.eth.net.getId().then(async (id) => await cb(null, parseInt(id))).catch(err => cb(err))
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions libs/remix-ui/helper/src/lib/remix-ui-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ export const isNumeric = (value) => {
return /^\+?(0|[1-9]\d*)$/.test(value)
}

export const shortenAddress = (address, etherBalance?) => {
export const shortenAddress = (address, etherBalance?, currency = 'ETH') => {
const len = address.length

return address.slice(0, 5) + '...' + address.slice(len - 5, len) + (etherBalance ? ' (' + etherBalance.toString() + ' ether)' : '')
return address.slice(0, 5) + '...' + address.slice(len - 5, len) + (etherBalance ? ' (' + etherBalance.toString() + ' ' + currency + ')' : '')
}

export const addressToString = (address) => {
Expand Down
5 changes: 4 additions & 1 deletion libs/remix-ui/run-tab/src/lib/actions/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export const fillAccountsList = async (plugin: RunTab, dispatch: React.Dispatch<

for (const account of accounts) {
const balance = await plugin.blockchain.getBalanceInEther(account)
loadedAccounts[account] = shortenAddress(account, balance)
if (provider.startsWith('injected') && plugin.blockchain && plugin.blockchain['networkNativeCurrency'] && plugin.blockchain['networkNativeCurrency'].symbol)
loadedAccounts[account] = shortenAddress(account, balance, plugin.blockchain['networkNativeCurrency'].symbol)
else
loadedAccounts[account] = shortenAddress(account, balance)
if (safeAddresses.length && safeAddresses.includes(account)) loadedAccounts[account] = `[SMART] ${loadedAccounts[account]}`
}
dispatch(fetchAccountsListSuccess(loadedAccounts))
Expand Down
1 change: 0 additions & 1 deletion libs/remix-ui/run-tab/src/lib/actions/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ export const setupEvents = (plugin: RunTab) => {
if (error) {
const netUI = 'can\'t detect network'
setNetworkNameFromProvider(dispatch, netUI)

return
}
const networkProvider = plugin.networkModule.getNetworkProvider.bind(plugin.networkModule)
Expand Down