-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP: balanceOf working test * Add readData. * Attempt transfer implementation. Broken commit. * Refactor a bit. * Transfer test now passing. * Some cleanup. * Uncomment tests. * Linting... * Some more linting ... * Fix off-chain stepper. * Cosmetics. * Indentation.
- Loading branch information
1 parent
f4c49af
commit b060a2f
Showing
13 changed files
with
823 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
pragma solidity ^0.5.2; | ||
pragma experimental ABIEncoderV2; | ||
|
||
|
||
library EVMTokenBag { | ||
|
||
struct Output { | ||
address owner; | ||
uint valueOrId; | ||
bytes32 data; | ||
address color; | ||
} | ||
|
||
struct TokenBag { | ||
Output[16] bag; | ||
} | ||
|
||
function balanceOf( | ||
TokenBag memory self, | ||
address color, | ||
address owner | ||
) internal pure returns (uint value) { | ||
Output memory output; | ||
for (uint i = 0; i < self.bag.length; i++) { | ||
output = self.bag[i]; | ||
if (output.owner == owner && output.color == color) { | ||
value = output.valueOrId; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function readData( | ||
TokenBag memory self, | ||
address color, | ||
uint id | ||
) internal pure returns (bytes32 data) { | ||
Output memory output; | ||
for (uint i = 0; i < self.bag.length; i++) { | ||
output = self.bag[i]; | ||
if (output.valueOrId == id && output.color == color) { | ||
data = output.data; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function transfer( | ||
TokenBag memory self, | ||
address color, | ||
address from, | ||
address to, | ||
uint value | ||
) internal pure returns (bool) { | ||
Output memory source; | ||
Output memory dest; | ||
// find source | ||
for (uint i = 0; i < self.bag.length; i++) { | ||
if (self.bag[i].owner == from && self.bag[i].color == color) { | ||
source = self.bag[i]; | ||
break; | ||
} | ||
} | ||
|
||
// sender does not have enough tokens to send | ||
if (source.valueOrId < value) return false; | ||
|
||
// find dest and/or empty | ||
uint emptyId = 1337; | ||
for (uint i = 0; i < self.bag.length; i++) { | ||
if (self.bag[i].owner == to && self.bag[i].color == color) { | ||
dest = self.bag[i]; | ||
break; | ||
} | ||
// check if empty slot | ||
if (self.bag[i].owner == address(0) && emptyId == 1337) { | ||
emptyId = i; | ||
} | ||
} | ||
|
||
// if no dest and no empty slot, throw | ||
if (dest.owner == address(0) && emptyId == 1337) return false; | ||
|
||
// if no dest, but we found an empty slot, assign empty slot to reciver | ||
if (dest.owner == address(0)) { | ||
self.bag[emptyId].owner = to; | ||
self.bag[emptyId].color = color; | ||
dest = self.bag[emptyId]; | ||
} | ||
|
||
// now do the state transition and return success | ||
// OVERFLOWS??????? | ||
source.valueOrId -= value; | ||
dest.valueOrId += value; | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.