Web3js

無法使用其簽名呼叫合約函式

  • November 15, 2019

Web3.js 文件在這裡說智能合約的方法可以通過以下方式獲得:

  1. 名稱:myContract.methods.myMethod
  2. 帶參數的名稱:myContract.methods$$ ‘myMethod(uint256)’ $$
  3. 簽名:myContract.methods$$ ‘0x58cf5f10’ $$

但是當我使用第二個和第三個選項呼叫該方法時,我無法檢索該值,而是顯示了很多對象的簽名.. 這是我的程式碼

async function d () {

 var  myContAddr = '0x8175036782E9564C084eD1DA6C44C27150F24316';
 var myContractAbiDefenition = myAbi;
 var myContractInstance = new web3.eth.Contract(myContractAbiDefenition, myContAddr);

 tokens = await myContractInstance.methods['0xc4e41b22'].call();
 //tokens = await myContractInstance.methods['getTotalSupply()'].call(); // its also give same output
 //  tokens = await myContractInstance.methods.getTotalSupply().call(); // this gives me required output "Tokens :  6"  
 console.log("Tokens : ",tokens);
}
d();

這是我的輸出

Tokens :  { call:
  { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
 send:
  { [Function: bound _executeMethod] request: [Function: bound _executeMethod] },
 encodeABI: [Function: bound _encodeMethodABI],
 estimateGas: [Function: bound _executeMethod],
 arguments: [],
 _method:
  { constant: true,
    inputs: [],
    name: 'getTotalSupply',
    outputs: [ [Object] ],
    payable: false,
    stateMutability: 'view',
    type: 'function',
    signature: '0xc4e41b22' },
 _parent:
  Contract {
    currentProvider: [Getter/Setter],
    _requestManager:
     RequestManager {
       provider: [HttpProvider],
       providers: [Object],
       subscriptions: {} },
    givenProvider: null,
    providers:
     { WebsocketProvider: [Function: WebsocketProvider],
       HttpProvider: [Function: HttpProvider],
       IpcProvider: [Function: IpcProvider] },
    _provider:
     HttpProvider {
       host: 'HTTP://127.0.0.1:7545',
       httpAgent: [Agent],
       withCredentials: false,
       timeout: 0,
       headers: undefined,
       connected: false },
    setProvider: [Function],
    BatchRequest: [Function: bound Batch],
    extend:
     { [Function: ex]
       formatters: [Object],
       utils: [Object],
       Method: [Function: Method] },
    clearSubscriptions: [Function],
    options: { address: [Getter/Setter], jsonInterface: [Getter/Setter] },
    transactionBlockTimeout: 50,
    transactionConfirmationBlocks: 24,
    transactionPollingTimeout: 750,
    defaultChain: undefined,
    defaultHardfork: undefined,
    defaultCommon: undefined,
    defaultAccount: [Getter/Setter],
    defaultBlock: [Getter/Setter],
    methods:
     { balances: [Function: bound _createTxObject],
       '0x27e235e3': [Function: bound _createTxObject],
       'balances(address)': [Function: bound _createTxObject],
       buyToken: [Function: bound _createTxObject],
       '0xa4821719': [Function: bound _createTxObject],
       'buyToken()': [Function: bound _createTxObject],
       getTotalSupply: [Function: bound _createTxObject],
       '0xc4e41b22': [Function: bound _createTxObject],
       'getTotalSupply()': [Function: bound _createTxObject],
       owner: [Function: bound _createTxObject],
       '0x8da5cb5b': [Function: bound _createTxObject],
       'owner()': [Function: bound _createTxObject],
       setTotalSupply: [Function: bound _createTxObject],
       '0xf7ea7a3d': [Function: bound _createTxObject],
       'setTotalSupply(uint256)': [Function: bound _createTxObject],
       totalSupply: [Function: bound _createTxObject],
       '0x18160ddd': [Function: bound _createTxObject],
       'totalSupply()': [Function: bound _createTxObject] },
    events: { allEvents: [Function: bound ] },
    _address: '0x8175036782E9564C084eD1DA6C44C27150F24316',
    _jsonInterface:
     [ [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object],
       [Object] ] },
 _ethAccounts:
  Accounts {
    currentProvider: [Getter/Setter],
    _requestManager:
     RequestManager {
       provider: [HttpProvider],
       providers: [Object],
       subscriptions: {} },
    givenProvider: null,
    providers:
     { WebsocketProvider: [Function: WebsocketProvider],
       HttpProvider: [Function: HttpProvider],
       IpcProvider: [Function: IpcProvider] },
    _provider:
     HttpProvider {
       host: 'HTTP://127.0.0.1:7545',
       httpAgent: [Agent],
       withCredentials: false,
       timeout: 0,
       headers: undefined,
       connected: false },
    setProvider: [Function],
    _ethereumCall:
     { getNetworkId: [Function],
       getChainId: [Function],
       getGasPrice: [Function],
       getTransactionCount: [Function] },
    wallet:
     Wallet {
       _accounts: [Circular],
       length: 0,
       defaultKeyName: 'web3js_wallet' } } }

之後你失踪()myContractInstance.methods['0xc4e41b22']

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