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

chore: update web3 to 4.8 #2509

Merged
merged 9 commits into from
May 17, 2024
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
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"sha3": "2.1.4",
"svelte": "4.2.15",
"svelte-i18n": "4.0.0",
"web3": "1.10.4",
"web3": "4.8.0",
"zxcvbn": "4.4.2"
},
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function generateBaseEvmActivity(
recipient: string
gasUsed: number
blockNumber: number
estimatedGas?: number
estimatedGas?: bigint
gasPrice?: BigIntLike
timestamp?: number
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ async function generateBaseEvmActivityFromBlockscoutTransaction(
recipient: blockscoutTransaction.to.hash.toLowerCase(),
from: blockscoutTransaction.from.hash.toLowerCase(),
gasUsed: Number(blockscoutTransaction.gas_used),
estimatedGas: localTransaction?.estimatedGas,
estimatedGas: localTransaction?.estimatedGas ? BigInt(localTransaction.estimatedGas) : undefined,
gasPrice: blockscoutTransaction.gas_price,
transactionHash: blockscoutTransaction.hash,
timestamp: new Date(blockscoutTransaction.timestamp).getTime(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function generateEvmActivityFromLocalEvmTransaction(
recipient: to?.toString().toLowerCase(),
from: from?.toString().toLowerCase(),
gasUsed: Number(gasUsed),
estimatedGas,
estimatedGas: estimatedGas ? BigInt(estimatedGas) : undefined,
gasPrice: gasPrice ?? undefined,
transactionHash,
timestamp,
Expand Down Expand Up @@ -63,7 +63,7 @@ export async function generateEvmActivityFromLocalEvmTransaction(
recipient: parsedData.recipientAddress ?? to?.toString().toLowerCase(),
from: from?.toString().toLowerCase(),
gasUsed: Number(gasUsed),
estimatedGas,
estimatedGas: estimatedGas ? BigInt(estimatedGas) : undefined,
gasPrice: gasPrice ?? undefined,
transactionHash,
timestamp,
Expand Down
6 changes: 4 additions & 2 deletions packages/shared/src/lib/core/layer-2/abis/erc165.abi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Abi, AbiType, StateMutabilityType } from '@core/layer-2'
import { ContractAbi } from 'web3'
import { AbiType } from '../enums/abi-type.enum'
import { StateMutabilityType } from '../enums/state-mutability-type.enum'

export const ERC165_ABI: Abi = [
export const ERC165_ABI: ContractAbi = [
// / @notice Query if a contract implements an interface
// / @param interfaceID The interface identifier, as specified in ERC-165
// / @dev Interface identification is specified in ERC-165. This function
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/src/lib/core/layer-2/abis/erc20.abi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractAbi } from 'web3'
import { AbiType, StateMutabilityType } from '../enums'
import { Abi } from '../types'

export const ERC20_ABI: Abi = [
export const ERC20_ABI: ContractAbi = [
{
constant: true,
inputs: [],
Expand Down
18 changes: 12 additions & 6 deletions packages/shared/src/lib/core/layer-2/abis/erc721.abi.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { AbiType, StateMutabilityType } from '../enums'
import { Abi } from '../types'
import { ContractAbi } from 'web3'
import { ERC165_ABI } from './erc165.abi'
import { AbiType } from '../enums/abi-type.enum'
import { StateMutabilityType } from '../enums/state-mutability-type.enum'

const ERC721_BASE_ABI: Abi = [
const ERC721_BASE_ABI: ContractAbi = [
{
inputs: [],
payable: false,
Expand Down Expand Up @@ -400,7 +401,7 @@ const ERC721_BASE_ABI: Abi = [
},
]

export const ERC721_METADATA_ABI: Abi = [
export const ERC721_METADATA_ABI: ContractAbi = [
// / @notice A descriptive name for a collection of NFTs in this contract
// /
// / function name() external view returns (string _name);
Expand Down Expand Up @@ -468,7 +469,7 @@ export const ERC721_METADATA_ABI: Abi = [
},
]

export const ERC721_ENUMERABLE_ABI: Abi = [
export const ERC721_ENUMERABLE_ABI: ContractAbi = [
// / @notice Count NFTs tracked by this contract
// / @return A count of valid NFTs tracked by this contract, where each one of
// / them has an assigned and queryable owner not equal to the zero address
Expand Down Expand Up @@ -556,4 +557,9 @@ export const ERC721_ENUMERABLE_ABI: Abi = [
},
]

export const ERC721_ABI: Abi = [...ERC165_ABI, ...ERC721_BASE_ABI, ...ERC721_METADATA_ABI, ...ERC721_ENUMERABLE_ABI]
export const ERC721_ABI: ContractAbi = [
...ERC165_ABI,
...ERC721_BASE_ABI,
...ERC721_METADATA_ABI,
...ERC721_ENUMERABLE_ABI,
]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContractAbi } from 'web3'
import { AbiType, StateMutabilityType } from '../enums'
import { Abi } from '../types'

export const ISC_SANDBOX_ABI: Abi = [
export const ISC_SANDBOX_ABI: ContractAbi = [
{
inputs: [
{ internalType: 'address', name: 'target', type: 'address' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { SendFlowType } from '@core/wallet/enums'
import { getLayer2AccountBalanceForToken } from '../stores'
import { FALLBACK_ESTIMATED_GAS, GAS_LIMIT_MULTIPLIER } from '../constants'
import { FALLBACK_ESTIMATED_GAS } from '../constants'
import { IEvmNetwork, calculateGasFee } from '@core/network'
import { addGasBuffer } from '../utils'

export async function canAccountMakeEvmTransaction(
accountIndex: number,
network: IEvmNetwork,
sendFlowType: SendFlowType | undefined
): Promise<boolean> {
const baseTokenAccountBalance = getLayer2AccountBalanceForToken(accountIndex, network.id)
const gasLimit = Math.floor(
FALLBACK_ESTIMATED_GAS[sendFlowType ?? SendFlowType.BaseCoinTransfer] * GAS_LIMIT_MULTIPLIER
)
const gasLimit = addGasBuffer(FALLBACK_ESTIMATED_GAS[sendFlowType ?? SendFlowType.BaseCoinTransfer])

const gasPrice = await network.getRequiredGasPrice()
if (gasPrice === undefined) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function getGasFeeForLayer1ToLayer2Transaction(sendFlowParameters:
return gasFeeEstimate
} catch (err) {
console.error(err)
return BigInt(FALLBACK_ESTIMATED_GAS[sendFlowParameters.type])
return FALLBACK_ESTIMATED_GAS[sendFlowParameters.type]
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { IscChain } from '@core/network'
import { getSelectedAccount } from '@core/account/stores'
import { ContractType } from '@core/layer-2/enums'
import { ISC_MAGIC_CONTRACT_ADDRESS } from '@core/layer-2/constants'
import { handleError } from '@core/error/handlers'
import { TransferredAsset } from '../types'
import { evmAddressToAgentId, getAgentBalanceParameters, getSmartContractHexName } from '../helpers'
import { buildAssetAllowance } from '../utils'
import { ISC_SANDBOX_ABI } from '..'

export function getIscTransferSmartContractData(
recipientAddress: string,
Expand All @@ -26,7 +26,7 @@ export function getIscTransferSmartContractData(
const parameters = getAgentBalanceParameters(agentId)
const allowance = buildAssetAllowance(iscChain, transferredAsset)

const contract = iscChain.getContract(ContractType.IscMagic, ISC_MAGIC_CONTRACT_ADDRESS)
const contract = iscChain.getContract(ISC_SANDBOX_ABI, ISC_MAGIC_CONTRACT_ADDRESS)
const method = contract.methods.call(accountsCoreContract, transferAllowanceTo, parameters, allowance)
return method.encodeABI() ?? ''
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { HEX_PREFIX } from '@core/utils'
import type { SendFlowParameters } from '@core/wallet/types'
import { SpecialStream } from '../classes'
import { ACCOUNTS_CONTRACT, EXTERNALLY_OWNED_ACCOUNT, GAS_LIMIT_MULTIPLIER, TRANSFER_ALLOWANCE } from '../constants'
import { ACCOUNTS_CONTRACT, EXTERNALLY_OWNED_ACCOUNT, TRANSFER_ALLOWANCE } from '../constants'
import { encodeAddress, encodeAssetAllowance, encodeSmartContractParameters } from '../helpers'
import { getIscChain } from '@core/network'
import { addGasBuffer } from '../utils'

export function getLayer2MetadataForTransfer(sendFlowParameters: SendFlowParameters): string {
const metadataStream = new SpecialStream()
Expand All @@ -18,20 +19,20 @@ export function getLayer2MetadataForTransfer(sendFlowParameters: SendFlowParamet

const gasFee = sendFlowParameters.gasFee ?? BigInt(0)
const gasPerToken = iscChain.getMetadata()?.gasFeePolicy.gasPerToken
let gasLimit: number
let gasLimit: bigint
if (gasPerToken) {
// More information can be found here: https://wiki.iota.org/isc/reference/core-contracts/governance/#ratio32
gasLimit = (Number(gasFee) * gasPerToken['a']) / gasPerToken['b']
gasLimit = (gasFee * BigInt(gasPerToken['a'])) / BigInt(gasPerToken['b'])
} else {
gasLimit = Number(gasFee) * GAS_LIMIT_MULTIPLIER
gasLimit = addGasBuffer(gasFee)
}

metadataStream.writeUInt8('senderContract', EXTERNALLY_OWNED_ACCOUNT)

metadataStream.writeUInt32('targetContract', ACCOUNTS_CONTRACT)
metadataStream.writeUInt32('contractFunction', TRANSFER_ALLOWANCE)
// Gas budget is the ISC equivalent of gas limit in ethereum and what we use throughout the code
metadataStream.writeUInt64SpecialEncoding('gasLimit', BigInt(Math.floor(gasLimit)))
metadataStream.writeUInt64SpecialEncoding('gasLimit', gasLimit)

const smartContractParameters = Object.entries({ a: encodedAddress })
const parameters = encodeSmartContractParameters(smartContractParameters)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
import { ERC20_ABI, ERC721_ABI, ISC_SANDBOX_ABI } from '../abis'
import { ContractType } from '../enums'
import { Abi } from '../types'

export const EVM_CONTRACT_ABIS: { [key in ContractType]: Abi } = {
[ContractType.Erc20]: ERC20_ABI,
[ContractType.Erc721]: ERC721_ABI,
[ContractType.IscMagic]: ISC_SANDBOX_ABI,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { SendFlowType } from '@core/wallet/enums'
* Base Token Transfer: < 24200 glow
* Native Token Transfer: < 24350 glow
* NFT Transfer: < 24700 glow */
export const FALLBACK_ESTIMATED_GAS: { [key in SendFlowType]: number } = {
[SendFlowType.BaseCoinTransfer]: 10_000,
[SendFlowType.TokenTransfer]: 10_000,
[SendFlowType.NftTransfer]: 50_000,
[SendFlowType.TokenUnwrap]: 53_892,
[SendFlowType.NftUnwrap]: 72_307,
export const FALLBACK_ESTIMATED_GAS: { [key in SendFlowType]: bigint } = {
[SendFlowType.BaseCoinTransfer]: BigInt(10_000),
[SendFlowType.TokenTransfer]: BigInt(10_000),
[SendFlowType.NftTransfer]: BigInt(50_000),
[SendFlowType.TokenUnwrap]: BigInt(53_892),
[SendFlowType.NftUnwrap]: BigInt(72_307),
}

This file was deleted.

2 changes: 0 additions & 2 deletions packages/shared/src/lib/core/layer-2/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ export * from './contract-functions.constant'
export * from './empty-buffer.constants'
export * from './erc20-tokens-poll-interval.constant'
export * from './ethereum-address-length.constant'
export * from './evm-contract-abis.constant'
export * from './externally-owned-account-type-id.constant'
export * from './externally-owned-account.constant'
export * from './fallback-estimated-gas.constant'
export * from './gas-limit-multiplier.constant'
export * from './isc-magic-contract-address.constant'
export * from './layer2-tokens-poll-interval.constant'
export * from './target-contracts.constant'
Expand Down

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/src/lib/core/layer-2/enums/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from './abi-type.enum'
export * from './allowance.enum'
export * from './asset-type.enum'
export * from './contract-type.enum'
export * from './erc721-interface-id.enum'
export * from './evm-error-message.enum'
export * from './gas-speed.enum'
Expand Down
3 changes: 0 additions & 3 deletions packages/shared/src/lib/core/layer-2/types/abi.type.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/shared/src/lib/core/layer-2/types/contract.type.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { TypedTxData } from '@ethereumjs/tx'

export type EvmTransactionData = TypedTxData & { estimatedGas?: number; timestamp?: number }
export type EvmTransactionData = TypedTxData & { estimatedGas?: bigint; timestamp?: number }
2 changes: 0 additions & 2 deletions packages/shared/src/lib/core/layer-2/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export * from './abi.type'
export * from './contract.type'
export * from './evm-transaction-options.type'
export * from './evm-transaction-data.type'
export * from './layer-2-account-balance.type'
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/src/lib/core/layer-2/utils/addGasBuffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const GAS_LIMIT_MULTIPLIER = BigInt(110)

export function addGasBuffer(estimatedGas: bigint): bigint {
return (estimatedGas * GAS_LIMIT_MULTIPLIER) / BigInt(100)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IEvmNetwork } from '@core/network'
import { Converter, HEX_PREFIX } from '@core/utils'
import { GAS_LIMIT_MULTIPLIER } from '../constants'
import { EvmTransactionData } from '../types'
import { addGasBuffer } from './addGasBuffer'

export async function buildEvmTransactionData(
evmNetwork: IEvmNetwork,
Expand All @@ -19,7 +19,7 @@ export async function buildEvmTransactionData(
data,
value: amount.toString(),
})
const gasLimit = Math.floor(estimatedGas * GAS_LIMIT_MULTIPLIER)
const gasLimit = addGasBuffer(estimatedGas)

const to = destinationAddress

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IAccountState } from '@core/account'
import { calculateAndAddPersistedTokenBalanceChange, calculateAndAddPersistedNftBalanceChange } from '@core/activity'
import { ISC_MAGIC_CONTRACT_ADDRESS } from '@core/layer-2/constants'
import { ContractType } from '@core/layer-2/enums'
import { getSmartContractHexName, evmAddressToAgentId, getAgentBalanceParameters } from '@core/layer-2/helpers'
import { IscChain } from '@core/network'
import { isIrc27Nft, getNftsFromNftIds, Nft } from '@core/nfts'
Expand All @@ -12,6 +11,7 @@ import { getOrRequestTokenFromPersistedTokens } from '@core/token/actions'
import { Converter, PartialWithId } from '@core/utils'
import { KeyValue } from '@ui'
import { get } from 'svelte/store'
import { ISC_SANDBOX_ABI } from '../abis'

export async function fetchIscAssetsForAccount(
profileId: string,
Expand Down Expand Up @@ -40,7 +40,7 @@ async function getL2NativeTokenBalancesForAddress(evmAddress: string, iscChain:
const agentID = evmAddressToAgentId(evmAddress, iscChain.aliasAddress)
const parameters = getAgentBalanceParameters(agentID)
try {
const contract = iscChain.getContract(ContractType.IscMagic, ISC_MAGIC_CONTRACT_ADDRESS)
const contract = iscChain.getContract(ISC_SANDBOX_ABI, ISC_MAGIC_CONTRACT_ADDRESS)
const nativeTokenResult = (await contract.methods
.callView(accountsCoreContract, getBalanceFunc, parameters)
.call()) as { items: KeyValue<string>[] }
Expand All @@ -67,7 +67,7 @@ async function fetchL2Irc27Nfts(
const agentID = evmAddressToAgentId(evmAddress, iscChain.aliasAddress)
const parameters = getAgentBalanceParameters(agentID)
try {
const contract = iscChain.getContract(ContractType.IscMagic, ISC_MAGIC_CONTRACT_ADDRESS)
const contract = iscChain.getContract(ISC_SANDBOX_ABI, ISC_MAGIC_CONTRACT_ADDRESS)
const nftResult = (await contract.methods
.callView(accountsCoreContract, getBalanceFunc, parameters)
.call()) as { items: KeyValue<string>[] }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { IEvmNetwork } from '@core/network/interfaces'
import { ContractType } from '../enums'
import { IToken } from '@core/token/interfaces'
import { ERC20_ABI } from '../abis'

export function getErc20TransferSmartContractData(
recipientAddress: string,
token: IToken,
amount: bigint,
evmNetwork: IEvmNetwork
): string {
const contract = evmNetwork.getContract(ContractType.Erc20, token.id)
const contract = evmNetwork.getContract(ERC20_ABI, token.id)
return contract.methods.transfer(recipientAddress, String(amount)).encodeABI() ?? ''
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IEvmNetwork } from '@core/network/interfaces'
import { ContractType } from '../enums'
import { IErc721Nft } from '@core/nfts'
import { ERC721_ABI } from '../abis'

export function getErc721TransferSmartContractData(
originAddress: string,
Expand All @@ -11,6 +11,6 @@ export function getErc721TransferSmartContractData(
const nftAddress = nft.contractMetadata?.address ?? ''
const nftTokenId = nft.tokenId ?? ''

const contract = evmNetwork.getContract(ContractType.Erc721, nftAddress)
const contract = evmNetwork.getContract(ERC721_ABI, nftAddress)
return contract.methods.safeTransferFrom(originAddress, recipientAddress, nftTokenId).encodeABI() ?? ''
}
Loading
Loading