Go-Ethereum

Geth——閱讀公眾號從契約

  • January 10, 2017

設置 testrpc 並附加 geth 後:

testrpc -a 10
geth attach rpc:http://localhost:8545

我在 geth 命令行上初始化以下合約:

pragma solidity ^0.4.6;
contract AddressList {  
   event addressregistered(address addy);

   uint public NoMembers; 
   address[] public MemberAddresses;

   function registerAddress() { 
       MemberAddresses.push(msg.sender);    
       NoMembers = MemberAddresses.length;    
       addressregistered(msg.sender);
       }  
}

var addresslistContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"registerAddress","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"NoMembers","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"MemberAddresses","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"addy","type":"address"}],"name":"addressregistered","type":"event"}]);
var addresslist = addresslistContract.new(
  {
    from: web3.eth.accounts[0], 
    data: '0x606060405234610000575b610260806100196000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631832425f14610054578063525af8601461006357806392fecc4714610086575b610000565b34610000576100616100e3565b005b34610000576100706101f1565b6040518082815260200191505060405180910390f35b34610000576100a160048080359060200190919050506101f7565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6001805480600101828181548183558181151161012c5781836000526020600020918201910161012b91905b8082111561012757600081600090555060010161010f565b5090565b5b505050916000526020600020900160005b33909190916101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550506001805490506000819055507feb8ae6c1136c030439e9ea9ffc5cfb3d6daa418c8a5463bc839c4dd4d8382eab33604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a15b565b60005481565b600181815481101561000057906000526020600020900160005b915054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a723058207e10191d65bf6dfd6a5c15d4fd564ad9b5659a037257ac3d50e20147b3a1fe010029', 
    gas: '4700000'
  }, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
   }
})

然後我儲存 10 個地址:

for (var x = 0; x < 10; x++){
   addresslist.registerAddress({from:eth.accounts[x],gas:3000000}) 
}

最後我嘗試閱讀 MemberAddresses 列表:

> addresslist.MemberAddresses()
"0x50522be14817f1e774e9077f58e7ef4f8d50137f"

如您所見,它返回一個地址;第一個元素。如果我傳遞一個數字,它將返回該元素。我想要一份清單。我可以在 geth 控制台上返回所有沒有循環的數組元素嗎?

你可以添加一個getter函式

function getAddresses() constant returns (address[]){
   return MemberAddresses;      
}

不太重要的是,約定是駝峰式變數,並大寫事件:

event AddressRegistered(address addy);

uint public noMembers; 
address[] public memberAddresses;

請注意,其他合約將無法使用此功能,因為 EVM 不支持動態返回大小。不過,您仍然可以從 web3 使用它。

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