Skip to content

Commit

Permalink
feat: add denarius, the collateral
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcentur1on committed Aug 14, 2023
1 parent bcc1e48 commit 9bb5868
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 85 deletions.
17 changes: 17 additions & 0 deletions src/Denarius.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {Script} from "forge-std/Script.sol";
import {Denarius} from "./Denarius.sol";

contract DenariusDeploy is Script {
function run() external returns (Denarius) {
vm.startBroadcast();

Denarius tpl = new Denarius();

vm.stopBroadcast();

return tpl;
}
}
17 changes: 17 additions & 0 deletions src/Denarius.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {ERC20} from "openzeppelin/token/ERC20/ERC20.sol";
import {ERC20Burnable} from "openzeppelin/token/ERC20/extensions/ERC20Burnable.sol";
import {ERC20Permit} from "openzeppelin/token/ERC20/extensions/ERC20Permit.sol";
import {Ownable} from "openzeppelin/access/Ownable.sol";

contract Denarius is ERC20, ERC20Burnable, Ownable, ERC20Permit {
constructor() ERC20("Denarius", "SPQR") ERC20Permit("Denarius") {
_mint(msg.sender, 1 * 10**decimals());
}

function mint(address to, uint256 amount) public onlyOwner {
_mint(to, amount);
}
}
31 changes: 31 additions & 0 deletions src/Denarius.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import {Test, console2} from "forge-std/Test.sol";
import {Denarius} from "./Denarius.sol";

//Denarius test
contract DenariusTest is Test {
Denarius internal template;
address public sender;

function setUp() public {
sender = address(0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496);
template = new Denarius();
}

function testFailBasicSanity() public {
assertTrue(false);
}

function testBasicSanity() public {
assertTrue(true);
}

function testBalance() public {
uint256 supposedBalance = 1 * uint256(10**template.decimals());
uint256 balance = template.balanceOf(sender);
console2.log("Balances: %d - %d", supposedBalance, balance);
assertEq(supposedBalance, balance);
}
}
173 changes: 88 additions & 85 deletions vat_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,35 @@ export FOUNDRY_ETH_KEYSTORE_DIR=$ETH_KEYSTORE
export ETH_PASSWORD="$ETH_KEYSTORE/passwd.txt"
export FOUNDRY_ETH_PASSWORD_FILE=$ETH_PASSWORD


## Deploy `Vat`

0. Deploy `Vat` from `vat.sol`

Example:

```bash
forge-script.sh ../src/SampleVat.s.sol:SampleVatDeploy --fork-url=$RPC_URL --broadcast -vvvv
./scripts/forge-script.sh ./src/SampleVat.s.sol:SampleVatDeploy --fork-url=$RPC_URL --broadcast -vvvv
```

## Deploy your ERC-20 token `$CENT`
## Deploy your ERC-20 token `$DENARIUS`

0. Deploy an ERC-20 that you control

Example:

```bash
./scripts/forge-script.sh ./src/Denarius.s.sol:DenariusDeploy --fork-url=$RPC_URL --broadcast -vvvv
```

## Deploy `GemJoin`

0. Deploy a `GemJoin` contract from `join.sol`
```solidity
GemJoin(address vat, bytes32 ilk, address gem)
```
- `vat`: `<vat_addr>`
- `ilk`: `'CENT-A'`
- `gem`: `$CENT` ERC20 token address
```solidity
GemJoin(address vat, bytes32 ilk, address gem)
```
- `vat`: `<vat_addr>`
- `ilk`: `'CENT-A'`
- `gem`: `$CENT` ERC20 token address

## Deploy your own "Dai" token `$DAI`

Expand All @@ -51,111 +54,111 @@ forge-script.sh ../src/SampleVat.s.sol:SampleVatDeploy --fork-url=$RPC_URL --bro
Example:

```bash
forge-script.sh ../src/Cent.s.sol:CenturionDaiDeploy --fork-url=$RPC_URL --broadcast -vvvv
./scripts/forge-script.sh ./src/Cent.s.sol:CenturionDaiDeploy --fork-url=$RPC_URL --broadcast -vvvv
```

## Authorize the contracts on the `Vat`:

0. `rely` on both join contracts:
```solidity
vat.rely(<gem_join_addr>);
vat.rely(<dai_join_addr>);
```
```solidity
vat.rely(<gem_join_addr>);
vat.rely(<dai_join_addr>);
```

## Deploy `DaiJoin`

0. Deploy a `DaiJoin` contract from `join.sol`
```solidity
DaiJoin(address vat, address dai)
```
- `vat`: `<vat_addr>`
- `gem`: `$MYDAI` ERC20 token address
```solidity
DaiJoin(address vat, address dai)
```
- `vat`: `<vat_addr>`
- `gem`: `$MYDAI` ERC20 token address
1. Allow `DaiJoin` to **mint** `$MYDAI`
1. Allow `DaiJoin` to **burn** `$MYDAI`

## Initialize the collateral type

1. Initialize Vat for `CENT-A`
```solidity
vat.init('CENT-A');
```
```solidity
vat.init('CENT-A');
```
2. Set the global debt ceiling `Line` (with capital `L`)
```solidity
vat.file('Line', 1_000_000 * 10**45); // RAD: 45 decimals
```
```solidity
vat.file('Line', 1_000_000 * 10**45); // RAD: 45 decimals
```
3. Set collateral debt ceiling `line` (with lower `l`)
```solidity
vat.file('CENT-A', 'line', 1_000_000 * 10**45); // RAD: 45 decimals
```
```solidity
vat.file('CENT-A', 'line', 1_000_000 * 10**45); // RAD: 45 decimals
```
4. Set collateral price (`spot`)
```solidity
vat.file('CENT-A', 'spot', 1 * 10**27) // RAY: 27 decimals
```
- This makes so 1 `$CENT` = 1 `DAI` and that the collateralization ratio is 100%
```solidity
vat.file('CENT-A', 'spot', 1 * 10**27) // RAY: 27 decimals
```
- This makes so 1 `$CENT` = 1 `DAI` and that the collateralization ratio is 100%

## Borrow `$MYDAI` from `$CENT`

1. Approve `GemJoin` to spend your `$CENT`
```solidity
cent.approve(<gem_join_addr>, type(uint256).max);
```
```solidity
cent.approve(<gem_join_addr>, type(uint256).max);
```
2. Add your `$CENT` to the protocol by calling `GemJoin.join()`:
```solidity
gemJoin.join(<your_addr>, <amount>); // <amount> with 10**18 precision
```
- This will add collateral to the system, but it will remain **unemcumbered** (not locked).
```solidity
gemJoin.join(<your_addr>, <amount>); // <amount> with 10**18 precision
```
- This will add collateral to the system, but it will remain **unemcumbered** (not locked).
3. Draw internal `dai` from the `Vat` using `frob()`:
```solidity
vat.frob(
'CENT-A', // ilk
<your_wallet>,
<your_wallet>,
<your_wallet>, // To keep it simple, use your address for both `u`, `v` and `w`
int dink, // with 10**18 precision
int dart // with 10**18 precision
)
```
- `dink`: how much collateral to lock(+)/unlock(-)
- Collateral is now **encumbered** (locked) into the system.
- `dart`: how much **normalized debt** to add(+)/remove(-)
- Remember that `debt = ilk.rate * urn.art`
- To get the value for `dart`, divide the desired amount by `ilk.rate` (this is a floating point division, which can be tricky)
- See the [RwaUrn](https://github.com/makerdao/rwa-toolkit/blob/8d30ed2cb657641253d45b57c894613e26b4ae1b/src/urns/RwaUrn.sol#L156-L178) component to understand how it can be done
- Recommendation: respect `dink = dart/2` when drawing
```solidity
vat.frob(
'CENT-A', // ilk
<your_wallet>,
<your_wallet>,
<your_wallet>, // To keep it simple, use your address for both `u`, `v` and `w`
int dink, // with 10**18 precision
int dart // with 10**18 precision
)
```
- `dink`: how much collateral to lock(+)/unlock(-)
- Collateral is now **encumbered** (locked) into the system.
- `dart`: how much **normalized debt** to add(+)/remove(-)
- Remember that `debt = ilk.rate * urn.art`
- To get the value for `dart`, divide the desired amount by `ilk.rate` (this is a floating point division, which can be tricky)
- See the [RwaUrn](https://github.com/makerdao/rwa-toolkit/blob/8d30ed2cb657641253d45b57c894613e26b4ae1b/src/urns/RwaUrn.sol#L156-L178) component to understand how it can be done
- Recommendation: respect `dink = dart/2` when drawing
4. Get ERC-20 `$MYDAI`
```solidity
daiJoin.exit(<your_wallet>, <amount>); // <amount> with 10**18 precision
```
```solidity
daiJoin.exit(<your_wallet>, <amount>); // <amount> with 10**18 precision
```

## Repay your loan to get `$CENT` back

1. Approve `DaiJoin` to spend your `$MYDAI`
```solidity
dai.approve(<dai_join_addr>, type(uint256).max);
```
```solidity
dai.approve(<dai_join_addr>, type(uint256).max);
```
2. Add your `$MYDAI` to the protocol by calling `DaiJoin.join()`:
```solidity
daiJoin.join(<your_addr>, <amount>); // <amount> with 10**18 precision
```
- This will burn ERC-20 `$MYDAI` and add it to `<your_addr>` internal balance on the `Vat`.
```solidity
daiJoin.join(<your_addr>, <amount>); // <amount> with 10**18 precision
```
- This will burn ERC-20 `$MYDAI` and add it to `<your_addr>` internal balance on the `Vat`.
3. Repay internal `dai` in the `Vat` using `frob()`:
```solidity
vat.frob(
'CENT-A', // ilk
<your_wallet>,
<your_wallet>,
<your_wallet>, // To keep it simple, use your address for both `u`, `v` and `w`
int dink, // with 10**18 precision
int dart // with 10**18 precision
)
```
- `dink`: how much collateral to unlock. **MUST BE NEGATIVE**
- Collateral is now **uncumbered** (unlocked), but still in the system.
- `dart`: how much **normalized debt** to remove. **MUST BE NEGATIVE**
- Remember that `debt = ilk.rate * urn.art`
- To get the value for `dart`, divide the desired amount by `ilk.rate` (this is a floating point division, which can be tricky)
- See the [RwaUrn](https://github.com/makerdao/rwa-toolkit/blob/8d30ed2cb657641253d45b57c894613e26b4ae1b/src/urns/RwaUrn.sol#L156-L178) component to understand how it can be done
```solidity
vat.frob(
'CENT-A', // ilk
<your_wallet>,
<your_wallet>,
<your_wallet>, // To keep it simple, use your address for both `u`, `v` and `w`
int dink, // with 10**18 precision
int dart // with 10**18 precision
)
```
- `dink`: how much collateral to unlock. **MUST BE NEGATIVE**
- Collateral is now **uncumbered** (unlocked), but still in the system.
- `dart`: how much **normalized debt** to remove. **MUST BE NEGATIVE**
- Remember that `debt = ilk.rate * urn.art`
- To get the value for `dart`, divide the desired amount by `ilk.rate` (this is a floating point division, which can be tricky)
- See the [RwaUrn](https://github.com/makerdao/rwa-toolkit/blob/8d30ed2cb657641253d45b57c894613e26b4ae1b/src/urns/RwaUrn.sol#L156-L178) component to understand how it can be done
4. Get your `$CENT` back:
```solidity
gemJoin.exit(<your_wallet>, <amount>); // <amount> with 10**18 precision
```
```solidity
gemJoin.exit(<your_wallet>, <amount>); // <amount> with 10**18 precision
```

0 comments on commit 9bb5868

Please sign in to comment.