|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-2022 Balanced.network. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | + package network.balanced.score.tokens.wicx; |
| 18 | + |
| 19 | +import network.balanced.score.lib.interfaces.tokens.IRC2; |
| 20 | +import score.Address; |
| 21 | +import score.Context; |
| 22 | +import score.DictDB; |
| 23 | +import score.VarDB; |
| 24 | +import score.annotation.EventLog; |
| 25 | +import score.annotation.External; |
| 26 | +import score.annotation.Optional; |
| 27 | +import score.annotation.Payable; |
| 28 | + |
| 29 | +import java.math.BigInteger; |
| 30 | + |
| 31 | +public class PayableIRC2Base implements IRC2 { |
| 32 | + |
| 33 | + private final static String NAME = "name"; |
| 34 | + private final static String SYMBOL = "symbol"; |
| 35 | + private final static String DECIMALS = "decimals"; |
| 36 | + private final static String TOTAL_SUPPLY = "total_supply"; |
| 37 | + private final static String BALANCES = "balances"; |
| 38 | + |
| 39 | + static final Address ZERO_ADDRESS = new Address(new byte[Address.LENGTH]); |
| 40 | + |
| 41 | + private final VarDB<String> name = Context.newVarDB(NAME, String.class); |
| 42 | + private final VarDB<String> symbol = Context.newVarDB(SYMBOL, String.class); |
| 43 | + private final VarDB<BigInteger> decimals = Context.newVarDB(DECIMALS, BigInteger.class); |
| 44 | + private final VarDB<BigInteger> totalSupply = Context.newVarDB(TOTAL_SUPPLY, BigInteger.class); |
| 45 | + protected final DictDB<Address, BigInteger> balances = Context.newDictDB(BALANCES, BigInteger.class); |
| 46 | + |
| 47 | + protected PayableIRC2Base(String _tokenName, String _symbolName, @Optional BigInteger _decimals) { |
| 48 | + if (this.name.get() == null) { |
| 49 | + _decimals = _decimals == null ? BigInteger.valueOf(18L) : _decimals; |
| 50 | + Context.require(_decimals.compareTo(BigInteger.ZERO) >= 0, "Decimals cannot be less than zero"); |
| 51 | + |
| 52 | + this.name.set(ensureNotEmpty(_tokenName)); |
| 53 | + this.symbol.set(ensureNotEmpty(_symbolName)); |
| 54 | + this.decimals.set(_decimals); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @EventLog(indexed = 3) |
| 59 | + public void Transfer(Address _from, Address _to, BigInteger _value, byte[] _data) { |
| 60 | + } |
| 61 | + |
| 62 | + private String ensureNotEmpty(String str) { |
| 63 | + Context.require(str != null && !str.trim().isEmpty(), "str is null or empty"); |
| 64 | + assert str != null; |
| 65 | + return str.trim(); |
| 66 | + } |
| 67 | + |
| 68 | + @External(readonly = true) |
| 69 | + public String name() { |
| 70 | + return name.get(); |
| 71 | + } |
| 72 | + |
| 73 | + @External(readonly = true) |
| 74 | + public String symbol() { |
| 75 | + return symbol.get(); |
| 76 | + } |
| 77 | + |
| 78 | + @External(readonly = true) |
| 79 | + public BigInteger decimals() { |
| 80 | + return decimals.get(); |
| 81 | + } |
| 82 | + |
| 83 | + @External(readonly = true) |
| 84 | + public BigInteger totalSupply() { |
| 85 | + return totalSupply.getOrDefault(BigInteger.ZERO); |
| 86 | + } |
| 87 | + |
| 88 | + @External(readonly = true) |
| 89 | + public BigInteger balanceOf(Address _owner) { |
| 90 | + return balances.getOrDefault(_owner, BigInteger.ZERO); |
| 91 | + } |
| 92 | + |
| 93 | + @External |
| 94 | + @Payable |
| 95 | + public void transfer(Address _to, BigInteger _value, @Optional byte[] _data) { |
| 96 | + transfer(Context.getCaller(), _to, _value, _data); |
| 97 | + } |
| 98 | + |
| 99 | + protected void transfer(Address _from, Address _to, BigInteger _value, byte[] _data) { |
| 100 | + Context.require(_value.compareTo(BigInteger.ZERO) >= 0, this.name.get() + ": _value needs to be positive"); |
| 101 | + Context.require(balanceOf(_from).compareTo(_value) >= 0, this.name.get() + ": Insufficient balance"); |
| 102 | + |
| 103 | + this.balances.set(_from, balanceOf(_from).subtract(_value)); |
| 104 | + this.balances.set(_to, balanceOf(_to).add(_value)); |
| 105 | + |
| 106 | + byte[] dataBytes = (_data == null) ? "None".getBytes() : _data; |
| 107 | + Transfer(_from, _to, _value, dataBytes); |
| 108 | + |
| 109 | + if (_to.isContract()) { |
| 110 | + Context.call(_to, "tokenFallback", _from, _value, dataBytes); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + protected void mint(Address owner, BigInteger amount) { |
| 115 | + Context.require(!ZERO_ADDRESS.equals(owner), this.name.get() + ": Owner address cannot be zero address"); |
| 116 | + Context.require(amount.compareTo(BigInteger.ZERO) >= 0, this.name.get() + ": Amount needs to be positive"); |
| 117 | + |
| 118 | + totalSupply.set(totalSupply().add(amount)); |
| 119 | + balances.set(owner, balanceOf(owner).add(amount)); |
| 120 | + Transfer(ZERO_ADDRESS, owner, amount, "mint".getBytes()); |
| 121 | + } |
| 122 | + |
| 123 | + protected void burn(Address owner, BigInteger amount) { |
| 124 | + Context.require(!ZERO_ADDRESS.equals(owner), this.name.get() + ": Owner address cannot be zero address"); |
| 125 | + Context.require(amount.compareTo(BigInteger.ZERO) >= 0, this.name.get() + ": Amount needs to be positive"); |
| 126 | + Context.require(balanceOf(owner).compareTo(amount) >= 0, this.name.get() + ": Insufficient Balance"); |
| 127 | + |
| 128 | + balances.set(owner, balanceOf(owner).subtract(amount)); |
| 129 | + totalSupply.set(totalSupply().subtract(amount)); |
| 130 | + Transfer(owner, ZERO_ADDRESS, amount, "burn".getBytes()); |
| 131 | + } |
| 132 | +} |
0 commit comments