Skip to content

Commit

Permalink
stable asset factory
Browse files Browse the repository at this point in the history
  • Loading branch information
Crypto Dev committed Nov 10, 2024
1 parent 9ac380c commit b609ee9
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 164 deletions.
160 changes: 160 additions & 0 deletions contracts/StableAssetFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
import "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";

import "./StableAsset.sol";
import "./TapETH.sol";
import "./misc/ConstantExchangeRateProvider.sol";

/**
* @title StableAsset Application
* @author Nuts Finance Developer
* @notice The StableSwap Application provides an interface for users to interact with StableSwap pool contracts
* @dev The StableSwap Application contract allows users to mint pool tokens, swap between different tokens, and redeem pool tokens to underlying tokens.
* This contract should never store assets.
*/
contract StableAssetFactory is Initializable, ReentrancyGuardUpgradeable {
using SafeMathUpgradeable for uint256;
using SafeERC20Upgradeable for IERC20Upgradeable;

struct CreatePoolArgument {
address tokenA;
address tokenB;
uint256 precisionA;
uint256 precisionB;
uint256 mintFee;
uint256 swapFee;
uint256 redeemFee;
uint256 A;
}

/**
* @dev This event is emitted when the governance is modified.
* @param governance is the new value of the governance.
*/
event GovernanceModified(address governance);

/**
* @dev This event is emitted when the governance is modified.
* @param governance is the new value of the governance.
*/
event GovernanceProposed(address governance);

/**
* @dev This event is emitted when a new pool is created.
* @param poolToken is the pool token created.
*/
event PoolCreated(address proxyAdmin, address poolToken, address stableAsset);

/**
* @dev This is the account that has governance control over the StableAssetApplication contract.
*/
address public governance;

/**
* @dev Pending governance address,
*/
address public pendingGovernance;

address public stableAssetImplentation;
address public tapETHImplentation;
ConstantExchangeRateProvider public constantExchangeRateProvider;

/**
* @dev Initializes the StableSwap Application contract.
*/
function initialize(
address _stableAssetImplentation,
address _tapETHImplentation
) public initializer {
__ReentrancyGuard_init();
governance = msg.sender;
stableAssetImplentation = _stableAssetImplentation;
tapETHImplentation = _tapETHImplentation;
constantExchangeRateProvider = new ConstantExchangeRateProvider();
}

/**
* @dev Propose the govenance address.
* @param _governance Address of the new governance.
*/
function proposeGovernance(address _governance) public {
require(msg.sender == governance, "not governance");
pendingGovernance = _governance;
emit GovernanceProposed(_governance);
}

/**
* @dev Accept the govenance address.
*/
function acceptGovernance() public {
require(msg.sender == pendingGovernance, "not pending governance");
governance = pendingGovernance;
pendingGovernance = address(0);
emit GovernanceModified(governance);
}

function createPool(CreatePoolArgument calldata argument) public {
ProxyAdmin proxyAdmin = new ProxyAdmin();
proxyAdmin.transferOwnership(msg.sender);
bytes memory tapETHInit = abi.encodeCall(
TapETH.initialize,
(address(this))
);
TransparentUpgradeableProxy tapETHProxy = new TransparentUpgradeableProxy(
address(tapETHImplentation),
address(proxyAdmin),
tapETHInit
);

address[] memory tokens = new address[](2);
uint256[] memory precisions = new uint256[](2);
uint256[] memory fees = new uint256[](3);
tokens[0] = argument.tokenA;
tokens[1] = argument.tokenB;
precisions[0] = argument.precisionA;
precisions[1] = argument.precisionB;
fees[0] = argument.mintFee;
fees[1] = argument.swapFee;
fees[2] = argument.redeemFee;
uint256 A = argument.A;
uint256 exchangeRateTokenIndex = 1;

bytes memory stableAssetInit = abi.encodeCall(
StableAsset.initialize,
(
tokens,
precisions,
fees,
TapETH(address(tapETHProxy)),
A,
constantExchangeRateProvider,
exchangeRateTokenIndex
)
);
TransparentUpgradeableProxy stableAssetProxy = new TransparentUpgradeableProxy(
address(stableAssetImplentation),
address(proxyAdmin),
stableAssetInit
);
StableAsset stableAsset = StableAsset(address(stableAssetProxy));
TapETH tapETH = TapETH(address(tapETHProxy));

stableAsset.proposeGovernance(msg.sender);
tapETH.addPool(address(stableAsset));
tapETH.proposeGovernance(msg.sender);
emit PoolCreated(
address(proxyAdmin),
address(tapETHProxy),
address(stableAssetProxy)
);
}
}
164 changes: 0 additions & 164 deletions docs/contracts/StableAsset.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,6 @@ function exchangeRateTokenIndex() external view returns (uint256)
*Index of tokens array for IExchangeRateProvider.*


