Web3js

web3.eth.Contract(abi, address) 不返回合約實例

  • October 27, 2019

我正在嘗試使用 web3 在 js 中創建我的契約實例,但是當我使用該函式時web3.eth.Contract(abi, address),結果不是預期的(我從 remix 中獲取 ABI)。

這是程式碼:

const Web3 = require('web3');
const web3 = new Web3('https://ropsten.infura.io/v3/53da5774bc2d410c90cc1e4c19d84dc4');

const account1 = '0xA4767B3a3a8D23912045E551c41c4C559572Fb85';

const contractAddress = '0xf779b15371Db4Eb0dD0eA3FD6f869cf9f5229649';
const abi = [ABI from remix];
var contract = new web3.eth.Contract(abi, contractAddress);

console.log(contract);

輸出不包含 ABI 中定義的方法,並且不能使用contract.methods.myMethods.....

這是輸出:

AbstractContract {
 providersModuleFactory: ProvidersModuleFactory {},
 providerDetector: ProviderDetector {},
 providerResolver:
  ProviderResolver { providersModuleFactory: ProvidersModuleFactory {} },
 givenProvider: null,
 _currentProvider:
  HttpProvider {
    host:
     'https://ropsten.infura.io/v3/53da5774bc2d410c90cc1e4c19d84dc4',
    timeout: 0,
    headers: undefined,
    connected: true,
    providersModuleFactory: ProvidersModuleFactory {},
    agent: { httpsAgent: [Agent] } },
 _defaultAccount: undefined,
 _defaultBlock: 'latest',
 _transactionBlockTimeout: 50,
 _transactionConfirmationBlocks: 24,
 _transactionPollingTimeout: 750,
 _defaultGasPrice: undefined,
 _defaultGas: undefined,
 BatchRequest: [Function],
 contractModuleFactory:
  ContractModuleFactory {
    utils:
     { randomHex: [Function: randomHex],
       jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
       hexToAscii: [Function: hexToAscii],
       asciiToHex: [Function: asciiToHex],
       getUnitValue: [Function: getUnitValue],
       fromWei: [Function: fromWei],
       toWei: [Function: toWei],
       toChecksumAddress: [Function: toChecksumAddress],
       keccak256: [Function],
       sha3: [Function],
       toDecimal: [Function: hexToNumber],
       hexToNumber: [Function: hexToNumber],
       fromDecimal: [Function: numberToHex],
       numberToHex: [Function: numberToHex],
       hexToString: [Function: hexToUtf8],
       toUtf8: [Function: hexToUtf8],
       stringToHex: [Function: utf8ToHex],
       fromUtf8: [Function: utf8ToHex],
       utf8ToHex: [Function: utf8ToHex],
       toAscii: [Function: hexToAscii],
       fromAscii: [Function: asciiToHex],
       padLeft: [Function: leftPad],
       padRight: [Function: rightPad],
       getSignatureParameters: [Function: getSignatureParameters],
       isAddress: [Function: isAddress],
       isBN: [Function: isBN],
       checkAddressChecksum: [Function: checkAddressChecksum],
       toBN: [Function: toBN],
       toHex: [Function: toHex],
       hexToNumberString: [Function: hexToNumberString],
       toTwosComplement: [Function: toTwosComplement],
       isHex: [Function: isHex],
       isHexStrict: [Function: isHexStrict],
       isBloom: [Function: isBloom],
       isTopic: [Function: isTopic],
       bytesToHex: [Function: bytesToHex],
       hexToBytes: [Function: hexToBytes],
       soliditySha3: [Function: soliditySha3] },
    formatters:
     { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
       isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
       inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
       inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
       txInputFormatter: [Function: txInputFormatter],
       inputCallFormatter: [Function: inputCallFormatter],
       inputTransactionFormatter: [Function: inputTransactionFormatter],
       inputSignFormatter: [Function: inputSignFormatter],
       outputTransactionFormatter: [Function: outputTransactionFormatter],
       outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
       outputBlockFormatter: [Function: outputBlockFormatter],
       inputLogFormatter: [Function: inputLogFormatter],
       outputLogFormatter: [Function: outputLogFormatter],
       inputPostFormatter: [Function: inputPostFormatter],
       outputPostFormatter: [Function: outputPostFormatter],
       inputAddressFormatter: [Function: inputAddressFormatter],
       outputSyncingFormatter: [Function: outputSyncingFormatter] },
    abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] },
    accounts:
     Accounts {
       providersModuleFactory: ProvidersModuleFactory {},
       providerDetector: ProviderDetector {},
       providerResolver: [ProviderResolver],
       givenProvider: null,
       _currentProvider: [HttpProvider],
       _defaultAccount: undefined,
       _defaultBlock: 'latest',
       _transactionBlockTimeout: 50,
       _transactionConfirmationBlocks: 24,
       _transactionPollingTimeout: 750,
       _defaultGasPrice: undefined,
       _defaultGas: undefined,
       BatchRequest: [Function],
       methodFactory: [MethodFactory],
       utils: [Object],
       formatters: [Object],
       wallet: [Wallet] },
    methodModuleFactory:
     ModuleFactory {
       accounts: [Accounts],
       subscriptionsFactory: [SubscriptionsFactory],
       formatters: [Object],
       utils: [Object] } },
 abiCoder:
  AbiCoder {
    utils:
     { randomHex: [Function: randomHex],
       jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
       hexToAscii: [Function: hexToAscii],
       asciiToHex: [Function: asciiToHex],
       getUnitValue: [Function: getUnitValue],
       fromWei: [Function: fromWei],
       toWei: [Function: toWei],
       toChecksumAddress: [Function: toChecksumAddress],
       keccak256: [Function],
       sha3: [Function],
       toDecimal: [Function: hexToNumber],
       hexToNumber: [Function: hexToNumber],
       fromDecimal: [Function: numberToHex],
       numberToHex: [Function: numberToHex],
       hexToString: [Function: hexToUtf8],
       toUtf8: [Function: hexToUtf8],
       stringToHex: [Function: utf8ToHex],
       fromUtf8: [Function: utf8ToHex],
       utf8ToHex: [Function: utf8ToHex],
       toAscii: [Function: hexToAscii],
       fromAscii: [Function: asciiToHex],
       padLeft: [Function: leftPad],
       padRight: [Function: rightPad],
       getSignatureParameters: [Function: getSignatureParameters],
       isAddress: [Function: isAddress],
       isBN: [Function: isBN],
       checkAddressChecksum: [Function: checkAddressChecksum],
       toBN: [Function: toBN],
       toHex: [Function: toHex],
       hexToNumberString: [Function: hexToNumberString],
       toTwosComplement: [Function: toTwosComplement],
       isHex: [Function: isHex],
       isHexStrict: [Function: isHexStrict],
       isBloom: [Function: isBloom],
       isTopic: [Function: isTopic],
       bytesToHex: [Function: bytesToHex],
       hexToBytes: [Function: hexToBytes],
       soliditySha3: [Function: soliditySha3] },
    ethersAbiCoder: AbiCoder { coerceFunc: [Function] } },
 utils:
  { randomHex: [Function: randomHex],
    jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
    hexToAscii: [Function: hexToAscii],
    asciiToHex: [Function: asciiToHex],
    getUnitValue: [Function: getUnitValue],
    fromWei: [Function: fromWei],
    toWei: [Function: toWei],
    toChecksumAddress: [Function: toChecksumAddress],
    keccak256: { [Function: sha3] _Hash: [Object] },
    sha3: { [Function: sha3] _Hash: [Object] },
    toDecimal: [Function: hexToNumber],
    hexToNumber: [Function: hexToNumber],
    fromDecimal: [Function: numberToHex],
    numberToHex: [Function: numberToHex],
    hexToString: [Function: hexToUtf8],
    toUtf8: [Function: hexToUtf8],
    stringToHex: [Function: utf8ToHex],
    fromUtf8: [Function: utf8ToHex],
    utf8ToHex: [Function: utf8ToHex],
    toAscii: [Function: hexToAscii],
    fromAscii: [Function: asciiToHex],
    padLeft: [Function: leftPad],
    padRight: [Function: rightPad],
    getSignatureParameters: [Function: getSignatureParameters],
    isAddress: [Function: isAddress],
    isBN: [Function: isBN],
    checkAddressChecksum: [Function: checkAddressChecksum],
    toBN: [Function: toBN],
    toHex: [Function: toHex],
    hexToNumberString: [Function: hexToNumberString],
    toTwosComplement: [Function: toTwosComplement],
    isHex: [Function: isHex],
    isHexStrict: [Function: isHexStrict],
    isBloom: [Function: isBloom],
    isTopic: [Function: isTopic],
    bytesToHex: [Function: bytesToHex],
    hexToBytes: [Function: hexToBytes],
    soliditySha3: [Function: soliditySha3] },
 formatters:
  { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
    isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
    inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
    inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
    txInputFormatter: [Function: txInputFormatter],
    inputCallFormatter: [Function: inputCallFormatter],
    inputTransactionFormatter: [Function: inputTransactionFormatter],
    inputSignFormatter: [Function: inputSignFormatter],
    outputTransactionFormatter: [Function: outputTransactionFormatter],
    outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
    outputBlockFormatter: [Function: outputBlockFormatter],
    inputLogFormatter: [Function: inputLogFormatter],
    outputLogFormatter: [Function: outputLogFormatter],
    inputPostFormatter: [Function: inputPostFormatter],
    outputPostFormatter: [Function: outputPostFormatter],
    inputAddressFormatter: [Function: inputAddressFormatter],
    outputSyncingFormatter: [Function: outputSyncingFormatter] },
 abiMapper:
  AbiMapper {
    utils:
     { randomHex: [Function: randomHex],
       jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
       hexToAscii: [Function: hexToAscii],
       asciiToHex: [Function: asciiToHex],
       getUnitValue: [Function: getUnitValue],
       fromWei: [Function: fromWei],
       toWei: [Function: toWei],
       toChecksumAddress: [Function: toChecksumAddress],
       keccak256: [Function],
       sha3: [Function],
       toDecimal: [Function: hexToNumber],
       hexToNumber: [Function: hexToNumber],
       fromDecimal: [Function: numberToHex],
       numberToHex: [Function: numberToHex],
       hexToString: [Function: hexToUtf8],
       toUtf8: [Function: hexToUtf8],
       stringToHex: [Function: utf8ToHex],
       fromUtf8: [Function: utf8ToHex],
       utf8ToHex: [Function: utf8ToHex],
       toAscii: [Function: hexToAscii],
       fromAscii: [Function: asciiToHex],
       padLeft: [Function: leftPad],
       padRight: [Function: rightPad],
       getSignatureParameters: [Function: getSignatureParameters],
       isAddress: [Function: isAddress],
       isBN: [Function: isBN],
       checkAddressChecksum: [Function: checkAddressChecksum],
       toBN: [Function: toBN],
       toHex: [Function: toHex],
       hexToNumberString: [Function: hexToNumberString],
       toTwosComplement: [Function: toTwosComplement],
       isHex: [Function: isHex],
       isHexStrict: [Function: isHexStrict],
       isBloom: [Function: isBloom],
       isTopic: [Function: isTopic],
       bytesToHex: [Function: bytesToHex],
       hexToBytes: [Function: hexToBytes],
       soliditySha3: [Function: soliditySha3] },
    abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] },
    contractModuleFactory:
     ContractModuleFactory {
       utils: [Object],
       formatters: [Object],
       abiCoder: [AbiCoder],
       accounts: [Accounts],
       methodModuleFactory: [ModuleFactory] } },
 options: { address: '0xf779b15371Db4Eb0dD0eA3FD6f869cf9f5229649' },
 PromiEvent: [Function: PromiEvent],
 methodFactory:
  MethodFactory {
    accounts:
     Accounts {
       providersModuleFactory: ProvidersModuleFactory {},
       providerDetector: ProviderDetector {},
       providerResolver: [ProviderResolver],
       givenProvider: null,
       _currentProvider: [HttpProvider],
       _defaultAccount: undefined,
       _defaultBlock: 'latest',
       _transactionBlockTimeout: 50,
       _transactionConfirmationBlocks: 24,
       _transactionPollingTimeout: 750,
       _defaultGasPrice: undefined,
       _defaultGas: undefined,
       BatchRequest: [Function],
       methodFactory: [MethodFactory],
       utils: [Object],
       formatters: [Object],
       wallet: [Wallet] },
    utils:
     { randomHex: [Function: randomHex],
       jsonInterfaceMethodToString: [Function: jsonInterfaceMethodToString],
       hexToAscii: [Function: hexToAscii],
       asciiToHex: [Function: asciiToHex],
       getUnitValue: [Function: getUnitValue],
       fromWei: [Function: fromWei],
       toWei: [Function: toWei],
       toChecksumAddress: [Function: toChecksumAddress],
       keccak256: [Function],
       sha3: [Function],
       toDecimal: [Function: hexToNumber],
       hexToNumber: [Function: hexToNumber],
       fromDecimal: [Function: numberToHex],
       numberToHex: [Function: numberToHex],
       hexToString: [Function: hexToUtf8],
       toUtf8: [Function: hexToUtf8],
       stringToHex: [Function: utf8ToHex],
       fromUtf8: [Function: utf8ToHex],
       utf8ToHex: [Function: utf8ToHex],
       toAscii: [Function: hexToAscii],
       fromAscii: [Function: asciiToHex],
       padLeft: [Function: leftPad],
       padRight: [Function: rightPad],
       getSignatureParameters: [Function: getSignatureParameters],
       isAddress: [Function: isAddress],
       isBN: [Function: isBN],
       checkAddressChecksum: [Function: checkAddressChecksum],
       toBN: [Function: toBN],
       toHex: [Function: toHex],
       hexToNumberString: [Function: hexToNumberString],
       toTwosComplement: [Function: toTwosComplement],
       isHex: [Function: isHex],
       isHexStrict: [Function: isHexStrict],
       isBloom: [Function: isBloom],
       isTopic: [Function: isTopic],
       bytesToHex: [Function: bytesToHex],
       hexToBytes: [Function: hexToBytes],
       soliditySha3: [Function: soliditySha3] },
    formatters:
     { outputBigNumberFormatter: [Function: outputBigNumberFormatter],
       isPredefinedBlockNumber: [Function: isPredefinedBlockNumber],
       inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
       inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
       txInputFormatter: [Function: txInputFormatter],
       inputCallFormatter: [Function: inputCallFormatter],
       inputTransactionFormatter: [Function: inputTransactionFormatter],
       inputSignFormatter: [Function: inputSignFormatter],
       outputTransactionFormatter: [Function: outputTransactionFormatter],
       outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
       outputBlockFormatter: [Function: outputBlockFormatter],
       inputLogFormatter: [Function: inputLogFormatter],
       outputLogFormatter: [Function: outputLogFormatter],
       inputPostFormatter: [Function: inputPostFormatter],
       outputPostFormatter: [Function: outputPostFormatter],
       inputAddressFormatter: [Function: inputAddressFormatter],
       outputSyncingFormatter: [Function: outputSyncingFormatter] },
    contractModuleFactory:
     ContractModuleFactory {
       utils: [Object],
       formatters: [Object],
       abiCoder: [AbiCoder],
       accounts: [Accounts],
       methodModuleFactory: [ModuleFactory] },
    methodModuleFactory:
     ModuleFactory {
       accounts: [Accounts],
       subscriptionsFactory: [SubscriptionsFactory],
       formatters: [Object],
       utils: [Object] },
    abiCoder: AbiCoder { utils: [Object], ethersAbiCoder: [AbiCoder] } },
 abiModel: AbiModel { abi: { methods: [Object], events: {} } },
 methods:
  MethodsProxy {
    contract: [Circular],
    abiModel: AbiModel { abi: [Object] },
    methodFactory:
     MethodFactory {
       accounts: [Accounts],
       utils: [Object],
       formatters: [Object],
       contractModuleFactory: [ContractModuleFactory],
       methodModuleFactory: [ModuleFactory],
       abiCoder: [AbiCoder] },
    methodEncoder: MethodEncoder { abiCoder: [AbiCoder] },
    methodOptionsValidator: MethodOptionsValidator { utils: [Object] },
    methodOptionsMapper:
     MethodOptionsMapper { utils: [Object], formatters: [Object] },
    PromiEvent: [Function: PromiEvent] },
 events:
  EventSubscriptionsProxy {
    contract: [Circular],
    eventSubscriptionFactory:
     EventSubscriptionFactory { utils: [Object], formatters: [Object] },
    abiModel: AbiModel { abi: [Object] },
    eventOptionsMapper:
     EventOptionsMapper {
       formatters: [Object],
       eventFilterEncoder: [EventFilterEncoder] },
    eventLogDecoder: EventLogDecoder { abiCoder: [AbiCoder] },
    allEventsLogDecoder: AllEventsLogDecoder { abiCoder: [AbiCoder] },
    allEventsOptionsMapper:
     AllEventsOptionsMapper {
       formatters: [Object],
       allEventsFilterEncoder: [AllEventsFilterEncoder] },
    PromiEvent: [Function: PromiEvent] } }

我也嘗試從 json 文件中獲取 ABI,同樣的問題。

有人可以幫助我嗎?謝謝 :)

我通過降級解決了它web3.1.0.0-beta.37。這是我能找到的最新版本,沒有這個問題。

我在我的反應應用程序中遇到了同樣的問題,web3js 1.0@betaXX所以我切換到ethers.js

這是我使用 ethers.js 的方式。注意:我在其他地方使用 web3js 實例。我只是使用 ethers.js 與合約進行互動。但實際上你可以用 ethers.js 完全替換 web3js。

let web3 = this.props.web3; // or wherever you get your web3 instance from
let provider = new ethers.providers.Web3Provider(web3.givenProvider);
let deth = new ethers.Contract(
 contractAddress,
 abi,
 provider
);

希望有幫助!

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