Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[common-v2.1.7-alpha.1, core-v2.6.0-alpha.1, react-v2.2.5-alpha.1, vue-2.1.5-alpha.1, demo-v2.0.6]: Enhancement - Convert Wei to Eth #1137

Merged
merged 15 commits into from
Jul 28, 2022
3 changes: 2 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/common",
"version": "2.1.6",
"version": "2.1.7-alpha.1",
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down Expand Up @@ -81,6 +81,7 @@
},
"dependencies": {
"@ethereumjs/common": "2.6.2",
"bignumber.js": "^9.0.0",
"ethers": "5.5.4",
"joi": "^17.4.2",
"rxjs": "^7.5.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/common/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ export default {
inlineSources: !production
})
],
external: ['joi', 'rxjs', 'ethers', '@ethereumjs/common']
external: ['joi', 'rxjs', 'ethers', '@ethereumjs/common', 'bignumber.js']
}
4 changes: 2 additions & 2 deletions packages/common/src/elements/AddressTable.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { Account, AccountsList } from '../types'
import { utils } from 'ethers'
import { weiToEth } from '../utils'

export let accountsListObject: AccountsList | undefined
export let accountSelected: Account | undefined = undefined
Expand Down Expand Up @@ -120,7 +120,7 @@
>
<td>{account.derivationPath}</td>
<td class="asset-td"
>{utils.formatEther(account.balance.value)}
>{weiToEth(account.balance.value)}
{account.balance.asset}</td
>
</tr>
Expand Down
6 changes: 3 additions & 3 deletions packages/common/src/hdwallets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type Common from '@ethereumjs/common'
import type { BigNumber } from 'ethers'
import type { CustomNetwork, EIP1193Provider, RPCResponse } from './types'
import type { TransactionRequest } from '@ethersproject/providers'
import type BigNumber from 'bignumber.js'

