Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
kyzia551 committed Feb 2, 2024
0 parents commit bc32898
Show file tree
Hide file tree
Showing 62 changed files with 3,581 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# http://editorconfig.org

root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{Makefile,**.mk}]
# Use tabs for indentation (Makefiles require tabs)
indent_style = tab
34 changes: 34 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Deployment via ledger
MNEMONIC_INDEX=
LEDGER_SENDER=

# Deployment via private key
PRIVATE_KEY=

# Test rpc_endpoints
RPC_MAINNET=https://eth.llamarpc.com
RPC_AVALANCHE=https://api.avax.network/ext/bc/C/rpc
RPC_OPTIMISM=https://optimism.llamarpc.com
RPC_POLYGON=https://polygon.llamarpc.com
RPC_ARBITRUM=https://arbitrum.llamarpc.com
RPC_FANTOM=https://rpc.ftm.tools
RPC_HARMONY=https://api.harmony.one
RPC_METIS=https://andromeda.metis.io/?owner=1088
RPC_BASE=https://base.llamarpc.com
RPC_ZKEVM=https://zkevm-rpc.com
RPC_GNOSIS=https://rpc.ankr.com/gnosis
RPC_BNB=https://binance.llamarpc.com

# Etherscan api keys for verification & download utils
ETHERSCAN_API_KEY_MAINNET=
ETHERSCAN_API_KEY_POLYGON=
ETHERSCAN_API_KEY_AVALANCHE=
ETHERSCAN_API_KEY_FANTOM=
ETHERSCAN_API_KEY_OPTIMISM=
ETHERSCAN_API_KEY_ARBITRUM=
ETHERSCAN_API_KEY_BASE=
ETHERSCAN_API_KEY_ZKEVM=
ETHERSCAN_API_KEY_GNOSIS=
ETHERSCAN_API_KEY_BNB=


13 changes: 13 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Main workflow

on:
pull_request:
push:
branches:
- main
workflow_dispatch:

jobs:
test:
uses: bgd-labs/github-workflows/.github/workflows/foundry-test.yml@main
secrets: inherit
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# build and cache
cache/
out/

# general
.env

# editors
.idea
.vscode

# well, looks strange to ignore package-lock, but we have only pretter and it's temproray
package-lock.json
node_modules

# ignore foundry deploy artifacts
broadcast/
12 changes: 12 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/aave-address-book"]
path = lib/aave-address-book
url = https://github.com/bgd-labs/aave-address-book
[submodule "lib/cl-synchronicity-price-adapter"]
path = lib/cl-synchronicity-price-adapter
url = https://github.com/bgd-labs/cl-synchronicity-price-adapter
[submodule "lib/aave-helpers"]
path = lib/aave-helpers
url = https://github.com/bgd-labs/aave-helpers
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
out
lib
cache
node_modules
24 changes: 24 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false
}
},
{
"files": "*.ts",
"options": {
"printWidth": 100,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"bracketSpacing": false
}
}
]
}
10 changes: 10 additions & 0 deletions .solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "solhint:recommended",
"rules": {
"compiler-version": ["error", "^0.8.0"],
"func-visibility": ["warn", { "ignoreConstructors": true }],
"compiler-fixed": false,
"quotes": ["error", "single"],
"indent": [2]
}
}
1 change: 1 addition & 0 deletions .solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
119 changes: 119 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Business Source License 1.1

License text copyright (c) 2020 MariaDB Corporation Ab, All Rights Reserved.
“Business Source License” is a trademark of MariaDB Corporation Ab.

-----------------------------------------------------------------------------

Parameters

Licensor: Aave DAO, represented by its governance smart contracts

Licensed Work: Price cap adapters
The Licensed Work is (c) 2024 Aave DAO, represented by its governance smart contracts

Additional Use Grant: You are permitted to use, copy, and modify the Licensed Work, subject to
the following conditions:
- Your use of the Licensed Work shall not, directly or indirectly, enable, facilitate,
or assist in any way with the migration of users and/or funds from the Aave ecosystem.
The "Aave ecosystem" is defined in the context of this License as the collection of
software protocols and applications approved by the Aave governance, including all
those produced within compensated service provider engagements with the Aave DAO.
The Aave DAO is able to waive this requirement for one or more third-parties, if and
only if explicitly indicating it on a record 'authorizations' on adi.aavelicense.eth,
controlled by the Aave DAO.
- You are neither an individual nor a direct or indirect participant in any incorporated
organization, DAO, or identifiable group, that has deployed in production any original
or derived software ("fork") of the Aave ecosystem for purposes competitive to Aave,
within the preceding two years.
The Aave DAO is able to waive this requirement for one or more third-parties, if and
only if explicitly indicating it on a record 'authorizations' on adi.aavelicense.eth,
controlled by the Aave DAO.
- You must ensure that the usage of the Licensed Work does not result in any direct or
indirect harm to the Aave ecosystem or the Aave brand. This encompasses, but is not limited to,
reputational damage, omission of proper credit/attribution, or utilization for any malicious
intent.

