1
0
邓心一 6 rokov pred
rodič
commit
761c03bca4

+ 1 - 1
contracts/ShowOwnership.sol

@@ -56,7 +56,7 @@ contract ShowOwnership is ERC721, ShowHelper {
         showExchange[yours] = 0;
     }
 
-    function _transfer(address _from, address _to, uint _tokenId) private {
+    function _transfer(address _from, address _to, uint _tokenId) internal {
         ownerShowCount[_from] = ownerShowCount[_from].sub(1);
         ownerShowCount[_to] = ownerShowCount[_to].add(1);
         showToOwner[_tokenId] = _to;

+ 19 - 6
contracts/ShowTrader.sol

@@ -7,23 +7,36 @@ contract ShowTrader is ShowOwnership {
     event Buy(uint id);
 
     mapping (uint=>uint) public showToPrice;
+    mapping (address=>uint) public ownerAccount;
+    mapping (address=>bool) accountIsInit;
     uint productsCount;
 
     constructor () public {
         productsCount = 0;
     }
 
+    function initAccount() external {
+        if (accountIsInit[msg.sender] == false) {
+            ownerAccount[msg.sender] = 1000; // Init money of user
+            accountIsInit[msg.sender] = true;
+        }
+    }
+
     function sell(uint id, uint price) external onlyOwnerOf(id) {
         require(price != 0); // 0 means not for sale
-        productsCount = productsCount.add(1);
+        if (showToPrice[id] == 0)
+            productsCount = productsCount.add(1);
         _sell(id, price);
     }
 
-    function buy(uint id) external payable {
-        require(msg.value >= showToPrice[id]);
-        msg.sender.transfer(msg.value.sub(showToPrice[id])); // Take charge
-        ownerOf(id).transfer(showToPrice[id]);
-        transfer(msg.sender, id);
+    function buy(uint id) external {
+        require(showToPrice[id] != 0);
+        require(ownerAccount[msg.sender] >= showToPrice[id]);
+        ownerAccount[msg.sender] = ownerAccount[msg.sender].sub(showToPrice[id]);
+        address seller = showToOwner[id];
+        ownerAccount[seller] = ownerAccount[seller].add(showToPrice[id]);
+        _transfer(seller, msg.sender, id);
+        showToPrice[id] = 0;
         productsCount = productsCount.sub(1);
     }
 

+ 56 - 1
test/test-contract.js

@@ -1,7 +1,7 @@
 /* eslint no-undef: "off" */
 const logger = console.log.bind(console) // eslint-disable-line no-console
 
-function logEvents(result, name) {
+function logEvents(result, name) { // eslint-disable-line no-unused-vars
     result.logs.forEach((log) => {
         if (log.event === name) {
             logger(log)
@@ -97,4 +97,59 @@ contract('ShowTrader Test', (accounts) => {
             assert.equal(owner2, player1, 'owner of show2 should be player1!')
         })
     })
+    it('should buy & sell', () => {
+        const player1 = accounts[1]
+        const player2 = accounts[2]
+        let id1
+        return ShowTrader.deployed()
+        .then((instance) => {
+            trader = instance
+            return trader.getShowsByOwner.call(player1)
+        })
+        .then((shows1) => {
+            id1 = shows1[0].toNumber()
+            logger(id1)
+            return trader.initAccount({ from: player1 })
+        })
+        .then(() => {
+            logger()
+            return trader.ownerAccount(player1)
+        })
+        .then((remains) => {
+            logger(remains.toNumber())
+            return trader.initAccount({ from: player2 })
+        })
+        .then(() => {
+            logger()
+            return trader.ownerAccount(player2)
+        })
+        .then((remains) => {
+            logger(remains.toNumber())
+            return trader.sell(id1, 233, { from: player1 })
+        })
+        .then(() => {
+            logger()
+            return trader.showToPrice(id1)
+        })
+        .then((price) => {
+            logger(price.toNumber())
+            return trader.buy(id1, { from: player2 })
+        })
+        .then(() => {
+            logger()
+            return trader.showToOwner(id1)
+        })
+        .then((owner) => {
+            logger(owner)
+            logger(player1)
+            return trader.ownerAccount(player1)
+        })
+        .then((remains) => {
+            logger(remains.toNumber())
+            return trader.ownerAccount(player2)
+        })
+        .then((remains) => {
+            logger(remains.toNumber())
+        })
+    })
 })