Skip to content

Commit

Permalink
fix(unlock-app): post ethers6 migration - more cleanup (#14039)
Browse files Browse the repository at this point in the history
* monkey patch bigint serializing

* prevent erc20 decimals error

* consistent bigint on subscriptions operations

* fix bigint in approvals

* condtional check on `recurringPayments`

* better syntax

* use `DEFAULT_TOKEN_DECIMALS` constant
  • Loading branch information
clemsos authored Jun 13, 2024
1 parent 3bbec5f commit a769d5b
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions locksmith/src/operations/subscriptionOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ export const getSubscriptionsForLockByOwner = async ({
})
}

// https://links.ethers.org/v5-errors-NUMERIC_FAULT-division-by-zero
const possibleRenewals =
// https://links.ethers.org/v5-errors-NUMERIC_FAULT-division-by-zero
BigInt(price) > 0 ? BigInt(userBalance) / price : BigInt(0)
BigInt(price) > BigInt(0) ? BigInt(userBalance) / BigInt(price) : BigInt(0)

// Add the default crypto subscription details.
const cryptoSubscription: Subscription = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,15 @@ export default async function getPurchaseKeysArguments({
}

// calculate total price for all keys
const totalPrice = keyPrices.reduce(
(total, kp) => total + kp,
utils.bigNumberify(0)
)

const totalPrice = keyPrices.reduce((total, kp) => total + kp, BigInt(0))
let totalAmountToApprove = totalApproval

if (!totalAmountToApprove) {
// total amount to approve
totalAmountToApprove = recurringPayments
? keyPrices // for reccuring payments
.map((kp, i) => kp * recurringPayments[i])
.reduce((total, approval) => total + approval, utils.bigNumberify(0))
.map((kp, i) => kp * recurringPayments.map(BigInt)[i])
.reduce((total, approval) => total + approval, BigInt(0))
: totalPrice
}

Expand Down
2 changes: 2 additions & 0 deletions packages/unlock-js/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const UNLIMITED_KEYS_COUNT = -1

export const ZERO = ethers.ZeroAddress

export const DEFAULT_TOKEN_DECIMALS = 18

export default {
MAX_UINT,
UNLIMITED_KEYS_COUNT,
Expand Down
3 changes: 2 additions & 1 deletion packages/unlock-js/src/erc20.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ethers } from 'ethers'
import utils from './utils'
import erc20abi from './erc20abi'
import { DEFAULT_TOKEN_DECIMALS } from './constants'

// The SAI contract does not have the symbol method implemented correctly
const SAI_ADDRESS = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359'.toLowerCase()
Expand Down Expand Up @@ -42,7 +43,7 @@ export async function getErc20Decimals(
} catch (e) {
console.error(e)
/** Some ERC20 contracts do not have the right decimals method. Defaults to 18 */
return 18
return DEFAULT_TOKEN_DECIMALS
}
return utils.toNumber(decimals)
}
Expand Down
11 changes: 8 additions & 3 deletions packages/unlock-js/src/web3Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getErc20BalanceForAddress,
getErc20Decimals,
} from './erc20'
import { ETHERS_MAX_UINT } from './constants'
import { ETHERS_MAX_UINT, ZERO, DEFAULT_TOKEN_DECIMALS } from './constants'
import { TransactionOptions, WalletServiceCallback } from './types'
import { passwordHookAbi } from './abis/passwordHookAbi'
import { discountCodeHookAbi } from './abis/discountCodeHookAbi'
Expand Down Expand Up @@ -432,8 +432,13 @@ export default class Web3Service extends UnlockService {

async getTokenDecimals(contractAddress: string, network: number) {
const provider = this.providerForNetwork(network)
const decimals = await getErc20Decimals(contractAddress, provider)
return decimals
if (contractAddress !== ZERO) {
const decimals = await getErc20Decimals(contractAddress, provider)
return decimals
} else {
// default for native tokens
return DEFAULT_TOKEN_DECIMALS
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion unlock-app/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ErrorFallback } from '~/components/interface/ErrorFallback'
import { queryClient } from '~/config/queryClient'
import { SessionProvider } from '~/hooks/useSession'
import { ConnectModalProvider } from '~/hooks/useConnectModal'

import '~/utils/bigint'
import { Inter } from 'next/font/google'

const inter = Inter({
Expand Down
6 changes: 6 additions & 0 deletions unlock-app/src/utils/bigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* eslint-disable no-extend-native */

// @ts-expect-error 🚧 This is a workaround for ethers 6 support
BigInt.prototype.toJSON = function () {
return this.toString()
}

0 comments on commit a769d5b

Please sign in to comment.