Skip to content

Commit

Permalink
feat(new_kakarot_interface): new interface for Kakarot
Browse files Browse the repository at this point in the history
  • Loading branch information
akhercha committed Nov 1, 2024
1 parent d4480ce commit d2c3b15
Show file tree
Hide file tree
Showing 9 changed files with 410 additions and 90 deletions.
10 changes: 5 additions & 5 deletions kakarot-evm-interface/.env.example
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
RPC_URL=https://sepolia-rpc.kakarot.org
ETHERSCAN_VERIFY_URL=https://api.routescan.io/v2/network/testnet/evm/920637907288165/etherscan

DEPLOYER_PRIVATE_KEY=0x0
CAIRO_PRAGMA_ORACLE_ADDRESS=0x36031daa264c24520b11d93af622c848b2499b66b41d611bac95e13cfca131a
CAIRO_PRAGMA_SUMMARY_STATS_ADDRESS=0x379afb83d2f8e38ab08252750233665a812a24278aacdde52475618edbf879c

PRAGMA_ORACLE_DEPLOYED_CAIRO_ADDRESS=0x0
EVM_PRIVATE_KEY=0x0
EVM_PRAGMA_CALLER_ADDRESS=0x0

PRAGMA_CALLER_DEPLOYED_ADDRESS=0x0

EXAMPLE_ADDRESS=0x0
PAIR_ID=0
16 changes: 10 additions & 6 deletions kakarot-evm-interface/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ RPC_URL=https://sepolia-rpc.kakarot.org
# Etherscan URL used to verify the contract - default is Sepolia
ETHERSCAN_VERIFY_URL=https://api.routescan.io/v2/network/testnet/evm/1802203764_2/etherscan

# Cairo address of the Pragma Oracle - see deployments
CAIRO_PRAGMA_ORACLE_ADDRESS=0x0
CAIRO_PRAGMA_SUMMARY_STATS_ADDRESS=0x0

# Deployer that will be used to deploy PragmaCaller to Kakarot
DEPLOYER_PRIVATE_KEY=0x0
EVM_PRIVATE_KEY=0x0

# Address of the pre-deployed PragmaOracle cairo contract
PRAGMA_ORACLE_DEPLOYED_CAIRO_ADDRESS=0x3a99b4b9f711002f1976b3973f4b2031fe6056518615ff0f4e6dd829f972764
# Set this to the PragmaCaller deployed address on Kakarot once deployed
EVM_PRAGMA_CALLER_ADDRESS=0x0

