我已经在Ropsten网络上通过Metamask部署了ERC20令牌合同。问题是我试图通过web3与它进行交互。我已经按照SO和SE中提供的答案,与令牌转移和方法调用相关。
正如您将看到的,我不是在这里转移令牌,但我使用我的可靠性合同方法来设置令牌的初始所有者。合同中的方法接收2个参数,一个地址和一个tokenId。
当我使用代码将令牌转移到测试Metamask帐户时,它失败了,从某种意义上说,如果我进入此测试帐户并导入令牌,它就没有。
作为参考,这个tokenId基本上由我的变量nextTokenIdToAssign提供,我这样称呼:
const tokenIdToAssign = contract.nextTokenIdToAssign.call(function(err, res){
if(!err) { tokenIdToAssign = res; }
else { console.log("Error"); }
});
奇怪的是,当我尝试console.log时,它返回undefined。另外,在问题评论之后,我使用了this question
const test = web3.eth.getCode(contractAddress);
它只返回null。注释表明我的构造函数或父构造函数中存在崩溃。
我在这里缺少一些重要的细节吗?
完整的代码如下:
const config = require('../config');
var Web3 = require('web3');
var web3 = new Web3();
const infuraApi = (config.infura.infuraApiKey);
//Set a provider (HttpProvider)
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('https://ropsten.infura.io/'+infuraApi));
}
const EthereumTx = require('ethereumjs-tx');
var accountAddressHex = (config.metamaskAccount.metamaskAddressHex);
var accountAddressPrivateKey = (config.metamaskAccount.metamaskAddressPrivateKey);
var privateKey = new Buffer(accountAddressPrivateKey, 'hex');
var count = web3.eth.getTransactionCount(accountAddressHex);
var contractAddress = (config.solidityContract.contractAddress);
var contractAbiArray = (config.solidityContract.contractABI);
var contract = web3.eth.contract(contractAbiArray).at(contractAddress);
const testSendAccount= "0x...";
const gasPrice = web3.eth.gasPrice;
const gasPriceHex = web3.toHex(gasPrice);
const gasLimitHex = web3.toHex(30000000);
//const tokenTransferAmount = 1;
var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){
if(!err) { tokenIdToAssignHex = hash;
console.log(tokenIdToAssignHex); }
else { console.log("Error"); }
});
var tokenIdToAssign = contract.nextTokenIdToAssign.call(function(err, res){
if(!err) { tokenIdToAssign = res; }
else { console.log("Error"); }
});
const test = web3.eth.getCode(contractAddress);
var rawTransaction = {
"from": accountAddressHex,
"nonce": web3.toHex(count),
"gasPrice": gasPriceHex,
"gasLimit": gasLimitHex,
"to": contractAddress,
"value": "0x0",
"data": contract.setInitialOwner.getData(testSendAccount, tokenIdToAssign, {from: accountAddressHex}), //contract.transfer.getData("0xCb...", 10, {from: "0x26..."}),
"chainId": 0x03 //Ropsten id is 3, replace with 1 for main
};
var tx = new EthereumTx(rawTransaction);
tx.sign(privateKey);
var serializedTx = tx.serialize();
web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash) {
if (!err) { console.log( 'contract creation tx: ' + hash); }
else {
console.log(err);
return;
}
});
编辑1
以下是setInitialOwner的合约代码:
function setInitialOwner(address _to, uint256 _tokenId) public
onlyOwner
tokensRemainingToAssign
tenKLimit (_tokenId)
yesZeroAddressOwner (_tokenId)
notSelfSend (_to, _tokenId) {
tokenIdToOwner[_tokenId] = _to;
balanceOfAddress[_to] = balanceOfAddress[_to].add(1);
emit Assign(_to, _tokenId);
tokenIndexArray.push(_tokenId);
uint256 length = balanceOf(msg.sender);
ownedTokensIndexMapping[_tokenId] = length;
addressToTokenIdByIndex[msg.sender][length] = _tokenId;
nextTokenIdToAssign = nextTokenFunc(nextTokenIdToAssign);
hypeKillsTokensRemainingToAssign = tokensRemainingToAssign.sub(1);
}
编辑2 我更改了tokenToAssign并添加了
var tokenIdToAssignHex = contract.nextTokenIdToAssign.sendTransaction( {from: accountAddressHex}, function(err, hash){
if(!err) { tokenIdToAssignHex = hash;
console.log(tokenIdToAssignHex); }
else { console.log("Error"); }
});
答案 0 :(得分:0)
由于其他人可能遇到此问题,我将发布我到达的解决方案。这里的问题是我的nextTokenToAssign
变量没有正确更新。
我使用myetherwallet.com来部署合同并检查其价值。当我部署合同时,如果之后我没有调用构造函数,将nextTokenToAssign
的初始值设置为1,则在连续调用setInitialOwner
函数期间不会更新。所以解决方案就是简单地调用函数。