Skip to content

Commit

Permalink
devop: temp update to web-eth web3-util
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Mar 21, 2024
1 parent 775b887 commit 5488539
Show file tree
Hide file tree
Showing 25 changed files with 332 additions and 283 deletions.
4 changes: 2 additions & 2 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
"vue-router": "4.3.0",
"vue3-lottie": "^3.3.0",
"vuedraggable": "^4.1.0",
"web3-eth": "^1.10.4",
"web3-utils": "^1.10.4",
"web3-eth": "^4.5.0",
"web3-utils": "^4.2.1",
"yarn": "^1.22.22",
"zxcvbn": "^4.4.2"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
"rango-sdk-basic": "^0.1.46",
"reconnecting-websocket": "^4.4.0",
"uuid": "^9.0.1",
"web3-eth": "^1.10.4",
"web3-utils": "^1.10.4",
"web3-eth": "^4.5.0",
"web3-eth-contract": "^4.2.0",
"web3-utils": "^4.2.1",
"ws": "^8.16.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/common/estimateGasList.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fetch from "node-fetch";
import { numberToHex, toBN } from "web3-utils";
import { numberToHex, toBN } from "@enkryptcom/utils";
import { EVMTransaction, SupportedNetworkName } from "../types";
import { GAS_LIMITS } from "../configs";

Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkNames } from "@enkryptcom/types";
import { numberToHex } from "web3-utils";
import { numberToHex } from "@enkryptcom/utils";
import { ProviderName, SupportedNetworkName, WalletIdentifier } from "./types";

