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

feat: update web3 version #1430

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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/apps/faucet-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"express": "^4.18.1",
"express-rate-limit": "^7.1.3",
"node-cache": "^5.1.2",
"web3": "^1.10.2"
"web3": "^4.3.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
Expand Down
62 changes: 34 additions & 28 deletions packages/apps/faucet-server/src/services/web3.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* TODO: Remove eslint rule for explict any after the PR is merged
* Known issue for web3 v4 unknown ABI
* https://github.com/web3/web3.js/pull/6636
*/

import dotenv from 'dotenv';
dotenv.config({ path: `.env.development` });
import HMToken from '@human-protocol/core/artifacts/contracts/HMToken.sol//HMToken.json';
import HMToken from '@human-protocol/core/artifacts/contracts/HMToken.sol/HMToken.json';
import { describe, expect, it } from '@jest/globals';
import { checkFaucetBalance, getFaucetBalance, sendFunds } from './web3';
import { Contract } from 'web3-eth-contract';
import Web3 from 'web3';

let token: Contract;
let token: Contract<typeof HMToken.abi>;

const web3 = new Web3('http://127.0.0.1:8549');
const owner = web3.eth.accounts.privateKeyToAccount(
Expand All @@ -17,20 +25,18 @@ const externalUser = '0x23618e81E3f5cdF7f54C3d65f7FBc0aBf5B21E8f';

describe('Faucet', () => {
beforeEach(async () => {
const tokenContract = new web3.eth.Contract(HMToken.abi as []);
token = await tokenContract
.deploy({
data: HMToken.bytecode,
arguments: [
web3.utils.toWei('100000', 'ether'),
'Human Token',
18,
'HMT',
],
})
.send({
from: owner.address,
});
const tokenContract = new web3.eth.Contract(HMToken.abi);
token = await (tokenContract.deploy as any)({
data: HMToken.bytecode,
arguments: [
web3.utils.toWei('100000', 'ether'),
'Human Token',
18,
'HMT',
],
}).send({
from: owner.address,
});
});

it('Check faucet balance before send', async () => {
Expand All @@ -44,34 +50,34 @@ describe('Faucet', () => {
web3,
token.options.address
);
const oldUserBalance = await token.methods.balanceOf(externalUser).call();
const oldUserBalance = await (token.methods.balanceOf as any)(
externalUser
).call();

await sendFunds(web3, token.options.address, externalUser);

const newFaucetBalance = await getFaucetBalance(
web3,
token.options.address
);
const newUserBalance = await token.methods.balanceOf(externalUser).call();
const newUserBalance = await (token.methods.balanceOf as any)(
externalUser
).call();

expect(Number(newFaucetBalance)).toBe(Number(oldFaucetBalance) - 10);
expect(oldUserBalance).toBe(
web3.utils
.toBN(newUserBalance)
.sub(web3.utils.toBN(web3.utils.toWei('10', 'ether')))
.toString()
web3.utils.toBigInt(newUserBalance) -
web3.utils.toBigInt(web3.utils.toWei('10', 'ether'))
);
});

it('Check balance', async () => {
expect(await checkFaucetBalance(web3, token.options.address)).toBeTruthy();

await token.methods
.transfer(
externalUser,
await token.methods.balanceOf(owner.address).call()
)
.send({ from: owner.address });
await (token.methods.transfer as any)(
externalUser,
await (token.methods.balanceOf as any)(owner.address).call()
).send({ from: owner.address });

expect(await checkFaucetBalance(web3, token.options.address)).toBeFalsy();
});
Expand Down
63 changes: 31 additions & 32 deletions packages/apps/faucet-server/src/services/web3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

/**
* TODO: Remove eslint rule for explict any after the PR is merged
* Known issue for web3 v4 unknown ABI
* https://github.com/web3/web3.js/pull/6636
*/

import hmtAbi from '@human-protocol/core/abis/HMToken.json';
import Web3 from 'web3';
import { HttpProvider } from 'web3-core';

import { ChainId } from '../constants/networks';
import { ChainId, FAUCET_NETWORKS } from '../constants/networks';
import {
AnonymousParams,
AnonymousPoW,
Expand All @@ -29,9 +36,9 @@ export const getFaucetBalance = async (
faucetAddress
)
return await web3.eth.getBalance(faucetAddress);
const HMT = new web3.eth.Contract(hmtAbi as [], hmtAddress);
const HMT = new web3.eth.Contract(hmtAbi, hmtAddress);
const balance = web3.utils.fromWei(
await HMT.methods.balanceOf(web3.eth.defaultAccount).call(),
await (HMT.methods.balanceOf as any)(web3.eth.defaultAccount).call(),
'ether'
);
return balance;
Expand All @@ -43,16 +50,15 @@ export const sendFunds = async (
toAddress: string,
faucetAddress?: string
): Promise<string | undefined> => {
const HMT = new web3.eth.Contract(hmtAbi as [], hmtAddress);
const HMT = new web3.eth.Contract(hmtAbi, hmtAddress);
let txHash = '';
try {
if (
(await web3.eth.getChainId()).toString() === ChainId.SKALE.toString() &&
faucetAddress
) {
const currentProvider = web3.currentProvider as HttpProvider;
const skalePOW = new AnonymousPoW({
rpcUrl: currentProvider.host,
rpcUrl: FAUCET_NETWORKS[ChainId.SKALE].rpcUrl,
} as AnonymousParams);
const txParams = {
to: faucetAddress,
Expand All @@ -61,19 +67,19 @@ export const sendFunds = async (
const receipt = await (await skalePOW.send(txParams)).wait();
txHash = receipt.transactionHash;
} else {
const gasNeeded = await HMT.methods
.transfer(
toAddress,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
)
.estimateGas({ from: web3.eth.defaultAccount });
const gasNeeded = await (HMT.methods.transfer as any)(
toAddress,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
).estimateGas({ from: web3.eth.defaultAccount });
const gasPrice = await web3.eth.getGasPrice();
const receipt = await HMT.methods
.transfer(
toAddress,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
)
.send({ from: web3.eth.defaultAccount, gas: gasNeeded, gasPrice });
const receipt = await (HMT.methods.transfer as any)(
toAddress,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
).send({
from: web3.eth.defaultAccount,
gas: gasNeeded.toString(),
gasPrice: gasPrice.toString(),
});
txHash = receipt.transactionHash;
}
} catch (err) {
Expand Down Expand Up @@ -102,22 +108,15 @@ export const checkFaucetBalance = async (
)
return true;

const HMT = new web3.eth.Contract(hmtAbi as [], hmtAddress);
const gasNeeded = await HMT.methods
.transfer(
web3.eth.defaultAccount,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
)
.estimateGas({ from: web3.eth.defaultAccount });
const HMT = new web3.eth.Contract(hmtAbi, hmtAddress);
const gasNeeded = await (HMT.methods.transfer as any)(
web3.eth.defaultAccount,
Web3.utils.toWei(process.env.DAILY_LIMIT as string, 'ether')
).estimateGas({ from: web3.eth.defaultAccount });
const gasPrice = await web3.eth.getGasPrice();
const balance = await web3.eth.getBalance(web3.eth.defaultAccount);

if (
web3.utils
.toBN(balance)
.cmp(web3.utils.toBN(gasNeeded).mul(web3.utils.toBN(gasPrice))) === -1
)
return false;
if (balance < web3.utils.toBigInt(gasNeeded) * gasPrice) return false;

return true;
};
Loading