Skip to content

Commit

Permalink
feat: Updated the Pragma Caller contract (#1506)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [x] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [ ] Other (please describe):

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #NA

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

- `PragmaCaller` now calls `get_data` with `AggregationMode`
- Added the `SummaryStats` contract calls for Pragma computational feeds
- Updated tests with `get_data` & `MockPragmaSummaryStats` contract

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1506)
<!-- Reviewable:end -->

---------

Co-authored-by: Oba <[email protected]>
Co-authored-by: enitrat <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent e3156b4 commit 6cbc820
Show file tree
Hide file tree
Showing 10 changed files with 610 additions and 96 deletions.
47 changes: 3 additions & 44 deletions cairo/mock_pragma/Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,55 +1,14 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "alexandria_data_structures"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4#46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4"

[[package]]
name = "alexandria_math"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4#46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4"
dependencies = [
"alexandria_data_structures",
]

[[package]]
name = "alexandria_sorting"
version = "0.1.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4#46c8d8ab9e3bfb68b70a29b3246f809cd8bf70e4"

[[package]]
name = "alexandria_storage"
version = "0.2.0"
source = "git+https://github.com/keep-starknet-strange/alexandria.git?rev=92c3c1b4ac35a4a56c14abe992814581aee875a8#92c3c1b4ac35a4a56c14abe992814581aee875a8"

[[package]]
name = "cubit"
version = "1.2.0"
source = "git+https://github.com/influenceth/cubit?rev=2ccb2536dffa3f15ebd38b755c1be65fde1eab0c#2ccb2536dffa3f15ebd38b755c1be65fde1eab0c"

[[package]]
name = "mock_pragma"
version = "0.1.0"
dependencies = [
"pragma",
"pragma_lib",
]

[[package]]
name = "openzeppelin"
version = "0.7.0"
source = "git+https://github.com/OpenZeppelin/cairo-contracts.git?tag=v0.7.0#bb8c56817577b66cea9f18a241fe59726db42dd5"

[[package]]
name = "pragma"
name = "pragma_lib"
version = "1.0.0"
source = "git+https://github.com/astraly-labs/pragma-oracle?tag=v1.0.5#71e762dcb725b95a4a4966190219bca8380bc823"
dependencies = [
"alexandria_data_structures",
"alexandria_math",
"alexandria_sorting",
"alexandria_storage",
"cubit",
"openzeppelin",
]
source = "git+https://github.com/astraly-labs/pragma-lib?tag=2.3.1#24bb4da111ae7eb00e7cf40d4f1767c86d6447cd"
2 changes: 1 addition & 1 deletion cairo/mock_pragma/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2023_10"

[dependencies]
starknet = "2.2.0"
pragma = { git = "https://github.com/astraly-labs/pragma-oracle", tag = "v1.0.5" }
pragma_lib = { git = "https://github.com/astraly-labs/pragma-lib", tag = "2.3.1" }

[[target.starknet-contract]]
casm = true
Expand Down
1 change: 1 addition & 0 deletions cairo/mock_pragma/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod mock_pragma_oracle;
mod mock_pragma_summary_stats;
12 changes: 8 additions & 4 deletions cairo/mock_pragma/src/mock_pragma_oracle.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use pragma::entry::structs::{DataType, PragmaPricesResponse};
use pragma_lib::types::{DataType, AggregationMode, PragmaPricesResponse};

#[starknet::interface]
trait IOracle<TContractState> {
fn get_data_median(self: @TContractState, data_type: DataType) -> PragmaPricesResponse;
fn get_data(
self: @TContractState, data_type: DataType, aggregation_mode: AggregationMode
) -> PragmaPricesResponse;
}

