尝试运行:
\n1.) Dappuniversity 项目 (dappuniversity/dbank)\n2.) pet-shop-tutorial
\nTruffle v5.3.3(核心:5.3.3)\nNode v14.15.5
\n当 Truffle 要求 it\xe2\x80\x99s 编译器/solc 匹配 5.3.3 时,ser 如何在 0.8.4 编译代码以导入 OpenZeppelin\xe2\x80\x99s ERC20.sol 模板?
\n// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.4;\n\nimport "@openzeppelin/contracts/token/ERC20/ERC20.sol";\n\ncontract Token is ERC20 {\n\n //add minter variable\n\n //add minter changed event\n\n constructor() public payable ERC20("Name", "Symbol") {\n\n //assign initial minter\n\n }\n\n //Add pass minter role function\n\n function mint(address account, uint256 amount) public {\n\n //check if msg.sender has a minter role\n\n _mint(account, amount);\n\n }\n\n}\nRun Code Online (Sandbox Code Playgroud)\n未找到源 \xe2\x80\x9c@openzeppelin/contracts/token/ERC20/ERC20.sol\xe2\x80\x9d:不支持文件导入回调
\n使用 Ganache-cli 运行 Web3.py 会返回以下错误:
不支持方法 eth_maxPriorityFeePerGas。
然而,当直接使用像 Ropsten 这样的测试网络而不需要 Ganache-cli 时,这确实有效。
按照教程运行此代码:
from solcx import compile_standard, install_solc
import json
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
install_solc("0.6.0")
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"] …Run Code Online (Sandbox Code Playgroud) 我有几个 keccak,如果我能找到一种便宜的方法来获取所创建的 uint 的一部分,那么它们可以减少到一个。
pragma solidity ^0.4.19;
contract test {
function test() {
}
function sup() returns (uint test) {
uint _test = uint(keccak256("wow"));
return _test;
}
}
Run Code Online (Sandbox Code Playgroud)
这会返回一个甜蜜的随机数:13483274892375982735325
现在的计划是,我不用用不同的“种子”调用 keccak 5 次,而是可以将该数字分开并得到类似:1348、3274、8923 等的内容,然后将其用作我的随机数,例如:1348 % 10
但坚固性并不能仅仅做到这一点。有什么便宜又好用的东西吗?
我在安全帽控制台中创建了一个合约实例,如下所示:
\nconst contract_fac = await ethers.getContractFactory("ContractName");\nconst contract = await contract_fac.attach("CONTRACTADDR...");\nRun Code Online (Sandbox Code Playgroud)\n合约对象具有除 之外的所有公共/外部函数safeTransferFrom。为什么?
当我用它调用它时,它contract.safeTransferFrom(\xe2\x80\xa6)会抛出 JavaScript 错误 \xe2\x80\x9csafeTransferFrom 不是函数\xe2\x80\x9d。这是一个错误还是我不明白什么?safeTransferFrom函数在 ABI 中列出。
我使用未做任何更改的 OpenZeppelin (v. 4.2) 721 令牌模板、Ethers.js (v. 5.4.1) 和 Hardhat (v. 2.4.1)。
\n更新: \n问题已解决。safeTransferFrom 是一个重载函数。在 ethers 中,调用重载合约函数的语法与非重载函数不同。\n Ethers.js doc
\n错误的:
\ncontract.safeTransferFrom(addr1, addr2, 1);\nRun Code Online (Sandbox Code Playgroud)\n正确的:
\ncontract["safeTransferFrom(address,address,uint256)"](addr1, addr2, 1);\nRun Code Online (Sandbox Code Playgroud)\n 任何人都可以向我解释什么address(0)是Solidity?我在文档中发现了以下内容,但它对我来说并不合理:
如果目标帐户是零帐户(地址为0的帐户),则该事务会创建新合同.如前所述,该合同的地址不是零地址,而是从发送方及其发送的交易数量("nonce")得出的地址.这种合同创建事务的有效负载被认为是EVM字节码并被执行.此执行的输出将永久存储为合同代码.这意味着,为了创建合同,您不发送合同的实际代码,而是实际返回该代码的代码.
我正在尝试调用智能合约上的应付函数,该函数只接受一个参数。
如何将 eth 值与函数调用一起发送到 ethers.js 中的该函数?这些文档似乎没有给出太多关于执行此操作的最佳方法的示例。
我的函数调用
const reciept = await contract.buyPunk(1001);
Run Code Online (Sandbox Code Playgroud)
所有其他读写函数调用都按预期工作,但它调用了我尚未解决的付费函数。
我正在使用 MetaMask 将交易发送到我的 DApp 中的合约。我需要有关如何在用户单击注销按钮时断开 MetaMask 帐户与我的 DApp 的帮助。
前端:ReactJS
后端:Web3js、Solidity(以太坊)
I am new to solidity and I have been trying to print out simple messages using functions in solidity, but I have failed to deploy successfully, and there is an error that I can not figure out what's wrong.
This is what I have tried so far:
pragma solidity ^0.6.0;
contract test {
string public _feedback;
function reply(string memory feedback) public
{
feedback = "Well done!";
}
}
Run Code Online (Sandbox Code Playgroud)
The error I am receiving is "Warning: Function state mutability can be …
struct buyer{
uint amount;
Status status;
}
mapping(address=>buyer) public buyers;
mapping(uint=>address) buyerIndex;
uint public buyerNum;
//Order a product.
function(){
uint doubleValue=value*2;
uint amount=msg.value/doubleValue;
if(buyers[msg.sender]==null){ //Error in this line
buyer abuyer=buyer({amount:amount,status:Status.Created}); //Error in this line
buyerNum++;
buyerIndex[buyerNum]=msg.sender;
buyers[msg.sender]=abuyer;
}else{
buyers[msg.sender].amount+=amount;
}
Order(msg.sender,amount*doubleValue,amount);
}
Run Code Online (Sandbox Code Playgroud)
如果买方没有记录在买方映射中,那么buyerNum ++; 但我不知道如何判断买家是否在映射中