Я разработал свой смарт-контракт, используя tesrpc. Теперь я хочу развернуть их в тестовой сети. Но я не могу развернуть их ни с помощью EtherWallet (у меня ошибка «Нет данных в адресе контракта», даже если я увеличил комиссию), либо с помощью JS-скрипта с использованием API web3, где я установил Gas
значение 4700000
. Но у меня нет обратного адреса смарт-контракта.
Мой код смарт-контракта:
pragma solidity ^ 0.4.2;
contract Color {
address public contracts;
string public color;
function Color(address _c, string _color) {
contracts = _c;
color = _color;
}
function getContracts() constant returns(address contractAddress) {
return contracts;
}
function getColor() constant returns(string color) {
return color;
}
function setlastDirAddress(address c) {
contracts = c;
}
function setColor(string clr) {
color= clr;
}
}
РЕДАКТИРОВАТЬ: Вот мой JS-скрипт:
var Web3 = require('web3');
var solc = require('solc');
const fs = require('fs');
//Globals
var colorContract = {
abi: null,
address: null
};
//connect to testRPC / Geth locally
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// Checking Ethereum connection status
if (!web3.isConnected()) {
console.error("Ethereum - no conection to RPC server");
} else {
console.log("Ethereum - connected to RPC server");
}
//Setting up the account
web3.eth.defaultAccount = web3.eth.accounts[0];
var account = web3.eth.accounts[0];
function compileAndDeployColorContract() {
var contractAddress = process.argv[2];
var color = process.argv[3];
let source = fs.readFileSync('./color.sol', 'utf8');
let compiledContract = solc.compile(source, 1);
let abi = compiledContract.contracts['Color'].interface;
fs.writeFile('./colorABI.js', abi, (err) => {
if (err) throw err;
console.log('colorABI.js saved!');
});
let bytecode = compiledContract.contracts['Color'].bytecode;
let gasEstimate = web3.eth.estimateGas({
data: bytecode
});
//run out of gas when using this
let colorContract = web3.eth.contract(JSON.parse(abi));
//Contract Object parameters
var deployContractObject = {
from: account,
data: bytecode,
gas: '4700000'
};
var colorSC = colorContract.new(contractAddress, color, deployContractObject, function(err, colorSC) {
if (!err) {
// NOTE: The callback will fire twice!
// Once the contract has the transactionHash property set and once its deployed on an address.
// e.g. check tx hash on the first call (transaction send)
if (!colorSC.address) {
console.log('ColorTransaction Hash: ' + colorSC.transactionHash) // The hash of the transaction, which deploys the contract
// check address on the second call (contract deployed)
} else {
console.log('ColorContract Mined! address: ' + colorSC.address) // the contract address
}
// Note that the returned "myContractReturned" === "myContract",
// so the returned "myContractReturned" object will also get the address set.
}
else { console.log(err);}
});
}
compileAndDeployColorContract();
Я уже посмотрел здесь , здесь и здесь .
Спасибо за ваши ответы,
Это поздний ответ, но я оставляю это для будущих посетителей. Ваш код Solidity, кажется, работает нормально, за исключением того, что функция getColor()
не будет получать правильное значение в результате вашего оператора return; попробуйте: returns(string)
попробуйте использовать инструмент browser-solidity (Remix): browser-solidity(Remix) при загрузке кода контракта компилируется, а также предоставляет рабочий код развертывания web3.
Надеюсь это поможет
Аникет
FrenchieiSverige
Аникет
FrenchieiSverige
Аникет
console.log("Ethereum - connected to RPC server");
эта строка выполняется?