Skip to content

Commit

Permalink
procesando valores decimales de tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dfleta committed Feb 26, 2019
1 parent 9119ec6 commit 25df029
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
18 changes: 15 additions & 3 deletions src/main/java/enzinium/TokenContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PublicKey, Double> balances = new HashMap<>();

Expand Down Expand Up @@ -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<PublicKey, Double> balances) {
this.balances = balances;
}

public Map<PublicKey, Double> getBalances() {
return this.balances;
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/enzinium/TokenContractTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 25df029

Please sign in to comment.