@@ -23,12 +23,16 @@ public class Intent extends GeneralizedConnection {
23
23
public final VarDB <BigInteger > protocolFee = Context .newVarDB (Constant .PROTOCOL_FEE , BigInteger .class );
24
24
public static final VarDB <Address > feeHandler = Context .newVarDB (Constant .FEE_HANDLER , Address .class );
25
25
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);
29
31
final static DictDB <BigInteger , SwapOrder > orders = Context .newDictDB (Constant .ORDERS , SwapOrder .class );
30
32
final static DictDB <byte [], Boolean > finishedOrders = Context .newDictDB (Constant .FINISHED_ORDERS , Boolean .class );
31
33
34
+ public static Address nativeAddress = Address .fromString ("cx0000000000000000000000000000000000000000" );
35
+
32
36
@ EventLog (indexed = 3 )
33
37
public void SwapIntent (
34
38
BigInteger id ,
@@ -61,22 +65,22 @@ public Intent(String _nid, BigInteger _protocolFee, Address _feeHandler, Address
61
65
protocolFee .set (_protocolFee );
62
66
feeHandler .set (_feeHandler );
63
67
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 ());
66
70
}
67
71
68
72
@ External
69
73
@ Payable
70
74
public void swap (SwapOrderData swapOrderData ) {
71
75
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 ),
73
77
"Creator must be sender" );
74
78
Context .require (Context .getValue ().equals (swapOrderData .amount ),
75
79
"Deposit amount not equal to order amount" );
76
80
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" );
80
84
81
85
SwapOrder swapOrder = new SwapOrder (swapOrderData .id , swapOrderData .emitter ,
82
86
swapOrderData .srcNID ,
@@ -85,7 +89,8 @@ public void swap(SwapOrderData swapOrderData) {
85
89
swapOrderData .amount , swapOrderData .toToken , swapOrderData .toAmount ,
86
90
swapOrderData .data );
87
91
88
- deposit .at (swapOrderData .creator ).set (swapOrderData .token , swapOrderData .amount );
92
+ // deposit.at(swapOrderData.creator).set(swapOrderData.token,
93
+ // swapOrderData.amount);
89
94
90
95
_swap (swapOrder );
91
96
}
@@ -94,7 +99,7 @@ void _swap(SwapOrder swapOrder) {
94
99
BigInteger id = this .depositId .getOrDefault (BigInteger .ZERO ).add (BigInteger .valueOf (1 ));
95
100
swapOrder .id = id ;
96
101
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 ()),
98
103
"Emitter specified is not this" ); // emitter contract or eoa?
99
104
orders .set (id , swapOrder );
100
105
SwapIntent (id , swapOrder .emitter , swapOrder .srcNID , swapOrder .dstNID ,
@@ -143,11 +148,11 @@ public void cancel(BigInteger id) {
143
148
if (order == null ) {
144
149
Context .revert ("Order already has been cancelled" );
145
150
}
146
- Context .require (Address .fromString (extractAddress ( order .creator ) ).equals (Context .getCaller ()),
151
+ Context .require (Address .fromString (order .creator ).equals (Context .getCaller ()),
147
152
"Only creator can cancel this order" );
148
153
149
154
if (order .srcNID .equals (order .dstNID )) {
150
- _resolveCancel (nativeAddress .get (). toString (), order .toBytes ());
155
+ _resolveCancel (nativeAddress .toString (), order .toBytes ());
151
156
return ;
152
157
}
153
158
@@ -209,19 +214,19 @@ void _resolveFill(String srcNetwork, OrderFill _fill) {
209
214
orders .set (_fill .id , null );
210
215
OrderClosed (_fill .id );
211
216
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 );
215
220
} else {
216
- Context .call (tokenAddress , "transfer" , Address .fromString (extractAddress ( _fill .solver ) ), order .amount );
221
+ Context .call (tokenAddress , "transfer" , Address .fromString (_fill .solver ), order .amount );
217
222
}
218
223
}
219
224
220
225
void _transferResult (String _toAddress , String _toToken , BigInteger amount ,
221
226
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 )) {
225
230
Context .require (Context .getValue ().equals (amount .add (fee )), "\" Deposit amount not equal to order amount\" " );
226
231
_nativeTransfer (toAddress , amount );
227
232
_nativeTransfer (feeHandler .get (), fee );
@@ -250,22 +255,24 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
250
255
// String depositor = json.get("depositor").asString();
251
256
// String token = json.get("token").asString();
252
257
// 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" );
253
262
String type = json .get ("type" ).asString ();
263
+ // token validated context.getCaller()
254
264
// deposit.at(depositor).set(token, amount);
255
265
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);
259
268
260
269
// Context.require(amount.equals(swapOrder.amount), "Token amount must be
261
270
// equal");
262
271
// 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");
265
274
_swap (swapOrder );
266
275
} else {
267
- SwapOrder swapOrder = SwapOrder
268
- .fromBytes (hexStringToByteArray (json .get ("swapOrderDataBytes" ).asString ()));
269
276
270
277
SwapOrderData swapOrderData = new SwapOrderData ();
271
278
swapOrderData .id = swapOrder .id ;
@@ -316,25 +323,25 @@ public static Boolean getFinishedorders(byte[] messageHash) {
316
323
return finishedOrders .getOrDefault (messageHash , false );
317
324
}
318
325
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
+ // }
322
329
323
330
static void OnlyOwner () {
324
331
Context .require (owner .get ().equals (Context .getCaller ()), "Not Owner" );
325
332
}
326
333
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
+ // }
338
345
339
346
public static byte [] hexStringToByteArray (String s ) {
340
347
int len = s .length ();
0 commit comments