Skip to content

Commit

Permalink
rename MockAggregator to Aggregator and add scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jan 17, 2025
1 parent a6ead78 commit cc332f5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 14 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ The script has an example function which calls `getPrice()` for `ETH/USD`. You c
```sh
forge script script/AstriaOracle.s.sol:AstriaOracleScript --rpc-url $RPC_URL --broadcast --sig "getPrice()" -vvvv
```

### deploy an aggregator

In `.env`, set `ORACLE_CONTRACT_ADDRESS` to the contract address of a deployed `AstriaOracle` contract and set `CURRENCY_PAIR` to the desired currency pair.

```sh
forge script script/Aggregator.s.sol:AggregatorScript --rpc-url $RPC_URL --broadcast --sig "deploy()" -vvvv
```

### query aggregator
15 changes: 13 additions & 2 deletions local.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ PRIVATE_KEY=0x
# default local rpc url
RPC_URL="http://localhost:8545"

### contract deployment values
### AstriaOracle contract deployment values

# the `astriaOracleCallerAddress` built into the astria-geth node
# in production, the private key for this address must NOT be known.
EVM_ORACLE_SENDER_ADDRESS=0x0000000000000000000000000000000000000088

### contract call values
### AstriaOracle contract call values

ORACLE_CONTRACT_ADDRESS=0x
# the private key for `EVM_ORACLE_SENDER_ADDRESS`; this should only be known for
# contract testing
EVM_ORACLE_SENDER_ADDRESS_PRIVATE_KEY=0x
CURRENCY_PAIR_A="ETH/USD"
CURRENCY_PAIR_B="BTC/USD"

### Aggregator contract deployment values
CURRENCY_PAIR="ETH/USD"

### Aggregator contract call values
AGGREGATOR_CONTRACT_ADDRESS=0xCf7Ed3AccA5a467e9e704C703E8D87F634fB0Fc9
31 changes: 31 additions & 0 deletions script/Aggregator.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {Aggregator} from "../src/Aggregator.sol";
import {AggregatorV3Interface} from "../src/interfaces/AggregatorV3Interface.sol";

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

function deploy() public {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

address oracleContractAddress = vm.envAddress("ORACLE_CONTRACT_ADDRESS");
bytes32 currencyPairHash = keccak256(bytes(vm.envString("CURRENCY_PAIR")));
new Aggregator(oracleContractAddress, currencyPairHash);

vm.stopBroadcast();
}

function getLatestRoundData() public view {
AggregatorV3Interface aggregator = AggregatorV3Interface(vm.envAddress("AGGREGATOR_CONTRACT_ADDRESS"));
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
aggregator.latestRoundData();
console.logUint(roundId);
console.logInt(answer);
console.logUint(startedAt);
console.logUint(updatedAt);
console.logUint(answeredInRound);
}
}
30 changes: 23 additions & 7 deletions script/AstriaOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,41 @@ import {Script, console} from "forge-std/Script.sol";
import {AstriaOracle} from "../src/AstriaOracle.sol";

contract AstriaOracleScript is Script {
AstriaOracle public astriaOracle;

function setUp() public {}

function deploy() public {
vm.startBroadcast(vm.envUint("PRIVATE_KEY"));

address oracleSenderAddress = vm.envAddress("EVM_ORACLE_SENDER_ADDRESS");
AstriaOracle oracle = new AstriaOracle(oracleSenderAddress);
new AstriaOracle(oracleSenderAddress);

vm.stopBroadcast();
}

function initializeCurrencyPairsAndSetPrices() public {
address oracleContract = vm.envAddress("ORACLE_CONTRACT_ADDRESS");
AstriaOracle oracle = AstriaOracle(oracleContract);

vm.startBroadcast(vm.envUint("EVM_ORACLE_SENDER_ADDRESS_PRIVATE_KEY"));

bytes32 pairA = keccak256(bytes(vm.envString("CURRENCY_PAIR_A")));
bytes32 pairB = keccak256(bytes(vm.envString("CURRENCY_PAIR_B")));
bytes32[] memory pairs = new bytes32[](2);
pairs[0] = pairA;
pairs[1] = pairB;
uint128[] memory prices = new uint128[](2);
prices[0] = uint128(400000000000000);
prices[1] = uint128(500000000000000);

console.logBytes(address(oracle).code);
oracle.initializeCurrencyPair(pairA, 18);
oracle.initializeCurrencyPair(pairB, 18);
oracle.updatePriceData(pairs, prices);

vm.stopBroadcast();
}

function getPrice() public view {
address oracleContract = vm.envAddress("ORACLE_CONTRACT_ADDRESS");
AstriaOracle oracle = AstriaOracle(oracleContract);
bytes32 pair = keccak256("ETH/USD");
bytes32 pair = keccak256(bytes(vm.envString("CURRENCY_PAIR_A")));
uint256 latestBlockNumber = oracle.latestBlockNumber();
(uint128 price,) = oracle.priceData(latestBlockNumber, pair);
console.logUint(price);
Expand Down
10 changes: 5 additions & 5 deletions src/MockAggregator.sol → src/Aggregator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ pragma solidity ^0.8.21;
import {AggregatorV2V3Interface} from "./interfaces/AggregatorV2V3Interface.sol";
import {AstriaOracle} from "./AstriaOracle.sol";

// Mock currency pair aggregator contract for a single currency pair.
contract MockAggregator is AggregatorV2V3Interface {
// Currency pair aggregator contract for a single currency pair.
contract Aggregator is AggregatorV2V3Interface {
// core oracle contract
AstriaOracle public immutable oracle;

// currency pair hash
bytes32 public immutable currencyPairHash;

constructor(AstriaOracle _oracle, bytes32 _currencyPairHash) {
oracle = _oracle;
constructor(address _oracle, bytes32 _currencyPairHash) {
oracle = AstriaOracle(_oracle);
currencyPairHash = _currencyPairHash;
}

Expand Down Expand Up @@ -50,7 +50,7 @@ contract MockAggregator is AggregatorV2V3Interface {
}

function description() external pure returns (string memory) {
return "MockAggregator";
return "ExampleAggregator";
}

function version() external pure returns (uint256) {
Expand Down

0 comments on commit cc332f5

Please sign in to comment.