Go-Ethereum

Geth eth.contract(abi).at 函式是做什麼的?

  • January 6, 2017

Javascript-API/geth wiki 頁面聲明該at([address])函式執行以下操作:

// initiate contract for an address
var myContractInstance = MyContract.at('0x78e97bcc5b5dd9ed228fed7a4887c0d7287344a9');

**1. 這裡的“按地址實例化”是什麼意思?

2.at呼叫時發生了什麼?

3.地址參數應該來自已經部署的合約,還是新地址?_ 4. 應該退回什麼?**

當我at(..)從連接到 testrpc 的 geth 控制台使用時,它返回undefined. 應該是這樣嗎?例如,按照這個問題中的範例,’testrpc’ 設置如下:

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

編輯:

注意,我想我找到了原始碼

/**
* Should be called to get access to existing contract on a blockchain
*
* @method at
* @param {Address} contract address (required)
* @param {Function} callback {optional)
* @returns {Contract} returns contract if no callback was passed,
* otherwise calls callback function (err, contract)
*/
ContractFactory.prototype.at = function (address, callback) {
   var contract = new Contract(this.eth, this.abi, address);

   // this functions are not part of prototype,
   // because we dont want to spoil the interface
   addFunctionsToContract(contract);
   addEventsToContract(contract);

   if (callback) {
       callback(null, contract);
   }
   return contract;
};

/**
* Should be called to create new contract instance
*
* @method Contract
* @param {Array} abi
* @param {Address} contract address
*/
var Contract = function (eth, abi, address) {
   this._eth = eth;
   this.transactionHash = null;
   this.address = address;
   this.abi = abi;
};

此函式創建一個新MyContract對象,並將地址欄位設置為參數。

你應該傳入一個已經部署的合約的地址。

該函式返回一個對象,您可以使用該對象呼叫合約的 ABI 指定函式。

沒有副作用,並且該方法不會檢查以確保您傳入的合約確實存在。如果沒有,當您嘗試實際呼叫合約的方法時會出錯。

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