标签: smartcontracts

Solidity:错误:请将数字作为字符串或 BN 对象传递,以避免精度错误

有一个简单的 Solidity 合约:

contract SellStuff{

    address seller;
    string name;
    string description;
    uint256 price;

    function sellStuff(string memory _name, string memory _description, uint256 _price) public{
        seller = msg.sender;
        name = _name;
        description = _description;
        price = _price;
    }
    function getStuff() public view returns (
        address _seller, 
        string memory _name, 
        string memory _description, 
        uint256 _price){
            return(seller, name, description, price);
    }
}
Run Code Online (Sandbox Code Playgroud)

并运行 javascript 测试,如下所示:

var SellStuff= artifacts.require("./SellStuff.sol");

// Testing
contract('SellStuff', function(accounts){

    var sellStuffInstance;
    var seller = accounts[1];
    var stuffName = "stuff 1";
    var …
Run Code Online (Sandbox Code Playgroud)

javascript blockchain ethereum solidity smartcontracts

3
推荐指数
2
解决办法
1万
查看次数

使用 Hardhat 通过 Uniswap 流动性配置测试代币

我正在尝试分叉 Safemoon(或者实际上是 NotSafeMoon),并将其用作学习智能合约开发的工具。(我拥有大量您可能称之为“Web 2.0”的开发经验)。

假设我的构造函数中有类似的内容:

constructor () {
        _rOwned[_msgSender()] = _rTotal;
        IUniswapV2Router02 _uniswapV2Router = IUniswapV2Router02(0x10ED43C718714eb63d5aA57B78B54704E256024E);       // binance PANCAKE V2
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory()).createPair(address(this), _uniswapV2Router.WETH());
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,npx hardhat test出现以下失败:

Compilation finished successfully


  TestToken contract
    Deployment
      1) "before each" hook for "Has the right name"


  0 passing (807ms)
  1 failing

  1) TestToken contract
       "before each" hook for "Has the right name":
     Error: Transaction reverted: function call to a non-contract account
Run Code Online (Sandbox Code Playgroud)

现在,这确实很有意义,毕竟我正在尝试调用 Pancakeswap v2 路由器合约。我如何绕过这个限制?有没有办法将路由器的合约地址作为环境变量注入?我可以使用 UniswapRouter 的模拟构造函数吗?一般来说,这种事情是如何通过智能合约开发以保持可测试的方式完成的(以及如何测试)?

testing solidity smartcontracts hardhat

3
推荐指数
1
解决办法
2264
查看次数

bsc 通过钱包地址获取交易 Web3.py

如何跟踪 bsc 网络中钱包列表的代币交易?

我考虑使用websocket和过滤功能。我认为可以使用作为topics过滤器参数的一部分,并仅反映发送Transfer至/来自监视地址的事件,以便我的应用程序不必处理不必要的数据。

但我做错了,不知道如何正确地将钱包列表(或至少只有一个钱包)作为我的过滤函数的参数。怎么做?

我在从Transfer事件获取数据时遇到问题,因为我不知道如何解码HexBytes类型。我看到了 web3.js 的功能,但没有看到 web3.py 的功能。

address_list = ['0x67fdE6D04a82689a59E5188f9B572CBeF53D4763', '...', '...']

web3 = Web3(Web3.WebsocketProvider('wss://bsc.getblock.io/mainnet/?api_key=your_api_key'))
web3_filter = web3.eth.filter({'topics': address_list}) 
while True:
    for event in web3_filter.get_new_entries():
        print(web3.toJSON(web3.eth.wait_for_transaction_receipt(event).logs))
Run Code Online (Sandbox Code Playgroud)

python blockchain smartcontracts web3py

3
推荐指数
1
解决办法
1万
查看次数

ERC-721:如何获取所有代币 ID?

我想迭代以太坊 ERC-721 合约的所有代币 ID。有些合约有计数 id (0, 1, 2, 3, ...),这很容易,但有些合约有随机 id,例如https://etherscan.io/token/0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d#inventory

遗憾的是 etherscan 只显示最后使用的 10000 个令牌 id,但我想迭代所有 79490 个。有没有办法实现这一点?对我来说,一切都很好。使用一些 API 设置我自己的以太坊节点。

blockchain ethereum smartcontracts nft

3
推荐指数
2
解决办法
8545
查看次数

我尝试在 truffle 中部署我的智能合约,但当我“truffle 迁移”时显示“不支持的编译器”,我该如何解决这个问题?

PS D:\Projects\ANIS\Decentralized_Bank> truffle 编译

正在编译您的合同...

编译 .\src\contracts\Migrations.sol 编译 .\src\contracts\Migrations.sol 编译 .\src\contracts\Tether.sol
编译 .\src\contracts\Tether.sol
工件写入 D:\Projects\ANIS\ Decentralized_Bank\src\truffle_abis 使用以下命令成功编译:

  • solc:0.5.16+commit.9c3226ce.Emscripten.clang

PS D:\Projects\ANIS\Decentralized_Bank> [松露迁移]

正在编译您的合同...

错误:不支持的编译器:[object Object]

at C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\index.js:28:1
at Array.map (<anonymous>)
at compile (C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\index.js:26:1)
at Object.compile (C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\index.js:68:47)
at Object.compileAndSave (C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\workflow-compile\index.js:95:47)
at Object.run (C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:199:1)
at Command.run (C:\Users\OMEN\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\command.js:183:1)
Run Code Online (Sandbox Code Playgroud)

Truffle v5.4.11(核心:5.4.11)节点 v14.17.2

javascript deployment blockchain smartcontracts truffle

3
推荐指数
1
解决办法
663
查看次数

