I have the following code to create a contract. I am using solidity 0.4.24. The type of minPrice is string and it's 0.001 from the input. For some reason, I am getting the value of 0 on the contract. I see that on my front end and also, loaded the contract on remix. Has anyone run into something similar?
await factory.methods.createAccount(
web3.utils.fromAscii(publicDataKey),
web3.utils.fromAscii(privateDataKey),
web3.utils.toWei(minPrice, 'ether')
).send({
from: accounts[0]
});
Related
-----START EDIT-----
I don't know what I was doing wrong before, but the code below was somehow not working for me, and now is working, and it's exactly the same. I don't know how or what I was missing before, but both this minimal example and the real project I am working on is working now. Obviously I changed something, but I can't figure out what. I just know it's working now. Sorry for the confusion and thanks to everyone for helping.
-----END EDIT-----
I am new to Solidity and am using the Factory pattern for deploying a contract from another contract. I am trying to get the contract address of the deployed contract, but I am running into errors.
I already tried the solution in this question, but I'm getting the following error: Return argument type struct StorageFactory.ContractData storage ref is not implicitly convertible to expected type (type of first return variable) address.
Here is my code:
// START EDIT (adding version)
pragma solidity ^0.8.0;
// END EDIT
contract StorageFactory {
struct ContractData {
address contractAddress; // I want to save the deployed contract address in a mapping that includes this struct
bool exists;
}
// mapping from address of user who deployed new Storage contract => ContractData struct (which includes the contract address)
mapping(address => ContractData) public userAddressToStruct;
function createStorageContract(address _userAddress) public {
// require that the user has not previously deployed a storage contract
require(!userAddressToStruct[_userAddress].exists, "Account already exists");
// TRYING TO GET THE ADDRESS OF THE NEWLY CREATED CONTRACT HERE, BUT GETTING AN ERROR
address contractAddress = address(new StorageContract(_userAddress));
// trying to save the contractAddress here but unable to isolate the contract address
userAddressToStruct[_userAddress].contractAddress = contractAddress;
userAddressToStruct[_userAddress].exists = true;
}
}
// arbitrary StorageContract being deployed
contract StorageContract {
address immutable deployedBy;
constructor(address _deployedBy) {
deployedBy = _deployedBy;
}
}
How can I get this contract address, so I can store it in the ContractData struct? Thanks.
I compiled your contract, deployed it on Remix, and interacted without issue with this setting
pragma solidity >=0.7.0 <0.9.0;
I think you had this in your contract before
userAddressToStruct[_userAddress] = contractAddress;
instead of this
userAddressToStruct[_userAddress].contractAddress = contractAddress;
You can use the following code to get the address of the deployed contract:
address contractAddress;
(contractAddress,) = new StorageContract(_userAddress);
userAddressToStruct[_userAddress].contractAddress = contractAddress;
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.12;
struct account{
string _name;
uint _acc_id;
uint balance;
}
contract My_acc{
account public person;
constructor(string memory name, uint acc_id, uint _balance){
person._name = name;
person._acc_id = acc_id;
person.balance = _balance;
}
}
I am trying to pass values to struct variable through constructor But I am getting this following error.
creation of My_acc errored: Error encoding arguments: Error: invalid BigNumber string (argument="value", value="", code=INVALID_ARGUMENT, version=bignumber/5.5.0)
I tried running the same code and it worked just fine.
Constructor runs only once and that is at the time of deployment. Since you are passing the values to constructor, you should pass the values at the time of deployment.
My reputation does not seem to allow me to upload the image. Please go through the link below.
https://i.stack.imgur.com/AFBWd.png
So guys, I resolved the error. It wasn't actually an error. It was just a silly mistake that I made as a beginner.
SOLUTION: -
I wasn't actually passing values to constructor initially. Actually when we add constructor in our contract we basically get a input section with our deploy button automatically (even before actually deploying). which have to be used to pass values before deploying our contract.
I woudlike to estimateGas after an approve contract :
WETH = weth.address;
USDC = usdc.address;
await usdc.approve(addr1, addr2).estimateGas;
When I try this I have this error :
TypeError: usdt.approve(...).estimateGas is not a function
Truffle uses different syntax. You need to pass the function params to the estimateGas(), not to the approve() function.
await usdc.approve.estimateGas(addr1, addr2);
Docs: http://trufflesuite.com/docs/truffle/getting-started/interacting-with-your-contracts#special-methods-on-truffle-contract-objects
Note: Your code defines usdc and then the error message states usdt. I'm assuming that's just a typo while creating the question, unrelated to the original issue.
I deployed following contract in remix.ethereum
0x932FC462d97e23E9fe8d5a1F085d9D611B892666
and hooked it up to my UI at
https://tewkenbak.github.io/tewken/ (this is a contract for testing)
Following things happened
1) in remix.ethereum i wasnt able to compile the contract - no errors
2) i was able to deploy the contract to the mainnet
3) the contract doesnt give me any errors when interacting with it at
https://etherscan.io/address/0x932fc462d97e23e9fe8d5a1f085d9d611b892666#writeContract
4) when I try to interact with contract through my UI - above link i get following Error
the same time I get NO error message in the console
UPDATE
I just checked your source code, looks like you gave the wrong method name here and some other places:
if (walletMode === 'metamask') {
contract.buy(masternode, {
value: convertEthToWei(amount)
}, function (e, r) {
console.log(e, r)
})
}
there is no buy function in the contract.
You gave the wrong abi, since there is no function named buy in your smart contract.
In the http://truffleframework.com/tutorials/pet-shop example, there is the following contract, and the function adopt(uint petId) has only one parameter.
contract Adoption {
address[16] public adopters;
function adopt(uint petId) public returns (uint) {
require(petId >= 0 && petId <= 15);
adopters[petId] = msg.sender;
return petId;
}
function getAdopters() public returns (address[16]) {
return adopters;
}
}
However, in the javascript code app.js, the handleAdopt function call the contract function using the following code.
App.contracts.Adoption.deployed().then(function(instance) {
adoptionInstance = instance;
return adoptionInstance.adopt(petId, {from: account});
})
The function is called with the extra object {from: account}. Why? And is this parameter discarded in the solidity code?
BTW, there is an undefined global variable web3? Is the value be assigned by the MetaMask extension?
That is the transactionObject which describes general information about all transaction calls (gas limit, price, amount of ether to send, etc.). The JS code you posted is using the web3 library. That's not the direct call to the contract API. The web3 library converts it to an RPC. The transactionObject comes after all of the contract parameters. There is another parameter that comes after which is the callback with the results of the contract call (see here).
These are all of the options for the transactionobject described from the docs:
from: String - The address for the sending account. Uses the web3.eth.defaultAccount property, if not specified.
to: String - (optional) The destination address of the message, left undefined for a contract-creation transaction.
value: Number|String|BigNumber - (optional) The value transferred for the transaction in Wei, also the endowment if it's a contract-creation transaction.
gas: Number|String|BigNumber - (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded).
gasPrice: Number|String|BigNumber - (optional, default: To-Be-Determined) The price of gas for this transaction in wei, defaults to the mean network gas price.
data: String - (optional) Either a byte string containing the associated data of the message, or in the case of a contract-creation transaction, the initialisation code.
nonce: Number - (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce.