标签: truffle

松露测试中的时间延迟

我正在为我的合同编写测试用例,并且必须延迟断言检查,因为它对时间敏感。getCompletedCampaigns() 将获得已过截止日期的活动的地址。

\n\n
it("Time sensitive check", async () => {\n\n    var deadLine = Math.round(Date.now() / 1000) + 3;\n    let eventDetails = await contract.createCampaign("Campaign name",deadLine,\n    {\n        from: accounts[0]\n    });\n\n    addressFromEvent = eventDetails[\'logs\'][1][\'args\'][\'campaignAddress\'];\n\n    async function checker() {\n        let deployedCampaigns = await factory.getCompletedCampaigns();\n        assert.equal(addressFromEvent, deployedCampaigns[0]);\n    }\n\n    function timeout(ms) {\n        return new Promise(resolve => setTimeout(resolve, ms));\n    }\n\n    async function sleep() {\n        await timeout(5000);\n        return checker();\n    }\n\n    sleep();\n});\n
Run Code Online (Sandbox Code Playgroud)\n\n

即使断言应该失败,测试也会通过。断言发生在测试套件完成执行所有测试之后,并强制提示符从truffle develop控制台中出来,因为如果它失败了。在下面的测试中,我故意未通过测试。

\n\n
Contract: Testing CrowdCoin\n    \xe2\x9c\x93 CampaignFactory deployment\n    \xe2\x9c\x93 Create a new Campaign …
Run Code Online (Sandbox Code Playgroud)

javascript ethereum truffle

4
推荐指数
1
解决办法
3116
查看次数

如何发送wei/eth到合约地址?(使用truffle javascript测试)

我正在尝试将 wei/eth 发送到我的 Solidity 合约的地址,该合约具有外部应付回退功能。我下面的 truffle javascript 测试不会导致 instance.address 的余额获得任何 wei。instance.address不是接收wei的智能合约地址吗?有人能发现为什么 console.logging 余额结果为 0 吗?或者发现我错过了什么?

谢谢!

const TestContract = artifacts.require("TestContract");


contract('TestContract', async (accounts) => { 

it('should send 1 ether to TestContract', async () => {
  let instance = await TestContract.deployed();

  instance.send({from: accounts[1], value: 1000000000000000000}); 
  let balance = await web3.eth.getBalance(instance.address);
  console.log('instance.address balance: ' + parseInt(balance));
)}
Run Code Online (Sandbox Code Playgroud)

testing blockchain ethereum solidity truffle

4
推荐指数
1
解决办法
8829
查看次数

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

我牢固地编写了一个简单的智能合约,并尝试用松露迁移它。

$ truffle migrate
Compiling .\contracts\Election.sol...
Compiling .\contracts\Migrations.sol...

    /D/ethereum/electiondemo/contracts/Migrations.sol:1:1: SyntaxError: Source file requires different compiler version (current compiler is 0.5.0+commit.1d4f565a.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
    pragma solidity ^0.4.24;
    ^----------------------^

Compilation failed. See above.`enter code here`
Truffle v5.0.0 (core: 5.0.0)
Node v8.11.1
Run Code Online (Sandbox Code Playgroud)

实体版本为0.5.0。请找到以下智能合约代码:

pragma solidity ^0.5.0;

contract Election {
    // Read/write candidate
    string public candidate;

    // Constructor
    constructor ( ) public {
        candidate = "Candidate 1";
    }
}
Run Code Online (Sandbox Code Playgroud)

ethereum solidity truffle

4
推荐指数
2
解决办法
4346
查看次数

core.addProviders不是函数

我正在尝试运行deploy.js具有以下代码的文件:

const HDWalletProvider = require("truffle-hdwallet-provider");
const Web3 = require("web3");
const compiledFactory = require('./build/CampaignFactory.json');
//const web3 = new Web3(provider);

const provider = new HDWalletProvider(
  "[12 word mnemonic]",
  "[rinkeby api from infura]"
);

const web3 = new Web3(provider);

const deploy = async () => {
  const accounts = await web3.eth.getAccounts();

  console.log("Attempting to deply from account", accounts[0]);
  const result = await new web3.eth.Contract(JSON.parse(compiledFactory.interface))
    .deploy({ data: compiledFactory.bytecode })
    .send({ gas: '1000000', from: accounts[0] });

  //console.log(interface);
  console.log("Contract deployed to", result.options.address);
};
deploy();
Run Code Online (Sandbox Code Playgroud)

当我运行时 …

blockchain ethereum solidity truffle web3-donotuse

4
推荐指数
1
解决办法
1547
查看次数

将 BN 转换为数字

在松露控制台中我正在执行以下语句,

result = token.balanceOf(accounts[1])
Run Code Online (Sandbox Code Playgroud)

该语句返回以下输出。

<BN: 8ac7230489e80000>
Run Code Online (Sandbox Code Playgroud)

正如这里所建议的,我正在尝试使用toNumber()and toString。但我收到以下错误。

result = token.balanceOf(accounts[1])
result.toString()
output: '[object Promise]'
result.toNumber()
TypeError: result.toNumber is not a function
Run Code Online (Sandbox Code Playgroud)

javascript truffle web3js

4
推荐指数
1
解决办法
9308
查看次数

错误:SimpleSmartContract 尚未部署到检测到的网络(网络/工件不匹配)

我开发了一个简单的智能合约。当我运行松露测试时,它显示以下错误:我是新手,所以我无法弄清楚。

\n

PS F:\\pracdap> 松露测试

\n
Compiling your contracts...\n===========================\n\xe2\x88\x9a Fetching solc version list from solc-bin. Attempt #1\n> Compiling .\\contracts\\Migrations.sol\n> Compiling .\\contracts\\SimpleSmartContract.sol       \n\xe2\x88\x9a Fetching solc version list from solc-bin. Attempt #1\n> Compilation warnings encountered:\n\n    Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "urce file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more informa\n--> /F/pracdap/contracts/SimpleSmartContract.sol\n\n\n> Artifacts written to C:\\Users\\HP\\AppData\\Local\\Temp\\test--1224-GWVOn3NGyps8\n> Compiled successfully using:\n   - solc: 0.8.3+commit.8d00100c.Emscripten.clang\n\n\n\n  Contract: SimpleSmartContract\n    1) should be deployed\n …
Run Code Online (Sandbox Code Playgroud)

solidity truffle network-error-logging

4
推荐指数
1
解决办法
1900
查看次数

区块链教程错误:必须定义发送交易“来自”字段

我正在关注 dappuniversity 的区块链教程。

当我在行中创建任务时

await App.todoList.createTask(content)
Run Code Online (Sandbox Code Playgroud)

来自行 https://github.com/dappuniversity/eth-todo-list/blob/e3ed9a2cefb581c09730250c56c9d30a19cc63c8/src/app.js#L115

我收到以下错误:

Uncaught (in promise) Error: The send transactions "from" field must be defined!
    at Method.inputTransactionFormatter (truffle-contract.js:50747)
    at truffle-contract.js:51228
    at Array.map (<anonymous>)
    at Method.formatInput (truffle-contract.js:51226)
    at Method.toPayload (truffle-contract.js:51261)
    at Eth.send [as sendTransaction] (truffle-contract.js:51551)
Run Code Online (Sandbox Code Playgroud)

我需要在某处定义“来自”字段吗?

ethereum truffle

4
推荐指数
1
解决办法
3096
查看次数

如何使用 HardHat 调用可支付的 Solidity 函数?

我有一个名为“领养狗”的可靠函数,如下所示,它基本上是合同中的应付函数。

// 这失败了,因为我不知道如何在 Hardhat/ETHER.JS 中传递 ETH

哈德哈特

 const Adopt = await ethers.getContractFactory("Adopt");
    const adopt = await Adopt.deploy();
    await adopt.deployed();
    await adopt.adopt("Hachiko"); 
Run Code Online (Sandbox Code Playgroud)

合同

 function adopt(string calldata dog_breed) external payable {
             require(msg.value >= 1 ether ,"Min 1 ether needs to be transfered");
            require(user_list[msg.sender].user_allowed_to_adopt,"User not 
            allowed to participate for adoption");
            require(!user_list[msg.sender].adopted,"User has already 
            adopted the dog");
            
        User memory user=user_list[msg.sender];
        user.adopted=true;
        user_list[msg.sender]=user;
    }
Run Code Online (Sandbox Code Playgroud)

blockchain solidity truffle web3js hardhat

4
推荐指数
1
解决办法
7570
查看次数

尝试更新最新块时遇到错误

我是区块链新手,我只是尝试将一个简单的智能合约部署到 ropsten 测试网。我使用了https://github.com/t4sk/solidity-multi-sig-wallet中的智能合约代码。我还使用truffledevelopment提供的帐户

\n

我的 truffle-config.js:

\n
networks: {\ndevelopment: {\n     host: "127.0.0.1",     // Localhost (default: none)\n     port: 8545,            // Standard Ethereum port (default: none)\n     network_id: "*",       // Any network (default: none)\n    },\nropsten: {\n    provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/${infuraKey}`),\n    network_id: 3,       // Ropsten\'s id\n    gas: 5500000,        // Ropsten has a lower block limit than mainnet\n    confirmations: 2,    // # of confs to wait between deployments. (default: 0)\n    timeoutBlocks: 200,  // # of blocks before a deployment times …
Run Code Online (Sandbox Code Playgroud)

blockchain ethereum smartcontracts truffle web3js

4
推荐指数
1
解决办法
8962
查看次数

Truffle init 未创建 Migration.sol 文件

命令输出truffle version

“Truffle v5.5.27(核心:5.5.27)Ganache v7.4.0 Solidity - 0.8.16(solc-js)Node v16.16.0 Web3.js v1.7.4”

truffle init确实给出了如下成功消息

“初始化成功了,亲爱的!

尝试我们的脚手架命令来开始: $ truffle create Contract YourContractName #scaffold a Contract $ truffle create test YourTestName #scaffold a test"

并创建文件夹和 truffle-config.js(参考屏幕截图),但不创建 Migration.sol 文件。

我确实尝试使用 sudo 运行该命令但无济于事。我用的是mac m1。请指教。

migrate solidity truffle ganache

4
推荐指数
1
解决办法
4301
查看次数