Skip to content

Commit

Permalink
fix: update ledger singer pkg (#176)
Browse files Browse the repository at this point in the history
Co-authored-by: todd <[email protected]>
  • Loading branch information
zakir-code and todd-woko authored Jan 15, 2024
1 parent 2c20233 commit 70a0c8d
Show file tree
Hide file tree
Showing 7 changed files with 5,473 additions and 3,765 deletions.
2 changes: 1 addition & 1 deletion solidity/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {HardhatUserConfig} from "hardhat/config"
import "hardhat-dependency-compiler"
import "@nomiclabs/hardhat-ethers"
import "@nomicfoundation/hardhat-ethers"

import './tasks/task'

Expand Down
9 changes: 5 additions & 4 deletions solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
},
"license": "Apache-2.0",
"dependencies": {
"@ethers-ext/signer-ledger": "^6.0.0-beta.1",
"@ledgerhq/hw-transport-node-hid": "^6.28.1",
"@openzeppelin/contracts": "^4.8.3",
"@openzeppelin/contracts-upgradeable": "4.8.3",
"@types/chai": "^4.3.9",
Expand All @@ -40,13 +42,12 @@
"inquirer": "^8.1.0"
},
"devDependencies": {
"@ethersproject/hardware-wallets": "^5.7.0",
"@nomiclabs/hardhat-ethers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@types/inquirer": "^7.3.1",
"axios": "^0.27.2",
"bech32": "^2.0.0",
"ethers": "^5.0.0",
"hardhat": "^2.14.0",
"ethers": "^6.1.0",
"hardhat": "^2.19.4",
"hardhat-dependency-compiler": "^1.1.3",
"prettier": "^2.7.1",
"prettier-plugin-solidity": "^1.0.0-dev.23",
Expand Down
9 changes: 4 additions & 5 deletions solidity/tasks/bridge_tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TransactionToJson,
vote_power
} from "./subtasks";
import {BigNumber} from "ethers";
import {bech32} from "bech32";

const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
Expand All @@ -27,7 +26,7 @@ const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")

const allowanceAmount = await bridgeTokenContract.allowance(from, taskArgs.bridgeContract);