Change Date: The earlier of:
- 2027-07-27
- The date specified in the 'change-date' record on adi.aavelicense.eth, controlled by
the Aave DAO

Change License: MIT

-----------------------------------------------------------------------------

Notice

The Business Source License (this document, or the “License”) is not an Open
Source license. However, the Licensed Work will eventually be made available
under an Open Source License, as stated in this License.

-----------------------------------------------------------------------------

Terms

The Licensor hereby grants you the right to copy, modify, create derivative
works, redistribute, and make non-production use of the Licensed Work. The
Licensor may make an Additional Use Grant, above, permitting limited
production use.

Effective on the Change Date, or the fourth anniversary of the first publicly
available distribution of a specific version of the Licensed Work under this
License, whichever comes first, the Licensor hereby grants you rights under
the terms of the Change License, and the rights granted in the paragraph
above terminate.

If your use of the Licensed Work does not comply with the requirements
currently in effect as described in this License, you must purchase a
commercial license from the Licensor, its affiliated entities, or authorized
resellers, or you must refrain from using the Licensed Work.

All copies of the original and modified Licensed Work, and derivative works
of the Licensed Work, are subject to this License. This License applies
separately for each version of the Licensed Work and the Change Date may vary
for each version of the Licensed Work released by Licensor.

You must conspicuously display this License on each original or modified copy
of the Licensed Work. If you receive the Licensed Work in original or
modified form from a third party, the terms and conditions set forth in this
License apply to your use of that work.

Any use of the Licensed Work in violation of this License will automatically
terminate your rights under this License for the current and all other
versions of the Licensed Work.

This License does not grant you any right in any trademark or logo of
Licensor or its affiliates (provided that you may use a trademark or logo of
Licensor as expressly required by this License).

TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
TITLE.

MariaDB hereby grants you permission to use this License’s text to license
your works, and to refer to it using the trademark “Business Source License”,
as long as you comply with the Covenants of Licensor below.

Covenants of Licensor

In consideration of the right to use this License’s text and the “Business
Source License” name and trademark, Licensor covenants to MariaDB, and to all
other recipients of the licensed work to be provided by Licensor:

1. To specify as the Change License the GPL Version 2.0 or any later version,
or a license that is compatible with GPL Version 2.0 or a later version,
where “compatible” means that software provided under the Change License can
be included in a program with software provided under GPL Version 2.0 or a
later version. Licensor may specify additional Change Licenses without
limitation.

2. To either: (a) specify an additional grant of rights to use that does not
impose any additional restriction on the right granted in this License, as
the Additional Use Grant; or (b) insert the text “None”.

3. To specify a Change Date.

4. Not to modify this License in any other way.
17 changes: 17 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# include .env file and export its env vars
# (-include to ignore error if it does not exist)
-include .env

# deps
update:; forge update

# Build & test
build :; forge build --sizes
test :; forge test -vvv

# Utilities
download :; cast etherscan-source --chain ${chain} -d src/etherscan/${chain}_${address} ${address}
git-diff :
@mkdir -p diffs
@npx prettier ${before} ${after} --write
@printf '%s\n%s\n%s\n' "\`\`\`diff" "$$(git diff --no-index --diff-algorithm=patience --ignore-space-at-eol ${before} ${after})" "\`\`\`" > diffs/${out}.md
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Correlated-assets price oracle

Price oracle adapter smart contracts, introducing different types of range price protection on oracle feeds used by the Aave protocol.

<br>

## Types

### [RatioCapPriceAdapter](./src/contracts/PriceCapAdapterBase.sol)

Certain assets like LSTs (Liquid Staking Tokens) are highly correlated to an underlying, with an additional growth component on top of it, and sometimes, slashing dynamics. This initial version of an adapter for this use case adds an upper side protection.

High-level, the idea is doing periodic updates on 3 parameters: a snapshot ratio, its timestamp, and a max allowed ratio growth in yearly percentage.
Every time the price adapter is queried, it will get the current ratio of the asset/underlying and compared it with a dynamically calculated upper value of that ratio, using the previously defined parameters.
If the current ratio is above the ratio cap, the ratio cap is returned. If not, the current ratio is.

<br>

**Misc considerations**

