Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

[LP_swaps] feat: return funds for unfulfillable swap #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions packages/omgx/wallet/contracts/LP/L1LiquidityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -510,20 +510,46 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled, Ownable {
external
onlyFromCrossDomainAccount(address(L2LiquidityPoolAddress))
{
bool replyNeeded = false;

if (_tokenAddress != address(0)) {
IERC20(_tokenAddress).safeTransfer(_to, _amount);
if (_amount > IERC20(_tokenAddress).balanceOf(address(this))) {
replyNeeded = true;
} else {
IERC20(_tokenAddress).safeTransfer(_to, _amount);
}
} else {
//this is ETH
// balances[address(0)] = balances[address(0)].sub(_amount);
//_to.transfer(_amount); UNSAFE
(bool sent,) = _to.call{gas: SAFE_GAS_STIPEND, value: _amount}("");
require(sent, "Failed to send Ether");
if (_amount > address(this).balance) {
replyNeeded = true;
} else {
//this is ETH
// balances[address(0)] = balances[address(0)].sub(_amount);
//_to.transfer(_amount); UNSAFE
(bool sent,) = _to.call{gas: SAFE_GAS_STIPEND, value: _amount}("");
require(sent, "Failed to send Ether");
}
}

if (replyNeeded) {
// send cross domain message
bytes memory data = abi.encodeWithSelector(
iL2LiquidityPool.clientPayL2.selector,
_to,
_amount,
poolInfo[_tokenAddress].l2TokenAddress
);

sendCrossDomainMessage(
address(L2LiquidityPoolAddress),
data,
getFinalizeDepositL2Gas()
);
} else {
emit ClientPayL1(
_to,
_amount,
_tokenAddress
);
}

emit ClientPayL1(
_to,
_amount,
_tokenAddress
);
}
}
16 changes: 8 additions & 8 deletions packages/omgx/wallet/deployment/local/addresses.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"L1LiquidityPool": "0x247ADeE8E350cBEB05aB73DE88Be07CFbD736b2E",
"L2LiquidityPool": "0x88A7bA27B123e344f544EA19a50CA0c730FA87a2",
"L1ERC20": "0x22247e1bC94a8E640f179b2b1e8B50bF54C4e0F3",
"L2DepositedERC20": "0xFf413B7aeBcEfD231A2ce6592ABDD9aed820ec5a",
"L1ERC20Gateway": "0xa10542f51041645d61a49121383FD80b0D18D986",
"L1LiquidityPool": "0xB569529B9C2c45FB18d9c7c3A67BDaf8A1F1Ee06",
"L2LiquidityPool": "0x3C38fd8D4e647b230D006d98793Fa3A0ec60bC41",
"L1ERC20": "0x6C50B630eF1deff14b642E135A052F376a7E440a",
"L2DepositedERC20": "0xf7511f94161aA90Da8babc08D7F1D11cD60505F3",
"L1ERC20Gateway": "0x9BD764a33bA117a6bC453422CCD459bc66668B45",
"l1ETHGatewayAddress": "0x4F53A01556Dc6f120db9a1b4caE786D5f0b5792C",
"l1MessengerAddress": "0xA6404B184Ad3f6F41b6472f02ba45f25C6A66d49",
"L2TokenPool": "0xa94ec9484BfC9aB8d3fD80eB2735f132d257a135",
"AtomicSwap": "0xD4B82BDB1Abb1c1506173722c7d51FFCA659124f",
"L2ERC721": "0xEFA3B6C1203ED5fF8f4E1f6F7A09dCe529Fb9EbF"
"L2TokenPool": "0x2d7B1D4bA3E4d77025aE5bec3bc17781389Bd182",
"AtomicSwap": "0xe4B6ebE478865b5b4C94D9240cFa6DA5C73470d0",
"L2ERC721": "0x5952bFfE9d6C232439E265D0E1A775Bc721AFA90"
}
30 changes: 30 additions & 0 deletions packages/omgx/wallet/test/b_lp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,34 @@ describe('Liquidity Pool Test', async () => {
postPoolInfo.accOwnerReward.sub(depositAmount.mul(15).div(1000))
)
})

