Browse Source

Merge pull request #73 from maraoz/tokens

Token contracts work
Manuel Aráoz 9 years ago
parent
commit
dfbcb23373

+ 12 - 0
contracts/test-helpers/BasicTokenMock.sol

@@ -0,0 +1,12 @@
+pragma solidity ^0.4.4;
+import '../token/BasicToken.sol';
+
+// mock class using BasicToken
+contract BasicTokenMock is BasicToken {
+
+  function BasicTokenMock(address initialAccount, uint initialBalance) {
+    balances[initialAccount] = initialBalance;
+    totalSupply = initialBalance;
+  }
+
+}

+ 1 - 1
contracts/test-helpers/StandardTokenMock.sol

@@ -1,5 +1,5 @@
 pragma solidity ^0.4.4;
-import '../StandardToken.sol';
+import '../token/StandardToken.sol';
 
 // mock class using StandardToken
 contract StandardTokenMock is StandardToken {

+ 28 - 0
contracts/token/BasicToken.sol

@@ -0,0 +1,28 @@
+pragma solidity ^0.4.4;
+
+import './ERC20Basic.sol';
+import '../SafeMath.sol';
+
+/**
+ * Basic token
+ * Basic version of StandardToken, with no allowances
+ */
+contract BasicToken is ERC20Basic, SafeMath {
+
+  mapping(address => uint) balances;
+
+  function transfer(address _to, uint _value) returns (bool success) {
+    if (balances[msg.sender] < _value) {
+      throw;
+    } 
+    balances[msg.sender] = safeSub(balances[msg.sender], _value);
+    balances[_to] = safeAdd(balances[_to], _value);
+    Transfer(msg.sender, _to, _value);
+    return true;
+  }
+
+  function balanceOf(address _owner) constant returns (uint balance) {
+    return balances[_owner];
+  }
+  
+}

+ 1 - 1
contracts/token/CrowdsaleToken.sol

@@ -1,6 +1,6 @@
 pragma solidity ^0.4.4;
 
-import "../StandardToken.sol";
+import "./StandardToken.sol";
 
 /*
  * Simple ERC20 Token example, with crowdsale token creation

+ 0 - 0
contracts/ERC20.sol → contracts/token/ERC20.sol


+ 9 - 0
contracts/token/ERC20Basic.sol

@@ -0,0 +1,9 @@
+pragma solidity ^0.4.4;
+
+
+contract ERC20Basic {
+  uint public totalSupply;
+  function balanceOf(address who) constant returns (uint);
+  function transfer(address to, uint value) returns (bool ok);
+  event Transfer(address indexed from, address indexed to, uint value);
+}

+ 1 - 1
contracts/token/SimpleToken.sol

@@ -1,6 +1,6 @@
 pragma solidity ^0.4.4;
 
-import "../StandardToken.sol";
+import "./StandardToken.sol";
 
 /*
  * Very simple ERC20 Token example, where all tokens are pre-assigned

+ 1 - 1
contracts/StandardToken.sol → contracts/token/StandardToken.sol

@@ -1,7 +1,7 @@
 pragma solidity ^0.4.4;
 
 import './ERC20.sol';
-import './SafeMath.sol';
+import '../SafeMath.sol';
 
 /**
  * ERC20 token

+ 49 - 0
test/BasicToken.js

@@ -0,0 +1,49 @@
+contract('BasicToken', function(accounts) {
+
+  it("should return the correct totalSupply after construction", function(done) {
+    return BasicTokenMock.new(accounts[0], 100)
+      .then(function(token) {
+        return token.totalSupply();
+      })
+      .then(function(totalSupply) {
+        assert.equal(totalSupply, 100);
+      })
+      .then(done);
+  })
+
+  it("should return correct balances after transfer", function(done) {
+    var token;
+    return BasicTokenMock.new(accounts[0], 100)
+      .then(function(_token) {
+        token = _token;
+        return token.transfer(accounts[1], 100);
+      })
+      .then(function() {
+        return token.balanceOf(accounts[0]);
+      })
+      .then(function(balance) {
+        assert.equal(balance, 0);
+      })
+      .then(function() {
+        return token.balanceOf(accounts[1]);
+      })
+      .then(function(balance) {
+        assert.equal(balance, 100);
+      })
+      .then(done);
+  });
+
+  it("should throw an error when trying to transfer more than balance", function(done) {
+    var token;
+    return BasicTokenMock.new(accounts[0], 100)
+      .then(function(_token) {
+        token = _token;
+        return token.transfer(accounts[1], 101);
+      })
+      .catch(function(error) {
+        if (error.message.search('invalid JUMP') == -1) throw error
+      })
+      .then(done);
+  });
+
+});