# Once PragmaCaller has been deployed, write the address here and call `verify.sh`
PRAGMA_CALLER_DEPLOYED_ADDRESS=0x7491cA3699701a187C1a17308338Ad0bA258B082
# Set this to the pair you want to use for the PragmaAggregatorV3 interface
PAIR_ID=0
```

## Deploy
Expand All @@ -36,7 +40,7 @@ source .env
```shell
forge script script/PragmaCaller.s.sol \
--broadcast --rpc-url $RPC_URL \
--verifier-url '$ETHERSCAN_VERIFY_URL' \
--verifier-url $ETHERSCAN_VERIFY_URL \
--etherscan-api-key "verifyContract"
```

Expand Down
12 changes: 4 additions & 8 deletions kakarot-evm-interface/script/CallerExample.s.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

import {Script, console2} from "forge-std/Script.sol";
import {CallerExample} from "../src/CallerExample.sol";

contract DeployCallerExample is Script {
function setUp() public {}

function run() public {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
address pragmaCallerAddress = vm.envAddress("PRAGMA_CALLER_DEPLOYED_ADDRESS");
uint256 deployerPrivateKey = vm.envUint("EVM_PRIVATE_KEY");
address pragmaCallerAddress = vm.envAddress("EVM_PRAGMA_CALLER_ADDRESS");

vm.startBroadcast(deployerPrivateKey);

CallerExample callerExample = new CallerExample(pragmaCallerAddress);

console2.log("CallerExample deployed at:", address(callerExample));

vm.stopBroadcast();
Expand Down
13 changes: 4 additions & 9 deletions kakarot-evm-interface/script/PragmaAggregatorV3.s.sol
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
// SPDX-License-Identifier: Apache 2
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

import {Script, console2} from "forge-std/Script.sol";
import {PragmaAggregatorV3} from "../src/PragmaAggregatorV3.sol";

contract DeployPragmaAggregatorV3 is Script {
function setUp() public {}

function run() public {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
address pragmaCallerAddress = vm.envAddress("PRAGMA_CALLER_DEPLOYED_ADDRESS");

uint256 deployerPrivateKey = vm.envUint("EVM_PRIVATE_KEY");
address pragmaCallerAddress = vm.envAddress("EVM_PRAGMA_CALLER_ADDRESS");
uint256 pairId = vm.envUint("PAIR_ID");

vm.startBroadcast(deployerPrivateKey);

PragmaAggregatorV3 aggregator = new PragmaAggregatorV3(pragmaCallerAddress, pairId);

console2.log("PragmaAggregatorV3 deployed for pair ID", pairId, "at:", address(aggregator));

vm.stopBroadcast();
Expand Down
21 changes: 9 additions & 12 deletions kakarot-evm-interface/script/PragmaCaller.s.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

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

contract DeployPragmaCaller is Script {
function setUp() public {}

function run() public {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
uint256 pragmaOracleAddress = vm.envUint("PRAGMA_ORACLE_DEPLOYED_CAIRO_ADDRESS");

contract PragmaCallerScript is Script {
function run() external {
uint256 deployerPrivateKey = vm.envUint("EVM_PRIVATE_KEY");
uint256 pragmaOracleAddress = vm.envUint("CAIRO_PRAGMA_ORACLE_ADDRESS");
uint256 pragmaSummaryStatsAddress = vm.envUint("CAIRO_PRAGMA_SUMMARY_STATS_ADDRESS");

vm.startBroadcast(deployerPrivateKey);

PragmaCaller pragmaCaller = new PragmaCaller(pragmaOracleAddress);

PragmaCaller pragmaCaller = new PragmaCaller(pragmaOracleAddress, pragmaSummaryStatsAddress);
console2.log("PragmaCaller deployed at:", address(pragmaCaller));

vm.stopBroadcast();
Expand Down
154 changes: 139 additions & 15 deletions kakarot-evm-interface/src/CallerExample.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.28;

interface IPragmaCaller {
enum DataType { SpotEntry, FuturesEntry, GenericEntry }
enum AggregationMode {
Median,
Mean
}
enum DataType {
SpotEntry,
FuturesEntry,
GenericEntry
}

struct PragmaPricesRequest {
AggregationMode aggregationMode;
DataType dataType;
uint256 pairId;
uint256 expirationTimestamp;
}

struct DataRequest {
struct PragmaCalculateVolatilityRequest {
DataType dataType;
uint256 pairId;
uint256 expirationTimestamp;
uint64 startTimestamp;
uint64 endTimestamp;
uint64 numSamples;
AggregationMode aggregationMode;
}

struct PragmaPricesResponse {
Expand All @@ -18,28 +37,133 @@ interface IPragmaCaller {
uint256 maybe_expiration_timestamp;
}

function getDataMedianSpot(DataRequest memory request) external view returns (PragmaPricesResponse memory);
struct PragmaSummaryStatsResponse {
uint256 price;
uint256 decimals;
}

function getData(
PragmaPricesRequest memory request
) external view returns (PragmaPricesResponse memory);

function calculateVolatility(
PragmaCalculateVolatilityRequest memory request
) external view returns (PragmaSummaryStatsResponse memory);
}

contract CallerExample {
IPragmaCaller private pragmaCaller;
uint256 constant BTC_USD_FEED = 18669995996566340;
uint64 constant SECONDS_IN_ONE_WEEK = 604800;
uint64 constant CALCULATE_VOL_NUM_SAMPLES = 200;

constructor(address pragmaCallerAddress) {
pragmaCaller = IPragmaCaller(pragmaCallerAddress);
}

function getDataMedianSpot(uint256 pairId) public view returns (IPragmaCaller.PragmaPricesResponse memory) {
IPragmaCaller.DataRequest memory request = IPragmaCaller.DataRequest(
IPragmaCaller.DataType.SpotEntry,
pairId,
0
);
return pragmaCaller.getDataMedianSpot(request);
function getBtcSpotMedianPrice()
public
view
returns (IPragmaCaller.PragmaPricesResponse memory)
{
IPragmaCaller.PragmaPricesRequest memory request = IPragmaCaller
.PragmaPricesRequest(
IPragmaCaller.AggregationMode.Median,
IPragmaCaller.DataType.SpotEntry,
BTC_USD_FEED,
0
);
return pragmaCaller.getData(request);
}

function getBtcMedianPrice() public view returns (uint256) {
IPragmaCaller.PragmaPricesResponse memory response = getDataMedianSpot(BTC_USD_FEED);
return response.price;
function getBtcVolatilyOverLastWeek()
public
view
returns (IPragmaCaller.PragmaSummaryStatsResponse memory)
{
uint64 blockTimestamp = uint64(block.timestamp);

IPragmaCaller.PragmaCalculateVolatilityRequest
memory request = IPragmaCaller.PragmaCalculateVolatilityRequest(
IPragmaCaller.DataType.SpotEntry,
BTC_USD_FEED,
0,
blockTimestamp - SECONDS_IN_ONE_WEEK,
blockTimestamp,
CALCULATE_VOL_NUM_SAMPLES,
IPragmaCaller.AggregationMode.Median
);
return pragmaCaller.calculateVolatility(request);
}

function getDataMedianSpot(
uint256 pairId
) public view returns (IPragmaCaller.PragmaPricesResponse memory) {
IPragmaCaller.PragmaPricesRequest memory request = IPragmaCaller
.PragmaPricesRequest(
IPragmaCaller.AggregationMode.Median,
IPragmaCaller.DataType.SpotEntry,
pairId,
0
);
return pragmaCaller.getData(request);
}

function getDataMeanSpot(
uint256 pairId
) public view returns (IPragmaCaller.PragmaPricesResponse memory) {
IPragmaCaller.PragmaPricesRequest memory request = IPragmaCaller
.PragmaPricesRequest(
IPragmaCaller.AggregationMode.Mean,
IPragmaCaller.DataType.SpotEntry,
pairId,
0
);
return pragmaCaller.getData(request);
}

function getDataMedianPerp(
uint256 pairId
) public view returns (IPragmaCaller.PragmaPricesResponse memory) {
IPragmaCaller.PragmaPricesRequest memory request = IPragmaCaller
.PragmaPricesRequest(
IPragmaCaller.AggregationMode.Median,
IPragmaCaller.DataType.FuturesEntry,
pairId,
0
);
return pragmaCaller.getData(request);
}

function getDataMedianFuture(
uint256 pairId,
uint64 expiryTimestamp
) public view returns (IPragmaCaller.PragmaPricesResponse memory) {
IPragmaCaller.PragmaPricesRequest memory request = IPragmaCaller
.PragmaPricesRequest(
IPragmaCaller.AggregationMode.Median,
IPragmaCaller.DataType.FuturesEntry,
pairId,
uint256(expiryTimestamp)
);
return pragmaCaller.getData(request);
}

function getVolatilyOverLastWeek(
uint256 pairId
) public view returns (IPragmaCaller.PragmaSummaryStatsResponse memory) {
uint64 blockTimestamp = uint64(block.timestamp);

IPragmaCaller.PragmaCalculateVolatilityRequest
memory request = IPragmaCaller.PragmaCalculateVolatilityRequest(
IPragmaCaller.DataType.SpotEntry,
pairId,
0,
blockTimestamp - SECONDS_IN_ONE_WEEK,
blockTimestamp,
CALCULATE_VOL_NUM_SAMPLES,
IPragmaCaller.AggregationMode.Median
);
return pragmaCaller.calculateVolatility(request);
}
}
Loading

0 comments on commit d2c3b15

Please sign in to comment.