我正在为我的合同编写测试用例,并且必须延迟断言检查,因为它对时间敏感。getCompletedCampaigns() 将获得已过截止日期的活动的地址。
\n\nit("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});\nRun Code Online (Sandbox Code Playgroud)\n\n即使断言应该失败,测试也会通过。断言发生在测试套件完成执行所有测试之后,并强制提示符从truffle develop控制台中出来,因为如果它失败了。在下面的测试中,我故意未通过测试。
Contract: Testing CrowdCoin\n \xe2\x9c\x93 CampaignFactory deployment\n \xe2\x9c\x93 Create a new Campaign …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 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) 我牢固地编写了一个简单的智能合约,并尝试用松露迁移它。
$ 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) 我正在尝试运行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)
当我运行时 …
在松露控制台中我正在执行以下语句,
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) 我开发了一个简单的智能合约。当我运行松露测试时,它显示以下错误:我是新手,所以我无法弄清楚。
\nPS F:\\pracdap> 松露测试
\nCompiling 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) 我正在关注 dappuniversity 的区块链教程。
当我在行中创建任务时
await App.todoList.createTask(content)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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)
我需要在某处定义“来自”字段吗?
我有一个名为“领养狗”的可靠函数,如下所示,它基本上是合同中的应付函数。
// 这失败了,因为我不知道如何在 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) 我是区块链新手,我只是尝试将一个简单的智能合约部署到 ropsten 测试网。我使用了https://github.com/t4sk/solidity-multi-sig-wallet中的智能合约代码。我还使用truffledevelopment提供的帐户
\n我的 truffle-config.js:
\nnetworks: {\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) 命令输出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。请指教。
truffle ×10
ethereum ×6
solidity ×6
blockchain ×4
web3js ×3
javascript ×2
ganache ×1
hardhat ×1
migrate ×1
testing ×1