Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjj9219 committed Sep 9, 2024
1 parent 9065b31 commit c4c7e42
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 82 deletions.
125 changes: 92 additions & 33 deletions contracts/dex/DEX.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,115 @@ import {IDEX} from "./IDEX.sol";
/// @dev This contracts will interact with dex pallet
contract DEX is IDEX {
/// @dev The DEX precompile address.
address internal constant PRECOMPILE = address(0x0000000000000000000000000000000000000405);
address internal constant PRECOMPILE =
address(0x0000000000000000000000000000000000000405);

/// @inheritdoc IDEX
function getLiquidityPool(address tokenA, address tokenB) public view override returns (uint256, uint256) {
function getLiquidityPool(
address tokenA,
address tokenB
) public view override returns (uint256, uint256) {
require(tokenA != address(0), "DEX: tokenA is zero address");
require(tokenB != address(0), "DEX: tokenB is zero address");

(bool success, bytes memory returnData) =
PRECOMPILE.staticcall(abi.encodeWithSignature("getLiquidityPool(address,address)", tokenA, tokenB));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature(
"getLiquidityPool(address,address)",
tokenA,
tokenB
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint256, uint256));
}

/// @inheritdoc IDEX
function getLiquidityTokenAddress(address tokenA, address tokenB) public view override returns (address) {
function getLiquidityTokenAddress(
address tokenA,
address tokenB
) public view override returns (address) {
require(tokenA != address(0), "DEX: tokenA is zero address");
require(tokenB != address(0), "DEX: tokenB is zero address");

(bool success, bytes memory returnData) =
PRECOMPILE.staticcall(abi.encodeWithSignature("getLiquidityTokenAddress(address,address)", tokenA, tokenB));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature(
"getLiquidityTokenAddress(address,address)",
tokenA,
tokenB
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (address));
}

/// @inheritdoc IDEX
function getSwapTargetAmount(address[] memory path, uint256 supplyAmount) public view override returns (uint256) {
function getSwapTargetAmount(
address[] memory path,
uint256 supplyAmount
) public view override returns (uint256) {
for (uint256 i = 0; i < path.length; i++) {
require(path[i] != address(0), "DEX: token is zero address");
}
require(supplyAmount != 0, "DEX: supplyAmount is zero");

(bool success, bytes memory returnData) =
PRECOMPILE.staticcall(abi.encodeWithSignature("getSwapTargetAmount(address[],uint256)", path, supplyAmount));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature(
"getSwapTargetAmount(address[],uint256)",
path,
supplyAmount
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint256));
}

/// @inheritdoc IDEX
function getSwapSupplyAmount(address[] memory path, uint256 targetAmount) public view override returns (uint256) {
function getSwapSupplyAmount(
address[] memory path,
uint256 targetAmount
) public view override returns (uint256) {
for (uint256 i = 0; i < path.length; i++) {
require(path[i] != address(0), "DEX: token is zero address");
}
require(targetAmount != 0, "DEX: targetAmount is zero");

(bool success, bytes memory returnData) =
PRECOMPILE.staticcall(abi.encodeWithSignature("getSwapSupplyAmount(address[],uint256)", path, targetAmount));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature(
"getSwapSupplyAmount(address[],uint256)",
path,
targetAmount
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint256));
}

/// @inheritdoc IDEX
function swapWithExactSupply(address[] memory path, uint256 supplyAmount, uint256 minTargetAmount)
public
override
returns (bool)
{
function swapWithExactSupply(
address[] memory path,
uint256 supplyAmount,
uint256 minTargetAmount
) public override returns (bool) {
for (uint256 i = 0; i < path.length; i++) {
require(path[i] != address(0), "DEX: token is zero address");
}
Expand All @@ -93,19 +134,26 @@ contract DEX is IDEX {
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

emit Swaped(msg.sender, path, supplyAmount, abi.decode(returnData, (uint256)));
emit Swaped(
msg.sender,
path,
supplyAmount,
abi.decode(returnData, (uint256))
);
return true;
}

/// @inheritdoc IDEX
function swapWithExactTarget(address[] memory path, uint256 targetAmount, uint256 maxSupplyAmount)
public
override
returns (bool)
{
function swapWithExactTarget(
address[] memory path,
uint256 targetAmount,
uint256 maxSupplyAmount
) public override returns (bool) {
for (uint256 i = 0; i < path.length; i++) {
require(path[i] != address(0), "DEX: token is zero address");
}
Expand All @@ -121,10 +169,17 @@ contract DEX is IDEX {
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

emit Swaped(msg.sender, path, abi.decode(returnData, (uint256)), targetAmount);
emit Swaped(
msg.sender,
path,
abi.decode(returnData, (uint256)),
targetAmount
);
return true;
}

Expand Down Expand Up @@ -153,7 +208,9 @@ contract DEX is IDEX {
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

emit AddedLiquidity(msg.sender, tokenA, tokenB, maxAmountA, maxAmountB);
Expand Down Expand Up @@ -184,7 +241,9 @@ contract DEX is IDEX {
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

emit RemovedLiquidity(msg.sender, tokenA, tokenB, removeShare);
Expand Down
Loading

0 comments on commit c4c7e42

Please sign in to comment.