-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* impl DEXV2 to support bootstrap * format * fix
- Loading branch information
1 parent
95e0d85
commit 2b2a8ba
Showing
18 changed files
with
986 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import {DEX} from "./DEX.sol"; | ||
import {IBootstrap} from "./IBootstrap.sol"; | ||
|
||
/// @title DEX Predeploy Contract, V2, support bootstrap | ||
/// @author Acala Developers | ||
/// @notice You can use this predeploy contract to call dex pallet | ||
/// @dev This contracts will interact with dex pallet | ||
contract DEXV2 is DEX, IBootstrap { | ||
/// @inheritdoc IBootstrap | ||
function getProvisionPool( | ||
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( | ||
"getProvisionPool(address,address)", | ||
tokenA, | ||
tokenB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
return abi.decode(returnData, (uint256, uint256)); | ||
} | ||
|
||
/// @inheritdoc IBootstrap | ||
function getProvisionPoolOf( | ||
address who, | ||
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( | ||
"getProvisionPoolOf(address,address,address)", | ||
who, | ||
tokenA, | ||
tokenB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
return abi.decode(returnData, (uint256, uint256)); | ||
} | ||
|
||
/// @inheritdoc IBootstrap | ||
function getInitialShareExchangeRate( | ||
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( | ||
"getInitialShareExchangeRate(address,address)", | ||
tokenA, | ||
tokenB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
return abi.decode(returnData, (uint256, uint256)); | ||
} | ||
|
||
/// @inheritdoc IBootstrap | ||
function addProvision( | ||
address tokenA, | ||
address tokenB, | ||
uint256 amountA, | ||
uint256 amountB | ||
) public override returns (bool) { | ||
require(tokenA != address(0), "DEX: tokenA is zero address"); | ||
require(tokenB != address(0), "DEX: tokenB is zero address"); | ||
require( | ||
amountA != 0 || amountB != 0, | ||
"DEX: invalid contribution amount" | ||
); | ||
|
||
(bool success, bytes memory returnData) = PRECOMPILE.call( | ||
abi.encodeWithSignature( | ||
"addProvision(address,address,address,uint256,uint256)", | ||
msg.sender, | ||
tokenA, | ||
tokenB, | ||
amountA, | ||
amountB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
emit AddProvision(msg.sender, tokenA, tokenB, amountA, amountB); | ||
return true; | ||
} | ||
|
||
/// @inheritdoc IBootstrap | ||
function claimDexShare( | ||
address who, | ||
address tokenA, | ||
address tokenB | ||
) public override returns (bool) { | ||
require(who != address(0), "DEX: who is zero 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.call( | ||
abi.encodeWithSignature( | ||
"claimDexShare(address,address,address)", | ||
who, | ||
tokenA, | ||
tokenB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
emit ClaimShare(who, tokenA, tokenB, abi.decode(returnData, (uint256))); | ||
return true; | ||
} | ||
|
||
/// @inheritdoc IBootstrap | ||
function refundProvision( | ||
address who, | ||
address tokenA, | ||
address tokenB | ||
) public override returns (bool) { | ||
require(who != address(0), "DEX: who is zero 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.call( | ||
abi.encodeWithSignature( | ||
"refundProvision(address,address,address)", | ||
who, | ||
tokenA, | ||
tokenB | ||
) | ||
); | ||
assembly { | ||
if eq(success, 0) { | ||
revert(add(returnData, 0x20), returndatasize()) | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/// @title IBootstrap Predeploy Contract Interface | ||
/// @author Acala Developers | ||
/// @notice You can use this predeploy contract to call the bootstrap functions of dex pallet | ||
/// @dev The interface through which solidity contracts will interact with dex pallet | ||
interface IBootstrap { | ||
/// @notice AddProvision event. | ||
/// @param sender The sender of the transaction. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @param amountA The amount of tokenA contribute to provision pool. | ||
/// @param amountB The amount of tokenB contribute to provision pool. | ||
event AddProvision( | ||
address indexed sender, | ||
address indexed tokenA, | ||
address indexed tokenB, | ||
uint256 amountA, | ||
uint256 amountB | ||
); | ||
|
||
/// @notice Claim share event. | ||
/// @param who The owner of the claimed share. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @param amount The amount of claimed share token. | ||
event ClaimShare( | ||
address indexed who, | ||
address indexed tokenA, | ||
address indexed tokenB, | ||
uint256 amount | ||
); | ||
|
||
/// @notice Get total provision pool of the tokenA and tokenB. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @return Returns (provision_a, provision_b). | ||
function getProvisionPool( | ||
address tokenA, | ||
address tokenB | ||
) external view returns (uint256, uint256); | ||
|
||
/// @notice Get who's provision of the tokenA and tokenB. | ||
/// @param who The contributor. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @return Returns (provision_a, provision_b). | ||
function getProvisionPoolOf( | ||
address who, | ||
address tokenA, | ||
address tokenB | ||
) external view returns (uint256, uint256); | ||
|
||
/// @notice Get the initial share exchange rate of the ended provision pool of tokenA and tokenB. 100% = 1**18 | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @return Returns (rateA, rateB). | ||
function getInitialShareExchangeRate( | ||
address tokenA, | ||
address tokenB | ||
) external view returns (uint256, uint256); | ||
|
||
/// @notice Add provision to the bootstraping trading pair. | ||
/// @dev It'll emit an {AddProvision} event. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @param amountA The amount of tokenA contribute to liquidity pool. | ||
/// @param amountB The amount of tokenB contribute to liquidity pool. | ||
/// @return Returns a boolean value indicating whether the operation succeeded. | ||
function addProvision( | ||
address tokenA, | ||
address tokenB, | ||
uint256 amountA, | ||
uint256 amountB | ||
) external returns (bool); | ||
|
||
/// @notice Claim share token of the ended bootstrap trading pair for `who`. | ||
/// @dev It'll emit an {ClaimShare} event. | ||
/// @param who The contributor. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @return Returns a boolean value indicating whether the operation succeeded. | ||
function claimDexShare( | ||
address who, | ||
address tokenA, | ||
address tokenB | ||
) external returns (bool); | ||
|
||
/// @notice Refund the contribution token of the aborted bootstrap trading pair for `who`. | ||
/// @param who The contributor. | ||
/// @param tokenA The ERC20 address of the tokenA. | ||
/// @param tokenB The ERC20 address of the tokenB. | ||
/// @return Returns a boolean value indicating whether the operation succeeded. | ||
function refundProvision( | ||
address who, | ||
address tokenA, | ||
address tokenB | ||
) external returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.