Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add USDA plugin and scripts #1215

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ export interface ITokens {
// Mountain
USDM?: string
wUSDM?: string

// Angle
USDA?: string
stUSD?: string
}

export type ITokensKeys = Array<keyof ITokens>
Expand Down Expand Up @@ -256,6 +260,8 @@ export const networkConfig: { [key: string]: INetworkConfig } = {
sdUSDCUSDCPlus: '0x9bbF31E99F30c38a5003952206C31EEa77540BeF',
USDe: '0x4c9edd5852cd905f086c759e8383e09bff1e68b3',
sUSDe: '0x9D39A5DE30e57443BfF2A8307A4256c8797A3497',
USDA: '0x0000206329b97DB379d5E1Bf586BbDB969C63274',
stUSD: '0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776',
},
chainlinkFeeds: {
RSR: '0x759bBC1be8F90eE6457C44abc7d443842a976d02',
Expand Down Expand Up @@ -513,6 +519,8 @@ export const networkConfig: { [key: string]: INetworkConfig } = {
sUSDbC: '0x4c80e24119cfb836cdf0a6b53dc23f04f7e652ca',
wstETH: '0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452',
STG: '0xE3B53AF74a4BF62Ae5511055290838050bf764Df',
USDA: '0x0000206329b97DB379d5E1Bf586BbDB969C63274',
stUSD: '0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776',
},
chainlinkFeeds: {
DAI: '0x591e79239a7d679378ec8c847e5038150364c78f', // 0.3%, 24hr
Expand Down Expand Up @@ -560,6 +568,8 @@ export const networkConfig: { [key: string]: INetworkConfig } = {
saArbUSDT: '', // TODO our wrapper. remove from deployment script after placing here
USDM: '0x59d9356e565ab3a36dd77763fc0d87feaf85508c',
wUSDM: '0x57f5e098cad7a3d1eed53991d4d66c45c9af7812',
USDA: '0x0000206329b97DB379d5E1Bf586BbDB969C63274',
stUSD: '0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776',
},
chainlinkFeeds: {
ARB: '0xb2A824043730FE05F3DA2efaFa1CBbe83fa548D6',
Expand Down
21 changes: 21 additions & 0 deletions contracts/plugins/assets/angle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Angle USDA Collateral Plugin

## Summary

`USDA` is a stablecoin pegged to the dollar built by [Angle Labs](https://github.com/AngleProtocol). `USDA` holders can stake their stablecoins for `stUSD` in order to earn a native yield.

This plugin allows `stUSD` holders to use their tokens as collateral in the Reserve Protocol.

`stUSD` is an ERC4626 vault, most similar to the DAI savings module. The redeemable `USDA` amount can be obtained by dividing `stUSD.totalAssets()` by `stUSD.totalSupply()`.

`USDA` contract: <https://etherscan.io/address/0x0000206329b97DB379d5E1Bf586BbDB969C63274#code>

`stUSD` contract: <https://etherscan.io/address/0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776#code>

## Implementation

### Units

| tok | ref | target | UoA |
| ----- | ---- | ------ | --- |
| stUSD | USDA | USD | USD |
25 changes: 25 additions & 0 deletions contracts/plugins/assets/angle/USDAFiatCollateral.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.19;

import { CollateralConfig } from "../AppreciatingFiatCollateral.sol";
import { ERC4626FiatCollateral } from "../ERC4626FiatCollateral.sol";

/**
* @title USDA Fiat Collateral
* @notice Collateral plugin for USDA (Angle)
* tok = stUSD (ERC4626 vault)
* ref = USDA
* tar = USD
* UoA = USD
*/

contract USDAFiatCollateral is ERC4626FiatCollateral {
/// config.erc20 must be stUSD
/// @param config.chainlinkFeed Feed units: {UoA/ref}
/// @param revenueHiding {1} A value like 1e-6 that represents the maximum refPerTok to hide
constructor(CollateralConfig memory config, uint192 revenueHiding)
ERC4626FiatCollateral(config, revenueHiding)
{
require(config.defaultThreshold != 0, "defaultThreshold zero");
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at the stUSDA ERC4626 contract, and there seems to be an internal accrual happening on any deposit or withdrawal. since this accrual affects the pricePerShare() rate, we should make sure this gets updated on any call to collateral.refresh() (refresh() ensures that all state variables affecting price are updated so that the most accurate, up-to-date price can be calculated).

the best way to do this is probably to copy over the refresh call from the AppreciatingFiatCollateral and add a call to deposit(0, address(this)) at the top of the function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, but wondering if this is really needed for stUSD. View functions like totalAssets or convertToAssets already includes accruals (see https://github.com/AngleProtocol/angle-transmuter/blob/main/contracts/savings/Savings.sol#L109). So it is possible to compute up-to-date prices without having to trigger a state update, isn't it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes but there are state updates performed in the collateral plugin, so we should match that with any necessary state updates in the collateral itself.

8 changes: 5 additions & 3 deletions scripts/addresses/1-tmp-assets-collateral.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@
"sfrxETH": "0x4c891fCa6319d492866672E3D2AfdAAA5bDcfF67",
"apxETH": "0x05ffDaAA2aF48e1De1CE34d633db018a28e3B3F5",
"sUSDe": "0x35081Ca24319835e5f759163F7e75eaB753e0b7E",
"pyUSD": "0xa5cde4fB1132daF8f4a0D3140859271208d944E9"
"pyUSD": "0xa5cde4fB1132daF8f4a0D3140859271208d944E9",
"stUSD": "0xb6aD2903fAC2db628624DeE61e217F50Ad54638E"
},
"erc20s": {
"stkAAVE": "0x4da27a545c0c5B758a6BA100e3a049001de870f5",
Expand Down Expand Up @@ -123,6 +124,7 @@
"CVX": "0x4e3FBD56CD56c3e72c1403e103b45Db9da5B9D2B",
"apxETH": "0x9Ba021B0a9b958B5E75cE9f6dff97C7eE52cb3E6",
"sUSDe": "0x9D39A5DE30e57443BfF2A8307A4256c8797A3497",
"pyUSD": "0x6c3ea9036406852006290770bedfcaba0e23a0e8"
"pyUSD": "0x6c3ea9036406852006290770bedfcaba0e23a0e8",
"stUSD": "0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776"
}
}
}
92 changes: 92 additions & 0 deletions scripts/deployment/phase2-assets/collaterals/deploy_usda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import fs from 'fs'
import hre from 'hardhat'
import { ContractFactory } from 'ethers'
import { expect } from 'chai'
import { getChainId } from '../../../../common/blockchain-utils'
import { networkConfig } from '../../../../common/configuration'
import { fp } from '../../../../common/numbers'
import { CollateralStatus } from '../../../../common/constants'
import {
getDeploymentFile,
getAssetCollDeploymentFilename,
IAssetCollDeployments,
getDeploymentFilename,
fileExists,
} from '../../common'
import { USDAFiatCollateral } from '../../../../typechain'
import {
DELAY_UNTIL_DEFAULT,
PRICE_TIMEOUT,
ORACLE_TIMEOUT,
ORACLE_ERROR,
} from '../../../../test/plugins/individual-collateral/angle/constants'

async function main() {
// ==== Read Configuration ====
const [deployer] = await hre.ethers.getSigners()

const chainId = await getChainId(hre)

console.log(`Deploying Collateral to network ${hre.network.name} (${chainId})
with burner account: ${deployer.address}`)

if (!networkConfig[chainId]) {
throw new Error(`Missing network configuration for ${hre.network.name}`)
}

// Get phase1 deployment
const phase1File = getDeploymentFilename(chainId)
if (!fileExists(phase1File)) {
throw new Error(`${phase1File} doesn't exist yet. Run phase 1`)
}
// Check previous step completed
const assetCollDeploymentFilename = getAssetCollDeploymentFilename(chainId)
const assetCollDeployments = <IAssetCollDeployments>getDeploymentFile(assetCollDeploymentFilename)

const deployedCollateral: string[] = []

/******** Deploy USDA Collateral - stUSD **************************/
let collateral: USDAFiatCollateral

const USDAFiatCollateralFactory: ContractFactory = await hre.ethers.getContractFactory(
'USDAFiatCollateral'
)

collateral = <USDAFiatCollateral>await USDAFiatCollateralFactory.connect(deployer).deploy(
{
priceTimeout: PRICE_TIMEOUT.toString(),
chainlinkFeed: networkConfig[chainId].chainlinkFeeds.USDC,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USDA

oracleError: ORACLE_ERROR.toString(),
erc20: networkConfig[chainId].tokens.stUSD,
maxTradeVolume: fp('1e6').toString(), // $1m,
oracleTimeout: ORACLE_TIMEOUT.toString(), // 24 hr
targetName: hre.ethers.utils.formatBytes32String('USD'),
defaultThreshold: fp('0.01').add(ORACLE_ERROR).toString(), // ~1.5%
delayUntilDefault: DELAY_UNTIL_DEFAULT.toString(), // 72h
},
fp('1e-6').toString()
)

await collateral.deployed()

console.log(
`Deployed USDA (stUSD) Collateral to ${hre.network.name} (${chainId}): ${collateral.address}`
)
await (await collateral.refresh()).wait()
expect(await collateral.status()).to.equal(CollateralStatus.SOUND)

assetCollDeployments.collateral.stUSD = collateral.address
assetCollDeployments.erc20s.stUSD = networkConfig[chainId].tokens.stUSD
deployedCollateral.push(collateral.address.toString())

fs.writeFileSync(assetCollDeploymentFilename, JSON.stringify(assetCollDeployments, null, 2))

console.log(`Deployed collateral to ${hre.network.name} (${chainId})
New deployments: ${deployedCollateral}
Deployment file: ${assetCollDeploymentFilename}`)
}

main().catch((error) => {
console.error(error)
process.exitCode = 1
})
59 changes: 59 additions & 0 deletions scripts/verification/collateral-plugins/verify_usda.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import hre from 'hardhat'
import { getChainId } from '../../../common/blockchain-utils'
import { developmentChains, networkConfig } from '../../../common/configuration'
import { fp } from '../../../common/numbers'
import {
getDeploymentFile,
getAssetCollDeploymentFilename,
IAssetCollDeployments,
} from '../../deployment/common'
import { verifyContract } from '../../deployment/utils'
import {
PRICE_TIMEOUT,
ORACLE_ERROR,
ORACLE_TIMEOUT,
DELAY_UNTIL_DEFAULT,
} from '../../../test/plugins/individual-collateral/angle/constants'

let deployments: IAssetCollDeployments

async function main() {
// ********** Read config **********
const chainId = await getChainId(hre)
if (!networkConfig[chainId]) {
throw new Error(`Missing network configuration for ${hre.network.name}`)
}

if (developmentChains.includes(hre.network.name)) {
throw new Error(`Cannot verify contracts for development chain ${hre.network.name}`)
}

const assetCollDeploymentFilename = getAssetCollDeploymentFilename(chainId)
deployments = <IAssetCollDeployments>getDeploymentFile(assetCollDeploymentFilename)

/******** Verify stUSD COllateral **************************/
await verifyContract(
chainId,
deployments.collateral.stUSD,
[
{
priceTimeout: PRICE_TIMEOUT.toString(),
chainlinkFeed: networkConfig[chainId].chainlinkFeeds.USDC,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

USDA

oracleError: ORACLE_ERROR.toString(),
erc20: networkConfig[chainId].tokens.stUSD,
maxTradeVolume: fp('1e6').toString(), // $1m,
oracleTimeout: ORACLE_TIMEOUT.toString(),
targetName: hre.ethers.utils.formatBytes32String('USD'),
defaultThreshold: fp('0.01').add(ORACLE_ERROR).toString(),
delayUntilDefault: DELAY_UNTIL_DEFAULT.toString(),
},
fp('1e-6').toString(),
],
'contracts/plugins/assets/angle/USDAFiatCollateral.sol:USDAFiatCollateral'
)
}

main().catch((error) => {
console.error(error)
process.exitCode = 1
})
3 changes: 2 additions & 1 deletion scripts/verify_etherscan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ async function main() {
'collateral-plugins/verify_re7weth.ts',
'collateral-plugins/verify_apxeth.ts',
'collateral-plugins/verify_USDe.ts',
'collateral-plugins/verify_pyusd.ts'
'collateral-plugins/verify_pyusd.ts',
'collateral-plugins/verify_usda.ts'
)
} else if (chainId == '8453' || chainId == '84531') {
// Base L2 chains
Expand Down
Loading