From 62562dc3125e6e1ea3711e95789a548c7654bb7f Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Tue, 12 Sep 2023 11:43:48 +0800 Subject: [PATCH] fix tasks --- omnichain/staking/lib/convertToHexAddress.ts | 13 +++++++++++++ omnichain/staking/tasks/claim.ts | 6 +++++- omnichain/staking/tasks/rewards.ts | 5 ++++- 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 omnichain/staking/lib/convertToHexAddress.ts diff --git a/omnichain/staking/lib/convertToHexAddress.ts b/omnichain/staking/lib/convertToHexAddress.ts new file mode 100644 index 00000000..f4f48bbe --- /dev/null +++ b/omnichain/staking/lib/convertToHexAddress.ts @@ -0,0 +1,13 @@ +import { ethers } from "ethers"; + +export const convertToHexAddress = (address: string): string => { + let addr: string; + try { + // Check if it's a valid hex address + addr = ethers.utils.getAddress(address); + } catch (e) { + // If not, try to convert it to an address from bech32 + addr = ("0x" + Buffer.from(address).toString("hex")).slice(0, 42); + } + return addr; +}; diff --git a/omnichain/staking/tasks/claim.ts b/omnichain/staking/tasks/claim.ts index cb953968..0c76cb3a 100644 --- a/omnichain/staking/tasks/claim.ts +++ b/omnichain/staking/tasks/claim.ts @@ -1,14 +1,18 @@ import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; +import { convertToHexAddress } from "../lib/convertToHexAddress"; const main = async (args: any, hre: HardhatRuntimeEnvironment) => { const [signer] = await hre.ethers.getSigners(); console.log(`🔑 Using account: ${signer.address}\n`); + const staker = convertToHexAddress(args.staker); + console.log(staker); + const factory = await hre.ethers.getContractFactory("Staking"); const contract = factory.attach(args.contract); - const tx = await contract.claimRewards(args.staker); + const tx = await contract.claimRewards(staker); const receipt = await tx.wait(); diff --git a/omnichain/staking/tasks/rewards.ts b/omnichain/staking/tasks/rewards.ts index 8feb2809..16d03fef 100644 --- a/omnichain/staking/tasks/rewards.ts +++ b/omnichain/staking/tasks/rewards.ts @@ -1,14 +1,17 @@ import { task } from "hardhat/config"; import { HardhatRuntimeEnvironment } from "hardhat/types"; +import { convertToHexAddress } from "../lib/convertToHexAddress"; const main = async (args: any, hre: HardhatRuntimeEnvironment) => { const [signer] = await hre.ethers.getSigners(); console.log(`🔑 Using account: ${signer.address}\n`); + const staker = convertToHexAddress(args.staker); + const factory = await hre.ethers.getContractFactory("Staking"); const contract = factory.attach(args.contract); - console.log(await contract.queryRewards(args.staker)); + console.log(await contract.queryRewards(staker)); }; task("rewards", "Query staking rewards", main)