Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2ec3d90

Browse files
committedMar 24, 2024··
feat: javadocs added
1 parent e1e93ad commit 2ec3d90

File tree

2 files changed

+275
-131
lines changed

2 files changed

+275
-131
lines changed
 

‎contracts/javascore/ics20/src/main/java/ibc/ics20/ICS20Transfer.java

+214-52
Original file line numberDiff line numberDiff line change
@@ -40,59 +40,127 @@ public ICS20Transfer(Address _ibcHandler, byte[] _serializeIrc2) {
4040
serializedIrc2 = _serializeIrc2;
4141
}
4242

43+
/**
44+
* Set the admin address and ensure only admin can call this function.
45+
*
46+
* @param _admin the new admin address
47+
* @return void
48+
*/
4349
@External
4450
public void setAdmin(Address _admin) {
4551
onlyAdmin();
4652
admin.set(_admin);
4753
}
4854

55+
/**
56+
* Retrieves the admin address.
57+
*
58+
* @return the admin address
59+
*/
4960
@External(readonly = true)
5061
public Address getAdmin() {
5162
return admin.get();
5263
}
5364

65+
/**
66+
* Retrieves the IBC handler address.
67+
*
68+
* @return the IBC handler address
69+
*/
5470
@External(readonly = true)
5571
public Address getIBCAddress() {
5672
return ibcHandler.get();
5773
}
5874

75+
/**
76+
* Retrieves the destination port for the given channel ID.
77+
*
78+
* @param channelId the source channel id
79+
* @return the destination port associated with the channel ID
80+
*/
5981
@External(readonly = true)
6082
public String getDestinationPort(String channelId) {
6183
return destinationPort.get(channelId);
6284
}
6385

86+
/**
87+
* Retrieves the destination channel for the given channel ID.
88+
*
89+
* @param channelId the source channel id
90+
* @return the destination channel associated with the channel ID
91+
*/
6492
@External(readonly = true)
6593
public String getDestinationChannel(String channelId) {
6694
return destinationChannel.get(channelId);
6795
}
6896

97+
/**
98+
* Retrieves the token contract address for the given denom.
99+
*
100+
* @param denom the token denom
101+
* @return the token contract address
102+
*/
69103
@External(readonly = true)
70104
public Address getTokenContractAddress(String denom) {
71-
System.out.println(denom+"bbbbbbbbbbbbbbb");
72-
Context.require(tokenContracts.get(denom) != null, TAG+" : Token not registered");
105+
Context.require(tokenContracts.get(denom) != null, TAG + " : Token not registered");
73106
return tokenContracts.get(denom);
74107
}
75108

109+
/**
110+
* Register a token contract for cosmos chain.
111+
*
112+
* @param name
113+
* @param symbol
114+
* @param decimals
115+
*/
76116
@External
77117
public void registerCosmosToken(String name, String symbol, int decimals) {
78118
onlyAdmin();
79119
Address tokenAddress = Context.deploy(serializedIrc2, name, symbol, decimals);
80120
tokenContracts.set(name, tokenAddress);
81121
}
82122

123+
/**
124+
* Register a token contract for icon chain.
125+
*
126+
* @param tokenAddress the irc2 token contract address
127+
*/
83128
@External
84129
public void registerIconToken(Address tokenAddress) {
85130
onlyAdmin();
86131
tokenContracts.set(tokenAddress.toString(), tokenAddress);
87132
}
88133