#[starknet::interface]
Expand All @@ -20,7 +22,7 @@ trait IMockPragmaOracle<TContractState> {
#[starknet::contract]
mod MockPragmaOracle {
use starknet::ContractAddress;
use pragma::entry::structs::{DataType, PragmaPricesResponse};
use pragma_lib::types::{DataType, AggregationMode, PragmaPricesResponse};

use super::{IOracle, IMockPragmaOracle};

Expand All @@ -36,7 +38,9 @@ mod MockPragmaOracle {
//! Must be compatible with Cairo 2.2.0
#[external(v0)]
impl IPragmaOracleImpl of IOracle<ContractState> {
fn get_data_median(self: @ContractState, data_type: DataType) -> PragmaPricesResponse {
fn get_data(
self: @ContractState, data_type: DataType, aggregation_mode: AggregationMode
) -> PragmaPricesResponse {
match data_type {
DataType::SpotEntry => {
PragmaPricesResponse {
Expand Down
89 changes: 89 additions & 0 deletions cairo/mock_pragma/src/mock_pragma_summary_stats.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use pragma_lib::types::{DataType, AggregationMode};

#[starknet::interface]
trait ISummaryStats<TContractState> {
fn calculate_mean(
self: @TContractState,
data_type: DataType,
start: u64,
stop: u64,
aggregation_mode: AggregationMode
) -> (u128, u32);

fn calculate_volatility(
self: @TContractState,
data_type: DataType,
start_tick: u64,
end_tick: u64,
num_samples: u64,
aggregation_mode: AggregationMode
) -> (u128, u32);

fn calculate_twap(
self: @TContractState,
data_type: DataType,
aggregation_mode: AggregationMode,
time: u64,
start_time: u64,
) -> (u128, u32);
}

#[starknet::contract]
mod MockPragmaSummaryStats {
use core::zeroable::Zeroable;
use starknet::ContractAddress;
use pragma_lib::types::{DataType, AggregationMode};
use pragma_lib::abi::{IPragmaABIDispatcher, IPragmaABIDispatcherTrait};

use super::ISummaryStats;

#[storage]
struct Storage {
pragma_oracle: IPragmaABIDispatcher,
}

#[constructor]
fn constructor(ref self: ContractState, pragma_oracle_address: ContractAddress) {
assert(!pragma_oracle_address.is_zero(), 'Pragma Oracle cannot be 0');
let pragma_oracle = IPragmaABIDispatcher { contract_address: pragma_oracle_address };
self.pragma_oracle.write(pragma_oracle);
}

//! Must be compatible with Cairo 2.2.0
#[external(v0)]
impl ISummaryStatsImpl of ISummaryStats<ContractState> {
fn calculate_mean(
self: @ContractState,
data_type: DataType,
start: u64,
stop: u64,
aggregation_mode: AggregationMode
) -> (u128, u32) {
let data = self.pragma_oracle.read().get_data(data_type, aggregation_mode);
(data.price, data.decimals)
}

fn calculate_volatility(
self: @ContractState,
data_type: DataType,
start_tick: u64,
end_tick: u64,
num_samples: u64,
aggregation_mode: AggregationMode
) -> (u128, u32) {
let data = self.pragma_oracle.read().get_data(data_type, aggregation_mode);
(data.price, data.decimals)
}

fn calculate_twap(
self: @ContractState,
data_type: DataType,
aggregation_mode: AggregationMode,
time: u64,
start_time: u64,
) -> (u128, u32) {
let data = self.pragma_oracle.read().get_data(data_type, aggregation_mode);
(data.price, data.decimals)
}
}
}
2 changes: 2 additions & 0 deletions kakarot_scripts/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class ChainId(IntEnum):
{"contract_name": "EVM", "is_account_contract": False},
{"contract_name": "kakarot", "is_account_contract": False},
{"contract_name": "MockPragmaOracle", "is_account_contract": False},
{"contract_name": "MockPragmaSummaryStats", "is_account_contract": False},
{"contract_name": "OpenzeppelinAccount", "is_account_contract": True},
{"contract_name": "replace_class", "is_account_contract": False},
{"contract_name": "StarknetToken", "is_account_contract": False},
Expand All @@ -283,6 +284,7 @@ class ChainId(IntEnum):
"EVM",
"kakarot",
"MockPragmaOracle",
"MockPragmaSummaryStats",
"OpenzeppelinAccount",
"replace_class",
"StarknetToken",
Expand Down
4 changes: 4 additions & 0 deletions kakarot_scripts/deployment/starknet_deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ async def deploy_starknet_contracts(account):
)
starknet_deployments["Counter"] = await deploy_starknet("Counter")
starknet_deployments["MockPragmaOracle"] = await deploy_starknet("MockPragmaOracle")
starknet_deployments["MockPragmaSummaryStats"] = await deploy_starknet(
"MockPragmaSummaryStats",
starknet_deployments["MockPragmaOracle"],
)
starknet_deployments["UniversalLibraryCaller"] = await deploy_starknet(
"UniversalLibraryCaller"
)
Expand Down
17 changes: 17 additions & 0 deletions solidity_contracts/scripts/PragmaCaller.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma solidity ^0.8.13;

import "forge-std/Script.sol";
import {PragmaCaller} from "../src/CairoPrecompiles/PragmaCaller.sol";

contract PragmaCallerScript is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
uint256 pragmaOracleAddress = vm.envUint("PRAGMA_ORACLE_ADDRESS");
uint256 pragmaSummaryStatsAddress = vm.envUint("PRAGMA_SUMMARY_STATS_ADDRESS");
vm.startBroadcast(deployerPrivateKey);

PragmaCaller pragmaCaller = new PragmaCaller(pragmaOracleAddress, pragmaSummaryStatsAddress);

vm.stopBroadcast();
}
}
Loading

0 comments on commit 6cbc820

Please sign in to comment.