if (allowanceAmount.lt(BigNumber.from(taskArgs.amount))) {
if (hre.ethers.getBigInt(taskArgs.amount) > allowanceAmount) {
const erc20_factory = await hre.ethers.getContractFactory("ERC20TokenTest");
const data = erc20_factory.interface.encodeFunctionData(
"approve",
Expand Down Expand Up @@ -63,7 +62,7 @@ const sendToFx = task("send-to-fx", "call bridge contract sendToFx()")
const destination_bc = bech32.fromWords(bech32.decode(taskArgs.destination).words);
const destination_bc_hex = ('0x' + '0'.repeat(24) + Buffer.from(destination_bc).toString('hex')).toString()

const target = hre.ethers.utils.formatBytes32String(taskArgs.targetIbc);
const target = hre.ethers.encodeBytes32String(taskArgs.targetIbc);

const data = bridge_factory.interface.encodeFunctionData(
"sendToFx",
Expand Down Expand Up @@ -108,7 +107,7 @@ const initBridge = task("init-bridge", "init bridge contract")

const oracle_set = await GetOracleSet(fxRestUrl, chainName)
const gravity_id_str = await GetGravityId(fxRestUrl, chainName)
const gravity_id = hre.ethers.utils.formatBytes32String(gravity_id_str);
const gravity_id = hre.ethers.encodeBytes32String(gravity_id_str);

const external_addresses = [];
const powers = [];
Expand Down Expand Up @@ -170,7 +169,7 @@ const addBridgeToken = task("add-bridge-token", "add bridge token into bridge co
const from = await wallet.getAddress();

const bridge_factory = await hre.ethers.getContractFactory("FxBridgeLogic")
const ibc = hre.ethers.utils.formatBytes32String(targetIbc)
const ibc = hre.ethers.encodeBytes32String(targetIbc)
const data = bridge_factory.interface.encodeFunctionData('addBridgeToken', [tokenContract, ibc, isOriginal])

const tx = await hre.run(SUB_CREATE_TRANSACTION, {
Expand Down
90 changes: 38 additions & 52 deletions solidity/tasks/subtasks.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {subtask} from "hardhat/config";
import {LedgerSigner} from "@ethersproject/hardware-wallets";
import {BigNumber} from "ethers";
import {LedgerSigner} from "@ethers-ext/signer-ledger";
import {Block, HDNodeWallet, TransactionLike} from "ethers";
import axios from "axios";
import {ConfigurableTaskDefinition} from "hardhat/types";
import {boolean, string} from "hardhat/internal/core/params/argumentTypes";

const inquirer = require('inquirer')
import TransportNodeHid from "@ledgerhq/hw-transport-node-hid";
import inquirer from 'inquirer';

// sub task name
export const SUB_CHECK_PRIVATE_KEY: string = "sub:check-private-key";
Expand Down Expand Up @@ -33,12 +33,12 @@ export const GAS_LIMIT_FLAG = "gasLimit";
export const VALUE_FLAG = "value";

export const DEFAULT_DRIVE_PATH = "m/44'/60'/0'/0/0";
export const DEFAULT_PRIORITY_FEE = "1500000000";
export const DEFAULT_PRIORITY_FEE: string = "1500000000";

subtask(SUB_SEND_ETH, "send eth").setAction(
async (taskArgs, hre) => {
const {to, value, wallet, gasPrice, maxFeePerGas, maxPriorityFeePerGas, nonce, gasLimit, chainId} = taskArgs;
const transaction: Transaction = await hre.run(SUB_CREATE_TRANSACTION, {
const transaction: TransactionLike = await hre.run(SUB_CREATE_TRANSACTION, {
from: wallet.address,
to: to,
value: value,
Expand Down Expand Up @@ -69,23 +69,22 @@ subtask(SUB_CREATE_TRANSACTION, "create transaction").setAction(
if (gasPrice && maxFeePerGas) {
throw new Error("Please provide only one of gasPrice or maxFeePerGas and maxPriorityFeePerGas");
}
if (!gasPrice && !maxFeePerGas) {
await hre.ethers.provider.getBlock("latest").then(
async (block) => {
if (block.baseFeePerGas) {
maxPriorityFeePerGas = maxPriorityFeePerGas ? maxPriorityFeePerGas : BigNumber.from(DEFAULT_PRIORITY_FEE);
maxFeePerGas = block.baseFeePerGas.add(maxPriorityFeePerGas);
if (!maxPriorityFeePerGas) {
maxPriorityFeePerGas = DEFAULT_PRIORITY_FEE;
}
if (!maxFeePerGas) {
await hre.ethers.provider.getBlock("latest").then(async (block: Block | null) => {
if (block?.baseFeePerGas) {
maxPriorityFeePerGas = hre.ethers.getBigInt(maxPriorityFeePerGas);
maxFeePerGas = block?.baseFeePerGas + maxPriorityFeePerGas;
} else {
gasPrice = await hre.ethers.provider.getGasPrice()
const feeData = await hre.ethers.provider.getFeeData()
gasPrice = feeData.gasPrice
}
}
);
}
if (maxFeePerGas) {
maxPriorityFeePerGas = maxPriorityFeePerGas ? maxPriorityFeePerGas : BigNumber.from(DEFAULT_PRIORITY_FEE);
maxFeePerGas = BigNumber.from(maxFeePerGas).add(maxPriorityFeePerGas);
}
const transaction: Transaction = {
const transaction: TransactionLike = {
from: from,
to: to,
value: value,
Expand All @@ -97,14 +96,16 @@ subtask(SUB_CREATE_TRANSACTION, "create transaction").setAction(
data: data,
value: value
}),
chainId: chainId ? chainId : await hre.ethers.provider.getNetwork().then(network => network.chainId)
chainId: chainId ? chainId : await hre.ethers.provider.getNetwork().then((network: {
chainId: any;
}) => network.chainId)
}
if (gasPrice) {
transaction.gasPrice = gasPrice;
transaction.gasPrice = gasPrice.toString();
}
if (maxFeePerGas) {
transaction.maxFeePerGas = maxFeePerGas;
transaction.maxPriorityFeePerGas = maxPriorityFeePerGas;
transaction.maxFeePerGas = maxFeePerGas.toString();
transaction.maxPriorityFeePerGas = maxPriorityFeePerGas.toString();
}
return transaction;
}
Expand All @@ -113,12 +114,11 @@ subtask(SUB_CREATE_TRANSACTION, "create transaction").setAction(
subtask(SUB_GET_CONTRACT_ADDR, "get contract address").setAction(
async (taskArgs, hre) => {
const {from} = taskArgs;

const nodeUrl = await hre.run(SUB_GET_NODE_URL);
const provider = await new hre.ethers.providers.JsonRpcProvider(nodeUrl);
const provider = new hre.ethers.JsonRpcProvider(nodeUrl);
const nonce = await provider.getTransactionCount(from);

return hre.ethers.utils.getContractAddress({
return hre.ethers.getCreateAddress({
from: from,
nonce: nonce,
});
Expand Down Expand Up @@ -150,19 +150,18 @@ subtask(SUB_CREATE_LEDGER_WALLET, "create ledger wallet").setAction(
async (taskArgs, hre) => {
const {driverPath} = taskArgs;
const nodeUrl = await hre.run(SUB_GET_NODE_URL);
const provider = await new hre.ethers.providers.JsonRpcProvider(nodeUrl);

const _path = driverPath ? driverPath : DEFAULT_DRIVE_PATH;
const provider = new hre.ethers.JsonRpcProvider(nodeUrl);

const wallet = new LedgerSigner(provider, "hid", _path);
const wallet = new LedgerSigner(TransportNodeHid, provider, _path);
return {wallet};
});

subtask(SUB_PRIVATE_KEY_WALLET, "private key wallet account").setAction(
async (taskArgs, hre) => {
const {privateKey} = taskArgs;
const nodeUrl = await hre.run(SUB_GET_NODE_URL);
const provider = await new hre.ethers.providers.JsonRpcProvider(nodeUrl);
const provider = new hre.ethers.JsonRpcProvider(nodeUrl);
const wallet = new hre.ethers.Wallet(privateKey, provider);
return {provider, wallet};
});
Expand All @@ -171,7 +170,7 @@ subtask(SUB_MNEMONIC_WALLET, "mnemonic wallet account").setAction(
async (taskArgs, hre) => {
const {mnemonic, driverPath, index} = taskArgs;
const nodeUrl = await hre.run(SUB_GET_NODE_URL);
const provider = await new hre.ethers.providers.JsonRpcProvider(nodeUrl);
const provider = new hre.ethers.JsonRpcProvider(nodeUrl);

let _path = DEFAULT_DRIVE_PATH
if (driverPath) {
Expand All @@ -180,7 +179,7 @@ subtask(SUB_MNEMONIC_WALLET, "mnemonic wallet account").setAction(
if (index) {
_path = `m/44'/60'/0'/0/${index}`
}
const wallet = hre.ethers.Wallet.fromMnemonic(mnemonic, _path).connect(provider);
const wallet = HDNodeWallet.fromPhrase(mnemonic, undefined, _path).connect(provider);
return {provider, wallet};
}
);
Expand Down Expand Up @@ -208,32 +207,19 @@ subtask(SUB_CONFIRM_TRANSACTION, "confirm transaction").setAction(
return {answer: _answer};
});

type Transaction = {
from: string,
to?: string,
value?: BigNumber,
data?: string,
gasPrice?: BigNumber,
maxFeePerGas?: BigNumber,
maxPriorityFeePerGas?: BigNumber,
nonce: number,
gasLimit?: number,
chainId: number
}

// function Transaction to json string
export function TransactionToJson(transaction: Transaction): string {
export function TransactionToJson(transaction: TransactionLike): string {
return JSON.stringify({
from: transaction.from,
to: transaction.to,
value: transaction.value,
value: transaction.value?.toString(),
data: transaction.data,
gasPrice: transaction.gasPrice ? transaction.gasPrice.toString() : undefined,
maxFeePerGas: transaction.maxFeePerGas ? transaction.maxFeePerGas.toString() : undefined,
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? transaction.maxPriorityFeePerGas.toString() : undefined,
nonce: transaction.nonce,
gasLimit: transaction.gasLimit ? transaction.gasLimit.toString() : undefined,
chainId: transaction.chainId
gasPrice: transaction.gasPrice?.toString(),
maxFeePerGas: transaction.maxFeePerGas?.toString(),
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas?.toString(),
nonce: transaction.nonce?.toString(),
gasLimit: transaction.gasLimit?.toString(),
chainId: transaction.chainId?.toString()
}, null, 2);
}

Expand Down
6 changes: 3 additions & 3 deletions solidity/tasks/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const send = task("send", "send tx, Example: npx hardhat send 0x... transfer(add
return
}
const abi = parseAbiItemFromSignature(func)
const abiInterface = new hre.ethers.utils.Interface([abi])
const abiInterface = new hre.ethers.Interface([abi])

if (abi.inputs && (abi.inputs.length !== params.length)) {
throw new Error(`Please provide ${abi.inputs.length} params`)
Expand Down Expand Up @@ -78,7 +78,7 @@ task("call", "call contract, Example: npx hardhat call 0x... balanceOf(address)(
.addVariadicPositionalParam("params", "call contract params", undefined, string, true)
.setAction(async (taskArgs, hre) => {
const nodeUrl = await hre.run(SUB_GET_NODE_URL, taskArgs)
const provider = new hre.ethers.providers.JsonRpcProvider(nodeUrl);
const provider = new hre.ethers.JsonRpcProvider(nodeUrl);

const {from, params} = taskArgs;
const to = params[0];
Expand All @@ -90,7 +90,7 @@ task("call", "call contract, Example: npx hardhat call 0x... balanceOf(address)(
}

const abi = parseAbiItemFromSignature(func)
const abiInterface = new hre.ethers.utils.Interface([abi])
const abiInterface = new hre.ethers.Interface([abi])

if (abi.inputs && (abi.inputs.length !== params.length)) {
throw new Error(`Please provide ${abi.inputs.length} params`)
Expand Down
4 changes: 4 additions & 0 deletions solidity/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"lib": [
"es2020",
"dom"
],
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
Expand Down
Loading

0 comments on commit 70a0c8d

Please sign in to comment.