Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程

Posted

技术标签:

【中文标题】Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程【英文标题】:Truffle and Ganache tutorial for a Solidity smart contract migration error 【发布时间】:2021-07-11 00:02:20 【问题描述】:

按照“以太坊开发演练(第 2 部分:Truffle、Ganache、Geth 和 Mist)”教程,我粗略地初始化了 truffle 文件夹,复制到 Wrestling.sol,运行 compile truffle ,但是,在运行 migrate 之后,我没有看到任何关于从 ganache 部署合同的消息。

每个步骤似乎都已正确完成:

松露用法

nicholas@mordor:~/truffle$ ll
total 8
drwxrwxr-x  2 nicholas nicholas 4096 Apr 15 15:24 ./
drwxr-xr-x 71 nicholas nicholas 4096 Apr 15 15:18 ../
nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ truffle init

Starting init...
================

> Copying project files to /home/nicholas/truffle

Init successful, sweet!

nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ cp /home/nicholas/git/ethereum-walkthrough-1/Wrestling.sol . -vi
'/home/nicholas/git/ethereum-walkthrough-1/Wrestling.sol' -> './Wrestling.sol'
nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ ll
total 32
drwxrwxr-x  5 nicholas nicholas 4096 Apr 15 15:24 ./
drwxr-xr-x 71 nicholas nicholas 4096 Apr 15 15:18 ../
drwxr-xr-x  2 nicholas nicholas 4096 Apr 15 15:24 contracts/
drwxr-xr-x  2 nicholas nicholas 4096 Apr 15 15:24 migrations/
drwxr-xr-x  2 nicholas nicholas 4096 Apr 15 15:24 test/
-rw-r--r--  1 nicholas nicholas 4598 Apr 15 15:24 truffle-config.js
-rw-rw-r--  1 nicholas nicholas 2708 Apr 15 15:24 Wrestling.sol
nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ mv truffle-config.js truffle-config.js.bkup
nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ nano truffle-config.js 
nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ cat truffle-config.js
module.exports = 
  // See <http://truffleframework.com/docs/advanced/configuration>
  // for more about customizing your Truffle configuration!
  networks: 
    development: 
      host: "127.0.0.1",
      port: 7545,
      network_id: "*" // Match any network id
    
  
;

nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ truffle compile

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/Migrations.sol
> Artifacts written to /home/nicholas/truffle/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang

nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ truffle migrate --network development

Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.



Starting migrations...
======================
> Network name:    'development'
> Network id:      1618525649277
> Block gas limit: 6721975 (0x6691b7)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xb6045e7573e3834c4c79eb935e83500974d499f9d32dd7933b3dfa1631bb681d
   > Blocks: 0            Seconds: 0
   > contract address:    0xebCf528c359E57f5300bD0fb1Ae2911CD7581132
   > block number:        1
   > block timestamp:     1618525749
   > account:             0xAF400c33455309896aD4A3ceF3e9b801f4b84601
   > balance:             99.99616114
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00383886 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.00383886 ETH


nicholas@mordor:~/truffle$ 
nicholas@mordor:~/truffle$ pwd
/home/nicholas/truffle
nicholas@mordor:~/truffle$

ganache 控制台

nicholas@mordor:~/truffle$ ganache-cli -p 7545
Ganache CLI v6.12.2 (ganache-core: 2.13.2)

Available Accounts
==================
(0) 0xAF400c33455309896aD4A3ceF3e9b801f4b84601 (100 ETH)
(1) 0xCCCfA7723c87e987d05FfF47Cff11387519e8977 (100 ETH)
(2) 0xe05d12C9369F0D38a9bf3E710fceF7a480808114 (100 ETH)
(3) 0xd1988F3E8f53F096024EE240c1E0a411dFcfe6aE (100 ETH)
(4) 0x234b8f036e925A15D2B2760903d8F364f41fA064 (100 ETH)
(5) 0x27B5466FF034F1904568de1500Daf0f0A70d5393 (100 ETH)
(6) 0xd969E5d5E14c85329e9369e61Ac36301BE0E4287 (100 ETH)
(7) 0xC13e11eEA25ED477cEF68b512c0Fcd6113996369 (100 ETH)
(8) 0xB2d8e219eBFE5Ee3B0E4acE09DD30bfD5881FBa5 (100 ETH)
(9) 0xC1983b4ABfC233de38E00fD3727e0461f86535ee (100 ETH)

