我需要能够从合同中检索文档,由用户钱包对其进行签名,然后发送回合同并验证签名。
以下是我如何在客户端对 address0 进行签名:
let message : string = "check this message signed by account0";
let messageHash = keccak256(utils.toUtf8Bytes(message));
let signature = await address0.signMessage(messageHash);
await hm.connect(address0).verifyMessage(message, signature);
Run Code Online (Sandbox Code Playgroud)
这是我合同中的验证器:
function verifyMessage(string memory message,
bytes memory signature)
public view returns(bool) {
//hash the plain text message
bytes32 messagehash = keccak256(bytes(message));
//hash the prefix and messagehash together
bytes32 messagehash2 = keccak256(abi.encodePacked("\x19Ethereum Signed Messsage:\n32", messagehash));
//extract the signing contract address
address signeraddress = ECDSA.recover( messagehash2, signature);
if (msg.sender==signeraddress) {
//The message is authentic
return …Run Code Online (Sandbox Code Playgroud) 我正在尝试在 Hardhat 中测试我的智能合约,但为了做到这一点,我首先需要向我的合约发送一些 ERC20 代币(对于此测试,我使用 USDC)。
在我的测试中,我模拟了 USDC 鲸鱼,但如何实际将 USDC 转移到我的合约中?
it("USDC test", async function () {
const testContract =
await ethers.getContractFactory("TestContract")
.then(contract => contract.deploy());
await testContract.deployed();
// Impersonate USDC whale
await network.provider.request({
method: "hardhat_impersonateAccount",
params: [USDC_WHALE_ADDRESS],
});
const usdcWhale = await ethers.provider.getSigner(USDC_WHALE_ADDRESS);
// Need to transfer USDC from usdcWhale to testContract
});
Run Code Online (Sandbox Code Playgroud) 尝试使用 Hardhat 部署智能合约但出现配置错误。
这是完整的错误详细信息
Error HH9: Error while loading Hardhat's configuration.
You probably tried to import the "hardhat" module from your config or a file imported from it.
This is not possible, as Hardhat can't be initialized while its config is being defined.
Run Code Online (Sandbox Code Playgroud)
所有插件似乎都已正确安装。
部署.js
const hre = require("hardhat");
async function main() {
const TestContract = await hre.ethers.getContractFactory("TestContract");
const superInstance = await TestContract.deploy("TestContractHAT", "SMC");
await superInstance.deployed();
console.log("contract was deployed to:", superInstance.address());
}
// We recommend this pattern to be able …Run Code Online (Sandbox Code Playgroud) 以下代码找不到“ethers”:
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
task('read',async () => {
const contract = ethers.getContractFactory('AwesomeContract');
// ...
})
const config: HardhatUserConfig = {
solidity: "0.8.15",
};
export default config;
Run Code Online (Sandbox Code Playgroud)
开发者当然不能这样做:
import { ethers } from 'hardhat';
Run Code Online (Sandbox Code Playgroud)
因为它抛出HH9。
是否可以在打字稿任务中使用hardhat.ethers?
我目前在一个 nft 市场工作,希望对每笔销售收取 2.5% 的费用。我已经在线阅读和研究了如何计算坚固性百分比,并在这方面取得了成功,但问题是结果是一个巨大的数字,即 2.5 * 150/100 应该返回 3.75 但我得到这个数字: 3750000000000000000000000000000000000
我的测试
const auctionPrice = ethers.utils.parseUnits("1", "ether");
const [_, userAddress] = await ethers.getSigners();
let fee = (2.5 * auctionPrice) / 100;
const commission = ethers.utils.parseUnits(fee.toString(), "ether");
let commissionValue = commission.toString();
console.log(commission.toString());
console.log(fee);
await market
.connect(userAddress)
.createMarketSale(nftContractAddress, 1, commissionValue, {
value: auctionPrice,
});
Run Code Online (Sandbox Code Playgroud)
结果
250000000000000000000000000000000
1 failing
1) NFTMarket
Should create and execute market sales:
Error: Transaction reverted: function call failed to execute
at NFTMarket.createMarketSale (contracts/NFTMarket.sol:165)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at …Run Code Online (Sandbox Code Playgroud) 这是我的hardhat.config.js -
module.exports = {
solidity: "0.8.4",
networks: {
hardhat: {
chainId: 1337
},
mumbai: {
url: "https://rpc-mumbai.matic.today",
accounts: [process.env.pk]
},
// polygon: {
// url: "https://polygon-rpc.com/",
// accounts: [process.env.pk]
// }
}
};
Run Code Online (Sandbox Code Playgroud)
运行 npx Hardhat 测试时出现以下错误:
**Error HH8: There's one or more errors in your config file:
Invalid account: #0 for network: mumbai - Expected string, received undefined**`
Run Code Online (Sandbox Code Playgroud)
我的 Hardhat.config.js 文件似乎有一些错误,但无法找到。我正在使用 Nader Dabit 的 full-stack-web3 教程进行全栈 web3 开发。
hardhat ×6
solidity ×4
ethers.js ×3
blockchain ×1
config ×1
ecdsa ×1
erc20 ×1
ethereum ×1
javascript ×1
nft ×1
typescript ×1