truffle部署合约ganache测试
contract目录下 Storage.sol
// SPDX-License-Identifier: GPL-3.0 pragma solidity >=0.8.2 <0.9.0; /** * @title Storage * @dev Store & retrieve value in a variable * @custom:dev-run-script ./scripts/deploy_with_ethers.ts */ contract SimpleStorage { constructor () public { } uint256 storedData; function set(uint256 x) public { require(x>10,'number can not less then 10'); storedData = x; } function get() public view returns (uint256) { return storedData; } }
migrate目录下,1_deploy_storaga.js
const SimpleStorage = artifacts.require("SimpleStorage"); module.exports = function(deployer) { deployer.deploy(SimpleStorage); };
test目录,storage_test.js
const BN = require("bn.js"); module.exports = async function (callback) { try { let frac = new BN(200); let SimpleStorage = artifacts.require("SimpleStorage"); let tokenInstance = await SimpleStorage.deployed(); await tokenInstance.set(frac); let data = await tokenInstance.get(); console.log('data =' + JSON.stringify(data)); callback() } catch (e) { callback(e); } };
运行
合约编译
truffle compille
合约迁移
truffle migrate
合约测试
truffle exec .\storgae_test.js
测试结果,获得返回结果 200 的十六进制,c8
Using network 'development'. data ="c8"