Skip to content

Commit 19bdb66

Browse files
committed
fix: second review changes made
1 parent c1231c1 commit 19bdb66

File tree

1 file changed

+48
-41
lines changed
  • contracts/javascore/Intent_Contracts/app/src/main/java/network/icon/intent

1 file changed

+48
-41
lines changed

contracts/javascore/Intent_Contracts/app/src/main/java/network/icon/intent/Intent.java

+48-41
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ public class Intent extends GeneralizedConnection {
2323
public final VarDB<BigInteger> protocolFee = Context.newVarDB(Constant.PROTOCOL_FEE, BigInteger.class);
2424
public static final VarDB<Address> feeHandler = Context.newVarDB(Constant.FEE_HANDLER, Address.class);
2525
public static final VarDB<Address> owner = Context.newVarDB(Constant.OWNER, Address.class);
26-
public final VarDB<Address> nativeAddress = Context.newVarDB(Constant.NATIVE_ADDRESS, Address.class);
27-
final static BranchDB<String, DictDB<String, BigInteger>> deposit = Context.newBranchDB(Constant.DEPOSIT,
28-
BigInteger.class);
26+
// public final VarDB<Address> nativeAddress =
27+
// Context.newVarDB(Constant.NATIVE_ADDRESS, Address.class);
28+
// final static BranchDB<String, DictDB<String, BigInteger>> deposit =
29+
// Context.newBranchDB(Constant.DEPOSIT,
30+
// BigInteger.class);
2931
final static DictDB<BigInteger, SwapOrder> orders = Context.newDictDB(Constant.ORDERS, SwapOrder.class);
3032
final static DictDB<byte[], Boolean> finishedOrders = Context.newDictDB(Constant.FINISHED_ORDERS, Boolean.class);
3133

34+
public static Address nativeAddress = Address.fromString("cx0000000000000000000000000000000000000000");
35+
3236
@EventLog(indexed = 3)
3337
public void SwapIntent(
3438
BigInteger id,
@@ -61,22 +65,22 @@ public Intent(String _nid, BigInteger _protocolFee, Address _feeHandler, Address
6165
protocolFee.set(_protocolFee);
6266
feeHandler.set(_feeHandler);
6367
relayAddress.set(_relayer);
64-
nativeAddress.set(Address.fromString("cx0000000000000000000000000000000000000000"));
65-
owner.set(Context.getCaller());
68+
// nativeAddress.set(Address.fromString("cx0000000000000000000000000000000000000000"));
69+
owner.set(Context.getOwner());
6670
}
6771

6872
@External
6973
@Payable
7074
public void swap(SwapOrderData swapOrderData) {
7175
Context.require(swapOrderData.token != null, "Token can't be null");
72-
Context.require(Context.getCaller().toString().equals(extractAddress(swapOrderData.creator)),
76+
Context.require(Context.getCaller().toString().equals(swapOrderData.creator),
7377
"Creator must be sender");
7478
Context.require(Context.getValue().equals(swapOrderData.amount),
7579
"Deposit amount not equal to order amount");
7680

77-
Address token = Address.fromString(extractAddress(swapOrderData.token));
78-
Address nativAddress = nativeAddress.get();
79-
Context.require(token.equals(nativAddress), "Not a native token");
81+
Address token = Address.fromString(swapOrderData.token);
82+
// Address nativAddress = nativeAddress.get();
83+
Context.require(token.equals(nativeAddress), "Not a native token");
8084

8185
SwapOrder swapOrder = new SwapOrder(swapOrderData.id, swapOrderData.emitter,
8286
swapOrderData.srcNID,
@@ -85,7 +89,8 @@ public void swap(SwapOrderData swapOrderData) {
8589
swapOrderData.amount, swapOrderData.toToken, swapOrderData.toAmount,
8690
swapOrderData.data);
8791

88-
deposit.at(swapOrderData.creator).set(swapOrderData.token, swapOrderData.amount);
92+
// deposit.at(swapOrderData.creator).set(swapOrderData.token,
93+
// swapOrderData.amount);
8994

9095
_swap(swapOrder);
9196
}
@@ -94,7 +99,7 @@ void _swap(SwapOrder swapOrder) {
9499
BigInteger id = this.depositId.getOrDefault(BigInteger.ZERO).add(BigInteger.valueOf(1));
95100
swapOrder.id = id;
96101
Context.require(swapOrder.srcNID.equals(this.networkId.get()), "NID is misconfigured");
97-
Context.require(extractAddress(swapOrder.emitter).equals(Context.getAddress().toString()),
102+
Context.require(swapOrder.emitter.equals(Context.getAddress().toString()),
98103
"Emitter specified is not this"); // emitter contract or eoa?
99104
orders.set(id, swapOrder);
100105
SwapIntent(id, swapOrder.emitter, swapOrder.srcNID, swapOrder.dstNID,
@@ -143,11 +148,11 @@ public void cancel(BigInteger id) {
143148
if (order == null) {
144149
Context.revert("Order already has been cancelled");
145150
}
146-
Context.require(Address.fromString(extractAddress(order.creator)).equals(Context.getCaller()),
151+
Context.require(Address.fromString(order.creator).equals(Context.getCaller()),
147152
"Only creator can cancel this order");
148153

149154
if (order.srcNID.equals(order.dstNID)) {
150-
_resolveCancel(nativeAddress.get().toString(), order.toBytes());
155+
_resolveCancel(nativeAddress.toString(), order.toBytes());
151156
return;
152157
}
153158

@@ -209,19 +214,19 @@ void _resolveFill(String srcNetwork, OrderFill _fill) {
209214
orders.set(_fill.id, null);
210215
OrderClosed(_fill.id);
211216

212-
Address tokenAddress = Address.fromString(extractAddress(order.token));
213-
if (tokenAddress.equals(nativeAddress.get())) {
214-
Context.transfer(Address.fromString(extractAddress(_fill.solver)), order.amount);
217+
Address tokenAddress = Address.fromString(order.token);
218+
if (tokenAddress.equals(nativeAddress)) {
219+
Context.transfer(Address.fromString(_fill.solver), order.amount);
215220
} else {
216-
Context.call(tokenAddress, "transfer", Address.fromString(extractAddress(_fill.solver)), order.amount);
221+
Context.call(tokenAddress, "transfer", Address.fromString(_fill.solver), order.amount);
217222
}
218223
}
219224

220225
void _transferResult(String _toAddress, String _toToken, BigInteger amount,
221226
BigInteger fee) {
222-
Address toAddress = Address.fromString(extractAddress(_toAddress));
223-
Address toTokenAddress = Address.fromString(extractAddress(_toToken));
224-
if (toTokenAddress.equals(nativeAddress.get())) {
227+
Address toAddress = Address.fromString(_toAddress);
228+
Address toTokenAddress = Address.fromString(_toToken);
229+
if (toTokenAddress.equals(nativeAddress)) {
225230
Context.require(Context.getValue().equals(amount.add(fee)), "\"Deposit amount not equal to order amount\"");
226231
_nativeTransfer(toAddress, amount);
227232
_nativeTransfer(feeHandler.get(), fee);
@@ -250,22 +255,24 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
250255
// String depositor = json.get("depositor").asString();
251256
// String token = json.get("token").asString();
252257
// BigInteger amount = new BigInteger(json.get("amount").asString());
258+
SwapOrder swapOrder = SwapOrder
259+
.fromBytes(hexStringToByteArray(json.get("swapOrderDataBytes").asString()));
260+
Context.require(_value.equals(swapOrder.amount), "Value and amount must be equal");
261+
Context.require(_from.toString().equals(swapOrder.creator), "Depositer must be creator");
253262
String type = json.get("type").asString();
263+
// token validated context.getCaller()
254264
// deposit.at(depositor).set(token, amount);
255265
if (type.equals("swap")) {
256-
SwapOrder swapOrder = SwapOrder
257-
.fromBytes(hexStringToByteArray(json.get("swapOrderDataBytes").asString()));
258-
deposit.at(swapOrder.creator).set(swapOrder.token, swapOrder.amount);
266+
267+
// deposit.at(swapOrder.creator).set(swapOrder.token, swapOrder.amount);
259268

260269
// Context.require(amount.equals(swapOrder.amount), "Token amount must be
261270
// equal");
262271
// Context.require(swapOrder.getToken() != null, "Token can't be null");
263-
Context.require(Context.getCaller().toString().equals(extractAddress(swapOrder.getCreator())),
264-
"Creator must be sender");
272+
// Context.require(Context.getCaller().toString().equals(swapOrder.getCreator()),
273+
// "Creator must be sender");
265274
_swap(swapOrder);
266275
} else {
267-
SwapOrder swapOrder = SwapOrder
268-
.fromBytes(hexStringToByteArray(json.get("swapOrderDataBytes").asString()));
269276

270277
SwapOrderData swapOrderData = new SwapOrderData();
271278
swapOrderData.id = swapOrder.id;
@@ -316,25 +323,25 @@ public static Boolean getFinishedorders(byte[] messageHash) {
316323
return finishedOrders.getOrDefault(messageHash, false);
317324
}
318325

319-
public static BigInteger getDepositAmount(String depositer, String token) {
320-
return deposit.at(depositer).getOrDefault(token, BigInteger.ZERO);
321-
}
326+
// public static BigInteger getDepositAmount(String depositer, String token) {
327+
// return deposit.at(depositer).getOrDefault(token, BigInteger.ZERO);
328+
// }
322329

323330
static void OnlyOwner() {
324331
Context.require(owner.get().equals(Context.getCaller()), "Not Owner");
325332
}
326333

327-
public static String extractAddress(String input) {
328-
if (input.contains("0x2.icon/")) {
329-
int lastSlashIndex = input.lastIndexOf('/');
330-
if (lastSlashIndex != -1 && lastSlashIndex < input.length() - 1) {
331-
return input.substring(lastSlashIndex + 1);
332-
}
333-
} else {
334-
return input;
335-
}
336-
return null;
337-
}
334+
// public static String extractAddress(String input) {
335+
// if (input.contains("0x2.icon/")) {
336+
// int lastSlashIndex = input.lastIndexOf('/');
337+
// if (lastSlashIndex != -1 && lastSlashIndex < input.length() - 1) {
338+
// return input.substring(lastSlashIndex + 1);
339+
// }
340+
// } else {
341+
// return input;
342+
// }
343+
// return null;
344+
// }
338345

339346
public static byte[] hexStringToByteArray(String s) {
340347
int len = s.length();

0 commit comments

Comments
 (0)