Solidity
極其簡單的工廠契約適用於 testrpc/ganache-cli 但不適用於 rinkeby
由於某種原因,這個非常簡單的工廠模式不適用於 rinkeby。使用松露
v3.4.5
和 geth1.7.3-stable
。以下是契約:pragma solidity ^0.4.11; contract Factory { address[] public pizzas; function Factory() { // constructor } function cookPizza() returns (address _pizza) { Pizza newPizza = new Pizza(); pizzas.push(newPizza); return newPizza; } function getPizzas() public constant returns (address[]) { return pizzas; } } contract Pizza { function Pizza() { // constructor } }
當我在 ganache-cli 上部署並執行時:
truffle(development)> Factory.deployed().then(function(instance) { factory = instance}) undefined truffle(development)> factory.cookPizza() truffle(development)> factory.getPizzas() [ '0xc4f8cf2d5a37e74981fffdab1b2108931822359a' ]
它會創建一個新地址,附加並
getPizzas
顯示地址。當我在 rinkeby 上部署時:Running migration: 1_initial_migration.js Deploying Migrations... Migrations: 0x42e40188763019dd09a6166b76805d329bd71111 Saving successful migration to network... Saving artifacts... Running migration: 2_deploy_contracts.js Deploying Factory... Factory: 0xb9d60d9ddf16e3af3bce774a1e9286ecf5614166 Saving successful migration to network... Saving artifacts...
在控制台中:
> sender = eth.accounts[0] > abi = "abi" > contract = eth.contract(abi) > factory = contract.at("0xb9d60d9ddf16e3af3bce774a1e9286ecf5614166") > factory.cookPizza({from:sender}) "0xf0ce6f4c0a9665f593d3d837eca4977b85d011dcb3620bf3a2428d25225db967" > factory.getPizzas() []
沒有。這可能是什麼原因造成的?
我檢查了您的交易,但失敗了。https://rinkeby.etherscan.io/tx/0xf0ce6f4c0a9665f593d3d837eca4977b85d011dcb3620bf3a2428d25225db967
這就是 getPizzas() 返回空數組的原因。
向賬戶添加資金
$$ 0 $$並解鎖並重試。它應該工作。