134+
/**
135+
* Fallback function for token transfer.
136+
*
137+
* @param from Sender address
138+
* @param value Amount
139+
* @param _data Data in json bytes in format of
140+
* {
141+
* "method": "sendFungibleTokens",
142+
* "params": {
143+
* "denomination": "string",
144+
* "amount": "uint64",
145+
* "sender": "string",
146+
* "receiver": "string",
147+
* "sourcePort": "string",
148+
* "sourceChannel": "string",
149+
* "timeoutHeight": {
150+
* "latestHeight": "uint64",
151+
* "revisionNumber": "uint64",
152+
* },
153+
* "timeoutTimestamp": "uint64",
154+
* "memo":"string"
155+
* }
156+
* }
157+
*
158+
*/
89159
@External
90160
public void tokenFallback(Address from, BigInteger value, byte[] _data) {
91161
String method = "";
92162
JsonValue params = null;
93163

94-
// Context.require(registeredTokenContracts.getOrDefault(Context.getCaller(), false), TAG+" : Token not registered");
95-
96164
try {
97165
String data = new String(_data);
98166
JsonObject json = Json.parse(data).asObject();
@@ -119,10 +187,11 @@ public void tokenFallback(Address from, BigInteger value, byte[] _data) {
119187
height.setRevisionNumber(BigInteger.valueOf(timeoutHeight.getLong("revisionNumber", 0)));
120188
height.setRevisionHeight(BigInteger.valueOf(timeoutHeight.getLong("latestHeight", 0)));
121189

122-
Context.require(amount.equals(value), TAG+" : Mismatched amount");
123-
Context.require(sender.equals(from.toString()), TAG+" : Sender address mismatched");
124-
Context.require(tokenContracts.get(denomination) == Context.getCaller(), TAG+" : Sender Token Contract not registered");
125-
190+
Context.require(amount.equals(value), TAG + " : Mismatched amount");
191+
Context.require(sender.equals(from.toString()), TAG + " : Sender address mismatched");
192+
Context.require(tokenContracts.get(denomination) == Context.getCaller(),
193+
TAG + " : Sender Token Contract not registered");
194+
126195
sendFungibleToken(denomination, amount, sender, receiver, sourcePort, sourceChannel, height,
127196
timeoutTimestamp, memo);
128197
} else {
@@ -131,17 +200,41 @@ public void tokenFallback(Address from, BigInteger value, byte[] _data) {
131200

132201
}
133202

203+
/**
204+
* Sends ICX to the specified receiver via the specified channel and port.
205+
*
206+
* @param receiver the cross chain address of the receiver
207+
* @param sourcePort the source port
208+
* @param sourceChannel the source channel
209+
* @param timeoutHeight the timeout height
210+
* @param timeoutTimestamp the timeout timestamp
211+
* @param memo an optional memo
212+
*/
134213
@Payable
135214
@External
136215
public void sendICX(String receiver, String sourcePort, String sourceChannel, Height timeoutHeight,
137216
BigInteger timeoutTimestamp, @Optional String memo) {
138-
Context.require(Context.getValue().compareTo(BigInteger.ZERO) > 0, TAG+" : ICX amount should be greater than 0");
217+
Context.require(Context.getValue().compareTo(BigInteger.ZERO) > 0,
218+
TAG + " : ICX amount should be greater than 0");
139219

140220
sendFungibleToken("icx", Context.getValue(), Context.getCaller().toString(), receiver, sourcePort,
141221
sourceChannel, timeoutHeight, timeoutTimestamp, memo);
142222

143223
}
144224

225+
/**
226+
* Sends a irc2 token from the sender to the receiver.
227+
*
228+
* @param denomination the denomination of the token to send
229+
* @param amount the amount of the token to send
230+
* @param sender the address of the sender
231+
* @param receiver the cross chain address of the receiver
232+
* @param sourcePort the source port
233+
* @param sourceChannel the source channel
234+
* @param timeoutHeight the timeout height(latest height and revision number)
235+
* @param timeoutTimestamp the timeout timestamp
236+
* @param memo an optional memo for the transaction
237+
*/
145238
private void sendFungibleToken(String denomination, BigInteger amount, String sender, String receiver,
146239
String sourcePort, String sourceChannel, Height timeoutHeight, BigInteger timeoutTimestamp,
147240
@Optional String memo) {
@@ -158,17 +251,13 @@ private void sendFungibleToken(String denomination, BigInteger amount, String se
158251
String destPort = destinationPort.get(sourceChannel);
159252
String destChannel = destinationChannel.get(sourceChannel);
160253

161-
if(destChannel == null || destPort == null) {
254+
if (destChannel == null || destPort == null) {
162255
Context.revert(TAG + " : Connection not properly Configured");
163256
}
164257

165-
System.out.println("destPort: " + destPort + isSource);
166-
167258
BigInteger seq = Context.call(BigInteger.class, ibcHandler.get(), "getNextSequenceSend", sourcePort,
168259
sourceChannel);
169260

170-
System.out.println("seq: " + seq);
171-
172261
Packet newPacket = new Packet();
173262

174263
newPacket.setSequence(seq);
@@ -183,20 +272,28 @@ private void sendFungibleToken(String denomination, BigInteger amount, String se
183272
Context.call(ibcHandler.get(), "sendPacket", newPacket.encode());
184273
}
185274

275+
/**
276+
* Handles the reception of a packet
277+
*
278+
* @param packet the byte array representation of the packet to be processed
279+
* @param relayer the address of the relayer
280+
* @return a byte array representing the acknowledgement of the packet
281+
* processing
282+
*/
186283
@External
187284
public byte[] onRecvPacket(byte[] packet, Address relayer) {
188285
onlyIBC();
189286
Packet packetDb = Packet.decode(packet);
190287
ICS20Lib.FungibleTokenPacketData data;
191-
288+
192289
try {
193290
data = ICS20Lib.unmarshalFungibleTokenPacketData(packetDb.getData());
194291

195-
Context.require(!data.denom.equals(""), TAG+" : ICS20: invalid denomination");
196-
Context.require(!data.receiver.equals(""), TAG+" : ICS20: invalid receiver address");
197-
Context.require(!data.sender.equals(""), TAG+" : ICS20: invalid sender address");
198-
Context.require(data.amount.compareTo(BigInteger.ZERO)>0, TAG+" : ICS20: invalid amount");
199-
292+
Context.require(!data.denom.equals(""), TAG + " : ICS20: invalid denomination");
293+
Context.require(!data.receiver.equals(""), TAG + " : ICS20: invalid receiver address");
294+
Context.require(!data.sender.equals(""), TAG + " : ICS20: invalid sender address");
295+
Context.require(data.amount.compareTo(BigInteger.ZERO) > 0, TAG + " : ICS20: invalid amount");
296+
200297
} catch (Exception e) {
201298
return ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
202299
}
@@ -206,44 +303,51 @@ public byte[] onRecvPacket(byte[] packet, Address relayer) {
206303

207304
byte[] ack = ICS20Lib.SUCCESSFUL_ACKNOWLEDGEMENT_JSON;
208305

209-
if(!checkIfReceiverIsAddress(data.receiver)){
306+
if (!checkIfReceiverIsAddress(data.receiver)) {
210307
return ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
211308
}
212309

213-
if(isSource){
310+
Address receiverAddr = Address.fromString(data.receiver);
311+
312+
if (isSource) {
214313
String denomOnly = data.denom.substring(denomPrefix.length());
215-
216-
if(isNativeAsset(denomOnly)){
314+
315+
if (isNativeAsset(denomOnly)) {
217316
try {
218-
Context.transfer(Address.fromString(data.receiver), data.amount);
317+
Context.transfer(receiverAddr, data.amount);
219318
} catch (Exception e) {
220319
ack = ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
221320
}
222-
} else{
321+
} else {
223322
try {
224323
Address tokenContractAddress = getTokenContractAddress(denomOnly);
225-
Context.call(tokenContractAddress, "transfer", data.receiver, data.amount, data.memo.getBytes());
324+
Context.call(tokenContractAddress, "transfer", receiverAddr, data.amount, data.memo.getBytes());
226325
} catch (Exception e) {
227326
ack = ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
228327
}
229328
}
230-
}else{
329+
} else {
231330
denomPrefix = getDenomPrefix(packetDb.getDestinationPort(), packetDb.getDestinationChannel());
232331
String prefixedDenom = denomPrefix + data.denom;
233332

234-
// try {
333+
try {
235334
Address tokenContractAddress = getTokenContractAddress(prefixedDenom);
236-
System.out.println("ancgea");
237-
Context.call(tokenContractAddress, "mint", data.receiver, data.amount);
238-
System.out.println("ancgea");
239-
// }catch (Exception e){
240-
// ack = ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
241-
// }
335+
Context.call(tokenContractAddress, "mint", receiverAddr, data.amount);
336+
} catch (Exception e) {
337+
ack = ICS20Lib.FAILED_ACKNOWLEDGEMENT_JSON;
338+
}
242339
}
243340

244341
return ack;
245342
}
246343

344+
/**
345+
* Handles the acknowledgement of a packet.
346+
*
347+
* @param packet the packet being acknowledged
348+
* @param acknowledgement the acknowledgement received
349+
* @param relayer the relayer of the packet
350+
*/
247351
@External
248352
public void onAcknowledgementPacket(byte[] packet, byte[] acknowledgement, Address relayer) {
249353
onlyIBC();
@@ -253,79 +357,137 @@ public void onAcknowledgementPacket(byte[] packet, byte[] acknowledgement, Addre
253357
}
254358
}
255359

360+
/**
361+
* Handles the timeout of a packet by refunding the tokens associated with the
362+
* packet.
363+
*
364+
* @param packet the encoded packet data
365+
* @param relayer the address of the relayer
366+
*/
256367
@External
257368
public void onTimeoutPacket(byte[] packet, Address relayer) {
258369
Packet packetDb = Packet.decode(packet);
259370
refundTokens(packetDb);
260371
}
261372

373+
/**
374+
* Initializes the channel opening process.
375+
*
376+
* @param order the order of the channel
377+
* @param connectionHops the connection hops for the channel
378+
* @param portId the port ID for the channel
379+
* @param channelId the channel ID
380+
* @param counterpartyPb the counterparty information
381+
* @param version the version of the channel
382+
*/
262383
@External
263384
public void onChanOpenInit(int order, String[] connectionHops, String portId, String channelId,
264385
byte[] counterpartyPb, String version) {
265386
onlyIBC();
266-
Context.require(order == Channel.Order.ORDER_UNORDERED, TAG+" : must be unordered");
267-
Context.require(version.equals(ICS20_VERSION), TAG+" : version should be same with ICS20_VERSION");
387+
Context.require(order == Channel.Order.ORDER_UNORDERED, TAG + " : must be unordered");
388+
Context.require(version.equals(ICS20_VERSION), TAG + " : version should be same with ICS20_VERSION");
268389
Channel.Counterparty counterparty = Channel.Counterparty.decode(counterpartyPb);
269390
destinationPort.set(channelId, counterparty.getPortId());
270391
}
271392

393+
/**
394+
* Channel Opening Process
395+
*
396+
* @param order the order of the channel
397+
* @param connectionHops an array of connection hops
398+
* @param portId the port ID
399+
* @param channelId the channel ID
400+
* @param counterpartyPb the counterparty in protobuf format
401+
* @param version the version
402+
* @param counterPartyVersion the counterparty version
403+
*/
272404
@External
273405
public void onChanOpenTry(int order, String[] connectionHops, String portId, String channelId,
274406
byte[] counterpartyPb, String version, String counterPartyVersion) {
275407
onlyIBC();
276-
Context.require(order == Channel.Order.ORDER_UNORDERED, TAG+" : must be unordered");
277-
Context.require(counterPartyVersion.equals(ICS20_VERSION), TAG+" : version should be same with ICS20_VERSION");
408+
Context.require(order == Channel.Order.ORDER_UNORDERED, TAG + " : must be unordered");
409+
Context.require(counterPartyVersion.equals(ICS20_VERSION),
410+
TAG + " : version should be same with ICS20_VERSION");
278411
Channel.Counterparty counterparty = Channel.Counterparty.decode(counterpartyPb);
279412
destinationPort.set(channelId, counterparty.getPortId());
280413
destinationChannel.set(channelId, counterparty.getChannelId());
281414
}
282415

416+
/**
417+
* Handles the acknowledged by the counterparty.
418+
*
419+
* @param portId the identifier of the port on this chain
420+
* @param channelId the identifier of the channel that was opened
421+
* @param counterpartyChannelId the identifier of the channel on the
422+
* counterparty chain
423+
* @param counterPartyVersion the version of the ICS20 protocol used by the
424+
* counterparty
425+
*/
283426
@External
284427
public void onChanOpenAck(String portId, String channelId, String counterpartyChannelId,
285428
String counterPartyVersion) {
286429
onlyIBC();
287-
Context.require(counterPartyVersion.equals(ICS20_VERSION), TAG+" : version should be same with ICS20_VERSION");
430+
Context.require(counterPartyVersion.equals(ICS20_VERSION),
431+
TAG + " : version should be same with ICS20_VERSION");
288432
destinationChannel.set(channelId, counterpartyChannelId);
289433
}
290-
434+
435+
/**
436+
* Handles the confirmation of a channel.
437+
*
438+
* @param portId the identifier of the port on this chain
439+
* @param channelId the identifier of the channel that was opened
440+
*/
291441
@External
292442
public void onChanOpenConfirm(String portId, String channelId) {
293443
onlyIBC();
294444
}
295445

296-
446+
/**
447+
* Handles the closure of a channel.
448+
*
449+
* @param portId the identifier of the port on this chain
450+
* @param channelId the identifier of the channel that was opened
451+
*/
297452
@External
298453
public void onChanCloseInit(String portId, String channelId) {
299-
Context.revert( TAG+" : Not Allowed");
454+
Context.revert(TAG + " : Not Allowed");
300455
}
301456

457+
/**
458+
* Handles the closing of a channel.
459+
*
460+
* @param portId the identifier of the port on this chain
461+
* @param channelId the identifier of the channel that was opened
462+
*/
302463
@External
303464
public void onChanCloseConfirm(String portId, String channelId) {
304465
onlyIBC();
305466
}
306467

468+
/**
469+
* Refunds tokens based on the provided packet.
470+
*
471+
* @param packet the packet containing the token data
472+
*/
307473
private void refundTokens(Packet packet) {
308474
ICS20Lib.FungibleTokenPacketData data = ICS20Lib.unmarshalFungibleTokenPacketData(packet.getData());
309475

310476
String denomPrefix = getDenomPrefix(packet.getSourcePort(), packet.getSourceChannel());
311477
boolean isSource = !data.denom.startsWith(denomPrefix);
312-
System.out.println("denomPrefix: " + denomPrefix + " data.denom: " + data.denom + " isSource: " + isSource);
313478

314479
Address sender = Address.fromString(data.sender);
315480

316-
if(isSource){
317-
if(isNativeAsset(data.denom)){
481+
if (isSource) {
482+
if (isNativeAsset(data.denom)) {
318483
Context.transfer(sender, data.amount);
319484
return;
320485
}
321486

322487
Address tokenContractAddress = getTokenContractAddress(data.denom);
323-
System.out.println("native asset done"+sender.toString()+","+data.amount.toString()+","+tokenContractAddress.toString()+"," + data.memo);
324488
Context.call(tokenContractAddress, "transfer", sender, data.amount, data.memo.getBytes());
325-
}
326-
else{
489+
} else {
327490
Address tokenContractAddress = getTokenContractAddress(data.denom);
328-
System.out.println("native asset done"+sender.toString()+","+data.amount.toString()+","+tokenContractAddress.toString());
329491
Context.call(tokenContractAddress, "mint", sender, data.amount);
330492
}
331493
}
@@ -335,11 +497,11 @@ private static String getDenomPrefix(String port, String channel) {
335497
}
336498

337499
private void onlyAdmin() {
338-
Context.require(Context.getCaller().equals(admin.get()), TAG+" : Caller is not admin");
500+
Context.require(Context.getCaller().equals(admin.get()), TAG + " : Caller is not admin");
339501
}
340502

341503
private void onlyIBC() {
342-
Context.require(Context.getCaller().equals(getIBCAddress()), TAG+" : Caller is not IBC Contract");
504+
Context.require(Context.getCaller().equals(getIBCAddress()), TAG + " : Caller is not IBC Contract");
343505
}
344506

345507
private boolean isNativeAsset(String denom) {

‎contracts/javascore/ics20/src/test/java/ibc/ics20/ICS20TransferTest.java

+61-79
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@
88

99
import ibc.icon.interfaces.IIBCHandler;
1010
import ibc.icon.interfaces.IIBCHandlerScoreInterface;
11-
import ibc.ics25.handler.IBCHandler;
12-
import ibc.icon.structs.messages.MsgChannelOpenInit;
1311
import ibc.icon.test.MockContract;
14-
import ibc.ics20.ICS20Transfer;
15-
import ibc.ics23.commitment.Ops;
1612
import icon.proto.core.channel.Channel;
1713
import icon.proto.core.channel.Packet;
1814
import icon.proto.core.client.Height;
1915
import ics20.ICS20Lib;
20-
import s.java.lang.Byte;
2116

2217
import java.lang.String;
2318

@@ -26,27 +21,14 @@
2621
import org.mockito.MockedStatic;
2722
import score.Address;
2823
import score.Context;
29-
import score.annotation.External;
3024

3125
import java.math.BigInteger;
32-
import java.util.Arrays;
3326

34-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3527
import static org.junit.jupiter.api.Assertions.assertEquals;
36-
import static org.junit.jupiter.api.Assertions.assertNull;
3728
import static org.junit.jupiter.api.Assertions.assertThrows;
3829

39-
import com.iconloop.score.test.Account;
40-
import com.iconloop.score.test.Score;
41-
import com.iconloop.score.test.ServiceManager;
42-
import com.iconloop.score.test.TestBase;
43-
44-
import org.junit.jupiter.api.*;
4530
import org.junit.jupiter.api.function.Executable;
46-
import org.mockito.MockedStatic;
4731
import org.mockito.Mockito;
48-
import org.mockito.stubbing.Answer;
49-
5032
import static org.mockito.ArgumentMatchers.any;
5133
import static org.mockito.ArgumentMatchers.eq;
5234
import static org.mockito.Mockito.*;
@@ -97,85 +79,85 @@ void setup() throws Exception {
9779

9880
@Test
9981
void testGetIBCAddress() {
100-
assertEquals(ibcHandler.getAddress(), ics20Transfer.call("getIBCAddress"));
82+
assertEquals(ibcHandler.getAddress(), ics20Transfer.call("getIBCAddress"));
10183
}
10284

10385
@Test
10486
void testAdmin() {
105-
assertEquals(admin.getAddress(), ics20Transfer.call("getAdmin"));
87+
assertEquals(admin.getAddress(), ics20Transfer.call("getAdmin"));
10688

107-
Executable setAdmin = () -> ics20Transfer.invoke(owner, "setAdmin",
108-
owner.getAddress());
109-
expectErrorMessage(setAdmin, "Reverted(0): ICS20 : Caller is not admin");
89+
Executable setAdmin = () -> ics20Transfer.invoke(owner, "setAdmin",
90+
owner.getAddress());
91+
expectErrorMessage(setAdmin, "Reverted(0): ICS20 : Caller is not admin");
11092

111-
ics20Transfer.invoke(admin, "setAdmin", owner.getAddress());
112-
assertEquals(owner.getAddress(), ics20Transfer.call("getAdmin"));
93+
ics20Transfer.invoke(admin, "setAdmin", owner.getAddress());
94+
assertEquals(owner.getAddress(), ics20Transfer.call("getAdmin"));
11395
}
11496

11597
@Test
11698
void testRegisterCosmosToken() {
117-
Executable cosmosToken = () -> ics20Transfer.invoke(user,
118-
"registerCosmosToken", "test", "test", 0);
119-
expectErrorMessage(cosmosToken, "Reverted(0): ICS20 : Caller is not admin");
99+
Executable cosmosToken = () -> ics20Transfer.invoke(user,
100+
"registerCosmosToken", "test", "test", 0);
101+
expectErrorMessage(cosmosToken, "Reverted(0): ICS20 : Caller is not admin");
120102

121-
registerCosmosToken(admin, "abc", "ab", 18, dest_irc2_token);
103+
registerCosmosToken(admin, "abc", "ab", 18, dest_irc2_token);
122104

123-
assertEquals(dest_irc2_token.getAddress(),
124-
ics20Transfer.call("getTokenContractAddress", "abc"));
105+
assertEquals(dest_irc2_token.getAddress(),
106+
ics20Transfer.call("getTokenContractAddress", "abc"));
125107
}
126108

127109
@Test
128110
void testRegisterIconToken() {
129-
Executable icon = () -> ics20Transfer.invoke(user, "registerIconToken",
130-
src_irc2_token.getAddress());
131-
expectErrorMessage(icon, "Reverted(0): ICS20 : Caller is not admin");
132-
133-
ics20Transfer.invoke(admin, "registerIconToken",
134-
src_irc2_token.getAddress());
135-
assertEquals(src_irc2_token.getAddress(),
136-
ics20Transfer.call("getTokenContractAddress",
137-
src_irc2_token.getAddress().toString()));
111+
Executable icon = () -> ics20Transfer.invoke(user, "registerIconToken",
112+
src_irc2_token.getAddress());
113+
expectErrorMessage(icon, "Reverted(0): ICS20 : Caller is not admin");
114+
115+
ics20Transfer.invoke(admin, "registerIconToken",
116+
src_irc2_token.getAddress());
117+
assertEquals(src_irc2_token.getAddress(),
118+
ics20Transfer.call("getTokenContractAddress",
119+
src_irc2_token.getAddress().toString()));
138120
}
139121

140122
@Test
141123
void testTokenFallbackExceptions() {
142-
byte[] data = "test".getBytes();
143-
Executable tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
144-
user.getAddress(), BigInteger.ZERO, data);
145-
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 Invalid data: " +
146-
data.toString());
147-
148-
byte[] data2 = createByteArray("method", "iconToken", ICX,
149-
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
150-
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
151-
"memo");
152-
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
153-
user.getAddress(), BigInteger.ZERO, data2);
154-
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Unknown method");
155-
156-
byte[] data3 = createByteArray("sendFungibleTokens", "iconToken", ICX,
157-
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
158-
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
159-
"memo");
160-
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
161-
user.getAddress(), BigInteger.ZERO, data3);
162-
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Mismatched amount");
163-
164-
byte[] data4 = createByteArray("sendFungibleTokens", "iconToken", ICX,
165-
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
166-
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
167-
"memo");
168-
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
169-
user.getAddress(), ICX, data4);
170-
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Sender address mismatched");
171-
172-
byte[] data5 = createByteArray("sendFungibleTokens", "iconToken", ICX,
173-
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
174-
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
175-
"memo");
176-
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
177-
sender.getAddress(), ICX, data5);
178-
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Sender Token Contract not registered");
124+
byte[] data = "test".getBytes();
125+
Executable tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
126+
user.getAddress(), BigInteger.ZERO, data);
127+
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 Invalid data: " +
128+
data.toString());
129+
130+
byte[] data2 = createByteArray("method", "iconToken", ICX,
131+
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
132+
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
133+
"memo");
134+
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
135+
user.getAddress(), BigInteger.ZERO, data2);
136+
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Unknown method");
137+
138+
byte[] data3 = createByteArray("sendFungibleTokens", "iconToken", ICX,
139+
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
140+
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
141+
"memo");
142+
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
143+
user.getAddress(), BigInteger.ZERO, data3);
144+
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Mismatched amount");
145+
146+
byte[] data4 = createByteArray("sendFungibleTokens", "iconToken", ICX,
147+
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
148+
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
149+
"memo");
150+
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
151+
user.getAddress(), ICX, data4);
152+
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Sender address mismatched");
153+
154+
byte[] data5 = createByteArray("sendFungibleTokens", "iconToken", ICX,
155+
sender.getAddress().toString(), admin.getAddress().toString(), "transfer",
156+
"channel-0", BigInteger.ONE, BigInteger.ONE, BigInteger.valueOf(10000),
157+
"memo");
158+
tokenFallback = () -> ics20Transfer.invoke(user, "tokenFallback",
159+
sender.getAddress(), ICX, data5);
160+
expectErrorMessage(tokenFallback, "Reverted(0): ICS20 : Sender Token Contract not registered");
179161

180162
}
181163

@@ -267,7 +249,7 @@ void testOnRecvPacket_icx() {
267249
void testOnRecvPacket_source() {
268250
try (MockedStatic<Context> contextMock = Mockito.mockStatic(Context.class, Mockito.CALLS_REAL_METHODS)) {
269251
contextMock
270-
.when(() -> Context.call(src_irc2_token.getAddress(), "transfer", receiver.toString(), ICX,
252+
.when(() -> Context.call(src_irc2_token.getAddress(), "transfer", receiver, ICX,
271253
"memo".getBytes()))
272254
.thenReturn(true);
273255

@@ -280,7 +262,7 @@ void testOnRecvPacket_source() {
280262
void testOnRecvPacket_dest() {
281263
try (MockedStatic<Context> contextMock = Mockito.mockStatic(Context.class, Mockito.CALLS_REAL_METHODS)) {
282264
contextMock
283-
.when(() -> Context.call(dest_irc2_token.getAddress(), "mint", receiver.toString(), ICX))
265+
.when(() -> Context.call(dest_irc2_token.getAddress(), "mint", receiver, ICX))
284266
.thenReturn(true);
285267
_onRecvPacket(ICX, "dest_irc2_token");
286268
}

0 commit comments

Comments
 (0)
Please sign in to comment.