/**
* Creates the common instance used for signing
Expand Down Expand Up @@ -71,11 +71,11 @@ export const bigNumberFieldsToStrings = (
(transaction, txnProperty) => ({
...transaction,
...((transaction[txnProperty as keyof TransactionRequest] as BigNumber)
.toHexString
instanceof BigNumber
mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
? {
[txnProperty]: (
transaction[txnProperty as keyof TransactionRequest] as BigNumber
).toHexString()
).toString(16)
}
: {})
}),
Expand Down
4 changes: 3 additions & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export {
getHardwareWalletProvider
} from './hdwallets'

export { weiToEth } from './utils'

export type {
RequestPatch,
AccountSelectAPI,
Expand Down Expand Up @@ -126,4 +128,4 @@ export type {
CustomNetwork,
TransactionObject,
RPCResponse
}
}
mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ethers, BigNumber } from 'ethers'
import type { ConnectionInfo } from 'ethers/lib/utils'
import type EventEmitter from 'eventemitter3'
import type { TypedData as EIP712TypedData } from 'eip-712'
import type BigNumber from 'bignumber.js'
export type { TypedData as EIP712TypedData } from 'eip-712'

/**
Expand Down Expand Up @@ -232,7 +232,7 @@ export interface WalletModule {
export type GetInterfaceHelpers = {
chains: Chain[]
appMetadata: AppMetadata | null
BigNumber: typeof ethers.BigNumber
BigNumber: typeof BigNumber
EventEmitter: typeof EventEmitter
}

Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type BigNumber from 'bignumber.js'

export function weiToEth(wei: BigNumber): string {
return wei.div(1e18).toString(10)
}
mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 2 additions & 3 deletions packages/common/src/views/AccountSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import CloseButton from '../elements/CloseButton.svelte'
import AddressTable from '../elements/AddressTable.svelte'
import TableHeader from '../elements/TableHeader.svelte'
import { utils } from 'ethers'

import type { Subject } from 'rxjs'
import type {
ScanAccountsOptions,
SelectAccountOptions,
Account,
AccountsList
} from '../types'
import { weiToEth } from '../utils'

export let selectAccountOptions: SelectAccountOptions
export let accounts$: Subject<Account[]>
Expand Down Expand Up @@ -61,7 +60,7 @@
accountsListObject = {
all: allAccounts,
filtered: allAccounts.filter(account => {
return parseFloat(utils.formatEther(account.balance.value)) > 0
return parseFloat(weiToEth(account.balance.value)) > 0
})
}
loadingAccounts = false
Expand Down
22 changes: 11 additions & 11 deletions packages/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -640,14 +640,15 @@ setTimeout(
**`preflightNotifications`**
Notify can be used to deliver standard notifications along with preflight information by passing a `PreflightNotificationsOptions` object to the `preflightNotifications` action. This will return a a promise that resolves to the transaction hash (if `sendTransaction` resolves the transaction hash and is successful), the internal notification id (if no `sendTransaction` function is provided) or return nothing if an error occurs or `sendTransaction` is not provided or doesn't resolve to a string.

Preflight event types include
- `txRequest` : Alert user there is a transaction request awaiting confirmation by their wallet
- `txAwaitingApproval` : A previous transaction is awaiting confirmation
- `txConfirmReminder` : Reminder to confirm a transaction to continue - configurable with the `txApproveReminderTimeout` property; defaults to 15 seconds
- `nsfFail` : The user has insufficient funds for transaction (requires `gasPrice`, `estimateGas`, `balance`, `txDetails.value`)
- `txError` : General transaction error (requires `sendTransaction`)
- `txSendFail` : The user rejected the transaction (requires `sendTransaction`)
- `txUnderpriced` : The gas price for the transaction is too low (requires `sendTransaction`)
Preflight event types include

- `txRequest` : Alert user there is a transaction request awaiting confirmation by their wallet
- `txAwaitingApproval` : A previous transaction is awaiting confirmation
- `txConfirmReminder` : Reminder to confirm a transaction to continue - configurable with the `txApproveReminderTimeout` property; defaults to 15 seconds
- `nsfFail` : The user has insufficient funds for transaction (requires `gasPrice`, `estimateGas`, `balance`, `txDetails.value`)
- `txError` : General transaction error (requires `sendTransaction`)
- `txSendFail` : The user rejected the transaction (requires `sendTransaction`)
- `txUnderpriced` : The gas price for the transaction is too low (requires `sendTransaction`)

```typescript
interface PreflightNotificationsOptions {
Expand Down Expand Up @@ -678,8 +679,7 @@ const sendTransaction = () => {
return signer.sendTransaction(txDetails).then(tx => tx.hash)
}

const gasPrice = () =>
ethersProvider.getGasPrice().then(res => res.toString())
const gasPrice = () => ethersProvider.getGasPrice().then(res => res.toString())

const estimateGas = () => {
return ethersProvider.estimateGas(txDetails).then(res => res.toString())
Expand Down Expand Up @@ -811,7 +811,7 @@ The Onboard styles can customized via [CSS variables](https://developer.mozilla.
--account-center-app-btn-text-color
--account-center-app-btn-background
--account-center-app-btn-font-family

/* CUSTOMIZE SECTIONS OF THE CONNECT MODAL */
--onboard-connect-content-width
--onboard-connect-content-height
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/core",
"version": "2.5.0",
"version": "2.6.0-alpha.1",
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down Expand Up @@ -81,7 +81,7 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@web3-onboard/common": "^2.1.6",
"@web3-onboard/common": "^2.1.7-alpha.1",
"bignumber.js": "^9.0.0",
"bnc-sdk": "^4.4.1",
"bowser": "^2.11.0",
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/provider.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { fromEventPattern, Observable } from 'rxjs'
import { filter, takeUntil, take, share, switchMap } from 'rxjs/operators'
import partition from 'lodash.partition'
import { utils, providers } from 'ethers'
import { providers } from 'ethers'

