Sale.new({ from: state.account, value: value, gas: 4000000 }).then((instance) => {
router.push({ name: 'sale', params: { address: instance.address } })
}).catch((err) => {
alert(err)
commit(types.UPDATE_LOADING, false)
})
When the above code runs I receive this error: The contract code couldn't be stored, please check your gas amount.
The contract I am trying to create is almost exactly the same as the safe remote payment contract in the solidity docs with maybe just a few more lines.
The weird thing is that the contract gets created and I can see the successful transaction in MetaMask and the Ether is subtracted from my account. I've been testing in the Ropsten network.
is it possible to get the lines you added in so can test this issue?
pragma solidity ^0.4.8;
contract Sale {
uint public value;
address public seller;
address public buyer;
enum State { Created, Locked, Inactive }
State public state;
function Sale() payable {
seller = msg.sender;
value = msg.value / 2;
if (2 * value != msg.value) throw;
}
modifier require(bool _condition) {
if (!_condition) throw;
_;
}
modifier onlyBuyer() {
if (msg.sender != buyer) throw;
_;
}
modifier onlySeller() {
if (msg.sender != seller) throw;
_;
}
modifier inState(State _state) {
if (state != _state) throw;
_;
}
event Aborted(address indexed _sale, uint256 _time);
event Purchased(address indexed _sale, uint256 _time);
event Confirmed(address indexed _sale, uint256 _time);
/// Abort the purchase and reclaim the ether.
/// Can only be called by the seller before
/// the contract is locked.
function abort()
onlySeller
inState(State.Created)
{
state = State.Locked;
if (!seller.send(this.balance)) throw;
Aborted(address(this), now);
}
/// Confirm the purchase as buyer.
/// Transaction has to include `2 * value` ether.
/// The ether will be locked until confirmReceived
/// is called.
function purchase()
inState(State.Created)
require(msg.value == 2 * value)
payable
{
buyer = msg.sender;
state = State.Locked;
Purchased(address(this), now);
}
/// Confirm that you (the buyer) received the item.
/// This will release the locked ether.
function confirm()
onlyBuyer
inState(State.Locked)
{
state = State.Inactive;
if (!buyer.send(value) || !seller.send(this.balance)) throw;
Confirmed(address(this), now);
}
}
I'm pretty sure the only thing I added was the events so that the UI could know when to rehydrate the data. I also changed the names of some of the functions and the contract.
i see you are using promises. Are you using Ethjs instead of web3?
In that section of code I'm using Truffle, specifically truffle contract. I'm pretty sure Truffle uses web3.
I tried the same code on remix.ethereum.org.
On Kovan, I had success, no error:
https://kovan.etherscan.io/tx/0x3fe9f15b2777b9923c822b3333b0ff5fa7108c6ebfb00318707d461ae35c284e
I switched the network to Ropsten, and then had success, no error:
https://ropsten.etherscan.io/tx/0x15f87a5b4512d7d5c570bb5e8f1bc27797be7d20e51a75a4c9050c221feff93d
I notice you're getting your error from a promise. Is it possible that the error comes from an update to your contract abstraction library?
I also did not get an error on testnet.
Are you using testrpc?
I am using Ropsten testnet. So I wanted to test to see if it was in truffle-contract.
$ truffle console
truffle(development)> contract = require('truffle-contract')
truffle(development)> saleArtifacts = require('./build/contracts/Sale.json')
truffle(development)> Sale = contract(saleArtifacts)
truffle(development)> Sale.setProvider(web3.currentProvider)
truffle(development)> web3.personal.unlockAccount(web3.eth.accounts[0], '******', 150000)
truffle(development)> Sale.new({ from: web3.eth.accounts[0], value: web3.toWei(0.01, 'ether'), gas: 500000 }).then((instance) => { console.log(instance.address) }).catch((err) => { console.error(err) })
0xff625701f5d2e8b6784a86768d11f1eb53188424
So speaking directly to the testnet RPC works fine but when I try it in the Dapp going through metamask I get the error.
Also the error doesn't occur every time. It occurs maybe 1/3-1/2 of the time.
remix has the gas set 4700000 as well have you tried setting it to that?
I have tried up to 4,000,000 but even when I get the "check your gas" errors the contract is still created. And in the test I did just now it works fine with only 500,000 gas. To me, the error seems totally unrelated to gas amount and some how bubbles up from the system as this error.
can i get a hash for one of these transactions that you get an error but the contract is still created?
this is very strange
and it doesn't say it's failed in metamask tx history just so i'm clear?
Nope the MetaMask history says "Contract Published".
Also here's the code in the dapp I'm working on. https://github.com/wilfreddenton/safe-payment-dapp/blob/master/app/src/store/modules/sale.js. The relevant function starts a line 35.
To weigh in on this:
To me, the error seems totally unrelated to gas amount and some how bubbles up from the system as this error.
This error (The contract code couldn't be stored, please check your gas amount.) comes from web3. When it deploys a contract, then checks the code at the contract address that was created, if it can't find any code it throws up its hands and says "must be a gas issue". Most of the time, however, gas isn't the problem. This error message from web3 should be removed.
I've seen something like this happen recently from another Truffle user, but it wasn't an issue with Truffle. By chance, does Metamask's connection to Ropsten use Parity?
// It is important to also provide the
// "payable" keyword here, otherwise the function will
// automatically reject all Ether sent to it.
not a lot of great doc on the feature, but some comments on payable http://solidity.readthedocs.io/en/develop/contracts.html#modifiers
@wilfreddenton seems the solution is to add payable to the constructor if you want to initialize it with an eth balance
@kumavis if you look at the contract I copied and pasted above you'll see it has the payable keyword in the constructor.
ah new hint - seems like parity has a race condition for making the new contract code available.
web3 relies on the code being available right away, and so it fails
https://github.com/ethereum/web3.js/blob/7560f273359071afae31d010b97fa1872dacd66c/lib/web3/contract.js#L131-L150
@kumavis You would think that contract._eth.getCode would call the callback once the code was available? I searched in the repo for getCode but idk how it's being defined https://github.com/ethereum/web3.js/blob/aeb8baf91548b3200375bbe650ee5f132d85caf2/lib/web3/methods/eth.js#L115
getCode is just an rpc call to the node ( in this case, parity )
The timing is based on polling for the tx receipt on every new block https://github.com/ethereum/web3.js/blob/7560f273359071afae31d010b97fa1872dacd66c/lib/web3/contract.js#L101-L121
Ok I see. And just because there is a tx receipt doesn't mean that the contract code is available? If that's the case then should they be polling for the contract code in L131-L150 segment?
I think it has been a safe assumption that is no longer true with parity.
That could be fixed in parity or web3 or both.
Files an issue on parity here https://github.com/paritytech/parity/issues/5538
Thanks for your help.
as an extra note, infura says they do not cache either of these requests
no update on this ?
I faces same issue, when I tried to deploy contract
"The contract code couldn't be stored, please check your gas amount."
Same issue, any update?
Closing due to inactivity - if this is still relevant, feel free to open a new issue.
Most helpful comment
no update on this ?
I faces same issue, when I tried to deploy contract
"The contract code couldn't be stored, please check your gas amount."