#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

### feeErrorMargin

```solidity
function feeErrorMargin() external view returns (uint256)
```



*Fee error margin.*


#### Returns

| Name | Type | Description |
Expand Down Expand Up @@ -386,23 +369,6 @@ function initialize(address[] _tokens, uint256[] _precisions, uint256[] _fees, c
| _exchangeRateProvider | contract IExchangeRateProvider | undefined |
| _exchangeRateTokenIndex | uint256 | undefined |

### maxDeltaD

```solidity
function maxDeltaD() external view returns (uint256)
```



*Max delta D.*


#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |

### mint

```solidity
Expand Down Expand Up @@ -821,71 +787,6 @@ function updateA(uint256 _futureA, uint256 _futureABlock) external nonpayable
| _futureA | uint256 | The new A value. |
| _futureABlock | uint256 | The block number to update A value. |

### updateFeeErrorMargin

```solidity
function updateFeeErrorMargin(uint256 newValue) external nonpayable
```



*update fee error margin.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newValue | uint256 | undefined |

### updateMaxDeltaDMargin

```solidity
function updateMaxDeltaDMargin(uint256 newValue) external nonpayable
```



*update yield error margin.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newValue | uint256 | undefined |

### updateYieldErrorMargin

```solidity
function updateYieldErrorMargin(uint256 newValue) external nonpayable
```



*update yield error margin.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| newValue | uint256 | undefined |

### yieldErrorMargin

```solidity
function yieldErrorMargin() external view returns (uint256)
```



*Yield error margin.*


#### Returns

| Name | Type | Description |
|---|---|---|
| _0 | uint256 | undefined |



## Events
Expand Down Expand Up @@ -924,22 +825,6 @@ event FeeCollected(uint256 feeAmount, uint256 totalSupply)
| feeAmount | uint256 | is the amount of fee collected. |
| totalSupply | uint256 | is the total supply of LP token. |

### FeeMarginModified

```solidity
event FeeMarginModified(uint256 margin)
```



*This event is emitted when the fee margin is modified.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| margin | uint256 | is the new value of the margin. |

### GovernanceModified

```solidity
Expand Down Expand Up @@ -988,22 +873,6 @@ event Initialized(uint8 version)
|---|---|---|
| version | uint8 | undefined |

### MaxDeltaDModified

```solidity
event MaxDeltaDModified(uint256 delta)
```



*This event is emitted when the max delta D is modified.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| delta | uint256 | is the new value of the delta. |

### MintFeeModified

```solidity
Expand Down Expand Up @@ -1128,43 +997,10 @@ event YieldCollected(uint256[] amounts, uint256 feeAmount, uint256 totalSupply)
| feeAmount | uint256 | is the amount of yield collected. |
| totalSupply | uint256 | is the total supply of LP token. |

### YieldMarginModified

```solidity
event YieldMarginModified(uint256 margin)
```



*This event is emitted when the fee margin is modified.*

#### Parameters

| Name | Type | Description |
|---|---|---|
| margin | uint256 | is the new value of the margin. |



## Errors

### ImbalancedPool

```solidity
error ImbalancedPool(uint256 oldD, uint256 newD)
```





#### Parameters

| Name | Type | Description |
|---|---|---|
| oldD | uint256 | undefined |
| newD | uint256 | undefined |

### InsufficientMintAmount

```solidity
Expand Down
Loading

0 comments on commit b609ee9

Please sign in to comment.