Skip to content

Commit

Permalink
chore(store): upgrade web3-eth api
Browse files Browse the repository at this point in the history
refactor(`eth-utils`): remove `BatchQueue` (web3/web3.js#6224)
  • Loading branch information
bludnic committed Oct 17, 2023
1 parent 18c24c7 commit 59db8b9
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 178 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"vuex-persist": "^3.1.3",
"web3-eth": "^4.2.0",
"web3-eth-accounts": "^4.0.6",
"web3-eth-contract": "^4.1.0",
"web3-utils": "^4.0.6"
},
"devDependencies": {
Expand Down
35 changes: 0 additions & 35 deletions src/lib/eth-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,38 +106,3 @@ export function toFraction(amount, decimals, separator = '.') {

return whole + (fraction ? separator + fraction : '')
}

export class BatchQueue {
constructor(createBatchRequest) {
this._createBatchRequest = createBatchRequest
this._queue = []
this._timer = null
}

enqueue(key, supplier) {
if (typeof supplier !== 'function') return
if (this._queue.some((x) => x.key === key)) return

const requests = supplier()
this._queue.push({ key, requests: Array.isArray(requests) ? requests : [requests] })
}

start() {
this.stop()
this._timer = setInterval(() => this._execute(), 2000)
}

stop() {
clearInterval(this._timer)
}

_execute() {
const requests = this._queue.splice(0, 20)
if (!requests.length) return

const batch = this._createBatchRequest()
requests.forEach((x) => x.requests.forEach((r) => batch.add(r)))

batch.execute()
}
}
19 changes: 13 additions & 6 deletions src/store/modules/erc20/erc20-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import abiDecoder from 'abi-decoder'

import * as ethUtils from '../../../lib/eth-utils'
import { FetchStatus, INCREASE_FEE_MULTIPLIER } from '@/lib/constants'
import EthContract from 'web3-eth-contract'
import Erc20 from './erc20.abi.json'
import createActions from '../eth-base/eth-base-actions'
import getEndpointUrl from '@/lib/getEndpointUrl'

/** Timestamp of the most recent status update */
let lastStatusUpdate = 0
Expand All @@ -14,7 +16,7 @@ const STATUS_INTERVAL = 25000
abiDecoder.addABI(Erc20)

const initTransaction = (api, context, ethAddress, amount, increaseFee) => {
const contract = new api.Contract(Erc20, context.state.contractAddress)
const contract = new EthContract(Erc20, context.state.contractAddress)

const transaction = {
from: context.state.address,
Expand Down Expand Up @@ -52,17 +54,17 @@ const parseTransaction = (context, tx) => {
// Why comparing to eth.actions, there is no fee and status?
hash: tx.hash,
senderId: tx.from,
blockNumber: tx.blockNumber,
blockNumber: Number(tx.blockNumber),
amount,
recipientId,
gasPrice: +(tx.gasPrice || tx.effectiveGasPrice)
gasPrice: Number(tx.gasPrice || tx.effectiveGasPrice)
}
}

return null
}

const createSpecificActions = (api, queue) => ({
const createSpecificActions = (api) => ({
updateBalance: {
root: true,
async handler({ state, commit }, payload = {}) {
Expand All @@ -71,7 +73,9 @@ const createSpecificActions = (api, queue) => ({
}

try {
const contract = new api.Contract(Erc20, state.contractAddress)
const contract = new EthContract(Erc20, state.contractAddress)
const endpoint = getEndpointUrl('ETH')
contract.setProvider(endpoint)
const rawBalance = await contract.methods.balanceOf(state.address).call()
const balance = Number(ethUtils.toFraction(rawBalance, state.decimals))

Expand All @@ -88,7 +92,10 @@ const createSpecificActions = (api, queue) => ({
updateStatus(context) {
if (!context.state.address) return

const contract = new api.Contract(Erc20, context.state.contractAddress)
const contract = new EthContract(Erc20, context.state.contractAddress)
const endpoint = getEndpointUrl('ETH')
contract.setProvider(endpoint)

contract.methods
.balanceOf(context.state.address)
.call()
Expand Down
Loading

0 comments on commit 59db8b9

Please sign in to comment.