const FEE_CONFIGS = {
Expand Down
3 changes: 1 addition & 2 deletions packages/swap/src/providers/changelly/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type Web3Eth from "web3-eth";
import { v4 as uuidv4 } from "uuid";
import fetch from "node-fetch";
import { fromBase, toBase } from "@enkryptcom/utils";
import { numberToHex, toBN } from "web3-utils";
import { fromBase, toBase, numberToHex, toBN } from "@enkryptcom/utils";
import {
getQuoteOptions,
MinMaxResponse,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/providers/oneInch/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Web3Eth from "web3-eth";
import { numberToHex, toBN } from "web3-utils";
import { numberToHex, toBN } from "@enkryptcom/utils";
import fetch from "node-fetch";
import {
EVMTransaction,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/providers/paraswap/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Web3Eth from "web3-eth";
import { numberToHex, toBN } from "web3-utils";
import { numberToHex, toBN } from "@enkryptcom/utils";
import fetch from "node-fetch";
import {
EVMTransaction,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/providers/rango/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Web3Eth from "web3-eth";
import { numberToHex, toBN } from "web3-utils";
import { numberToHex, toBN } from "@enkryptcom/utils";
import {
EvmTransaction as RangoEvmTransaction,
RangoClient,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/providers/zerox/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type Web3Eth from "web3-eth";
import { numberToHex, toBN } from "web3-utils";
import { numberToHex, toBN } from "@enkryptcom/utils";
import fetch from "node-fetch";
import {
EVMTransaction,
Expand Down
3 changes: 1 addition & 2 deletions packages/swap/src/swapToken.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { fromBase, toBase } from "@enkryptcom/utils";
import { fromBase, toBase, toBN } from "@enkryptcom/utils";
import BigNumber from "bignumber.js";
import { toBN } from "web3-utils";
import { BN, TokenType } from "./types";

class SwapToken {
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NetworkNames, SignerType } from "@enkryptcom/types";
import type { toBN } from "web3-utils";
import type { toBN } from "@enkryptcom/utils";
import type Web3Eth from "web3-eth";

// eslint-disable-next-line no-shadow
Expand Down
6 changes: 0 additions & 6 deletions packages/swap/src/types/shims.d.ts

This file was deleted.

14 changes: 7 additions & 7 deletions packages/swap/src/utils/approvals.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Web3Eth from "web3-eth";
import { toBN } from "web3-utils";
import Web3Contract from "web3-eth-contract";
import { toBN } from "@enkryptcom/utils";
import Erc20abi from "./abi/erc20";
import { BN, EVMTransaction, TokenType, TransactionType } from "../types";
import { GAS_LIMITS } from "../configs";
Expand All @@ -13,9 +14,10 @@ const getAllowance = (options: {
spender: string;
web3eth: Web3Eth;
}): Promise<string> => {
const contract = new options.web3eth.Contract(
const contract = new Web3Contract(
Erc20abi as any,
options.contract
options.contract,
options.web3eth
);
return contract.methods.allowance(options.owner, options.spender).call();
};
Expand All @@ -26,8 +28,7 @@ const getTransfer = (options: {
to: string;
value: string;
}): EVMTransaction => {
const web3Eth = new Web3Eth();
const contract = new web3Eth.Contract(Erc20abi as any);
const contract = new Web3Contract(Erc20abi as any);
return {
from: options.from,
data: contract.methods.transfer(options.to, options.value).encodeABI(),
Expand All @@ -44,8 +45,7 @@ const getApproval = (options: {
spender: string;
value: string;
}): EVMTransaction => {
const web3Eth = new Web3Eth();
const contract = new web3Eth.Contract(Erc20abi as any);
const contract = new Web3Contract(Erc20abi as any);
return {
from: options.from,
data: contract.methods.approve(options.spender, options.value).encodeABI(),
Expand Down
3 changes: 2 additions & 1 deletion packages/swap/tests/fixtures/mainnet/configs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { NetworkNames } from "@enkryptcom/types";
import { isAddress, toBN } from "web3-utils";
import { isAddress } from "web3-utils";
import { toBN } from "@enkryptcom/utils";
import { NetworkType, TokenType, TokenTypeTo } from "../../../src/types";

const amount = toBN("100000000000000000000");
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/tests/oneInch.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import Web3Eth from "web3-eth";
import { numberToHex } from "web3-utils";
import { numberToHex } from "@enkryptcom/utils";
import OneInch, { ONEINCH_APPROVAL_ADDRESS } from "../src/providers/oneInch";
import {
EVMTransaction,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/tests/paraswap.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import Web3Eth from "web3-eth";
import { numberToHex } from "web3-utils";
import { numberToHex } from "@enkryptcom/utils";
import Parawap, { PARASWAP_APPROVAL_ADDRESS } from "../src/providers/paraswap";
import {
EVMTransaction,
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/tests/rango.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { toBN } from "web3-utils";
import { toBN } from "@enkryptcom/utils";
import Web3Eth from "web3-eth";
import Rango from "../src/providers/rango";
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/swap/tests/swapToken.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai";
import { toBN } from "web3-utils";
import { toBN } from "@enkryptcom/utils";
import { fromToken } from "./fixtures/mainnet/configs";
import SwapToken from "../src/swapToken";

Expand Down
2 changes: 1 addition & 1 deletion packages/swap/tests/zerox.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai";
import Web3Eth from "web3-eth";
import { numberToHex } from "web3-utils";
import { numberToHex } from "@enkryptcom/utils";
import Zerox from "../src/providers/zerox";
import {
EVMTransaction,
Expand Down
3 changes: 2 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"@ethereumjs/util": "^9.0.3",
"@polkadot/util-crypto": "^12.6.2",
"bip39": "^3.1.0",
"bn.js": "^5.2.1",
"chai": "^4.4.1",
"ethereum-cryptography": "^2.1.3",
"web3-utils": "^1.10.4"
"web3-utils": "^4.2.1"
},
"devDependencies": {
"@types/chai": "^4.3.14",
Expand Down
11 changes: 4 additions & 7 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import {
stripHexPrefix,
hexToBytes,
keccak256,
utf8ToHex,
numberToHex,
} from "web3-utils";
import { hexToBytes, keccak256, utf8ToHex, numberToHex } from "web3-utils";
import { bigIntToBytes, bigIntToHex } from "@ethereumjs/util";
import { encodeAddress as polkadotEncodeAddress } from "@polkadot/util-crypto";
import { encrypt, decrypt } from "./encrypt";
import MemoryStorage from "./memory-storage";
import { fromBase, toBase, isValidDecimals } from "./units";
import { toBN, stripHexPrefix } from "./number-to-bn";

const bufferToHex = (buf: Buffer | Uint8Array, nozerox = false): string =>
nozerox
? Buffer.from(buf).toString("hex")
: `0x${Buffer.from(buf).toString("hex")}`;

const hexToBuffer = (hex: string): Buffer =>
Buffer.from(
stripHexPrefix(hex).length % 2 === 1
Expand All @@ -24,6 +20,7 @@ const hexToBuffer = (hex: string): Buffer =>
);

export {
toBN,
stripHexPrefix,
utf8ToHex,
bufferToHex,
Expand Down
66 changes: 66 additions & 0 deletions packages/utils/src/number-to-bn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import BN from "bn.js";

export const isHex = (hex: string) =>
(typeof hex === "string" || typeof hex === "number") &&
/^(-0x|0x)?[0-9a-f]*$/i.test(hex);

export const stripHexPrefix = (str: string) => {
if (isHex(str)) return str.replace(/^(-)?0x/i, "$1");
return str;
};

// https://github.com/SilentCicero/number-to-bn/blob/master/src/index.js
/**
* Returns a BN object, converts a number value to a BN
* @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
* @return {Object} `output` BN object of the number
* @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
*/
export const numberToBN = (arg: string | number | BN) => {
if (typeof arg === "string" || typeof arg === "number") {
let multiplier = new BN(1);
const formattedString = String(arg).toLowerCase().trim();
const isHexPrefixed =
formattedString.substring(0, 2) === "0x" ||
formattedString.substring(0, 3) === "-0x";
let stringArg = stripHexPrefix(formattedString);
if (stringArg.substring(0, 1) === "-") {
stringArg = stripHexPrefix(stringArg.slice(1));
multiplier = new BN(-1, 10);
}
stringArg = stringArg === "" ? "0" : stringArg;

if (
(!stringArg.match(/^-?[0-9]+$/) && stringArg.match(/^[0-9A-Fa-f]+$/)) ||
stringArg.match(/^[a-fA-F]+$/) ||
(isHexPrefixed === true && stringArg.match(/^[0-9A-Fa-f]+$/))
) {
return new BN(stringArg, 16).mul(multiplier);
}

if (
(stringArg.match(/^-?[0-9]+$/) || stringArg === "") &&
isHexPrefixed === false
) {
return new BN(stringArg, 10).mul(multiplier);
}
} else if (typeof arg === "object" && arg.toString) {
if (arg.toString(10).match(/^-?[0-9]+$/) && (arg.mul || arg.divn)) {
return new BN(arg.toString(10), 10);
}
}

throw new Error(
`[number-to-bn] while converting number
${JSON.stringify(arg)}
to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported.`
);
};

export const toBN = (number: string | number) => {
try {
return numberToBN(number);
} catch (e) {
throw new Error(`${e} Given value: "${number}"`);
}
};
2 changes: 1 addition & 1 deletion packages/utils/src/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://github.com/ethers-io
Note, Richard is a god of ether gods. Follow and respect him, and use Ethers.io!
*/

import { toBN } from "web3-utils";
import { toBN } from "./number-to-bn";

const zero = toBN(0);
const negative1 = toBN(-1);
Expand Down
5 changes: 5 additions & 0 deletions packages/utils/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
hexToBuffer,
toBase,
fromBase,
toBN,
} from "../src";

describe("Utility functions", () => {
Expand All @@ -21,6 +22,10 @@ describe("Utility functions", () => {
expect(hexToBuffer("0x123456")).to.be.deep.equal(buf);
expect(hexToBuffer("123456")).to.be.deep.equal(buf);
});
it("toBN to string", () => {
const bn = toBN("0xabcdef");
expect(bn.toString()).to.be.equal("11259375");
});
it("To Base/From Base", () => {
expect(toBase("1193046", 18)).to.be.equal("1193046000000000000000000");
expect(fromBase("1193046000000000000000000", 18)).to.be.equal("1193046");
Expand Down
Loading

0 comments on commit 5488539

Please sign in to comment.