Skip to content

Instantly share code, notes, and snippets.

@furusiyya
Last active September 28, 2017 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furusiyya/eabf8c2c73c1dda7563450112f308d26 to your computer and use it in GitHub Desktop.
Save furusiyya/eabf8c2c73c1dda7563450112f308d26 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.16;
contract ReentrancyGuard {
event ReentrancyAttemp(address senderAddress);
/**
* @dev We use a single lock for the whole contract.
*/
bool private rentrancy_lock = false;
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* @notice If you mark a function `nonReentrant`, you should also
* mark it `external`. Calling one nonReentrant function from
* another is not supported. Instead, you can implement a
* `private` function doing the actual work, and a `external`
* wrapper marked as `nonReentrant`.
*/
modifier nonReentrant() {
if(rentrancy_lock){
ReentrancyAttemp(msg.sender);
return;
}
rentrancy_lock = true;
_;
rentrancy_lock = false;
}
}
contract Ownable {
/*
@title Ownable
@dev The Ownable contract has an owner address, and provides basic authorization control
functions, this simplifies the implementation of "user permissions".
*/
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev The Ownable constructor sets the original `owner` of the contract to the sender
* account.
*/
function Ownable(address _owner){
owner = _owner;
}
/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) onlyOwner public {
require(newOwner != address(0));
OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
}
contract NotaryPlatformToken{
function isTokenContract() returns (bool);
function transferFrom(address _from, address _to, uint256 _value) returns (bool);
}
contract CarContracts is Ownable, ReentrancyGuard{
struct Deal{
address seller;
address buyer;
bytes32 hash;
}
uint256[] private index;
mapping (uint256 => Deal) private carDeals;
uint256 private contractFee;
uint256 private feeSum;
address private notaryApp;
NotaryPlatformToken private notaryToken;
event LogDeal(address indexed sellerAddress, address indexed buyerAddress, uint256 dealID, uint256 dealfee, bytes32 dealHash);
event AppAccountUpdated(address indexed oldAddress,address indexed newAddress);
event TokenAddressUpdated(address indexed oldAddress,address indexed newAddress);
event ContractFeeUpdated(uint256 indexed oldValue,uint256 indexed newValue);
event Error();
function CarContracts() Ownable(msg.sender){
notaryApp = msg.sender;
notaryToken = NotaryPlatformToken(<address of token contract>);
require(notaryToken.isTokenContract());
contractFee = 10;
}
function ntryApp() external constant returns (address){ return notaryApp;}
function ntryToken() external constant returns (address){ return notaryToken;}
function fee() external constant returns (uint256){ return contractFee;}
function totalEarned() external constant returns (uint256){
return feeSum;
}
function totalContracts() external constant returns (uint256){
return index.length;
}
function updateFee(uint256 _value) external onlyOwner returns (bool){
require(_value > 0);
ContractFeeUpdated(contractFee,_value);
contractFee = _value;
return true;
}
function updateAppAccount(address _address) external onlyOwner returns (bool){
require(_address != 0x00);
AppAccountUpdated(notaryApp,_address);
notaryApp = _address;
return true;
}
function updateTokenAddress(address _address) external onlyOwner returns (bool){
require(_address != 0x00);
TokenAddressUpdated(notaryToken,_address);
notaryToken = NotaryPlatformToken(_address);
if(!notaryToken.isTokenContract()){
revert();
}
return true;
}
/**
* @param cid unique contractID calculated at server
* @param _seller Car seller public address
* @param _buyer Car buyer public address
* @param _hash Hash of contract attributes
* @notice Arguments sequence for hash function
* uint256 cid, address _seller, address _buyer,
* uint256 year,bytes make, bytes model, bytes vin,
* bytes carType, bytes color, bytes engine_no,
* uint8 mileage, uint256 totalPrice,
* uint256 downPayment, uint256 remainingPayment,
* uint256 remainingPaymentDate
*/
function carDeal(uint256 cid, address _seller, address _buyer,bytes32 _hash) nonReentrant() onlyApp() returns (bool){
require(carDeals[cid].seller == 0x00);
if(!notaryToken.transferFrom(_buyer, owner,contractFee)){
Error();
return false;
}
feeSum += contractFee;
carDeals[cid].seller = _seller;
carDeals[cid].buyer = _buyer;
carDeals[cid].hash = _hash;
index.push(cid);
LogDeal(_seller,_buyer,cid,contractFee,_hash);
return true;
}
function getDeal(uint256 cid) external constant returns(address,address,bytes32){
require(carDeals[cid].seller != 0x00);
return(carDeals[cid].seller,carDeals[cid].buyer,carDeals[cid].hash);
}
function getID(uint256 _index) external constant returns(uint256){
return(index[_index]);
}
modifier onlyApp(){
require(msg.sender == notaryApp);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment