New answers tagged web3js
0
votes
@ethereumjs/tx - "TypeError: Common is not a constructor"
If you are using import statement a workaround looks like this :
import {default as common} from '@ethereumjs/common';
const Common = common.default
const c = new Common({ chain: Chain.Mainnet, ...
1
vote
Why am I failing to interact with a smart contract I found on Etherscan.io?
That's because nothing is deployed in your Ganache's address 0x2929E21109901461659C0F26aD7f0e7633Ea6539.
You need to execute this on mainnet, to receive an answer from that contract.
0
votes
swapExactTokensForTokens estimateGas fails with execution reverted
This may have to do with the state of the LUNA liquidity pool. You may be trying to buy more LUNA than exists, given your inputs and the low price of LUNA. Try using the periphery contract quote() ...
0
votes
Can I store ether and ERC-20 tokens in one contract?
Suppose this contract can store 3 kinds of tokens. When people transfer these tokens to the contract, how the contract itself knows which token was sent?
Tokens are transferred using IERC20(address)....
0
votes
How to Buy with WBNB instead of BNB
I do not know if I fully understand your question, but when you use the swapExactETHForTokens function you are actually buying using wBNB, not BNB. If the router notices that you do not have enough ...
0
votes
How to get state of transaction from ethereum blockchain via an API
The web3 library is the usual way to do it!
https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#gettransaction
0
votes
TypeError: this.web3.eth.getMaxPriorityFeePerGas is not a function
Found solution here.
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
const alch = createAlchemyWeb3('https://eth-mainnet.alchemyapi.io/v2/demo');
alch.eth.getMaxPriorityFeePerGas()....
0
votes
Get all the past events of the contract
web3-ethereum-defi package now has a ultra fast concurrent event reader using thread pools. It can read multiple events over block ranges using concurrency and maximizing the potential performance ...
0
votes
sign transaction in web3 with metamask
As far as I know it's not possible to sign transaction with Metamask. I can imagine it's due to security reasons (some evil dapp could sign tx and then do not send it in network until it can execute ...
0
votes
Creating Metamask wallets using a script
Metamask uses "Hierarchical Deterministic Wallets", see What is a HD wallet for Ether and how to create one using nodejs?.
There are several libraries that you can use to manage and create ...
0
votes
Accepted
Nothing happens when I call web3.eth.sendTransaction
Found it after a few days, crypto is ban in Pakistan so I had to use a proxy.
0
votes
Subscribing to new block headers using python (web3py)
The most straightforward solution is to use filters. You can find several filter usage examples in web3.py docs. Here is the synchronous example:
from web3.auto import w3
import time
def handle_event(...
1
vote
How to call a contract's method via web3 1.x without a node (sign with a privateKey and send)?
Assuming you have a proper Web3 instance setup, the below is how I generally make contract calls from the server using a class (or where you have a fixed key that requires no user input). I use an ...
0
votes
How to sign a transaction using web3.js and ethereumjs-tx on the BSC testnet
First argument can only have 5 possible values, mainnet, ropsten, rinkeby, goerli and kovan. You can confirm here.
But you can work around that by overriding parameters with second argument.
So in ...
1
vote
I'm receiving undeclared identifier when calling this function from the free function outside of the contract
You need to pass 2 intergers on taker. e.g. uint s = taker(1,2);
0
votes
Get the 'from' address of a transaction from the smart contract event log?
No, this data isn't saved in logs, unless a contract explicitly emits it as one of its parameters.
Here's an example of a log event -
"logs": [
{
"transactionIndex&...
1
vote
Is there a way to retrieve private key of ganache account with web3?
According to the Ganache CLI documentation, during the setup of your ganache instance you can specify a path to save the accounts and private keys to.
ganache.server({ account_keys_path: "src/...
0
votes
how to calculate the correct amount with decimal
This code works with web3 without adding any additional libraries for any value of decimals:
const balance = await contract.methods.balanceOf(address).call();
const decimals = await contract.methods....
0
votes
Force two transactions in the same block to pay fees from another account
Not entirely sure if it is possible to force two transactions, especially from two different address to be in the same block, I would say your best bet would be to use Flashbots, they were created to ...
1
vote
Accepted
Can I store random data with eth_sendTransaction?
You can store a string, but encoded as a hex string. To store 'I love lamp' you need to set data: '0x49206c6f7665206c616d70'.
Use utf8ToHex to convert a string to hex bytes -
web3.utils.utf8ToHex('I ...
0
votes
How to pass bytes parameter to contract method call using Web3.js?
bytes is a variable type that holds a sequence of bytes from 1 to up to 32.
Read about it in the docs.
On Web3 you use numberToHex or utf8ToHex for strings.
3
votes
Accepted
Web3 make NodeJS wait until a promise gets resolved inside a subscription?
In order to subscribe to events, you need a Websocket endpoint. This is your code updated to use WSS:
var Web3 = require('web3')
async function waitForFunds() {
let web3 = new Web3(
new Web3....
0
votes
How to estimate gas for my function using web3 js only?
You can run estimateGas without using a contract, this is useful when you don't have the ABI but got the input data and want to estimate gas cost before submitting the transaction, or if you just don'...
0
votes
Can we have communications in a web-based dapp?
Arbo94,
The concept of oracles is for querying data from the off-chain world and executing smart contract functions from this data.
It's not to trigger external services like sending email, for ...
0
votes
How to disconnect MetaMask wallet using Web3modal
As told by Fredo Corleone we cannot disconnect the wallet in anyways, but rather we can pretend. This is totally true as of today. We can pretend to disconnect by using the following line of code:
...
0
votes
Accepted
ecrecover returns wrong address if toSignString consists of only number
Your issue is here :
let hash = web3.utils.soliditySha3("String");
As per the documentation, a non numerical string will be interpreted as string while a numerical string input will be ...
1
vote
Solidity want to ask a person to respond to a question by signing via their wallet
You can't communicate with anything outside the blockchain using Solidity.
The entire process you describes is executed by a front end app, like ReactJS, communicating with MetaMask.
The voter will ...
0
votes
I'm receiving undeclared identifier when calling this function from the free function outside of the contract
You'll need to post the full code to get a more conclusive answer, but first thing I see is that you haven't declared the above contract (or interface/library) into the seeing contract.
You'll need ...
0
votes
Web3 make NodeJS wait until a promise gets resolved inside a subscription?
I think that your function waitForFunds should be asynchronous like so in order to wait for the Promise to resolve: async function waitForFunds() {}
0
votes
Web3 js 1.0 is throwing big number error while transfering tokens
<pre>
import { BigNumber} from 'ethers';
....
const decimals = 18;
const trans_amount = 1500;
const amount= BigNumber.from(trans_amount).mul(BigNumber.from(10).pow(decimals));
consoel.og(&...
1
vote
How to connect web3.js 1.3.4 to MetaMask?
Had the same problem. I got:
MetaMask no longer injects web3. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3
I followed the link and was able to ...
0
votes
Constantly changing msg.value, how to send correct amount
There's no way of "pulling" ether from the user, so the only solution is to generate a big msg.value and return any extra at the end of the transaction. This value can be thought as the max ...
0
votes
How can i generate two equal signature with web3 and solidity?
You can import the ECDSA.sol from OpenZeppelin to verify signature.
In solidity:
function _recoverSigner(bytes32 hash, bytes memory _signature) internal returns(address) {
return ECDSA....
1
vote
How to get the blocks between timestamp
Pi's response is pretty good. But, I've ran into rare situations where the +r/-r excludes tunix_s, and then it recurses until recursion depth limit exceeded. These adjustments fix that issue:
def ...
0
votes
How can one secure Web3 JSON-RPC API endpoint?
I suggest you to use Caddy as a reverse proxy with HTTP Basic Authentication. Caddy comes with built-in TLS (HTTPS) certificate manager which makes the setup easier.
Here is an example Caddyfile ...
1
vote
Is it possible to decrypt cypher by signing from metamask?
So, is there a way to encrypt the message publicly such that it gets decrypted when user sings the encrypted message from the metamask wallet ??
There is not.
1
vote
Error: insufficient funds for intrinsic transaction cost on Ropsten
I had a similar problem with the Hello World tutorial on Ethereum.org. Turns out, if you switch your test network from ropsten to goerli, this problem goes away.
0
votes
Is it possible to decrypt cypher by signing from metamask?
I don't think Metamask supports that kind of two-way signing. Maybe some other wallet does, but I kinda doubt it, since that's not a common use case.
0
votes
How can I detect change in account in Metamask?
You use chainChanged event but you have to consider 3 things:
You have to set listener globally once. Either in navbar, or if using provider in provider or in next.js in _app component
When chain ...
1
vote
How to make connection between two different smart contract address
You have to create an interface with all the methods of your contract XYZ
interface IXYZ {
function doSomething() external;
}
and then you call it inside your contract where you want to use it with ...
1
vote
How to get value of a contract public property using web3.eth
None of the other answers work for me. This does:
import Web3 from "web3";
const address = "0xe9e7...";
const abi = "[{...";
const web3 = new Web3(); // pass your ...
1
vote
Accepted
In Solidity, when invoking an external contract, how do you override its library functions using a separately deployed contract?
Having quickly read the tweet, it appears the author is referring to library linking (https://docs.soliditylang.org/en/latest/using-the-compiler.html?highlight=linking#library-linking)
External ...
1
vote
sendTransaction returning “insufficient funds” when the account has enough ether
I think your problem with sendTransaction was because you didn't have any accounts imported in the web3 object so, sendTransaction was unable to sign it.
You can import a wallet in the web3 object ...
1
vote
How to connect to metamask from web app on mobile devices and trigger a smart contract call?
You will have to use MetaMask DApp Browser. MetaMask is a java script enabled extension that's why You need an app to support javascript extensions. But in MetaMask mobile app DApp browser metamsk is ...
0
votes
How to connect to metamask from web app on mobile devices and trigger a smart contract call?
please check these links for android os:
https://medium.com/@nschapeler/an-introduction-to-ethereum-development-on-android-using-web3j-and-infura-763940719997
https://github.com/wise4rmgod/Web3Js-...
0
votes
Getting uniswap v2 ETH/USDC price from reserves with web3
Here is a ready made Python library example for calculating prices for Uniswap v2-likes.
# Estimate how much ETH we will receive for 500 USDC.
# In this case the pool ETH price is $1700 so this should ...
1
vote
Accepted
How to convert wei value of USDC balance to proper number?
The fromWei function converts from wei to ether, meaning it divides the number by 1e18.
ERC20 tokens allows a custom decimal point precision, USDC specifically uses 6 decimal precision. So you just ...
1
vote
Accepted
Metamask not switching chain to Rinkeby testnet
I think you mixed up the functions for "switching" and for "adding" a network. The function to switch network should only have to pass the chainId.
This is what my code looks like:
...
1
vote
Do we need to pass Base_URI when we use URI_Storage in solidity openzeppelin
You don't have to. You can check out the logic of ERC721URIStorage below, it said that by default it will use your defined uri if no base_uri is defined, otherwise it will use <base_uri> + <...
1
vote
How to convert wei value of USDC balance to proper number?
One ETH is 10"18 wei.
So when you transfer 1ETH you actually transfer 10"18 wei. All because one ETH has 18 decimal places.
USDC, on other hand, has 6 decimal places. (Same as todays banks ...
Top 50 recent answers are included
Related Tags
web3js × 5687solidity × 1523
go-ethereum × 731
metamask × 596
truffle × 467
javascript × 450
nodejs × 401
transactions × 361
contract-development × 345
blockchain × 325
ethereumjs × 266
events × 251
dapps × 236
react × 196
infura × 187
contract-deployment × 185
contract-invocation × 183
erc-20 × 179
ether × 176
remix × 164
dapp-development × 159
web3-providers × 156
ethers.js × 152
tokens × 137
web3.py × 137