转让所有权 web3
Posted
技术标签:
【中文标题】转让所有权 web3【英文标题】:Transfer ownership web3 【发布时间】:2018-05-28 17:46:03 【问题描述】:我正在创建一个 dapp,以使用 testrpc 将合同的所有权从一个地址转移到另一个地址。但是,我一直遇到这个问题。我尝试使用 sentransaction 方法来执行此所有权更改。也许我以错误的方式调用交易所。 Solidity 版本 0.4.4 web3“版本”:“0.20.2”
web3.js:3127 Uncaught Error: VM Exception while processing transaction: invalid opcode
at Object.InvalidResponse (web3.js:3127)
at RequestManager.send (web3.js:6332)
at Eth.send [as sendTransaction] (web3.js:5066)
at SolidityFunction.sendTransaction (web3.js:4122)
at SolidityFunction.execute (web3.js:4208)
at transferOwnership (luxcure_manu.html:309)
at HTMLButtonElement.onclick (luxcure_manu.html:378
目前还没有完整的solidity合约。
pragma solidity ^0.4.4;
// TODO: Hash of the cert through IPFS Hash
// Transfer ownership of smart contract
contract LuxSecure
address public contract_owner; //Manufacturer/owner
//string public current_owner; //Current Owner of good
bytes32 public model; //Model
mapping(uint => address) public owners; //list of owners
uint256 public owners_count;
bytes32 public status; // (Public(Owned by no one), Private(Bought by another entity),stolen(Stolen from public or private))
bytes32 public date_manufactured; //Time
// Set manufacturer of the Good RUN ONCE ONLY
function manufacturer() public
if(owners_count == 0)
contract_owner = msg.sender;
//Modifier that only allows owner of the bag to Smart Contract AKA Good to use the function
modifier onlyOwner()
require(msg.sender == contract_owner);
_;
// Add a new product to the blockchain with a new serial
function addNewGoods(bytes32 _model,bytes32 _status, bytes32 _date_manufactured) public returns(bool made) //Declare Goods struct
setOwner(msg.sender);
model = _model;
status = _status;
date_manufactured = _date_manufactured;
return true;
//This function transfer ownership of contract from one entity to another
function transferOwnership(address _newOwner) public onlyOwner()
require(_newOwner != address(0));
contract_owner = _newOwner;
//Set the KEY to uint256 and VALUE owner Ethereum Address
function setOwner(address owner)public
owners_count += 1 ;
owners[owners_count] = owner;
//Get the previous owner in the mappings
function previousOwner()constant public returns(address)
if(owners_count != 0)
uint256 previous_owner = owners_count - 1;
return owners[previous_owner];
// Getter Methods
function getManufacturer() constant public returns(address)
return contract_owner;
function getCurrentOwner() constant public returns(address)
return owners[owners_count] ;
function getOwnerCount() constant public returns(uint256)
return owners_count;
function getModel() constant public returns(bytes32)
return model;
function getStatus() constant public returns(bytes32)
return status;
function getDateManufactured() constant public returns(bytes32)
return date_manufactured;
// end of LuxSecure
执行所有权转移的javascript
function transferOwnership()
var account_to_transfer = document.getElementById("ethereumaddress").value;
contract.transferOwnership(account_to_transfer,
from:web3.eth.accounts[0],
gas:4000000
);
【问题讨论】:
你能发布完整的合同吗?另外,您使用的是什么版本的 web3js? 刚刚添加了有关合约和 web3js 的更多信息 【参考方案1】:我在您的代码中没有发现任何特殊错误。也许前面的格式不好,但不能确定,因为我们这里有部分前面。
我不知道这是否会有所帮助,但有时,使用松露,我碰巧有一些函数从 testrpc/ganache-cli 返回错误的操作码,而代码中没有明显的错误。
删除 ABI,重新编译智能合约以获得全新的 ABI,然后重新部署合约解决了问题。
【讨论】:
您是否尝试在专用网络上部署来解决此问题?这是 testrpc 的固有问题? 我总是先部署在本地专用网络上,然后再部署在测试网上,并且只有在代码完全测试后才部署在主网上。我得到和谈论的操作码错误通常是在私有本地网络上部署时。我不知道是 testrpc 还是 ABI 失败了,但是删除 ABI 并使用 truffle 编译重新生成文件解决了这个问题。以上是关于转让所有权 web3的主要内容,如果未能解决你的问题,请参考以下文章