Update sample token to use DictDB
@@ -3,6 +3,7 @@ package example;
import avm.Address;
import avm.Blockchain;
+import avm.DictDB;
import foundation.icon.ee.tooling.abi.EventLog;
import foundation.icon.ee.tooling.abi.External;
import foundation.icon.ee.tooling.abi.Optional;
@@ -16,6 +17,7 @@ public class SampleToken
private final String symbol;
private final int decimals;
private final BigInteger totalSupply;
+ private DictDB<Address, BigInteger> balances;
private SampleToken(String name, String symbol, BigInteger decimals, BigInteger initialSupply) {
this.name = name;
@@ -45,7 +47,8 @@ public class SampleToken
}
// set the initial balance of the owner
- TokenStore.putBalance(Blockchain.getOrigin(), this.totalSupply);
+ this.balances = Blockchain.<Address, BigInteger>newDictDB("balances");
+ this.balances.putValue(Blockchain.getOrigin(), this.totalSupply);
}
private static SampleToken token;
@@ -83,21 +86,27 @@ public class SampleToken
@External(readonly=true)
public static BigInteger balanceOf(Address _owner) {
- return TokenStore.getBalance(_owner);
+ return token.balances.getValue(_owner);
}
@External
public static void transfer(Address _to, BigInteger _value, @Optional byte[] _data) {
Address _from = Blockchain.getCaller();
- BigInteger fromBalance = TokenStore.getBalance(_from);
- BigInteger toBalance = TokenStore.getBalance(_to);
+ BigInteger fromBalance = token.balances.getValue(_from);
+ if (fromBalance==null) {
+ fromBalance = BigInteger.valueOf(0);
+ }
+ BigInteger toBalance = token.balances.getValue(_to);
+ if (toBalance==null) {
+ toBalance = BigInteger.valueOf(0);
+ }
// check some basic requirements
Blockchain.require(_value.compareTo(BigInteger.ZERO) >= 0);
Blockchain.require(fromBalance.compareTo(_value) >= 0);
- TokenStore.putBalance(_from, fromBalance.subtract(_value));
- TokenStore.putBalance(_to, toBalance.add(_value));
+ token.balances.putValue(_from, fromBalance.subtract(_value));
+ token.balances.putValue(_to, toBalance.add(_value));
Transfer(_from, _to, _value, _data);
}
