diff --git a/src/main/java/enzinium/TokenContract.java b/src/main/java/enzinium/TokenContract.java index 5fe49c5..9b8d75a 100644 --- a/src/main/java/enzinium/TokenContract.java +++ b/src/main/java/enzinium/TokenContract.java @@ -12,6 +12,7 @@ public class TokenContract { private String symbol = null; private double totalSupply = 0d; private Double totalTokensSold = 0d; + private Double tokenPrice = 5d; private Map balances = new HashMap<>(); @@ -56,6 +57,18 @@ public double totalSupply() { return this.totalSupply; } + public void setTokenPrice(Double tokenPrice) { + this.tokenPrice = tokenPrice; + } + + public Double getTokenPrice() { + return this.tokenPrice; + } + + public void setBalances(Map balances) { + this.balances = balances; + } + public Map getBalances() { return this.balances; } @@ -131,10 +144,9 @@ public int totalTokensSold() { } public void payable(PublicKey recipient, Double enziniums) { - Double tokenPrice = 5d; try { - require(enziniums >= tokenPrice); - Double units = enziniums / tokenPrice; + require(enziniums >= this.getTokenPrice()); + Double units = Math.ceil(enziniums / tokenPrice); transfer(recipient, units); this.owner.transferEZI(enziniums); } catch (Exception e) { diff --git a/src/test/java/enzinium/TokenContractTest.java b/src/test/java/enzinium/TokenContractTest.java index 7b66d7d..644a69f 100644 --- a/src/test/java/enzinium/TokenContractTest.java +++ b/src/test/java/enzinium/TokenContractTest.java @@ -78,8 +78,16 @@ public void payable_test() { // verifico la transferencia de entradas ricknillos.payable(morty.getPK(), morty.getBalance()); assertEquals(4d, ricknillos.balanceOf(morty.getPK()), 0d); - // verifico la trasnferencia de EZI assertEquals(20d, ricknillos.owner().getBalance(), 0d); + + // sin EZI suficiente + ricknillos.payable(morty.getPK(), 4d); + assertEquals(4d, ricknillos.balanceOf(morty.getPK()), 0d); + assertEquals(20d, ricknillos.owner().getBalance(), 0d); + + ricknillos.payable(morty.getPK(), 8d); + assertEquals(5d, ricknillos.balanceOf(morty.getPK()), 0d); + assertEquals(20d, ricknillos.owner().getBalance(), 0d); } } \ No newline at end of file