Skip to content

Instantly share code, notes, and snippets.

@fassko
Created August 23, 2022 12:19
Show Gist options
  • Save fassko/4d9180d391eae3ed8083ff45e8288927 to your computer and use it in GitHub Desktop.
Save fassko/4d9180d391eae3ed8083ff45e8288927 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol";
import "hardhat/console.sol";
// 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2,0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db,0x78731D3Ca6b7E34aC0F824c42a7cC18A495cabaB,0x617F2E2fD72FD9D5503197092aC168c91465E7f2
enum ShipmentStatus {
InUkraine, // Registered in Ukraine
LeftUkraine, // Navigating in Black Sea
ArrivedInTurkey, // Arrived in Turkey
LeftTurkey, // Left Turkey
ArrivedInDestinationCountry // Arrived to the destination country
}
struct Shipment {
string destination;
ShipmentStatus status;
}
contract GrainExport is Ownable {
address private ukraine;
address private russia;
address private turkey;
address private un;
bool private ukraineSigned;
bool private russiaSigned;
bool private turkeySigned;
bool private unSigned;
event SignedByUkraine();
event SignedByRussia();
event SignedByTurkey();
event SignedByUN();
event AgreementSigned();
uint256 private currentShipmentId;
mapping(uint256 => Shipment) shipments;
event ShipmentCreated(uint256 shipmentId);
event ShipmentLeftUkraine(uint256 shipmentId);
event ShipmentArrivedInTurkey(uint256 shipmentId);
event ShipmentExamined(uint256 shipmentId);
event ShipmentArrivedInDestination(uint256 shipmentId, string destination);
modifier agreementSigned() {
require(ukraineSigned, "Ukraine not signed");
require(russiaSigned, "Russia not signed");
require(turkeySigned, "Turkey not signed");
require(unSigned, "United Nations not signed");
_;
}
constructor(address _ukraine, address _russia, address _turkey, address _un) {
ukraine = _ukraine;
russia = _russia;
turkey = _turkey;
un = _un;
}
function sign() external {
console.log("Signer address", msg.sender);
require(msg.sender == ukraine || msg.sender == russia || msg.sender == turkey || msg.sender == un, "Unknown signer");
if (msg.sender == ukraine) {
ukraineSigned = true;
emit SignedByUkraine();
}
if (msg.sender == russia) {
russiaSigned = true;
emit SignedByRussia();
}
if (msg.sender == turkey) {
turkeySigned = true;
emit SignedByTurkey();
}
if (msg.sender == un) {
unSigned = true;
emit SignedByUN();
}
if (ukraineSigned && russiaSigned && turkeySigned && unSigned) {
emit AgreementSigned();
}
}
function getAgreementStatus() view external {
console.log("Ukraine signed", ukraineSigned);
console.log("Russia signed", russiaSigned);
console.log("Turkey signed", turkeySigned);
console.log("United Nations signed", unSigned);
}
// -- Shipment -- //
function getShipment(uint256 shipmentId) external view returns(Shipment memory) {
console.log("Destination", shipments[shipmentId].destination);
console.log("Destination", uint256(shipments[shipmentId].status));
return shipments[shipmentId];
}
function createShipment(string memory _destination) external agreementSigned {
require(msg.sender == ukraine, "Caller is not Ukraine");
currentShipmentId++;
shipments[currentShipmentId] = Shipment(_destination, ShipmentStatus.InUkraine);
console.log("Shipment created", currentShipmentId);
emit ShipmentCreated(currentShipmentId);
}
function leftUkraine(uint256 shipmentId) external agreementSigned {
require(msg.sender == ukraine, "Caller is not Ukraine");
shipments[shipmentId].status = ShipmentStatus.LeftUkraine;
console.log("Shipment left Ukraine", shipmentId);
emit ShipmentLeftUkraine(shipmentId);
}
function arrivesInTurkey(uint256 shipmentId) external agreementSigned {
require(msg.sender == turkey, "Caller is not Turkey");
shipments[shipmentId].status = ShipmentStatus.ArrivedInTurkey;
console.log("Shipment arrived in Turkey", shipmentId);
emit ShipmentArrivedInTurkey(shipmentId);
}
function examined(uint256 shipmentId) external agreementSigned {
require(msg.sender == turkey, "Caller is not Turkey");
shipments[shipmentId].status = ShipmentStatus.LeftTurkey;
console.log("Shipment examined", shipmentId);
emit ShipmentExamined(shipmentId);
}
function arrivedInDestination(uint256 shipmentId) external agreementSigned {
require(msg.sender == un, "Caller is not United Nations");
shipments[shipmentId].status = ShipmentStatus.ArrivedInDestinationCountry;
console.log("Shipment", shipmentId, "arrived in", shipments[shipmentId].destination);
emit ShipmentArrivedInDestination(shipmentId, shipments[shipmentId].destination);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment