Skip to content

Commit

Permalink
test: performed swap in a single transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
0ptimizerr committed Dec 11, 2024
1 parent d157fa2 commit 54fc14b
Show file tree
Hide file tree
Showing 3 changed files with 513 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,33 @@ public Intent(String _nid, BigInteger _protocolFee, Address _feeHandler, Address
owner.set(Context.getCaller());
}

// @External
// @Payable
// public void swap(SwapOrderData swapOrderData) {
// Context.require(swapOrderData.token != null, "Token can't be null");
// Context.require(Context.getCaller().toString().equals(swapOrderData.creator),
// "Creator must be sender");

// Address token = Address.fromString(swapOrderData.token);
// Address nativAddress = nativeAddress.get();
// if (token.equals(nativAddress)) {
// Context.require(Context.getValue().equals(swapOrderData.amount),
// "Deposit amount not equal to order amount");
// } else {
// Context.require(Context.getValue().equals(BigInteger.valueOf(0)),
// "Nativ Token Must Be Zero");
// Context.call(token, "transfer", Context.getAddress(), swapOrderData.amount);
// }

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

// _swap(swapOrder);
// }
@External
@Payable
public void swap(SwapOrderData swapOrderData) {
Context.require(swapOrderData.token != null, "Token can't be null");
Context.require(Context.getCaller().toString().equals(swapOrderData.creator),
"Creator must be sender");

Address token = Address.fromString(swapOrderData.token);
Address nativAddress = nativeAddress.get();
if (token.equals(nativAddress)) {
Context.require(Context.getValue().equals(swapOrderData.amount),
"Deposit amount not equal to order amount");
} else {
Context.require(Context.getValue().equals(BigInteger.valueOf(0)),
"Nativ Token Must Be Zero");
Context.call(token, "transfer", Context.getAddress(), swapOrderData.amount);
}

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

_swap(swapOrder);
}

void _swap(SwapOrder swapOrder) {
BigInteger id = this.depositId.getOrDefault(BigInteger.ZERO).add(BigInteger.valueOf(1));
Expand Down
Loading

0 comments on commit 54fc14b

Please sign in to comment.