Skip to content

Commit

Permalink
Add fee on getSwapAmountCrossPool (#16)
Browse files Browse the repository at this point in the history
* Add swap fee

* fix CI

* update comments
  • Loading branch information
zjb0807 authored Aug 3, 2023
1 parent ad23177 commit 54917f7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
10 changes: 6 additions & 4 deletions contracts/StableAssetApplication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,16 @@ contract StableAssetApplication is Initializable, ReentrancyGuardUpgradeable {
* @param _sourceToken source token.
* @param _destToken dest token.
* @param _amount Amount of source token to swap.
* @return The Amount of dest token to get.
* @return The amount of fee to charge.
*/
function getSwapAmountCrossPool(
StableAsset _sourceSwap,
StableAsset _destSwap,
address _sourceToken,
address _destToken,
uint256 _amount
) public view returns (uint256) {
) public view returns (uint256, uint256) {
address[] memory sourceTokens = _sourceSwap.getTokens();
address[] memory destTokens = _destSwap.getTokens();
require(allowedPoolAddress[address(_sourceSwap)], "pool not allowed");
Expand All @@ -240,12 +242,12 @@ contract StableAssetApplication is Initializable, ReentrancyGuardUpgradeable {
uint256 destIndex = findTokenIndex(destTokens, _destToken);
uint256[] memory _mintAmounts = new uint256[](sourceTokens.length);
_mintAmounts[sourceIndex] = _amount;
(uint256 mintAmount, ) = _sourceSwap.getMintAmount(_mintAmounts);
(uint256 redeemAmount, ) = _destSwap.getRedeemSingleAmount(
(uint256 mintAmount, uint256 mintFee) = _sourceSwap.getMintAmount(_mintAmounts);
(uint256 redeemAmount, uint256 redeemFee) = _destSwap.getRedeemSingleAmount(
mintAmount,
destIndex
);
return redeemAmount;
return (redeemAmount, mintFee + redeemFee);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions docs/StableAssetApplication.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function allowedPoolAddress(address) external view returns (bool)
### getSwapAmountCrossPool

```solidity
function getSwapAmountCrossPool(contract StableAsset _sourceSwap, contract StableAsset _destSwap, address _sourceToken, address _destToken, uint256 _amount) external view returns (uint256)
function getSwapAmountCrossPool(contract StableAsset _sourceSwap, contract StableAsset _destSwap, address _sourceToken, address _destToken, uint256 _amount) external view returns (uint256, uint256)
```


Expand All @@ -56,7 +56,8 @@ function getSwapAmountCrossPool(contract StableAsset _sourceSwap, contract Stabl

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |
| _0 | uint256 | The Amount of dest token to get. |
| _1 | uint256 | The amount of fee to charge. |

### governance

Expand Down
18 changes: 7 additions & 11 deletions test/StableAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,11 @@ describe("StableAsset", function () {
await expect(upgrades.deployProxy(StableAsset, [[token1.address, ethers.constants.AddressZero], [PRECISION, PRECISION], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0, constant.address, 1]))
.to.be.revertedWith("token not set");

// TODO: uncomment after fix the TODO in initialize
// await expect(upgrades.deployProxy(StableAsset, [[token1.address, token2.address], [PRECISION, 10], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0]))
// .to.be.revertedWith("precision not set");
await expect(upgrades.deployProxy(StableAsset, [[token1.address, token2.address], [PRECISION, 10], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0, constant.address, 1])).to.be.revertedWith("precision not set");

// await expect(upgrades.deployProxy(StableAsset, [[token1.address, token17.address], [PRECISION, "10000000000000000"], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0]))
// .to.be.revertedWith("precision not set");
await expect(upgrades.deployProxy(StableAsset, [[token1.address, token17.address], [PRECISION, "10000000000000000"], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0, constant.address, 1])).to.be.revertedWith("precision not set");

// await expect(upgrades.deployProxy(StableAsset, [[token1.address, token19.address], [1, "1000000000000000000"], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0]))
// .to.be.revertedWithPanic(0x11);
await expect(upgrades.deployProxy(StableAsset, [[token1.address, token19.address], [1, "1000000000000000000"], [MINT_FEE, SWAP_FEE, REDEEM_FEE], feeRecipient.address, yieldRecipient.address, poolToken.address, 0, constant.address, 1])).to.be.revertedWithPanic(0x11);

/// Check deploy swap with fee recipient not set
await expect(upgrades.deployProxy(StableAsset, [[token1.address, token2.address], [PRECISION, PRECISION], [MINT_FEE, SWAP_FEE, REDEEM_FEE], ethers.constants.AddressZero, yieldRecipient.address, poolToken.address, 0, constant.address, 1]))
Expand Down Expand Up @@ -1457,13 +1453,13 @@ describe("StableAsset", function () {
await expect(swap.updateA(1000, 8)).to.be.revertedWith("block in the past");

/// Check updateA fails if A not set
await expect(swap.updateA(0, 26)).to.be.revertedWith("A not set");
await expect(swap.updateA(0, 40)).to.be.revertedWith("A not set");

/// Check updateA fails if A exceeds max
await expect(swap.updateA(1000000, 27)).to.be.revertedWith("A not set");
await expect(swap.updateA(1000000, 40)).to.be.revertedWith("A not set");

/// Update A to 1000 at block 28
await swap.updateA(1000, 30); // need extra block to update
/// Update A to 1000 at block 50
await swap.updateA(1000, 50); // need extra block to update
/// Check initial A is 100
expect(await swap.initialA()).to.equals(100);
/// Check future A is 1000
Expand Down
2 changes: 1 addition & 1 deletion test/StableAssetApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ describe("StableAssetApplication", function () {
/// Get swap amount cross pool with token1 to token2
const amount = await application.getSwapAmountCrossPool(swapOne.address, swapTwo.address, token1.address, token2.address, web3.utils.toWei('1'));
/// Check amount is greater than 0
expect(amount).to.greaterThan(0);
expect(amount.toString()).to.equal("993980347757552144,5994925804469116");
});

it('should swap cross pool', async () => {
Expand Down
2 changes: 1 addition & 1 deletion test/docs/StableAsset.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@
- Check updateA fails if block in the past
- Check updateA fails if A not set
- Check updateA fails if A exceeds max
- Update A to 1000 at block 28
- Update A to 1000 at block 50
- Check initial A is 100
- Check future A is 1000
- Deploy swap and tokens
Expand Down

0 comments on commit 54917f7

Please sign in to comment.