Private Keys
==================
(0) 0xca62f6371c91ee3843fbe4764c02612b05f388394510cc4000467a719499685f
(1) 0x6016ebd988cea6f78aa232f6aab40cada947ada74f928d87de86db825b7b11e5
(2) 0x6d1cfeed4f1748cb8a685c449960023cfae4fa5a7eba0087cb24c558594d2054
(3) 0x5d99edc99844c968b380088f1856995952307b91f533198ced4a706bbada9b62
(4) 0x0c7798bd411444673a3640311dd742b7daf944a93d936047b420e50d70a9ca90
(5) 0xf25d7a8f89aa63a7fef85192d7f9108494cf855c269d941378d9d7ffbd8c161c
(6) 0x7317293b8cd55dcce8a7729bfeaacb0265522348ecfdd992e353fc4d33e5ec99
(7) 0x5d257a1fb44624c4de13fea1d75cb708b8922936565cb3fa0dabb2e218e2c6a5
(8) 0x9e758500b8cd79757bb4eb307e3c1005d66984ec38ab47de219d81d803fd994c
(9) 0xe26b033f6e307cb3fc0784fe982fc00681c37389aee79dad69b33db4d4ef050d

HD Wallet
==================
Mnemonic:      person dice develop erupt pilot purpose cool bomb inherit cloth metal canal
Base HD Path:  m/44'/60'/0'/0/account_index

Gas Price
==================
20000000000

Gas Limit
==================
6721975

Call Gas Limit
==================
9007199254740991

Listening on 127.0.0.1:7545
eth_blockNumber
net_version
eth_accounts
eth_getBlockByNumber
eth_accounts
net_version
eth_getBlockByNumber
eth_getBlockByNumber
net_version
eth_getBlockByNumber
eth_estimateGas
net_version
eth_blockNumber
eth_getBlockByNumber
net_version
eth_sendTransaction

  Transaction: 0xb6045e7573e3834c4c79eb935e83500974d499f9d32dd7933b3dfa1631bb681d
  Contract created: 0xebcf528c359e57f5300bd0fb1ae2911cd7581132
  Gas usage: 191943
  Block Number: 1
  Block Time: Thu Apr 15 2021 15:29:09 GMT-0700 (Pacific Daylight Time)

eth_getTransactionReceipt
eth_getCode
eth_getTransactionByHash
eth_getBlockByNumber
eth_getBalance
eth_getBlockByNumber
eth_getBlockByNumber
eth_sendTransaction

  Transaction: 0xaefa402a80b986a218cecc26b215d3c06ccc557030bbe4bd1371978404164a64
  Gas usage: 42338
  Block Number: 2
  Block Time: Thu Apr 15 2021 15:29:09 GMT-0700 (Pacific Daylight Time)

eth_getTransactionReceipt

但可能是某个步骤被跳过或执行不正确?

【问题讨论】:

【参考方案1】:

您需要将Wrestling.sol 移动到contracts 文件夹。并创建部署JS代码。


正如tutorial 所说:

然后将上一教程中的 Wrestling.sol 文件复制到“contracts”文件夹中。

接下来,打开文件夹“migrations”并创建一个名为“2_deploy_contracts.js”的新文件。迁移只是帮助我们将合约部署到区块链的脚本。

将以下代码粘贴进去,然后保存。

const Wrestling = artifacts.require("./Wrestling.sol")

module.exports = function(deployer) 
  deployer.deploy(Wrestling);
;

【讨论】:

以上是关于Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程的主要内容,如果未能解决你的问题,请参考以下文章

智能合约语言 Solidity 教程系列9 - 错误处理

智能合约语言 Solidity 教程系列9 - 错误处理

智能合约语言 Solidity - 错误处理

执行智能合约时的 Solidity 错误消息:“如果您发送价值,则应支付被调用的函数......”

智能合约Solidity语言错误处理函数(requirerevertassert)使用详解

智能合约语言 Solidity 教程系列8 - Solidity API(特殊的变量及函数)