Advertisement
jlind0

Untitled

Mar 4th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pragma solidity ^0.4.18;
  2. contract Collateral {
  3.     address public owner;
  4.     string public description;
  5.     string public title;
  6.     string public url;
  7.     uint public estimatedValue;
  8.     uint public collateralizedDate;
  9.     uint public liquidationPrice;
  10.     uint public liquidationDate;
  11.     event Liquidated(uint price);
  12.     Ledger ledger;
  13.     function Collateral(address _owner, string _description, string _title, string _url, uint _estimatedValue) public {
  14.         owner = _owner;
  15.         description = _description;
  16.         title = _title;
  17.         url = _url;
  18.         estimatedValue = _estimatedValue;
  19.         collateralizedDate = now;
  20.     }
  21.     function setLedger(Ledger _ledger) public {
  22.         assert(msg.sender == owner);
  23.         ledger = _ledger;
  24.     }
  25.     function liquidate(uint price) public {
  26.         assert(msg.sender == owner);
  27.         price = price;
  28.         liquidationPrice = price;
  29.         liquidationDate = now;
  30.         Liquidated(price);
  31.         ledger.distribute(price);
  32.     }
  33. }
  34. contract Ledger {
  35.     struct LedgerEntry {
  36.         CollateralizedDebtObligation creditor;
  37.         uint payment;
  38.         uint date;
  39.     }
  40.     address owner;
  41.     mapping(address => Collateral) public collateral;
  42.     mapping(address => CollateralizedDebtObligation) creditors;
  43.     address[] creditorMap;
  44.     address[] collateralMap;
  45.     LedgerEntry[] public ledger;
  46.     function Ledger(address _owner) public {
  47.         owner = _owner;
  48.     }
  49.     function addCollateral(Collateral _collateral) public {
  50.         assert(msg.sender == owner);
  51.         collateral[address(_collateral)] = _collateral;
  52.         _collateral.setLedger(this);
  53.         collateralMap.push(address(_collateral));
  54.     }
  55.     function distribute(uint payment) public {
  56.         assert(msg.sender == owner);
  57.         uint totalShares = 0;
  58.         for (uint i = 0; i < creditorMap.length; i++) {
  59.             totalShares += creditors[creditorMap[i]].loan();
  60.         }
  61.         for (uint j = 0; j < creditorMap.length; j++) {
  62.             CollateralizedDebtObligation cdo = creditors[creditorMap[i]];
  63.             uint dist = (cdo.loan()*payment)/totalShares;
  64.             address(cdo).transfer(dist);
  65.             ledger.push(LedgerEntry({
  66.                 creditor: cdo,
  67.                 payment: dist,
  68.                 date: now
  69.             }));
  70.         }
  71.     }
  72. }
  73. contract BidWrapper {
  74.     Bid public bid;
  75.     uint public activeCoins;
  76.     function BidWrapper(Bid _bid) public {
  77.         bid = _bid;
  78.         activeCoins = bid.activeCoins();
  79.     }
  80.     function setActiveCoin(uint coin) public {
  81.         activeCoins = coin;
  82.     }
  83. }
  84. contract DutchAuctionBidExecutor {
  85.     BidWrapper[] activeBids;
  86.     BidWrapper[] affectedBids;
  87.     DutchCollateralizedAuction auction;
  88.     uint maxCoins;
  89.     function DutchAuctionBidExecutor(Bid[] _activeBids, uint _maxCoins, DutchCollateralizedAuction _auction) public {
  90.         maxCoins = _maxCoins;
  91.         auction = _auction;
  92.         for (uint i = 0; i < _activeBids.length; i++) {
  93.             activeBids.push(new BidWrapper(_activeBids[i]));
  94.         }
  95.     }
  96.     function totalCoinsAvailable() private returns (uint) {
  97.         uint totalCoinAvailable = maxCoins;
  98.         for (uint n = 0; n < activeBids.length; n++) {
  99.             totalCoinAvailable -= activeBids[n].activeCoins();
  100.         }
  101.         return totalCoinAvailable;
  102.     }
  103.     function getHighestAffectedBid(uint _maxCoins, uint _bid) private returns (uint) {
  104.         uint i = 0;
  105.         uint totalAllocated = 0;
  106.         do {
  107.             totalAllocated = activeBids[i].activeCoins()*activeBids[i].bid().bid();
  108.             i++;
  109.         }
  110.         while (i < activeBids.length && totalAllocated < _maxCoins * _bid);
  111.         return i;
  112.     }
  113.     function executeBidRequest(Bid newBid) public returns (uint) {
  114.         assert(msg.sender == address(auction));
  115.         uint totalAllocated = 0;
  116.         uint totalCoins = 0;
  117.         uint totalCoinAvailable = totalCoinsAvailable();
  118.         bool isPartial = false;
  119.         if (totalCoinAvailable == 0) {
  120.             uint i = getHighestAffectedBid(newBid.maxCoins(), newBid.bid());
  121.             uint j = 0;
  122.             while (j < i && (activeBids[0].activeCoins()*activeBids[0].bid().bid()) < newBid.maxCoins() * newBid.bid() - totalAllocated) {
  123.                 BidWrapper wbid = activeBids[0];
  124.                 totalAllocated += wbid.activeCoins() * newBid.bid();
  125.                 totalCoins += wbid.activeCoins();
  126.                 wbid.setActiveCoin(0);
  127.                 affectedBids.push(wbid);
  128.                 delete activeBids[0];
  129.                 j++;
  130.             }
  131.             isPartial = totalAllocated < newBid.maxCoins()*newBid.bid() && activeBids.length > 0;
  132.             if (isPartial) {
  133.                 BidWrapper kbid = activeBids[0];
  134.                 uint k = kbid.activeCoins();
  135.                 while (k <= kbid.bid().minCoins() && k * kbid.bid().bid() < newBid.maxCoins() * newBid.bid() - totalAllocated) {
  136.                     k--;
  137.                 }
  138.                 totalCoins += kbid.activeCoins() - k;
  139.                 totalAllocated += (kbid.activeCoins() - k) * newBid.bid();
  140.                 kbid.setActiveCoin(k);
  141.                 affectedBids.push(kbid);
  142.                
  143.             }
  144.         } else {
  145.             if (totalCoinAvailable > newBid.maxCoins()) {
  146.                 totalCoins = newBid.maxCoins();
  147.             } else {
  148.                 totalCoins = totalCoinAvailable;
  149.             }
  150.  
  151.         }
  152.         if (totalCoins < newBid.minCoins()) {
  153.             assert(false);
  154.         }
  155.         BidWrapper wrapNew = new BidWrapper(newBid);
  156.         auction.adjustActiveCoins(wrapNew);
  157.         allocateBid(wrapNew, totalAllocated, isPartial);
  158.         for (uint m = 0; m < affectedBids.length; m++) {
  159.             auction.adjustActiveCoins(affectedBids[m]);
  160.         }
  161.         return activeBids.length;
  162.     }
  163.     function getActiveBid(uint index) public returns (Bid) {
  164.         assert(msg.sender == address(auction));
  165.         assert(index < activeBids.length);
  166.         return activeBids[index].bid();
  167.     }
  168.     function allocateBid(BidWrapper newBid, uint totalAllocated, bool isPartial) private {
  169.         if (activeBids.length > 0) {
  170.            
  171.             activeBids.push(activeBids[activeBids.length - 1]);
  172.             uint i = 0;
  173.             if (isPartial) {
  174.                i = 1;
  175.             }
  176.             uint len = activeBids.length;
  177.             for (i; i < len; i++) {
  178.                 activeBids[i + 1] = activeBids[i];
  179.             }
  180.  
  181.             if (isPartial) {
  182.                 activeBids[1] = newBid;
  183.             } else {
  184.                 activeBids[0] = newBid;
  185.             }
  186.         }
  187.         else
  188.             activeBids.push(newBid);
  189.         if (totalAllocated < newBid.bid().maxCoins()*newBid.bid().bid())
  190.             msg.sender.transfer(newBid.bid().maxCoins()*newBid.bid().bid() - totalAllocated);
  191.     }
  192.     function destroy() public {
  193.         assert(msg.sender == address(auction));
  194.         selfdestruct(auction);
  195.     }
  196. }
  197. contract DutchCollateralizedAuction {
  198.     address public payee;
  199.     uint public startDate;
  200.     uint public endDate;
  201.     uint public maxCoins;
  202.     Collateral[] public colletaral;
  203.     Bid[] activeBids;
  204.     mapping(address => Bid) bids;
  205.     address[] bidMap;
  206.     function DutchCollateralizedAuction(address _payee, uint _auctionLength) public {
  207.         payee = _payee;
  208.         startDate = now;
  209.         endDate = startDate + _auctionLength * 1 days;
  210.     }
  211.    
  212.    
  213.     function adjustActiveCoins(BidWrapper bid) public {
  214.         bid.bid().adjustActiveCoins(bid.activeCoins());
  215.     }
  216.    
  217.     function placeBid(uint _maxCoins, uint _minCoins, uint _bid) public payable returns (Bid) {
  218.         assert(_maxCoins >= _minCoins);
  219.         assert(msg.value == _maxCoins * _bid);
  220.         assert(bids[msg.sender] != address(0));
  221.         Bid newBid = new Bid(_maxCoins, _minCoins, _bid, this);
  222.         DutchAuctionBidExecutor exec = new DutchAuctionBidExecutor(activeBids, maxCoins, this);
  223.         uint newActiveBids = exec.executeBidRequest(newBid);
  224.         if (newBid.activeCoins() > 0) {
  225.             delete activeBids;
  226.             for (uint i = 0; i < newActiveBids; i++) {
  227.                 activeBids.push(exec.getActiveBid(i));
  228.             }
  229.             bids[msg.sender] = newBid;
  230.             bidMap.push(msg.sender);
  231.             return newBid;
  232.         } else {
  233.             assert(false);
  234.         }
  235.     }
  236.    
  237.    
  238. }
  239. contract Bid {
  240.     uint public maxCoins;
  241.     uint public minCoins;
  242.     uint public bid;
  243.     uint public activeCoins;
  244.     uint public finalizedCoins;
  245.     DutchCollateralizedAuction public auction;
  246.     event ActiveCoinsChanged(uint activeCoins);
  247.     event CoinsFinalized(uint finalizedCoins);
  248.     function Bid(uint _maxCoins, uint _minCoins, uint _bid, DutchCollateralizedAuction _auction) public payable {
  249.         maxCoins = _maxCoins;
  250.         minCoins = _minCoins;
  251.         bid = _bid;
  252.         auction = _auction;
  253.     }
  254.     function adjustActiveCoins(uint coins) public {
  255.         assert(msg.sender == address(auction));
  256.         finalizedCoins = coins;
  257.         ActiveCoinsChanged(coins);
  258.     }
  259. }
  260. contract CollateralizedDebtObligation {
  261.     address public debtor;
  262.     uint public loan;
  263.     function CollateralizedDebtObligation(address _debtor, uint _loan) public payable {
  264.             debtor = _debtor;
  265.             _loan = loan;
  266.     }
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement