4

I'm currently working on a very simple(first project) market in solidity and i'm having an issue with a piece of code :

  contract smallMarket{

    struct transaction {
      uint timeStamp;
      uint qtyT;
      uint price;
      address client;
      bytes32 typee;
    }
    struct sellOrder {
      uint timeStamp;
      uint qty;
      uint price;
      address seller;
    }

    mapping(uint => transaction) public transactions;
    mapping(uint => sellOrder) public sellorders;

    uint public lastTransactionId;
    uint public lastBOrderId;
    uint public lastSOrderId;
    address public owner;

    function smallMarket(){
      owner = msg.sender;
      lastSOrderId = 0;
    }
    function newTransaction(uint amount,uint price,bytes32 typee){
      uint transac = lastTransactionId + 1;
      transaction t = transactions[transac];
      t.timeStamp = block.timestamp;
      t.qtyT = amount;
      t.price = price;
      t.client = msg.sender;
      t.typee = typee;
      lastTransactionId = transac; 
    }

    function newSellOrder(uint amount,uint price,uint currency) {
        uint order = lastSOrderId + 1; 
        sellOrder s = sellorders[order];
        s.timeStamp = block.timestamp;
        s.qty = amount;
        s.price = price;
        s.seller = msg.sender;
        lastSOrderId = order;
    }
    function buy(uint amount, uint price) {
      uint idCounter = lastSOrderId;
      uint _amountLeftToBuy = amount;
      while (_amountLeftToBuy > 0){
          if (price >= sellorders[idCounter].price && sellorders[idCounter].qty >= amount) {
            newTransaction(amount,price,'cas 1');
            _amountLeftToBuy = 0;

          }
          else if (price >= sellorders[idCounter].price && amount > sellorders[idCounter].qty ){
              newTransaction(amount,price,'cas 2');
              _amountLeftToBuy =  amount - sellorders[idCounter].qty;
              idCounter = idCounter - 1;
          }
          else if (sellorders[idCounter].price > price){
              newTransaction(amount,price,'cas 3');
              _amountLeftToBuy = 0;
          }
          else{}
      }    
    }
  }

I'm manually creating a sellorder struct for testing purpose and it fails on the buy function.

Manually create SO:

newSellOrder(5,200,1)  #=> (uint amount,uint price,uint currency)

Then calling buy function with different cases:

buy(6,201)  #=>(uint amount, uint price)
buy(4,201)
buy(4,199)

In my Buy function , there are 3 case :

price >= sellorders[idCounter].price && sellorders[idCounter].qty >= amount

Should work with => buy(4,201)

price >= sellorders[idCounter].price && amount > sellorders[idCounter].qty

=>buy(6,201)

sellorders[idCounter].price > price

=>buy(4,199)

My issue is that for some reason the cause #2 ( buy(6,201) ) never get triggered.( so only 2 transaction are created instead of 3)

Anybody see what i'm doing wrong here ?

1

So, after hours of debugging :

My iteration was wrong i wasn't referring to _amountLeftToBuy in my conditional but to amount.

  • Could you add some more details to your answer and how you solved the issue? Maybe somebody else has that same issue one day. – Waqar Lim Feb 5 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.