ParserError:源文件需要不同的编译器版本

我尝试了您在此处的讨论中(在其他问题中)和https://github.com/smartcontractkit/full-blockchain-solidity-course-py/discussions/522中提到的所有内容,但是它并没有为我解决问题,我还注意到当前的编译器版本仍然存在(当前编译器是0.6.12+commit.27d51765.Windows.msvc)。但是当我右键单击并选择Solidty:编译器信息时,它显示0.8.0。

从输出:

Retrieving compiler information:
Compiler using remote version: 'v0.8.0+commit.c7dfd78e', solidity version: 0.8.0+commit.c7dfd78e.Emscripten.clang
Run Code Online (Sandbox Code Playgroud)

不确定这是否与我面临的问题有关。无论如何,从运行布朗尼编译时看到的问题开始。我收到以下错误:

终端错误:

PS D:\Python projects\Solidity dev\demo\smartcontract-lottery> brownie compile
INFO: Could not find files for the given pattern(s).
Brownie v1.17.2 - Python development framework for Ethereum

Compiling contracts...
  Solc version: 0.6.12
  Optimizer: Enabled  Runs: 200
  EVM Version: Istanbul
CompilerError: solc returned the following errors:

C:/Users/rosne/.brownie/packages/OpenZeppelin/openzeppelin-contracts@4.3.0/contracts/access/Ownable.sol:3:1: ParserError: Source file requires different compiler version (current compiler is 0.6.12+commit.27d51765.Windows.msvc) - note that nightly builds are considered to be strictly …
Run Code Online (Sandbox Code Playgroud)

python ethereum solidity smartcontracts brownie

3
推荐指数
1
解决办法
1万
查看次数

Solidity:为什么使用初始化函数而不是构造函数?

我正在做审计智能合约,有人更喜欢使用这样的初始化函数:

 bool private isInit=false;
 string private hello;
 
 function init(string _hello) public onlyOwner {
   hello = _hello;
   isInit = true;
 } 

 function doSomething() public {
   require(isInit, "Wait for initialize");
   ...doSomething
 }
Run Code Online (Sandbox Code Playgroud)

你能解释一下为什么没有使用构造函数吗?

architecture state ethereum solidity smartcontracts

3
推荐指数
1
解决办法
4388
查看次数

将数组推入结构 - 尚不支持将类型 struct memory[] 内存复制到存储

我想创建一个 Pok\xc3\xa9mon 结构,其中包含一个结构数组。

\n
// SPDX-License-Identifier: GPL-3.0\n\npragma solidity >=0.7.0 <0.9.0;\n\ncontract PokemonFactory {\n\n  struct Pokemon {\n    uint256 id;\n    string name;\n    Ability[] abilities;\n  }\n\n  struct Ability {\n    string name;\n    string description;\n  }\n\n  Pokemon[] public pokemons;\n\n  function createPokemon(string memory _name, string memory _abilityNme, string memory _abilityDscription) public {\n    uint id = pokemons.length;\n    pokemons[id].abilities.push(Ability(_abilityNme, _abilityDscription));\n    pokemons.push(Pokemon(id, _name, pokemons[id].abilities));\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

但我收到以下错误:

\n
UnimplementedFeatureError: Copying of type struct PokemonFactory.Ability memory[] memory to storage not yet supported.\n
Run Code Online (Sandbox Code Playgroud)\n

solidity smartcontracts

3
推荐指数
1
解决办法
1279
查看次数

是否可以在genesis.json中预定义智能合约?

我遇到了与此问题完全相同的问题https://ethereum.stackexchange.com/questions/7707/is-it-possible-to-preload-contracts-in-the-genesis-block?rq=1

Agian,是否可以通过分配alloc - > code字段来预定义合约,

在此输入图像描述

但是,似乎无论我调用的合约中的哪个方法,无论此方法的逻辑和内容如何,​​它总是返回合同本身的字节码.

在此输入图像描述

这可能是我部署合同的原因:

在此输入图像描述

当我调用test()时,获取:

在此输入图像描述 这不是一个字符串.

当我调用test2()时,获取:

在此输入图像描述 这是一个非常大的数字.

当我调用test3(1)时,获取false,这不是1 == 1.

我已经浏览了相关的主题,发现了这个,https://ethereum.stackexchange.com/questions/30366/how-does-the-genesis-json-file-define-the-initial-state-of-the -blockchain

看起来存储设置是必要的但我完全不知道我应该写什么键/值.

那我怎么处理这个案子呢?

blockchain ethereum solidity smartcontracts go-ethereum

2
推荐指数
1
解决办法
323
查看次数

在测试网上使用 Remix 部署合约时出现的问题

我尝试使用 Remix 将合约部署到测试网(所有测试网都返回相同的消息),但出现此错误:

Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": -32000, "message": "gas required exceeds allowance (30000000) or always failing transaction" }
Run Code Online (Sandbox Code Playgroud)

但是当我尝试在 BSC Mainnet 中部署它时,一切正常,没有错误。有什么建议?

这是相关的代码

contract artemis is Context, IERC20, Ownable {
    // ...

    constructor() public {
        _rOwned[_msgSender()] = _rTotal;

        IUniswapV2Router02 _uniswapV2Router =
            IUniswapV2Router02(0x05fF2B0DB69458A0750badebc4f9e13aDd608C7F);
        // Create a Pancakeswap pair for this new token
        uniswapV2Pair = IUniswapV2Factory(_uniswapV2Router.factory())
            .createPair(address(this), _uniswapV2Router.WETH());

        // …
Run Code Online (Sandbox Code Playgroud)

solidity smartcontracts remix

2
推荐指数
1
解决办法
1449
查看次数