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: contract level redstone support #248

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion DEPLOYMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

* Token factory contract: `0x3e4f1948aece07d3f30c8c5c425f914ac74653827de48394466f2a887eebe9c7`

* Market implementation contract: `0x63528412ed65c545ef1e3fb1988721f78c675e48265b70d8d55fbc42e1460730`
* Market implementation contract: `0x7a5bd92931474e3f86026d91fc0d556a79423dd1a2f6dd881e463443ae7459eb`

* Market proxy contract: `0x6030cf103746576706d7dcc2ae6f6b32ba0db66907a8f9901a0170de5f06acc0`

Expand Down
6 changes: 6 additions & 0 deletions Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ source = "member"
dependencies = [
"market_abi",
"pyth_interface",
"redstone",
"standards git+https://github.com/FuelLabs/sway-standards?tag=v0.6.1#792639cdf391565e6e6a02482ea8a46d9604a6f5",
"std",
"sway_libs",
Expand Down Expand Up @@ -38,6 +39,11 @@ dependencies = [
"std",
]

[[package]]
name = "redstone"
source = "git+https://github.com/redstone-finance/redstone-fuel-sdk?tag=testnet-0.65.2#dc1a0a3550a005db05a2664fac87524ab8acf216"
dependencies = ["std"]

[[package]]
name = "src-20"
source = "member"
Expand Down
23 changes: 14 additions & 9 deletions abis/market_abi/src/abi.sw
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ abi Market {
fn supply_collateral(); // Payment is required: any collateral asset

#[payable, storage(write)]
fn withdraw_collateral(asset_id: AssetId, amount: u64, price_data_update: PriceDataUpdate);
fn withdraw_collateral(
asset_id: AssetId,
amount: u64,
price_data_update: PriceDataUpdate,
redstone_payload: Bytes,
);

#[storage(read)]
fn get_user_collateral(account: Identity, asset_id: AssetId) -> u64;
Expand All @@ -65,31 +70,31 @@ abi Market {
fn supply_base(); // Payment is required: base asset (USDC)

#[payable, storage(write)]
fn withdraw_base(amount: u64, price_data_update: PriceDataUpdate);
fn withdraw_base(amount: u64, price_data_update: PriceDataUpdate, redstone_payload: Bytes);

#[storage(read)]
fn get_user_supply_borrow(account: Identity) -> (u256, u256);

#[storage(read)]
fn available_to_borrow(account: Identity) -> u256;
fn available_to_borrow(account: Identity, redstone_payload: Bytes) -> u256;

// # 5. Liquidation management
// Liquidates the user if there is insufficient collateral for the borrowing.
#[payable, storage(write)]
fn absorb(accounts: Vec<Identity>, price_data_update: PriceDataUpdate);
fn absorb(accounts: Vec<Identity>, price_data_update: PriceDataUpdate, redstone_payload: Bytes);

#[storage(read)]
fn is_liquidatable(account: Identity) -> bool;
fn is_liquidatable(account: Identity, redstone_payload: Bytes) -> bool;

// # 6. Protocol collateral management
#[payable, storage(read)]
fn buy_collateral(asset_id: AssetId, min_amount: u64, recipient: Identity); // Payment is required: base asset (USDC)
fn buy_collateral(asset_id: AssetId, min_amount: u64, recipient: Identity, redstone_payload: Bytes); // Payment is required: base asset (USDC)

#[storage(read)]
fn collateral_value_to_sell(asset_id: AssetId, collateral_amount: u64) -> u64;
fn collateral_value_to_sell(asset_id: AssetId, collateral_amount: u64, redstone_payload: Bytes) -> u64;

#[storage(read)]
fn quote_collateral(asset_id: AssetId, base_amount: u64) -> u64;
fn quote_collateral(asset_id: AssetId, base_amount: u64, redstone_payload: Bytes) -> u64;

// ## 7. Reserves management
#[storage(read)]
Expand Down Expand Up @@ -144,7 +149,7 @@ abi Market {
fn get_pyth_contract_id() -> ContractId;

#[storage(read)]
fn get_price(price_feed_id: PriceFeedId) -> Price;
fn get_price(price_feed_id: PriceFeedId, redstone_feed_id: u256, redstone_payload: Bytes) -> Price;

#[storage(read)]
fn update_fee(update_data: Vec<Bytes>) -> u64;
Expand Down
6 changes: 6 additions & 0 deletions abis/market_abi/src/structs.sw
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub const BASE_ACCRUAL_SCALE: u256 = 1_000_000; // 1e6
pub const BASE_INDEX_SCALE_15: u256 = 1_000_000_000_000_000; // 1e15
pub const FACTOR_SCALE_18: u256 = 1_000_000_000_000_000_000; // 1e18
pub const ORACLE_CONF_BASIS_POINTS: u256 = 10_000; // 1e4
pub const TAI64_UNIX_ADJUSTMENT = (10 + (1 << 62));

/// This struct contains the configuration details for collateral management.
pub struct CollateralConfiguration {
Expand All @@ -27,6 +28,8 @@ pub struct CollateralConfiguration {
pub supply_cap: u64, // decimals: asset decimals
/// This field indicates whether the collateral is paused.
pub paused: bool,
/// This field holds the redstone feed ID for the asset.
pub redstone_feed_id: u256,
}

/// This struct contains the configuration details for a market.
Expand All @@ -37,6 +40,8 @@ pub struct MarketConfiguration {
pub base_token_decimals: u32,
/// This field holds the price feed ID for the base token.
pub base_token_price_feed_id: b256,
/// This field holds the redstone feed ID for the base token.
pub base_token_redstone_feed_id: u256,
/// This field represents the supply kink.
pub supply_kink: u256, // decimals: 18
/// This field represents the borrow kink.
Expand Down Expand Up @@ -75,6 +80,7 @@ impl MarketConfiguration {
base_token: AssetId::zero(),
base_token_decimals: 0,
base_token_price_feed_id: b256::zero(),
base_token_redstone_feed_id: 0,
supply_kink: 0,
borrow_kink: 0,
supply_per_second_interest_rate_slope_low: 0,
Expand Down
6 changes: 4 additions & 2 deletions contracts/market/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ name = "market"

# address will be the addres of the proxy contract
[proxy]
# address = "0x657ab45a6eb98a4893a99fd104347179151e8b3828fd8f2a108cc09770d1ebae"
address = "0xbeaa0e9479a83eb3eab0f66e0f0c0ce4af86a6fe6fd4549ae22d87083e086821"
enabled = true
address = "0x657ab45a6eb98a4893a99fd104347179151e8b3828fd8f2a108cc09770d1ebae"

[dependencies]
market_abi = { path = "../../abis/market_abi" }
pyth_interface = { git = "https://github.com/pyth-network/pyth-crosschain", rev = "7de4ce3ec8defd97641c1d25302abe117bf5092b" }
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" }
redstone = { git = "https://github.com/redstone-finance/redstone-fuel-sdk", tag = "testnet-0.65.2" }
standards = { git = "https://github.com/FuelLabs/sway-standards", tag = "v0.6.1" }
sway_libs = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.24.0" }
53 changes: 53 additions & 0 deletions contracts/market/src/lib.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
library;

pub trait ArrWrap {
fn _len(self) -> u64;
fn _get(self, i: u64) -> b256;
}

impl ArrWrap for [b256; 5] {
fn _len(self) -> u64 {
5
}

fn _get(self, i: u64) -> b256 {
self[i]
}
}

pub trait ToVec: ArrWrap {
fn to_vec(self) -> Vec<b256>;
}

impl<T> ToVec for T
where
T: ArrWrap,
{
fn to_vec(self) -> Vec<b256> {
let mut result = Vec::new();
let mut i = 0;
while (i < self._len()) {
result.push(self._get(i));
i += 1;
}

result
}
}

impl<T> Vec<T>
where
T: Eq,
{
pub fn contains(self, value: T) -> bool {
let mut i = 0;
while (i < self.len()) {
if value == self.get(i).unwrap() {
return true;
}
i += 1;
}

false
}
}
Loading
Loading