I am trying to deploy a smart contract to a private Blockchain which uses oraclizeAPI.sol library code.
Basically the smart contract is a small implementation of oraclize.
The import does not work, either with the github link or with local import, solc compilation fails because of import.
Both of the below does not work, the contract is not getting properly compiled by solc
1.import "oraclizeAPI.sol";
2.import "github.com/oraclize/ethereum-api/oraclizeAPI.sol";
So, next approch i took was to copy the code of oraclizeAPI.sol, directly into the contract code file.
Now the contract gets compiled properly but the i am falling shot on gas every time, while deploying.
Error:
The contract couldn't be stored, please check your gas amount.
Now here are the details of the Blockchain.
genesis.json
{
"nonce": "0x0000000000000042",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"difficulty": "0x4000",
"alloc": {
"84840c340067c75806273d2524dfbae646a7c68f":
{ "balance": "1606938044258990275541962092341162602522202993782792835301376" }
},
"config": {
"chainId": 15,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase": "0x84840c340067c75806273d2524dfbae646a7c68f",
"timestamp": "0x00",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"gasLimit": "0x8000000000000000"
}
I am currently trying to deply the contract using the coinbase id.
web3.eth.getBlock("latest").gasLimit
132661998547049420
web3.eth.getBalance('0x84840c340067c75806273d2524dfbae646a7c68f').e
60
contract code:
contract oraclizeExample is usingOraclize {
string public data;
event newOraclizeQuery(string description);
event newData(string data);
event eventC(string data);
function oraclizeExample() payable {
update();
}
function __callback(bytes32 myid, string result) {
if (msg.sender != oraclize_cbAddress()) throw;
data = result;
newData(result);
//return result;
}
function eventCheck(string dataFClient) returns (string) {
eventC(dataFClient);
return dataFClient;
}
function update() payable returns (string) {
newOraclizeQuery("Oraclize query was sent, standing by for the answer..");
oraclize_query("URL", "json(https://jewel-api.herokuapp.com/jewel/58d89d264d59a000110829bb).invoice_num");
return "update function was called!";
}
}
The code of contract creation.
var ContractABI = web3.eth.contract(JSON.parse(interface));
var SaveContract = ContractABI.new(
{
from: account,
data: bytecode,
gas: '93048696279858031'
}, function (e, contract) {
if(e){
console.log(e, contract);
return;
}
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
fs.writeFileSync('./contracts_data/'+ contract_name + '_final', 'Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash)
return;
}
});
If you want to check the complete contract code with the and the way i am doing this please got to this link.
https://github.com/utkarsh17ife/oraclizeExample/tree/master/contracts_data
Or the complete implementation with node as well:
https://github.com/utkarsh17ife/oraclizeExample
And yes i am able to mine other contract using this setup.
Comment if you need further info on this.
If you are using a private chain you must run the ethereum-bridge, you are getting a throw because your smart contract invokes the oraclize_query function on deployment (constructor function) but no Oraclize contracts were found on your chain.
Related
We are running a GoQuorum 27.7 network.
In Remix with 'default' as the EVM version, when running the function SELFBALANCE it gives the error: invalid opcode: SELFBALANCE .
However, when manually choosing "petersburg" as the EVM version, it works fine.
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
contract TestCalls {
uint256 public XYSTORED = 0;
function setBalance() external returns(uint256) {
XYSTORED = address(this).balance;
return XYSTORED;
}
function withdawlCIC() external {
payable(msg.sender).transfer(XYSTORED);
XYSTORED = address(this).balance;
}
function emergencyWithdrawl() external {
payable(msg.sender).transfer(XYSTORED);
}
function ViewXXBalance() external view returns(uint256 balance) {
balance = address(this).balance;
}
}
What is the approach to allow let it work without having to choose "petersburg" every time a contract with this function is deployed?
It is a QBFT network.
"config": {
"chainId": 1234,
"homesteadBlock": 0,
"eip150Block": 0,
"eip150Hash": "0x0000000000000000000000000000000000000000000000>
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0,
"constantinopleBlock": 0,
..........
I tried adding the following transitions to Genesis. The issue persisted:
"transitions": [{
"block": 54074,
"istanbulBlock": true
},
{
"block": 547122,
"petersburgBlock": true
}]
To run a function in Remix to retrieve the self balance.
I'm creating a proxy contract that connect to existing ERC-20 contract.
this proxy contract should able to connects with metamask and show token balances.
every things works fine when add token in metamask with proxy address, it show symbol and decimal number correctly but not balance. shown zero instead.
proxy contract code:
contract Proxy {
address private _implementation;
event Upgraded(address indexed implementation);
function implementation() public view returns (address) {
return _implementation;
}
function upgradeTo(address impl) public {
_implementation = impl;
emit Upgraded(impl);
}
function () payable external {
address _impl = implementation();
require(_impl != address(0));
assembly {
let ptr := mload(0x40)
calldatacopy(ptr, 0, calldatasize)
let result := delegatecall(gas, _impl, ptr, calldatasize, 0, 0)
let size := returndatasize
returndatacopy(ptr, 0, size)
switch result
case 0 { revert(ptr, size) }
default { return(ptr, size) }
}
}
}
the function balanceOf working fine when i add token in metamask with ERC-20 contract address. but show zero by proxy contract
function balanceOf(address tokenOwner) public view returns (uint256) {
return balances[tokenOwner];
}
My efforts
for test i wrote this function:
function test(address theAddress) public view returns (address) {
return theAddress ;
}
when i call argument '0xC357c241b98B15B3A08aeC3AcD49fBC0cbD74fcE'
on ERC-20 contract returns same address but on proxy returns
this value:
0xc357c241b98b19150f7f8f1d47ad1cd500000000
another test that i do is this function:
function test2(string memory theString) public view returns (string memory) {
return theString ;
}
this function works fine on both proxy and ERC-20 contract!!
thanks all.
Edit 1
my test with web3.js
var interval ;
document.addEventListener('DOMContentLoaded', function() {
interval = setInterval(run , 1000);
}, false);
function run(){
web3 = new Web3(web3.currentProvider);
console.log("call");
if(web3.eth.accounts[0] === undefined)
return;
clearInterval(interval);
console.log(web3.eth.accounts[0]);
web3.eth.defaultAccount = web3.eth.accounts[0];
var CoursetroContract = web3.eth.contract( JSON.parse(`[
{
"constant": true,
"inputs": [
{
"name": "theAddress",
"type": "address"
}
],
"name": "test",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "theString",
"type": "string"
}
],
"name": "test2",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]`));
var contract = CoursetroContract.at('0xd3744cac3a2f796c16b45e5be582c1c5f3039482'); //proxy
//var contract = CoursetroContract.at('0xd025c8835b2a4bd2f9eeb1d682db224f7b301868'); //erc20
contract.test(0xC357c241b98B15B3A08aeC3AcD49fBC0cbD74fcE,
function(err,result){
console.log("err" ,err);
console.log("result" , result);
}
);
Edit 2
this contract addresses is already available in Ropsten Testnet
The proxy contract works a bit differently.
let result := delegatecall(gas, _impl, ptr, calldatasize, 0, 0)
The DELEGATECALL in your proxy contract calls the contract via the address specified in _impl. So, as a result, it runs the _impl code ( in your case ERC20 ) in proxy contracts's environment. As a result, the storage of proxy is modified and not ERC20 contracts storage. Link to how delegatecall works.
So my suggestion would be to look at how you are initializing your ERC20 contract and setting its balance.
You would have to do something like this
erc20Contract = await erc20Contract.at(proxy.address)
erc20Contract.initialize()
The first line gives you the interface of erc20Contract at proxy contract's address.
And the second line would redo the work of the constructor at proxy contract's address and storage.
I'm trying to get the balance of an address on my smart contract using web3, but the balance is always 0. Using metamask on Rinkeby since my contract is deployed on rinkeby. https://rinkeby.etherscan.io/address/0x8e3a88be716ce7c8119c36558ec97bc634592255
You can verify the wallet has a balance by putting it in the balanceOf function on etherScan. Use the address 0x8b54A82a12bD5A7bA33B4842cA677E55f78a8612
let provider = web3.currentProvider;
web3 = new Web3(provider);
let abi = 'too long of a string to post here';
let MyContract = web3.eth.contract(JSON.parse(abi));
let myContractInstance = MyContract.at('0x8e3a88be716ce7c8119c36558ec97bc634592255');
let address = '0x8b54A82a12bD5A7bA33B4842cA677E55f78a8612';
function balanceOf(address) {
if (!address) {
return;
}
this.myContractInstance.balanceOf.call(address, function(error, balance) {
if (error) {
return;
}
alert(balance.c[0] + ' RHC');
});
}
balanceOf(address);
Here is the getBalance function on my contract
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
Website implemented on http://robinhoodcoin.net/metamask.html
Code https://github.com/robinhoodcoin/robinhoodcoin.github.io/blob/master/metamask.html
EDIT
when I change the provider to be the following:
var web3 = new Web3(new Web3.providers.HttpProvider('https://rinkeby.infura.io/'));
I am able to get the balance. SO there is something up with using metamask as the provider.
The line at https://github.com/robinhoodcoin/robinhoodcoin.github.io/blob/master/metamask.html#L118 has a typo. It reads:
self.myContractInstance = self.MyContract.at(self.address);
but the address is stored at self.contractAddress, so it should read:
self.myContractInstance = self.MyContract.at(self.contractAddress);
After making that fix, the page works fine for me with MetaMask.
I am doing some research about Web3js.
I try to build one contract that can let anyone to purchase my token by ether.
I use testrpc to build my pricate chain
Here is my transfer function
handleTransfer: function(event) {
event.preventDefault();
var amount = parseInt($('#TTTransferAmount').val()); // get the ether amout
console.log('Pay ' + amount + ' ether');
var GustavoCoinCrowdsaleInstance;
web3.eth.getAccounts(function(error, accounts) {
if (error) {
console.log(error);
}
var account = accounts[0];
App.contracts.GustavoCoinCrowdsale.deployed().then(function(instance) {
GustavoCoinCrowdsaleInstance = instance;
console.log(typeof amount );
console.log( web3.toWei(amount, "ether"));
return GustavoCoinCrowdsaleInstance.sendTransaction({ from: account, value: web3.toWei(amount, "ether")})
}).then(function(result) {
alert('Pay Successful!');
$('#TTTransferAmount').val(0);
return App.getBalances();
}).catch(function(err) {
console.log(err.message);
});
});
}
I try to use web3.toWei function to converts an ethereum unit into wei
when I type console.log(web3.toWei(1,'ether'));
The result is fine,it will return 1000000000000000000
but when I type console.log(web3.toWei(0.1,'ether'));
it will return 0
Does toWei function only accept integer?
What should I need to do if I want to use 0.1 ether to purchase token?
I used a Greeter contract and compiled it,which generate web3 deploy code as follows:
var _greeting = "This is a contract" ;
var ballot_sol_greeterContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"type":"constructor"}]);
var ballot_sol_greeter = ballot_sol_greeterContract.new(
_greeting,
{
from: web3.eth.accounts[0],
data: '0x6060604052341561000c57fe5b6040516103ac3803806103ac833981016040528080518201919050505b5b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b806001908051906020019061008292919061008a565b505b5061012f565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100cb57805160ff19168380011785556100f9565b828001600101855582156100f9579182015b828111156100f85782518255916020019190600101906100dd565b5b509050610106919061010a565b5090565b61012c91905b80821115610128576000816000905550600101610110565b5090565b90565b61026e8061013e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610046578063cfae321714610058575bfe5b341561004e57fe5b6100566100f1565b005b341561006057fe5b610068610185565b60405180806020018281038252838181518152602001915080519060200190808383600083146100b7575b8051825260208311156100b757602082019150602081019050602083039250610093565b505050905090810190601f1680156100e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561018257600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b61018d61022e565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102235780601f106101f857610100808354040283529160200191610223565b820191906000526020600020905b81548152906001019060200180831161020657829003601f168201915b505050505090505b90565b6020604051908101604052806000815250905600a165627a7a72305820c1f3ef4f75f67078057f269b87485e55352081a112b0c1d16d369e9ec82a99b30029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
I created a javascript file which and tried to load the file using loadScript() it displayed true and then i mined which generate a hex address.
then I stopped the miner and called the contract using greeter.greet().It shows
ReferenceError: 'greeter' is not defined
at <anonymous>:1:1
and on executing
eth.getCode(greeter.address)
ReferenceError: 'greeter' is not defined
at <anonymous>:1:13
I used online solidity Compiler
and geth v1.6.6. stable version
contract mortal {
/* Define variable owner of the type address*/
address owner;
/* this function is executed at initialization and sets the owner of the contract */
function mortal() { owner = msg.sender; }
/* Function to recover the funds on the contract */
function kill() { if (msg.sender == owner) suicide(owner); }
}
contract greeter is mortal {
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}
Also while deploying the code :
>loadScript("firstContract.js")
INFO [06-27|19:48:01] Submitted contract creation
fullhash=0x1037088a27d9cacf9ced417381489ba58658c09e4377ec1f66a9bc24c1f29269
contract=0x2c7e7791dfe505c4f3d456d762990558d4cbe171
null [object Object]
true