Storage

無法儲存/檢索繼承屬性的值

  • June 18, 2017

合約程式碼:

pragma solidity ^0.4.11;

contract Base {
   uint stored;
   function set (uint _stored) {
       stored = _stored;
   }
   function get () constant returns (uint) {
       return stored;
   }
}

contract Child {
   Base b;
   function set (uint _stored) {
       b.set(_stored);
   }
   function get () constant returns (uint) {
       return b.get();
   }
}

部署為:

var i_sol_baseContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_stored","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]);
var i_sol_base = i_sol_baseContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000c57fe5b5b60c68061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b11460445780636d4ce63c146061575bfe5b3415604b57fe5b605f60048080359060200190919050506084565b005b3415606857fe5b606e608f565b6040518082815260200191505060405180910390f35b806000819055505b50565b600060005490505b905600a165627a7a72305820bcd4b3fa50cc6fc6b02c37155d3b2a414e76e8e152dc32cb9d7b75e06e5dfca60029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

var i_sol_childContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_stored","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"}]);
var i_sol_child = i_sol_childContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x6060604052341561000c57fe5b5b6102078061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806360fe47b1146100465780636d4ce63c14610066575bfe5b341561004e57fe5b610064600480803590602001909190505061008c565b005b341561006e57fe5b61007661012e565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166360fe47b1826040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040180828152602001915050600060405180830381600087803b151561011957fe5b6102c65a03f1151561012757fe5b5050505b50565b6000600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16636d4ce63c6000604051602001526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050602060405180830381600087803b15156101be57fe5b6102c65a03f115156101cc57fe5b50505060405180519050505b905600a165627a7a72305820a394fde97515faefe918adeb9034603b30ce48d9f132e9ea9b29823befd3d12b0029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

當我呼叫基本契約時,設置和獲取值是有效的setget但是,getChild總是返回0。在這種情況下有什麼問題?

來自 geth 的輸出:

> i_sol_base.set(100, {from: eth.coinbase})
"0x4b35370e66082ec3eb88af8aa4c8bce64a9149daa479fdd17256b2208bd9a90a"
> i_sol_base.get()
100
> i_sol_child.set(200, {from: eth.coinbase})
"0xb9e8464468dfed83fd6d3f369bae229cf4e06de892e30f39080340c3247da777"
> i_sol_child.get()
0
> i_sol_child.get({from: eth.coinbase})
0

我還沒有看過這個的部署方面,但我發現了契約的問題。使用 Remix 進行更正和測試。

pragma solidity ^0.4.11;

contract Base {
   uint stored;
   function set (uint _stored) {
       stored = _stored;
   }
   function get () constant returns (uint) {
       return stored;
   }
}

contract Child {

   // this describes the ABI for Base above ^
   // but it is silent on deployed address.

   Base b;

   // This constructor will run once. Child needs a hint
   // about where it can find a deployed Base to talk to. 

   function Child(address bAddress) {
       b = Base(bAddress);
   }

   function set (uint _stored) {
       b.set(_stored);
   }

   // need to return what was received (or else default 0)

   function get () constant returns (uint) {
       return b.get();
   }
}

在 Remix 中展示它的工作原理。

在此處輸入圖像描述

  1. 部署基地。
  2. 複製基地址。
  3. 部署 Child 並傳入 Base Address。
  4. 得到
  5. 達達:-)
  6. 當你在 JS 方面工作時,你有一份已知良好的契約可以使用。

希望能幫助到你。

引用自:https://ethereum.stackexchange.com/questions/18277