- Maximum precision is not the objective of this implementation, as anyway, the cap is thought to have a good margin, given that the risk parameters of the Aave protocol should protect enough.
- Some basic safety checks are applied when setting parameters, but as this is access controlled to a trusted entity (e.g. Aave governance), they are not designed to be exhaustive. The idea is to build additional update layers on top of this system (e.g. Risk Stewards) to cover extra limitations.
- Timestamp of snapshots should not decrease from one parameters update to the next.
- To optimise calculations, the maximum yearly ratio growth received in yearly bps (e.g. 5_00 for 5%) is converted to ratio growth per second internally. We expose in yearly bps percentage to follow similar approach as on other Aave systems (BGD config engine), and because we believe it is more intuitive for integrations.
- Given that each asset on which to apply this adapter has its own characteristics, the base contract is `abstract` to allow the child to define its own specificities (mainly the way of calculating/fetching the current ratio).
- On construction, an extra `MINIMUM_SNAPSHOT_DELAY` is configured, indicating the minimum time (in seconds) to have passed since the snapshot ratio timestamp, and the current moment (block.timestamp). This is required because frequently, the update of the ration in a correlated asset is a "discrete" event, which causes a spike of value by time until enough time passes. E.g. if the snapshot ratio is set 1 second before the ratio was updated in an LST, 2 seconds after the increase by unit of time would be really high.

Defining a proper value depends on specific characteristics of the correlated asset, but generally, a conservative way would be setting a long enough delay, like 7 days.

<br>

### [FixCapPriceAdapter](./src/contracts/PriceCapAdapterStable.sol)

In some cases, the relation between an underlying asset and its correlated is direct, without any type of continuous growth expected. For example, this is the case of USD-pegged stable coins, where USD is the underlying and let's say USDC is the correlated asset.

Initially we thought to model this as a sub-case of `RatioCapPriceAdapter`, with 0 ratio growth, but finally we decided to create a simplified version of the adapter, removing completely the growth component.

<br>

<br>

## License

Copyright © 2024, Aave DAO, represented by its governance smart contracts.

Created by [BGD Labs](https://bgdlabs.com/).

The default license of this repository is [BUSL1.1](./LICENSE), but all interfaces and the content of the [libs folder](./src/contracts/libs/) and [Polygon tunnel](./src/contracts/adapters/polygon/tunnel/) folders are open source, MIT-licensed.

**IMPORTANT**. The BUSL1.1 license of this repository allows for any usage of the software, if respecting the *Additional Use Grant* limitations, forbidding any use case damaging anyhow the Aave DAO's interests.
5 changes: 5 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# To load the variables in the .env file
source .env

# To deploy and verify our contract
forge script script/Ghost.s.sol:Deploy --rpc-url $RPC_URL --private-key $PRIVATE_KEY --broadcast --verify --etherscan-api-key $ETHERSCAN_API_KEY -vvvv
38 changes: 38 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[profile.default]
src = 'src'
test = 'tests'
script = 'scripts'
out = 'out'
libs = ['lib']
remappings = [
]
fs_permissions = [{ access = "write", path = "./reports" }]

[rpc_endpoints]
mainnet = "${RPC_MAINNET}"
optimism = "${RPC_OPTIMISM}"
avalanche = "${RPC_AVALANCHE}"
polygon = "${RPC_POLYGON}"
arbitrum = "${RPC_ARBITRUM}"
fantom = "${RPC_FANTOM}"
harmony = "${RPC_HARMONY}"
metis = "${RPC_METIS}"
base = "${RPC_BASE}"
zkevm = "${RPC_ZKEVM}"
gnosis = "${RPC_GNOSIS}"
bnb = "${RPC_BNB}"

[etherscan]
mainnet = { key="${ETHERSCAN_API_KEY_MAINNET}", chainId=1 }
optimism = { key="${ETHERSCAN_API_KEY_OPTIMISM}", chainId=10 }
avalanche = { key="${ETHERSCAN_API_KEY_AVALANCHE}", chainId=43114 }
polygon = { key="${ETHERSCAN_API_KEY_POLYGON}", chainId=137 }
arbitrum = { key="${ETHERSCAN_API_KEY_ARBITRUM}", chainId=42161 }
fantom = { key="${ETHERSCAN_API_KEY_FANTOM}", chainId=250 }
metis = { key="any", chainId=1088, url='https://andromeda-explorer.metis.io/' }
base = { key="${ETHERSCAN_API_KEY_BASE}", chainId=8453 }
zkevm = { key="${ETHERSCAN_API_KEY_ZKEVM}", chainId=1101 }
gnosis = { key="${ETHERSCAN_API_KEY_GNOSIS}", chainId=100 }
bnb = { key="${ETHERSCAN_API_KEY_BNB}", chainId=56 }

# See more config options https://github.com/gakonst/foundry/tree/master/config
1 change: 1 addition & 0 deletions lib/aave-address-book
Submodule aave-address-book added at 555924
1 change: 1 addition & 0 deletions lib/aave-helpers
Submodule aave-helpers added at 3b4c2a
1 change: 1 addition & 0 deletions lib/cl-synchronicity-price-adapter
1 change: 1 addition & 0 deletions lib/forge-std
Submodule forge-std added at 4513bc
Loading

0 comments on commit bc32898

Please sign in to comment.