Java erc20 chaincode implementation Signed-off-by: renjithpta <renjithkn@gmail.com>

Signed-off-by: renjithpta <renjithkn@gmail.com>
This commit is contained in:
renjithpta 2022-04-14 13:25:12 +00:00
parent 6f9adab554
commit 006c3fa3b4
3 changed files with 8 additions and 7 deletions

0
token-erc-20/chaincode-java/gradle/wrapper/gradle-wrapper.jar vendored Normal file → Executable file
View file

View file

View file

@ -88,8 +88,8 @@ public final class ERC20TokenContract implements ContractInterface {
if (!stringIsNullOrEmpty(currentBalanceStr)) {
currentBalance = Long.parseLong(currentBalanceStr);
}
long updatedBalance = currentBalance + amount;
// Used safe math .
long updatedBalance = Math.addExact(currentBalance, amount);
stub.putStringState(balanceKey.toString(), String.valueOf(updatedBalance));
// Increase totalSupply
@ -98,7 +98,8 @@ public final class ERC20TokenContract implements ContractInterface {
if (!stringIsNullOrEmpty(totalSupplyStr)) {
totalSupply = Long.parseLong(totalSupplyStr);
}
totalSupply = totalSupply + amount;
// Used safe math .
totalSupply = Math.addExact(totalSupply, amount);
stub.putStringState(TOTAL_SUPPLY_KEY.getValue(), String.valueOf(totalSupply));
Transfer transferEvent = new Transfer("0x0", minter, amount);
stub.setEvent(TRANSFER_EVENT.getValue(), transferEvent.toJSONString().getBytes(UTF_8));
@ -136,7 +137,7 @@ public final class ERC20TokenContract implements ContractInterface {
throw new ChaincodeException("The balance does not exist", BALANCE_NOT_FOUND.toString());
}
long currentBalance = Long.parseLong(currentBalanceStr);
long updatedBalance = currentBalance - amount;
long updatedBalance = Math.subtractExact(currentBalance, amount);
stub.putStringState(balanceKey.toString(), String.valueOf(updatedBalance));
@ -145,7 +146,7 @@ public final class ERC20TokenContract implements ContractInterface {
if (stringIsNullOrEmpty(totalSupplyBytes)) {
throw new ChaincodeException("TotalSupply does not exist", NOT_FOUND.toString());
}
long totalSupply = Long.parseLong(totalSupplyBytes) - amount;
long totalSupply = Math.subtractExact(Long.parseLong(totalSupplyBytes), amount);
stub.putStringState(TOTAL_SUPPLY_KEY.getValue(), String.valueOf(totalSupply));
// Emit the Transfer event
@ -364,8 +365,8 @@ public final class ERC20TokenContract implements ContractInterface {
}
// Update the balance
long fromUpdatedBalance = fromCurrentBalance - value;
long toUpdatedBalance = toCurrentBalance + value;
long fromUpdatedBalance = Math.subtractExact(fromCurrentBalance, value);
long toUpdatedBalance = Math.addExact(toCurrentBalance, value);
stub.putStringState(fromBalanceKey.toString(), String.valueOf(fromUpdatedBalance));