forked from icon-project/java-score-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHSP20BurnableTest.java
More file actions
93 lines (81 loc) · 3.75 KB
/
Copy pathHSP20BurnableTest.java
File metadata and controls
93 lines (81 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2022 HAVAH Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.havah.contract.example;
import com.iconloop.score.test.Account;
import com.iconloop.score.test.Score;
import com.iconloop.score.test.ServiceManager;
import com.iconloop.score.test.TestBase;
import io.havah.contract.HSP20BurnableToken;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import score.Address;
import java.math.BigInteger;
import static java.math.BigInteger.TEN;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
public class HSP20BurnableTest extends TestBase {
private static final String name = "MyHSP20Burnable";
private static final String symbol = "MIB";
private static final int decimals = 18;
private static final BigInteger initialSupply = BigInteger.valueOf(1000);
private static BigInteger totalSupply = initialSupply.multiply(TEN.pow(decimals));
private static final ServiceManager sm = getServiceManager();
private static final Account owner = sm.createAccount();
private static Score tokenScore;
private static HSP20BurnableToken tokenSpy;
@BeforeAll
public static void setup() throws Exception {
tokenScore = sm.deploy(owner, HSP20BurnableToken.class,
name, symbol, decimals, initialSupply);
owner.addBalance(symbol, totalSupply);
// setup spy object against the tokenScore object
tokenSpy = (HSP20BurnableToken) spy(tokenScore.getInstance());
tokenScore.setInstance(tokenSpy);
}
@Test
void burn() {
final Address zeroAddress = new Address(new byte[Address.LENGTH]);
Account alice = sm.createAccount();
alice.addBalance(symbol, transferToken(owner, alice, TEN));
assertEquals(totalSupply, tokenScore.call("totalSupply"));
// burn one token
BigInteger amount = TEN.pow(decimals);
tokenScore.invoke(alice, "burn", amount);
alice.subtractBalance(symbol, amount);
totalSupply = totalSupply.subtract(amount);
assertEquals(alice.getBalance(symbol), tokenScore.call("balanceOf", alice.getAddress()));
assertEquals(totalSupply, tokenScore.call("totalSupply"));
verify(tokenSpy).Transfer(alice.getAddress(), zeroAddress, amount);
// burn all the tokens
amount = (BigInteger) tokenScore.call("balanceOf", alice.getAddress());
tokenScore.invoke(alice, "burn", amount);
totalSupply = totalSupply.subtract(amount);
assertEquals(BigInteger.ZERO, tokenScore.call("balanceOf", alice.getAddress()));
assertEquals(totalSupply, tokenScore.call("totalSupply"));
verify(tokenSpy).Transfer(alice.getAddress(), zeroAddress, amount);
}
BigInteger transferToken(Account from, Account to, BigInteger tokenAmount) {
BigInteger value = TEN.pow(decimals).multiply(tokenAmount);
tokenScore.invoke(from, "transfer", to.getAddress(), value);
from.subtractBalance(symbol, value);
assertEquals(from.getBalance(symbol),
tokenScore.call("balanceOf", from.getAddress()));
assertEquals(value,
tokenScore.call("balanceOf", to.getAddress()));
return value;
}
}