I used the following tutorial as well as the tutorial you used.
I used the following steps to deploy your contract:
Install Truffle globally (suggest v5.0.20)
npm install -g truffle
Create your project directory
mkdir vs
cd vs
Use npm
to create a package.json
file
npm init -y
Install ZeppelinOS in your project folder
npm install zos
Initialize the ZeppelinOS project
npx zos init vs
save VehicleService.sol into contracts directory. Please note I changed the import statement from import '../node_modules/zos-lib/contracts/Initializable.sol';
to import "zos-lib/contracts/Initializable.sol";
pragma solidity ^0.5.0;
import "zos-lib/contracts/Initializable.sol";
/**
* Contract to store the mileage of a vehicle
*/
contract VehicleService is Initializable {
struct Vehicle {
string vin;
address owner;
uint kilometers;
string ipfsHash;
}
mapping (string => Vehicle) vehicles;
event Creation(address indexed from, string vin);
event Transfer(address indexed from, address indexed to, string vin);
// constructor () public {}//this will not work with ZeppelinOS
/**
* Create new vehicle in mapping.
* If vehicle already exists => transaction fail and burns gas.
*/
function createVehicle(string memory vin, string memory ipfsHash) public {
assert(vehicles[vin].owner == address(0x0));
vehicles[vin].vin = vin;
vehicles[vin].owner = msg.sender;
vehicles[vin].kilometers = 0;
vehicles[vin].ipfsHash = ipfsHash;
emit Creation(msg.sender, vin);
}
/**
* Update the mileage of the vehicle.
* If the mileage entered is smaller than the previous mileage stored => transaction fail and burns gas.
*/
function updateKilometers(string memory vin, uint kilometers) public {
Vehicle storage transferObject = vehicles[vin];
assert(transferObject.owner == msg.sender);
assert(transferObject.kilometers < kilometers);
transferObject.kilometers = kilometers;
}
/**
* Returns the details of the vehicle
*/
function getVehicle(string memory vin) public view returns(address owner, uint kilometers, string memory ipfsHash) {
owner = vehicles[vin].owner;
kilometers = vehicles[vin].kilometers;
ipfsHash = vehicles[vin].ipfsHash;
}
}
This contract imports another contract from the zos-lib
package, so we have to install it:
npm install zos-lib
Add the contract to the project
npx zos add VehicleService
run ganache (I tend to use ganache-cli and haven’t played much with ganache GUI version)
Change the port in truffle-config.js
so it matches ganache (Ganache GUI 7545)
module.exports = {
networks: {
local: {
host: 'localhost',
port: 7545,
gas: 5000000,
gasPrice: 5e9,
network_id: '*',
}
}
}
Starts a session to work with a desired network
npx zos session --network local --from ADD_ADDRESS_OF_SECOND_GANACHE_ACCOUNT
Accept defaults for timeout and expiration time
Deploy the project
npx zos push
Create an upgradeable instance for the logic contract (your contract doesn’t take any parameters in an Initialize function)
npx zos create VehicleService
Interact with your contract e.g. using truffle console
truffle console --network local
vs = await VehicleService.deployed()
await vs.createVehicle("VIN1", "Qmabc")
await vs.getVehicle("VIN1")