it("should revert unfulfillable swaps", async () => {
const preBobL2ERC20Balance = await L2DepositedERC20.balanceOf(env.bobl2Wallet.address)
const preBobL1ERC20Balance = await L1ERC20.balanceOf(env.bobl1Wallet.address)
const requestedLiquidity = (await L1ERC20.balanceOf(L1LiquidityPool.address)).add(1)
const fastExitAmount = requestedLiquidity.mul(1000).div(950)

const approveBobL2TX = await L2DepositedERC20.connect(env.bobl2Wallet).approve(
L2LiquidityPool.address,
fastExitAmount,
)
await approveBobL2TX.wait()

await env.waitForRevertXDomainTransaction(
L2LiquidityPool.connect(env.bobl2Wallet).clientDepositL2(
fastExitAmount,
L2DepositedERC20.address
),
Direction.L2ToL1,
Relayer.custom,
)

const postBobL1ERC20Balance = await L1ERC20.balanceOf(env.bobl1Wallet.address)
const postBobL2ERC20Balance = await L2DepositedERC20.balanceOf(env.bobl2Wallet.address)

expect(preBobL1ERC20Balance).to.deep.eq(postBobL1ERC20Balance)

const exitFees = fastExitAmount.mul(50).div(1000)
expect(postBobL2ERC20Balance).to.deep.eq(preBobL2ERC20Balance.sub(exitFees))
})
})
10 changes: 10 additions & 0 deletions packages/omgx/wallet/test/shared/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,14 @@ export class OptimismEnv {
): Promise<CrossDomainMessagePair> {
return waitForXDomainTransaction(relayer === Relayer.origin ? this.watcher : this.customWatcher, tx, direction)
}

async waitForRevertXDomainTransaction(
tx: Promise<TransactionResponse> | TransactionResponse,
direction: Direction,
relayer: Relayer = Relayer.origin,
) {
const {remoteReceipt} = await waitForXDomainTransaction(relayer === Relayer.origin ? this.watcher : this.customWatcher, tx, direction)
const [xDomainMsgHash] = await this.watcher.getMessageHashesFromL1Tx(remoteReceipt.transactionHash)
await this.watcher.getL2TransactionReceipt(xDomainMsgHash)
}
}
3 changes: 2 additions & 1 deletion packages/omgx/wallet/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"resolveJsonModule": true,
},
"include": ["./test"],
"files": ["./hardhat.config.ts"]
Expand Down
16 changes: 8 additions & 8 deletions packages/omgx/wallet/wallet/src/deployment/local/addresses.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"L1LiquidityPool": "0x196A1b2750D9d39ECcC7667455DdF1b8d5D65696",
"L2LiquidityPool": "0x3e4CFaa8730092552d9425575E49bB542e329981",
"L1ERC20": "0xf952b82ED61797Cd0b93F02e42aC6B85EB4dF127",
"L2DepositedERC20": "0x65F72DF8a668BC6272B059BB7F53ADc91066540C",
"L1ERC20Gateway": "0x6353d6c697cc36446EC727Ae032B518b7FbEc82d",
"L1LiquidityPool": "0xB569529B9C2c45FB18d9c7c3A67BDaf8A1F1Ee06",
"L2LiquidityPool": "0x3C38fd8D4e647b230D006d98793Fa3A0ec60bC41",
"L1ERC20": "0x6C50B630eF1deff14b642E135A052F376a7E440a",
"L2DepositedERC20": "0xf7511f94161aA90Da8babc08D7F1D11cD60505F3",
"L1ERC20Gateway": "0x9BD764a33bA117a6bC453422CCD459bc66668B45",
"l1ETHGatewayAddress": "0x4F53A01556Dc6f120db9a1b4caE786D5f0b5792C",
"l1MessengerAddress": "0xA6404B184Ad3f6F41b6472f02ba45f25C6A66d49",
"L2TokenPool": "0x8d00515443437fB466f89CA0B155A889b10EBa68",
"AtomicSwap": "0x3C38fd8D4e647b230D006d98793Fa3A0ec60bC41",
"L2ERC721": "0xcdB44B1EC9F2aDd7740EeAc4701c928B80727469"
"L2TokenPool": "0x2d7B1D4bA3E4d77025aE5bec3bc17781389Bd182",
"AtomicSwap": "0xe4B6ebE478865b5b4C94D9240cFa6DA5C73470d0",
"L2ERC721": "0x5952bFfE9d6C232439E265D0E1A775Bc721AFA90"
}