Skip to content

Commit

Permalink
fix: removed permit impl and added token fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
0ptimizerr committed Dec 6, 2024
1 parent 609656a commit 1fc1d87
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 385 deletions.
2 changes: 2 additions & 0 deletions contracts/javascore/Intent_Contracts/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
compileOnly 'foundation.icon:javaee-api:0.9.6'
implementation 'com.github.sink772:javaee-tokens:0.6.4'
implementation 'org.json:json:20230227'
testImplementation 'foundation.icon:javaee-unittest:0.10.0'
testImplementation 'org.mockito:mockito-core:4.8.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
Expand Down Expand Up @@ -38,3 +39,4 @@ test {
}



Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import network.icon.intent.structs.OrderFill;
import network.icon.intent.structs.OrderMessage;
import network.icon.intent.structs.SwapOrder;
import network.icon.intent.utils.PermitTransferFromData;
import network.icon.intent.utils.SignatureTransferDetailsData;
import network.icon.intent.utils.SwapOrderData;
import score.*;
import score.annotation.EventLog;
import score.annotation.External;
import score.annotation.Payable;
import org.json.JSONObject;

public class Intent extends GeneralizedConnection {

Expand All @@ -24,11 +23,10 @@ public class Intent extends GeneralizedConnection {
public static final VarDB<Address> feeHandler = Context.newVarDB(Constant.FEE_HANDLER, Address.class);
public static final VarDB<Address> owner = Context.newVarDB(Constant.OWNER, Address.class);
public final VarDB<Address> nativeAddress = Context.newVarDB(Constant.NATIVE_ADDRESS, Address.class);
final static BranchDB<Address, DictDB<Address, BigInteger>> deposit = Context.newBranchDB(Constant.DEPOSIT,
final static BranchDB<String, DictDB<String, BigInteger>> deposit = Context.newBranchDB(Constant.DEPOSIT,
BigInteger.class);
final static DictDB<BigInteger, SwapOrder> orders = Context.newDictDB(Constant.ORDERS, SwapOrder.class);
final static DictDB<byte[], Boolean> finishedOrders = Context.newDictDB(Constant.FINISHED_ORDERS, Boolean.class);
public final VarDB<PermitTransferFromData> permit = Context.newVarDB(Constant.PERMIT, PermitTransferFromData.class);

@EventLog(indexed = 3)
public void SwapIntent(
Expand Down Expand Up @@ -90,42 +88,6 @@ public void swap(SwapOrderData swapOrderData) {
_swap(swapOrder);
}

@External
public void swapPermit2(
SwapOrderData order,
byte[] signature,
PermitTransferFromData permit) {
Permit2 permit2 = new Permit2();
order.id = BigInteger.ZERO;

SignatureTransferDetailsData transferDetails = new SignatureTransferDetailsData();
transferDetails.to = Context.getAddress();
transferDetails.requestedAmount = order.amount;

permit2.permitWitnessTransferFrom(
permit,
transferDetails,
Address.fromString(order.creator),
Context.hash("keccak-256", order.data.getBytes()),
"",
signature);

SwapOrder swapOrder = new SwapOrder(
order.id,
order.emitter,
order.srcNID,
order.dstNID,
order.creator,
order.destinationAddress,
order.token,
order.amount,
order.toToken,
order.toAmount,
order.data);

_swap(swapOrder);
}

void _swap(SwapOrder swapOrder) {
BigInteger id = this.depositId.getOrDefault(BigInteger.ZERO).add(BigInteger.valueOf(1));
swapOrder.id = id;
Expand Down Expand Up @@ -271,12 +233,19 @@ void _nativeTransfer(Address to, BigInteger amount) {
}

@External
public void setPermit(PermitTransferFromData _permit) {
permit.set(_permit);
}

@Payable
public void fallback() {
public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
Context.require(_value.compareTo(BigInteger.ZERO) > 0, "Zero transfers not allowed");
String unpackedData = new String(_data);
Context.require(!unpackedData.equals(""), "Token Fallback: Data can't be empty");

JSONObject jsonObject = new JSONObject(unpackedData);
// string(address of depositer) -> string (deposits token address) -> amount of
// token
String depositor = jsonObject.get("depositor").toString();
String token = jsonObject.get("token").toString();
BigInteger amount = jsonObject.getBigInteger("amount");

deposit.at(depositor).set(token, amount);
}

@External
Expand Down Expand Up @@ -308,6 +277,10 @@ public static Boolean getFinishedorders(byte[] messageHash) {
return finishedOrders.getOrDefault(messageHash, false);
}

public static BigInteger getDepositAmount(String depositer, String token) {
return deposit.at(depositer).getOrDefault(token, BigInteger.ZERO);
}

static void OnlyOwner() {
Context.require(owner.get().equals(Context.getCaller()), "Not Owner");
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@ public class Constant {
public final static String RELAY_ADDRESS = "relayAddress";
public final static String CONN_SN = "connSn";

// Permit Variables
public final static String TOKEN_PERMISSIONS_TYPE = "TokenPermissions(address token,uint256 amount)";
public final static String _TOKEN_PERMISSION_TYPE = "_token_permission_type";
public final static String NONCE_BITMAP = "nonceBitmap";
public final static String _HASHED_NAME = "_hashed_name";
public final static String _TYPE_HASH = "_type_hash";
public final static String _TOKEN_PERMISSION_TYPEHASH = "_token_permission_typehash";
public final static String _PERMIT_TRANSFER_FROM_WITNESS_TYPEHASH_STUB = "PermitWitnessTransferFrom(TokenPermissions permitted,address spender,uint256 nonce, uint256 deadline,";
public final static String _pERMIT_tRANSFER_fROM_wITNESS_tYPEHASH_sTUB = "_pERMIT_tRANSFER_fROM_wITNESS_tYPEHASH_sTUB";
public final static String _CHAIN_ID = "_chain_id";
public final static String _CACHED_DOMAIN_SEPARATOR = "_CACHED_DOMAIN_SEPARATOR";
public final static String _CACHED_CHAIN_ID = "_CACHED_CHAIN_ID";

// Intent Variables
public final static String DEPOSIT_ID = "depositId";
public final static String NETWORK_ID = "networkId";
Expand Down
Loading

0 comments on commit 1fc1d87

Please sign in to comment.