web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.15k stars 4.9k forks source link

Uncaught TypeError: https.Agent is not a constructor #4453

Closed thisprojectworks closed 2 years ago

thisprojectworks commented 2 years ago

Is there an existing issue for this?

Current Behavior

I am developing a dapp with Vue 3, the error when I try to instantiate web3 with an RPC of HTTPS or HTTP.

The error is as follows:

Uncaught TypeError: https.Agent is not a constructor

After doing some research, I have been able to verify that the error comes from the web3-providers-http module.

Expected Behavior

When I configure the Metamask provider (window.ethereum) everything works fine. Since I can do write and read transactions, without problem on the blockchain. What I hope is that it works correctly without the error and can make transactions.

Steps to Reproduce

import Web3 from "web3"

const web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545/')

const contract = web3.eth.Contract([{inputs:[{internalType:"string",name:"name",type:"string"}], '0x094616f0bdfb0b526bd735bf66eca0ad254ca81f')

contract.methods.name().call()

Web3.js Version

1.6.0

Environment

Anything Else?

No response

nazarhussain commented 2 years ago

@thisprojectworks Thanks for reporting the issue. We will investigate it further and get back to you.

thisprojectworks commented 2 years ago

@thisprojectworks Thanks for reporting the issue. We will investigate it further and get back to you.

Thank you, on the other hand I posted the issue in node.js repository, and they comment this:

Judging by the "web3.js" readme, it looks like my hunch is correct. There is even a known issues list where it's discussing node API polyfills and browserify.

So while your code may be executing on the backend, the backend code is most likely being bundled with polyfills during the bundle/webpack/browserify/etc. process, which override the real built-in node.js modules.

https://github.com/nodejs/help/issues/3574#issuecomment-941861607

spacesailor24 commented 2 years ago

I'm having trouble running the code you provided. There were some syntax errors that I corrected and running the following code:

const Web3 = require("web3") // I changed this from importing, but that shouldn't affect things

const web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545/')
const contract = new web3.eth.Contract(
    [
        {
            inputs: [{ internalType:"string", name:"name", type:"string" }],
        }
    ],
    '0x094616f0bdfb0b526bd735bf66eca0ad254ca81f'
)

contract.methods.name().call()

Gives the following error:

contract.methods.name().call()
                 ^

TypeError: contract.methods.name is not a function

I found the transaction that created the contract on Binance's block explorer, but I'm not sure how to get the ABI from the transaction's input data:

0x60806040523480156200001157600080fd5b50604051620019cc380380620019cc833981016040819052620000349162000256565b6200003f33620000a9565b825162000054906001906020860190620000f9565b5081516200006a906002906020850190620000f9565b5060048190556040805160608101909152602d8082526200199f602083013980516200009f91600991602090910190620000f9565b505050506200031c565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b8280546200010790620002c9565b90600052602060002090601f0160209004810192826200012b576000855562000176565b82601f106200014657805160ff191683800117855562000176565b8280016001018555821562000176579182015b828111156200017657825182559160200191906001019062000159565b506200018492915062000188565b5090565b5b8082111562000184576000815560010162000189565b600082601f830112620001b157600080fd5b81516001600160401b0380821115620001ce57620001ce62000306565b604051601f8301601f19908116603f01168101908282118183101715620001f957620001f962000306565b816040528381526020925086838588010111156200021657600080fd5b600091505b838210156200023a57858201830151818301840152908201906200021b565b838211156200024c5760008385830101525b9695505050505050565b6000806000606084860312156200026c57600080fd5b83516001600160401b03808211156200028457600080fd5b62000292878388016200019f565b94506020860151915080821115620002a957600080fd5b50620002b8868287016200019f565b925050604084015190509250925092565b600181811c90821680620002de57607f821691505b602082108114156200030057634e487b7160e01b600052602260045260246000fd5b50919050565b634e487b7160e01b600052604160045260246000fd5b611673806200032c6000396000f3fe6080604052600436106101355760003560e01c8063715018a6116100ab5780639dc29fac1161006f5780639dc29fac14610303578063a457c2d714610316578063a5500c3014610336578063a9059cbb14610356578063dd62ed3e14610376578063f2fde38b146103bc57600080fd5b8063715018a61461027e57806376f3bdb6146102935780638da5cb5b146102b357806395d89b41146102db57806398c93ad0146102f057600080fd5b806323b872dd116100fd57806323b872dd146101dc578063313ce567146101fc578063395093511461021857806340c10f1914610238578063512d7cfd1461024b57806370a082311461025e57600080fd5b806301e336671461013a57806306fdde031461014f578063095ea7b31461017a57806318160ddd146101aa5780631b9a91a4146101c9575b600080fd5b61014d610148366004611236565b6103dc565b005b34801561015b57600080fd5b50610164610566565b604051610171919061140f565b60405180910390f35b34801561018657600080fd5b5061019a6101953660046111d1565b6105f8565b6040519015158152602001610171565b3480156101b657600080fd5b506003545b604051908152602001610171565b61014d6101d73660046111d1565b61060e565b3480156101e857600080fd5b5061019a6101f7366004611236565b6106c7565b34801561020857600080fd5b5060405160128152602001610171565b34801561022457600080fd5b5061019a6102333660046111d1565b610771565b61014d6102463660046111d1565b6107ad565b61014d610259366004611277565b6107e5565b34801561026a57600080fd5b506101bb6102793660046111ad565b61085c565b34801561028a57600080fd5b5061014d6108a5565b34801561029f57600080fd5b5061014d6102ae3660046111ad565b6108db565b3480156102bf57600080fd5b506000546040516001600160a01b039091168152602001610171565b3480156102e757600080fd5b50610164610927565b61014d6102fe36600461136b565b610936565b61014d6103113660046111d1565b61096c565b34801561032257600080fd5b5061019a6103313660046111d1565b6109a0565b34801561034257600080fd5b5061014d6103513660046113dd565b610a39565b34801561036257600080fd5b5061019a6103713660046111d1565b610a68565b34801561038257600080fd5b506101bb6103913660046111fd565b6001600160a01b03918216600090815260086020908152604080832093909416825291909152205490565b3480156103c857600080fd5b5061014d6103d73660046111ad565b610a75565b6000546001600160a01b0316331461040f5760405162461bcd60e51b81526004016104069061150c565b60405180910390fd5b6040516370a0823160e01b81523060048201526000906001600160a01b038416906370a082319060240160206040518083038186803b15801561045157600080fd5b505afa158015610465573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061048991906113f6565b905081610494578091505b6000821180156104a45750818110155b6104dd5760405162461bcd60e51b815260206004820152600a60248201526918985908185b5bdd5b9d60b21b6044820152606401610406565b60405163a9059cbb60e01b81526001600160a01b0385811660048301526024820184905284169063a9059cbb90604401602060405180830381600087803b15801561052757600080fd5b505af115801561053b573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061055f9190611349565b5050505050565b60606001805461057590611570565b80601f01602080910402602001604051908101604052809291908181526020018280546105a190611570565b80156105ee5780601f106105c3576101008083540402835291602001916105ee565b820191906000526020600020905b8154815290600101906020018083116105d157829003601f168201915b5050505050905090565b6000610605338484610b10565b50600192915050565b6000546001600160a01b031633146106385760405162461bcd60e51b81526004016104069061150c565b4781610642578091505b6000821180156106525750818110155b61068b5760405162461bcd60e51b815260206004820152600a6024820152696e6f2062616c616e636560b01b6044820152606401610406565b6040516001600160a01b0384169083156108fc029084906000818181858888f193505050501580156106c1573d6000803e3d6000fd5b50505050565b60006106d4848484610c7b565b6001600160a01b0384166000908152600860209081526040808320338452909152902054828110156107595760405162461bcd60e51b815260206004820152602860248201527f45524332303a207472616e7366657220616d6f756e74206578636565647320616044820152676c6c6f77616e636560c01b6064820152608401610406565b6107668533858403610b10565b506001949350505050565b3360008181526008602090815260408083206001600160a01b038716845290915281205490916106059185906107a8908690611541565b610b10565b6000546001600160a01b031633146107d75760405162461bcd60e51b81526004016104069061150c565b6107e18282610e89565b5050565b60005b825181101561085757828181518110610803576108036115dc565b60200260200101516001600160a01b0316306001600160a01b031660008051602061161e8339815191528460405161083d91815260200190565b60405180910390a38061084f816115ab565b9150506107e8565b505050565b6001600160a01b03811660009081526006602052604081205460ff1661088457505060045490565b506001600160a01b031660009081526007602052604090205490565b919050565b6000546001600160a01b031633146108cf5760405162461bcd60e51b81526004016104069061150c565b6108d96000610f71565b565b6000546001600160a01b031633146109055760405162461bcd60e51b81526004016104069061150c565b600580546001600160a01b0319166001600160a01b0392909216919091179055565b60606002805461057590611570565b6000546001600160a01b031633146109605760405162461bcd60e51b81526004016104069061150c565b61085760098383611109565b6000546001600160a01b031633146109965760405162461bcd60e51b81526004016104069061150c565b6107e18282610fc1565b3360009081526008602090815260408083206001600160a01b038616845290915281205482811015610a225760405162461bcd60e51b815260206004820152602560248201527f45524332303a2064656372656173656420616c6c6f77616e63652062656c6f77604482015264207a65726f60d81b6064820152608401610406565b610a2f3385858403610b10565b5060019392505050565b6000546001600160a01b03163314610a635760405162461bcd60e51b81526004016104069061150c565b600455565b6000610605338484610c7b565b6000546001600160a01b03163314610a9f5760405162461bcd60e51b81526004016104069061150c565b6001600160a01b038116610b045760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608401610406565b610b0d81610f71565b50565b6001600160a01b038316610b725760405162461bcd60e51b8152602060048201526024808201527f45524332303a20617070726f76652066726f6d20746865207a65726f206164646044820152637265737360e01b6064820152608401610406565b6001600160a01b038216610bd35760405162461bcd60e51b815260206004820152602260248201527f45524332303a20617070726f766520746f20746865207a65726f206164647265604482015261737360f01b6064820152608401610406565b6001600160a01b03831660009081526006602052604090205460ff1680610bf857503a155b600990610c185760405162461bcd60e51b81526004016104069190611464565b506001600160a01b0383811660008181526008602090815260408083209487168084529482529182902085905590518481527f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92591015b60405180910390a3505050565b6001600160a01b038316610cdf5760405162461bcd60e51b815260206004820152602560248201527f45524332303a207472616e736665722066726f6d20746865207a65726f206164604482015264647265737360d81b6064820152608401610406565b6001600160a01b038216610d415760405162461bcd60e51b815260206004820152602360248201527f45524332303a207472616e7366657220746f20746865207a65726f206164647260448201526265737360e81b6064820152608401610406565b6001600160a01b03831660009081526006602052604090205460099060ff16610d7d5760405162461bcd60e51b81526004016104069190611464565b506001600160a01b03831660009081526007602052604090205481811015610df65760405162461bcd60e51b815260206004820152602660248201527f45524332303a207472616e7366657220616d6f756e7420657863656564732062604482015265616c616e636560d01b6064820152608401610406565b6001600160a01b03808516600090815260076020526040808220858503905591851681529081208054849290610e2d908490611541565b90915550506001600160a01b0380841660008181526006602052604090819020805460ff1916600117905551909186169060008051602061161e83398151915290610e7b9086815260200190565b60405180910390a350505050565b6001600160a01b038216610edf5760405162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f2061646472657373006044820152606401610406565b8060036000828254610ef19190611541565b90915550506001600160a01b03821660009081526007602052604081208054839290610f1e908490611541565b90915550506001600160a01b038216600081815260066020526040808220805460ff191660011790555160008051602061161e83398151915290610f659085815260200190565b60405180910390a35050565b600080546001600160a01b038381166001600160a01b0319831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b6001600160a01b0382166110215760405162461bcd60e51b815260206004820152602160248201527f45524332303a206275726e2066726f6d20746865207a65726f206164647265736044820152607360f81b6064820152608401610406565b6001600160a01b038216600090815260076020526040902054818110156110955760405162461bcd60e51b815260206004820152602260248201527f45524332303a206275726e20616d6f756e7420657863656564732062616c616e604482015261636560f01b6064820152608401610406565b6001600160a01b03831660009081526007602052604081208383039055600380548492906110c4908490611559565b90915550506001600160a01b0383166000818152600660209081526040808320805460ff191690555185815291929160008051602061161e8339815191529101610c6e565b82805461111590611570565b90600052602060002090601f016020900481019282611137576000855561117d565b82601f106111505782800160ff1982351617855561117d565b8280016001018555821561117d579182015b8281111561117d578235825591602001919060010190611162565b5061118992915061118d565b5090565b5b80821115611189576000815560010161118e565b80356108a081611608565b6000602082840312156111bf57600080fd5b81356111ca81611608565b9392505050565b600080604083850312156111e457600080fd5b82356111ef81611608565b946020939093013593505050565b6000806040838503121561121057600080fd5b823561121b81611608565b9150602083013561122b81611608565b809150509250929050565b60008060006060848603121561124b57600080fd5b833561125681611608565b9250602084013561126681611608565b929592945050506040919091013590565b6000806040838503121561128a57600080fd5b823567ffffffffffffffff808211156112a257600080fd5b818501915085601f8301126112b657600080fd5b81356020828211156112ca576112ca6115f2565b8160051b604051601f19603f830116810181811086821117156112ef576112ef6115f2565b604052838152828101945085830182870184018b101561130e57600080fd5b600096505b8487101561133857611324816111a2565b865260019690960195948301948301611313565b509997909101359750505050505050565b60006020828403121561135b57600080fd5b815180151581146111ca57600080fd5b6000806020838503121561137e57600080fd5b823567ffffffffffffffff8082111561139657600080fd5b818501915085601f8301126113aa57600080fd5b8135818111156113b957600080fd5b8660208285010111156113cb57600080fd5b60209290920196919550909350505050565b6000602082840312156113ef57600080fd5b5035919050565b60006020828403121561140857600080fd5b5051919050565b600060208083528351808285015260005b8181101561143c57858101830151858201604001528201611420565b8181111561144e576000604083870101525b50601f01601f1916929092016040019392505050565b600060208083526000845481600182811c91508083168061148657607f831692505b8583108114156114a457634e487b7160e01b85526022600452602485fd5b8786018381526020018180156114c157600181146114d2576114fd565b60ff198616825287820196506114fd565b60008b81526020902060005b868110156114f7578154848201529085019089016114de565b83019750505b50949998505050505050505050565b6020808252818101527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604082015260600190565b60008219821115611554576115546115c6565b500190565b60008282101561156b5761156b6115c6565b500390565b600181811c9082168061158457607f821691505b602082108114156115a557634e487b7160e01b600052602260045260246000fd5b50919050565b60006000198214156115bf576115bf6115c6565b5060010190565b634e487b7160e01b600052601160045260246000fd5b634e487b7160e01b600052603260045260246000fd5b634e487b7160e01b600052604160045260246000fd5b6001600160a01b0381168114610b0d57600080fdfeddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa2646970667358221220fd5003565070cd4478ee31d908c69ac8663e049f7f4386b6e238dc57945a690a64736f6c63430008070033436c61696d205a455045206f6e2057656273697465204649525354202d3a207777772e5a6570652e7669702020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000009ed194db19b238c0000000000000000000000000000000000000000000000000000000000000000000075a6570652e696f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075a6570652e696f00000000000000000000000000000000000000000000000000

So could you provide the correct json-interface (the complete ABI) for your contract? A test repo that reproduces the issues you're seeing would be even better in aiding debugging of this

spacesailor24 commented 2 years ago

This might have something to do with your client url (https://data-seed-prebsc-1-s1.binance.org:8545/) not being a valid endpoint for submiting the request to

Using the following code (connects to a different Binance chain node):

const Web3 = require("web3");

(async () => {
    // const web3 = new Web3('https://data-seed-prebsc-1-s1.binance.org:8545/')
    // ^ Your original Client URL
    const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
    const contract = new web3.eth.Contract(
        [{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Paused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":false,"internalType":"uint256","name":"value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"}],"name":"Unpaused","type":"event"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MINTER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PAUSER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"spender","type":"address"}],"name":"allowance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"subtractedValue","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"addedValue","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"mint","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpause","outputs":[],"stateMutability":"nonpayable","type":"function"}],
        '0x99956D38059cf7bEDA96Ec91Aa7BB2477E0901DD'
    );

console.log(await contract.methods.getOwner().call())
})()

I get the output:

0x5737bf56559Dd32D8880Cfa236fE613E2162e157

Using the endpoint you provided, the error I receive is:


(node:839588) UnhandledPromiseRejectionWarning: Error: Returned values aren't valid, did it run Out of Gas? You might also see this error if you are not using the correct ABI for the contract you are retrieving data from, requesting data from a block number that does not exist, or querying a node which is not fully synced.
nazarhussain commented 2 years ago

@thisprojectworks I looked into the discussion you referred in your comment. We use Webpack 4 for bundling and internally its uses https-browserify as a polyfill. We don't inject anything by ourself in the build process.

It would be nice if you can try reproducing the issue with some simple script directly with https-browserify or any simple webpack build and if you can then report the issue to them.

Since we tested this issue on our end and we can't reproduce it as @spacesailor24 described. I don't think this issue is anything related to web3 library itself. So I am closing this issue for now, if you find any further facts that issue related to web3, feel free to reopen this issue.

cielu commented 2 years ago

@nazarhussain 这问题还是存在啊,I get the same problem by using ant-design & ts .

T-Damer commented 2 years ago

If you're using vite.js, try adding yarn add agent-base and the in the vite.config.ts:

...
resolve: {
    alias: {
      process: 'process/browser',
      stream: 'stream-browserify',
      util: 'util',
      https: 'agent-base',
      zlib: 'browserify-zlib',
    },
  },
...

This should fix your problems. It happens because the Vite has no node tools inside, trying to use them will cause an error usually, so you need to install them manually

thisprojectworks commented 2 years ago

Thanks for you response @T-Damer , but still the same error: Uncaught ReferenceError: process is not defined

I'm trying to use inside a Vuex Store.

T-Damer commented 2 years ago

@thisprojectworks, I found another solution: yarn add -D @esbuild-plugins/node-globals-polyfill

Inside the vite.config.ts add lines from below. Please notice, that you may not install the agent-base and stream-browserify, it will work anyway (for me at least):

import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill'

export default defineConfig({
...
  optimizeDeps: {
    esbuildOptions: {
      define: {
        global: 'globalThis',
      },
      plugins: [
        GlobalsPolyfills({
          process: true,
          buffer: true,
        }),
      ],
    },
  },
  resolve: {
    alias: {
      stream: 'stream-browserify',
      https: 'agent-base',
    },
  },
...
)}
rodrigoslino commented 2 years ago

@T-Damer thanks for the solution! it worked well!

I also had to run: yarn add events for the EventEmitter in node

baixiaoyu2997 commented 2 years ago

for nuxt3 user ,there is a solution https://github.com/nuxt/framework/discussions/4393

PLQin commented 2 years ago

I also encountered this problem:

Code:

import Web3 from 'web3';
import ZntLockerLens from './ZntLockerLens';

export default function Compon() {
  const web3 = new Web3('https://http-testnet.xx.com');
  const contract = new web3.eth.Contract(
    ZntLockerLens.abi,
    '0x492B7bCD1732FBB82974694dfb6B3FExxDCA9D0A',
  );
  (async () => {
    const result = await contract.methods
      .getLockInfo('0xC02a0c4634B4f2aFBb29b7xx81153E791AdB7D1e')
      .call();
    console.log(result);
  })();

  return (
    <>
      hello world
    </>
  );
}

Error:

Uncaught TypeError: https.Agent is not a constructor
    at new HttpProvider (mf-dep_vendors-node_modules_web3_lib_index_js.ce72c01d.async.js:88933:31)
    at RequestManager.push../node_modules/web3-core-requestmanager/lib/index.js.RequestManager.setProvider (mf-dep_vendors-node_modules_web3_lib_index_js.ce72c01d.async.js:79031:24)
    at new RequestManager (mf-dep_vendors-node_modules_web3_lib_index_js.ce72c01d.async.js:79006:10)
    at Object.packageInit (mf-dep_vendors-node_modules_web3_lib_index_js.ce72c01d.async.js:81282:31)
    at new Web3 (mf-dep_vendors-node_modules_web3_lib_index_js.ce72c01d.async.js:91467:10)
    at Compon (index.tsx:27:16)
    at renderWithHooks (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:14994:18)
    at mountIndeterminateComponent (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:17820:13)
    at beginWork (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:19058:16)
    at HTMLUnknownElement.callCallback (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:3954:14)
    at Object.invokeGuardedCallbackDev (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:4003:16)
    at invokeGuardedCallback (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:4065:31)
    at beginWork$1 (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:23968:7)
    at performUnitOfWork (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:22780:12)
    at workLoopSync (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:22711:5)
    at renderRootSync (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:22674:7)
    at performSyncWorkOnRoot (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:22297:18)
    at mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:11336:26
    at unstable_runWithPriority (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:27147:12)
    at runWithPriority$1 (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:11285:10)
    at flushSyncCallbackQueueImpl (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:11331:9)
    at flushSyncCallbackQueue (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:11318:3)
    at scheduleUpdateOnFiber (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:21897:9)
    at dispatchAction (mf-dep_vendors-node_modules_react-dom_index_js.08f8193e.async.js:16148:5)
    at checkForUpdates (mf-dep_vendors-node_modules_umi_node_modules_umijs_runtime_dist_index_esm_js.61c59d87.async.js:952:7)
    at mf-dep_vendors-node_modules_umi_node_modules_umijs_runtime_dist_index_esm_js.61c59d87.async.js:510:16
    at Set.forEach (<anonymous>)
    at LoadableSubscription._update (mf-dep_vendors-node_modules_umi_node_modules_umijs_runtime_dist_index_esm_js.61c59d87.async.js:509:23)
    at mf-dep_vendors-node_modules_umi_node_modules_umijs_runtime_dist_index_esm_js.61c59d87.async.js:492:15

package.json:

{
  "name": "",
  "private": true,
  "scripts": {
    "start": "yarn start:2",
    "build": "cross-env UMI_ENV=prod umi build ",
    "build-test": "cross-env UMI_ENV=test umi build "
  },
  "lint-staged": {
    "*.{js,jsx,tx,tsx,less,md,json}": [
      "eslint --fix"
    ]
  },
  "dependencies": {
    "@ant-design/charts": "^1.3.6",
    "@ant-design/icons": "^4.7.0",
    "@ant-design/pro-descriptions": "^1.10.5",
    "@ant-design/pro-form": "^1.48.0",
    "@ant-design/pro-layout": "^6.5.0",
    "@ant-design/pro-table": "^2.56.9",
    "ahooks": "^3.0.0-alpha.17",
    "antd": "^4.17.2",
    "axios": "^0.24.0",
    "bignumber.js": "^9.0.2",
    "classnames": "^2.3.1",
    "cookie-js": "^0.0.1",
    "ethers": "^5.5.1",
    "lodash": "^4.17.21",
    "moment": "^2.29.1",
    "path-to-regexp": "^6.2.0",
    "react": "17.x",
    "react-color": "^2.19.3",
    "react-dom": "17.x",
    "umi": "^3.5.20",
    "umi-request": "^1.4.0",
    "web3": "^1.7.3"
  },
  "devDependencies": {
    "@commitlint/cli": "^13.2.1",
    "@commitlint/config-conventional": "^13.2.0",
    "@esbuild-plugins/node-globals-polyfill": "^0.1.1",
    "@types/lodash": "^4.14.177",
    "@types/mockjs": "^1.0.6",
    "@types/react": "^17.0.0",
    "@types/react-dom": "^17.0.0",
    "@typescript-eslint/eslint-plugin": "^5.1.0",
    "@typescript-eslint/parser": "^5.1.0",
    "@umijs/plugin-esbuild": "^1.3.1",
    "@umijs/preset-react": "1.x",
    "@umijs/test": "^3.5.20",
    "cross-env": "^7.0.3",
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-config-standard": "^16.0.3",
    "eslint-plugin-import": "^2.25.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "eslint-plugin-promise": "^5.1.0",
    "eslint-plugin-react": "^7.26.1",
    "husky": "^7.0.0",
    "lint-staged": "^10.0.7",
    "mockjs": "^1.1.0",
    "msw": "^0.35.0",
    "prettier": "^2.4.1",
    "typescript": "^4.1.2",
    "yorkie": "^2.0.0"
  },
  "msw": {
    "workerDirectory": "public"
  }
}
PLQin commented 2 years ago

Ethers solves this problem:

    // import { ethers, Contract, Wallet, getDefaultProvider } from 'ethers';
    // import ZntLockerLens from './ZntLockerLens';

    const provider = new ethers.providers.JsonRpcProvider(
      'https://http-testnet.xx.com',
    );
    const contract = new Contract(
      '0x492B7bCD1732FBB8297a24694dxx6B3FEDCA9D0A',
      ZntLockerLens.abi,
      provider,
    );
    (async () => {
      const result = await contract.getLockInfo(
        '0xC02a0c4634B4f2aF73Bb29bxx1153E791AdB7D1e',
      );
      console.log(result);
    })();
CHENQIKUAI commented 2 years ago

@thisprojectworks, I found another solution: yarn add -D @esbuild-plugins/node-globals-polyfill

Inside the vite.config.ts add lines from below. Please notice, that you may not install the agent-base and stream-browserify, it will work anyway (for me at least):

import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill'

export default defineConfig({
...
  optimizeDeps: {
    esbuildOptions: {
      define: {
        global: 'globalThis',
      },
      plugins: [
        GlobalsPolyfills({
          process: true,
          buffer: true,
        }),
      ],
    },
  },
  resolve: {
    alias: {
      stream: 'stream-browserify',
      https: 'agent-base',
    },
  },
...
)}

I change the https to http, and it workd for me. https: 'agent-base' -> http: 'agent-base'

thisprojectworks commented 2 years ago

@thisprojectworks, I found another solution: yarn add -D @esbuild-plugins/node-globals-polyfill Inside the vite.config.ts add lines from below. Please notice, that you may not install the agent-base and stream-browserify, it will work anyway (for me at least):

import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill'

export default defineConfig({
...
  optimizeDeps: {
    esbuildOptions: {
      define: {
        global: 'globalThis',
      },
      plugins: [
        GlobalsPolyfills({
          process: true,
          buffer: true,
        }),
      ],
    },
  },
  resolve: {
    alias: {
      stream: 'stream-browserify',
      https: 'agent-base',
    },
  },
...
)}

I change the https to http, and it workd for me. https: 'agent-base' -> http: 'agent-base'

This is the best solution find it. Many thanks. Works perfectly.

valkyone commented 1 year ago

@T-Damer I have an error when adding your line of code.

failed to load config from C:\Proyect\vite.config.js error when starting dev server: TypeError: GlobalsPolyfills is not a function

KBKUN024 commented 8 months ago

for nuxt3 user ,there is a solution https://github.com/nuxt/framework/discussions/4393

hello,this link can not works,can you add a new link?My project is also a nuxt3,I don't know how to fix this error