Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@georgek146
Created December 9, 2017 06:00
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 georgek146/7c9bcc987cf992d9c80525cfb327581b to your computer and use it in GitHub Desktop.
Save georgek146/7c9bcc987cf992d9c80525cfb327581b to your computer and use it in GitHub Desktop.
dsf
pragma solidity ^0.4.6;
import "./owned.sol";
import "./priceTicker.sol";
contract tokenRecipient {
function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData);
}
contract Hedge is owned {
/* Public variables of the token */
string public standard = 'Token 0.1';
// protection buyer
string public pbTokenName;
string public pbTokenSymbol;
uint8 public pbTokenDecimals;
uint256 public pbTotalSupply;
uint256 public buyPrice;
// protection seller
string public psTokenName;
string public psTokenSymbol;
uint8 public psTokenDecimals;
uint256 public psTotalSupply;
uint256 public issuePrice;
// claims and payments
uint256 public periodMintRate;
uint256 public paymentPeriod;
uint256 public contractFee;
uint256 public protectionSellerPrinciple;
uint256 public totalBuyerClaim;
uint256 public totalPremium;
uint256 public totalCoverage;
uint256 public premium;
// events, oracles, etc.
bool public creditStatus;
bool public expiry;
address public oracleContract;
address public project_wallet;
/* Sets the constructor variables */
function Hedge(
string _pbTokenName,
string _pbTokenSymbol,
uint8 _pbTokenDecimals,
string _psTokenName,
string _psTokenSymbol,
uint8 _psTokenDecimals,
uint256 _periodMintRate,
address _oracleContract
) {
pbTokenName = _pbTokenName;
pbTokenSymbol = _pbTokenSymbol;
pbTokenDecimals = _pbTokenDecimals;
psTokenName = _psTokenName;
psTokenSymbol = _psTokenSymbol;
psTokenDecimals = _psTokenDecimals;
periodMintRate = _periodMintRate;
oracleContract = _oracleContract;
}
/* This creates an array with all balances */
mapping (address => uint256) public balanceOfBuyer;
mapping (address => uint256) public balanceOfSeller;
mapping (address => mapping (address => uint256)) public allowance;
/* This generates a public event on the blockchain that will notify clients */
event Transfer(address indexed from, address indexed to, uint256 value);
event CreditEvent(bool status);
event Expiry(bool expiry);
/* Set a project Wallet */
function defineProjectWallet(address target) onlyOwner {
project_wallet = target;
}
/* Mint coins */
function mintBuyerToken(uint256 mintedAmount) onlyOwner {
balanceOfBuyer[this] += mintedAmount;
pbTotalSupply += mintedAmount;
Transfer(0, this, mintedAmount);
Transfer(this, this, mintedAmount);
}
function mintIssuerToken(uint256 mintedAmount) onlyOwner {
balanceOfSeller[this] += mintedAmount;
psTotalSupply += mintedAmount;
Transfer(0, this, mintedAmount);
Transfer(this, this, mintedAmount);
}
/* Distroy coins */
function distroyBuyerToken(uint256 burnAmount) onlyOwner {
balanceOfBuyer[this] -= burnAmount;
pbTotalSupply -= burnAmount;
}
function distroyIssuerToken(uint256 burnAmount) onlyOwner {
balanceOfSeller[this] -= burnAmount;
psTotalSupply -= burnAmount;
}
/* Send coins */
// send buyer coins
function transferBuyer(address _to, uint256 _value) {
if (balanceOfBuyer[msg.sender] < _value) throw; // Check if the sender has enough
if (balanceOfBuyer[_to] + _value < balanceOfBuyer[_to]) throw; // Check for overflows
balanceOfBuyer[msg.sender] -= _value; // Subtract from the sender
balanceOfBuyer[_to] += _value; // Add the same to the recipient
Transfer(msg.sender, _to, _value); // Notify anyone listening that this transfer took place
}
// send issuer coins
function transferIssuer(address _to, uint256 _value) {
if (balanceOfSeller[msg.sender] < _value) throw;
if (balanceOfSeller[_to] + _value < balanceOfSeller[_to]) throw;
balanceOfSeller[msg.sender] -= _value;
balanceOfSeller[_to] += _value;
Transfer(msg.sender, _to, _value);
}
/* Allow another contract to spend some tokens in your behalf */
function approve(address _spender, uint256 _value) returns (bool success) {
allowance[msg.sender][_spender] = _value;
tokenRecipient spender = tokenRecipient(_spender);
return true;
}
/* Approve and then comunicate the approved contract in a single tx */
function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/* A contract attempts to get the coins */
function transferBuyerFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balanceOfBuyer[_from] < _value) throw; // Check if the sender has enough
if (balanceOfBuyer[_to] + _value < balanceOfBuyer[_to]) throw; // Check for overflows
if (_value > allowance[_from][msg.sender]) throw; // Check allowance
balanceOfBuyer[_from] -= _value; // Subtract from the sender
balanceOfBuyer[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
function transferIssuerFrom(address _from, address _to, uint256 _value) returns (bool success) {
if (balanceOfSeller[_from] < _value) throw; // Check if the sender has enough
if (balanceOfSeller[_to] + _value < balanceOfSeller[_to]) throw; // Check for overflows
if (_value > allowance[_from][msg.sender]) throw; // Check allowance
balanceOfSeller[_from] -= _value; // Subtract from the sender
balanceOfSeller[_to] += _value; // Add the same to the recipient
allowance[_from][msg.sender] -= _value;
Transfer(_from, _to, _value);
return true;
}
/* Set token price */
function setPrices(uint256 _buyerTokenPrice, uint256 _issuerTokenPrice, uint256 _paymentPeriod, uint256 _contractFee) onlyOwner {
buyPrice = _buyerTokenPrice;
issuePrice = _issuerTokenPrice;
paymentPeriod = _paymentPeriod;
contractFee = _contractFee;
}
/* Buyer tokens, purchase and sale */
function buyBuyerTokens() payable {
uint amount = msg.value / buyPrice;
if (balanceOfBuyer[this] < amount) throw;
if(paymentPeriod == 1) {
balanceOfBuyer[msg.sender] += amount;
balanceOfBuyer[this] -= amount;
Transfer(this, msg.sender, amount);
} else if(paymentPeriod > 1) {
uint maxPurchase = balanceOfBuyer[msg.sender] * periodMintRate;
if(amount > maxPurchase) throw;
balanceOfBuyer[msg.sender] += amount;
balanceOfBuyer[this] -= amount;
Transfer(this, msg.sender, amount);
} else {
throw;
}
}
function sellBuyerTokens(uint amount) returns (uint revenue) {
if(creditStatus == false) throw;
if(balanceOfBuyer[msg.sender] < amount) throw;
uint buyerPercent = amount / pbTotalSupply;
uint buyerClaim = buyerPercent * totalBuyerClaim;
balanceOfBuyer[this] += amount;
balanceOfBuyer[msg.sender] -= amount;
revenue = buyerClaim;
if(!msg.sender.send(revenue)) {
throw;
} else {
Transfer(msg.sender, this, amount);
return revenue;
}
}
/* Issuer tokens, purchase and sale */
function buyIssuerTokens() payable {
uint amount = msg.value / issuePrice;
if(balanceOfSeller[this] < amount) throw;
balanceOfSeller[msg.sender] += amount;
balanceOfSeller[this] -= amount;
Transfer(this, msg.sender, amount);
}
function sellIssuerTokens(uint amount) returns (uint revenue){
if (expiry == false) throw;
if (balanceOfSeller[msg.sender] < amount) throw;
uint principle = amount * issuePrice;
uint premiumPercent = amount / psTotalSupply;
uint premium = premiumPercent * totalPremium;
if (creditStatus == false){
// pay out the principle and the premiums collected
balanceOfSeller[this] += amount;
balanceOfSeller[msg.sender] -= amount;
revenue = premium + principle;
if(!msg.sender.send(revenue)) {
throw;
} else {
Transfer(msg.sender, this, amount);
return revenue;
}
} else if (creditStatus == true){
balanceOfSeller[this] += amount;
balanceOfSeller[msg.sender] -= amount;
revenue = premium;
if(!msg.sender.send(revenue)) {
throw;
} else {
Transfer(msg.sender, this, amount);
return revenue;
}
}
}
/* Collect contract fee */
function collectContractFee() onlyOwner {
if(balanceOfSeller[this] < contractFee) throw;
if(balanceOfBuyer[this] < contractFee) throw;
balanceOfSeller[msg.sender] += contractFee;
balanceOfSeller[this] -= contractFee;
balanceOfBuyer[msg.sender] += contractFee;
balanceOfBuyer[this] -= contractFee;
Transfer(this, msg.sender, contractFee);
}
/* Credit event and claims */
function setCreditStatus(bool _status) onlyOwner {
creditStatus = _status;
CreditEvent(creditStatus);
}
function getClaims() onlyOwner {
protectionSellerPrinciple = issuePrice * psTotalSupply;
totalBuyerClaim = protectionSellerPrinciple;
totalPremium = this.balance - protectionSellerPrinciple;
}
/* Expiry Status Event */
function setExpiryStatus(bool _status) onlyOwner {
expiry = _status;
Expiry(expiry);
}
/* After contract ends move funds */
function moveFunds() onlyOwner {
if (expiry == false) throw;
if (!project_wallet.send(this.balance)) throw;
}
/* External calls to priceTicker oracle contract */
function markContractFromPriceTicker(){
priceTicker pT = priceTicker(oracleContract);
creditStatus = pT.markContract();
CreditEvent(creditStatus);
}
/* This unnamed function is called whenever someone tries to send ether to it */
function () {
throw; // Prevents accidental sending of ether
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment