Skip to content

Commit

Permalink
Check vault collateralization
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasdenH committed Apr 20, 2022
1 parent 100c507 commit 1886b0d
Show file tree
Hide file tree
Showing 25 changed files with 47,559 additions and 40,434 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ build/

# production
/build
/cache

# misc
.DS_Store
Expand Down
5 changes: 5 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,8 @@ ganache-cli --fork https://mainnet.infura.io/v3/6f4f43507fa24302a651b52073c98d8a
npx truffle compile
npx truffle test .\test\TestYieldLever.sol
```

# Forge
```
forge deploy
```
74 changes: 37 additions & 37 deletions cache/solidity-files-cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
"_format": "ethers-rs-sol-cache-3",
"paths": {
"artifacts": "out",
"sources": "src",
"sources": "contracts",
"tests": "test",
"libraries": [
"lib"
]
},
"files": {
"F:\\Git\\yvarb\\lib\\ds-test\\src/test.sol": {
"lastModificationDate": 1650365129624,
"contentHash": "2c9806925f5d78ba4f4fb0570611cca3",
"sourceName": "lib\\ds-test\\src/test.sol",
"F:\\Git\\yvarb\\contracts\\YieldLever.sol": {
"lastModificationDate": 1650375593897,
"contentHash": "1ba0b264834e0d00ab81200be900d058",
"sourceName": "contracts\\YieldLever.sol",
"solcConfig": {
"settings": {
"optimizer": {
Expand All @@ -39,17 +39,35 @@
}
},
"imports": [],
"versionRequirement": ">=0.5.0",
"versionRequirement": "^0.8.13",
"artifacts": {
"DSTest": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "test.sol\\DSTest.json"
"Cauldron": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\Cauldron.json"
},
"IERC20": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IERC20.json"
},
"IFYToken": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IFYToken.json"
},
"IToken": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IToken.json"
},
"YieldLadle": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\YieldLadle.json"
},
"YieldLever": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\YieldLever.json"
},
"yVault": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\yVault.json"
}
}
},
"F:\\Git\\yvarb\\src\\YieldLever.sol": {
"lastModificationDate": 1650373944406,
"contentHash": "50fa26e3effb92bcb0894fd72797e8f2",
"sourceName": "src\\YieldLever.sol",
"F:\\Git\\yvarb\\lib\\ds-test\\src/test.sol": {
"lastModificationDate": 1650365129624,
"contentHash": "2c9806925f5d78ba4f4fb0570611cca3",
"sourceName": "lib\\ds-test\\src/test.sol",
"solcConfig": {
"settings": {
"optimizer": {
Expand All @@ -76,28 +94,10 @@
}
},
"imports": [],
"versionRequirement": "^0.8.13",
"versionRequirement": ">=0.5.0",
"artifacts": {
"Cauldron": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\Cauldron.json"
},
"IERC20": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IERC20.json"
},
"IFYToken": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IFYToken.json"
},
"IToken": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\IToken.json"
},
"YieldLadle": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\YieldLadle.json"
},
"YieldLever": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\YieldLever.json"
},
"yVault": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "YieldLever.sol\\yVault.json"
"DSTest": {
"0.8.13+commit.abaa5c0e.Windows.msvc": "test.sol\\DSTest.json"
}
}
},
Expand Down Expand Up @@ -139,8 +139,8 @@
}
},
"F:\\Git\\yvarb\\test\\YieldLever.t.sol": {
"lastModificationDate": 1650374139987,
"contentHash": "3d1fb30be8bb8cd0cc1888e7d1b38376",
"lastModificationDate": 1650377922411,
"contentHash": "334d3f300bbcaa33b95cb6619ce8b752",
"sourceName": "test\\YieldLever.t.sol",
"solcConfig": {
"settings": {
Expand Down Expand Up @@ -168,8 +168,8 @@
}
},
"imports": [
"lib\\ds-test\\src/test.sol",
"src\\YieldLever.sol"
"contracts\\YieldLever.sol",
"lib\\ds-test\\src/test.sol"
],
"versionRequirement": "^0.8.13",
"artifacts": {
Expand Down
7 changes: 7 additions & 0 deletions src/YieldLever.sol → contracts/YieldLever.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ struct Debt {
uint128 sum; // Current debt for a given underlying, across all series
}

struct SpotOracle {
address oracle; // Address for the spot price oracle
uint32 ratio; // Collateralization ratio to multiply the price for
// bytes8 free
}

interface YieldLadle {
function pools(bytes6 seriesId) external view returns (address);
function build(bytes6 seriesId, bytes6 ilkId, uint8 salt)
Expand Down Expand Up @@ -96,6 +102,7 @@ interface Cauldron {
function give(bytes12 vaultId, address receiver)
external
returns(Vault memory vault);
function spotOracles(bytes6 baseId, bytes6 ilkId) external view returns (SpotOracle memory);
event VaultGiven(bytes12 indexed vaultId, address indexed receiver);
}

Expand Down
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[default]
src = 'src'
src = 'contracts'
out = 'out'

# See more config options https://github.com/gakonst/foundry/tree/master/config
10,229 changes: 5,726 additions & 4,503 deletions frontend/src/abi/YieldLever.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions frontend/src/abi/YieldLever.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
BytesLike as Arrayish,
BigNumber,
BigNumberish,
} from "ethers";
import { EthersContractContextV5 } from "ethereum-abi-types-generator";
} from 'ethers';
import { EthersContractContextV5 } from 'ethereum-abi-types-generator';

export type ContractContext = EthersContractContextV5<
YieldLever,
Expand Down Expand Up @@ -57,20 +57,20 @@ export interface ContractCallOverrides {
export type YieldLeverEvents = undefined;
export interface YieldLeverEventsContext {}
export type YieldLeverMethodNames =
| "new"
| "invest"
| "doInvest"
| "unwind"
| "doRepay"
| "doClose";
| 'new'
| 'invest'
| 'doInvest'
| 'unwind'
| 'doRepay'
| 'doClose';
export interface YieldLever {
/**
* Payable: false
* Constant: false
* StateMutability: nonpayable
* Type: constructor
*/
"new"(overrides?: ContractTransactionOverrides): Promise<ContractTransaction>;
'new'(overrides?: ContractTransactionOverrides): Promise<ContractTransaction>;
/**
* Payable: false
* Constant: false
Expand Down
47 changes: 40 additions & 7 deletions frontend/src/components/Invest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ enum ApprovalState {
ApprovalRequired,
Transactable,
DebtTooLow,
Undercollateralized
}

interface State {
Expand Down Expand Up @@ -95,6 +96,17 @@ export default class Invest extends React.Component<Properties, State> {
/>
);
break;
case ApprovalState.Undercollateralized:
component = (
<input
key="undercollateralized"
className="button"
type="button"
value="Undercollateralized!"
disabled={true}
/>
);
break;
}

return (
Expand Down Expand Up @@ -180,6 +192,11 @@ export default class Invest extends React.Component<Properties, State> {
}

private async checkApprovalState() {
// First, set to loading
this.setState({
approvalState: ApprovalState.Loading
});

const series = await this.loadSeries();
const allowance: BigNumber = await this.contracts.usdcContract.allowance(
this.account,
Expand All @@ -193,7 +210,6 @@ export default class Invest extends React.Component<Properties, State> {
BigNumber.from(10).pow(cauldronDebt.dec)
);

console.log(cauldronDebt);
console.log(
"Allowance: " +
utils.formatUnits(allowance, UNITS_USDC) +
Expand All @@ -203,25 +219,42 @@ export default class Invest extends React.Component<Properties, State> {

// Compute the amount of Fytokens
const fyTokens = await this.fyTokens();
this.setState({
fyTokens,
});

if (minDebt.gt(fyTokens)) {
this.setState({ approvalState: ApprovalState.DebtTooLow });
const { ratio } = await this.contracts.cauldronContract.spotOracles(series.baseId, ILK_ID);
const currentCollateralizationRatio = this.collateralizationRatio(fyTokens);

if (minDebt.gt(fyTokens)) { // Check whether the minimum debt is reached
this.setState({
fyTokens,
approvalState: ApprovalState.DebtTooLow
});
} else if (currentCollateralizationRatio.lt(ratio)) { // Check whether the vault would be collateralized
this.setState({
fyTokens,
approvalState: ApprovalState.Undercollateralized
});
} else {
const interest = await this.computeInterest();
if (allowance.lt(this.state.usdcToInvest)) {
this.setState({
fyTokens,
approvalState: ApprovalState.ApprovalRequired,
interest,
});
} else {
this.setState({ approvalState: ApprovalState.Transactable, interest });
this.setState({
fyTokens,
approvalState: ApprovalState.Transactable,
interest
});
}
}
}

private collateralizationRatio(fyTokens: BigNumber): BigNumber {
return this.totalToInvest().div(fyTokens.div(1_000_000));
}

private async cauldronDebt(
cauldronContract: Cauldron,
baseId: string
Expand Down
Loading

0 comments on commit 1886b0d

Please sign in to comment.