import type {
import {
ChainId,
EIP1102Request,
EIP1193Provider,
ProviderAccounts,
Chain,
AccountsListener,
ChainListener,
SelectAccountsRequest
SelectAccountsRequest,
weiToEth
} from '@web3-onboard/common'

mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
import { disconnectWallet$ } from './streams'
Expand All @@ -21,6 +22,7 @@ import { validEnsChain } from './utils'
import disconnect from './disconnect'
import { state } from './store'
import { getBlocknativeSdk } from './services'
import BigNumber from 'bignumber.js'

export const ethersProviders: {
[key: string]: providers.StaticJsonRpcProvider
Expand Down Expand Up @@ -339,14 +341,16 @@ export async function getBalance(
): Promise<Balances | null> {
// chain we don't recognize and don't have a rpcUrl for requests
if (!chain) return null

const provider = getProvider(chain)
const { wallets } = state.get()

try {
const balanceWei = await provider.getBalance(address)

const wallet = wallets.find(wallet => wallet.label)
mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
const provider = wallet.provider
const balanceHex = await provider.request({ method: 'eth_getBalance', params:[address,'latest'] })
const balanceWei = new BigNumber(parseInt(balanceHex, 16))
return balanceWei
? { [chain.token || 'eth']: utils.formatEther(balanceWei) }
? { [chain.token || 'eth']: weiToEth(balanceWei).slice(0,6) }
mahmud-bn marked this conversation as resolved.
Show resolved Hide resolved
: null
} catch (error) {
console.error(error)
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/update-balances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ async function updateBalances(addresses?: string[]): Promise<void> {
)
) {
const updatedBalance = await getBalance(account.address, chain)

return { ...account, balance: updatedBalance }
}
return account
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/views/connect/Index.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { ProviderRpcErrorCode, WalletModule } from '@web3-onboard/common'
import { BigNumber } from 'ethers'
import { BehaviorSubject, takeUntil } from 'rxjs'
import EventEmitter from 'eventemitter3'
import { _ } from 'svelte-i18n'
Expand All @@ -20,7 +19,7 @@
import Sidebar from './Sidebar.svelte'
import { configuration } from '../../configuration'
import { getBlocknativeSdk } from '../../services'

import BigNumber from 'bignumber.js'
import {
getChainId,
requestAccounts,
Expand Down
4 changes: 2 additions & 2 deletions packages/demo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "demo",
"version": "2.0.5",
"version": "2.0.6",
"devDependencies": {
"assert": "^2.0.0",
"buffer": "^6.0.3",
Expand All @@ -23,7 +23,7 @@
},
"dependencies": {
"@web3-onboard/coinbase": "^2.0.8",
"@web3-onboard/core": "^2.4.1-alpha.1",
"@web3-onboard/core": "^2.6.0-alpha.1",
"@web3-onboard/dcent": "^2.0.5",
"@web3-onboard/fortmatic": "^2.0.6",
"@web3-onboard/gnosis": "^2.0.5",
Expand Down
6 changes: 3 additions & 3 deletions packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/react",
"version": "2.2.4",
"version": "2.2.5-alpha.1",
"description": "A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down Expand Up @@ -62,8 +62,8 @@
"typescript": "^4.5.5"
},
"dependencies": {
"@web3-onboard/common": "^2.1.6",
"@web3-onboard/core": "^2.5.0",
"@web3-onboard/common": "^2.1.7-alpha.1",
"@web3-onboard/core": "^2.6.0-alpha.1",
"use-sync-external-store": "1.0.0"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@web3-onboard/vue",
"version": "2.1.4",
"version": "2.1.5-alpha.1",
"description": "A collection of Vue Composables for integrating Web3-Onboard in to a Vue or Nuxt project. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
"keywords": [
"Ethereum",
Expand Down Expand Up @@ -62,8 +62,8 @@
"dependencies": {
"@vueuse/core": "^8.4.2",
"@vueuse/rxjs": "^8.2.0",
"@web3-onboard/common": "^2.1.6",
"@web3-onboard/core": "^2.5.0",
"@web3-onboard/common": "^2.1.7-alpha.1",
"@web3-onboard/core": "^2.6.0-alpha.1",
"vue-demi": "^0.12.4"
},
"peerDependencies": {
Expand Down