Skip to content

Forge - test against coingecko prices #579

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

Open
wants to merge 2 commits into
base: development
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
10 changes: 9 additions & 1 deletion contracts/test/oracles/default/ChainlinkOracleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { ChainlinkPriceOracleV2 } from "../../../oracles/default/ChainlinkPriceO
import { ICToken } from "../../../external/compound/ICToken.sol";

import { BaseTest } from "../../config/BaseTest.t.sol";
import { CoingeckoAPICaller } from "./CoingeckoAPICaller.sol";

contract ChainlinkOraclesTest is BaseTest {
contract ChainlinkOraclesTest is BaseTest, CoingeckoAPICaller {
ChainlinkPriceOracleV2 oracle;

address usdcPolygon = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
Expand Down Expand Up @@ -75,4 +76,11 @@ contract ChainlinkOraclesTest is BaseTest {

assertApproxEqAbs(usdtPrice, usdcPrice, 1e16, "usd prices differ too much");
}

function testPriceAgainstCoingecko() public debuggingOnly {
string memory base = "usd";
string memory asset = "ethereum-classic";
uint256 price = getCoinGeckoPrice(asset, base); // scaled to 1e18
emit log_named_uint("ethereum-classic price in usd", price);
}
}
49 changes: 49 additions & 0 deletions contracts/test/oracles/default/CoingeckoAPICaller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0;

import "forge-std/Vm.sol";
import "forge-std/Test.sol";

contract CoingeckoAPICaller is Test {
string internal constant COINGECKO_BASE_URL = "https://api.coingecko.com/api/v3/simple/price?vs_currencies=";
string internal constant PRECISION_QUERY_URL = "&precision=18&ids=";

function getCoinGeckoPrice(string memory asset, string memory base) internal returns (uint256) {
string memory url = concat4(COINGECKO_BASE_URL, base, PRECISION_QUERY_URL, asset);
string[] memory command = new string[](2);
command[0] = "curl";
command[1] = url;

string memory response = string(vm.ffi(command));
emit log_named_string("response", response);
string memory jqFilter = concat4(".", asset, ".", base);
string memory floatPointPrice = string(vm.parseJsonString(response, jqFilter));
return floatPointToUint(floatPointPrice);
}

function concat4(
string memory str0,
string memory str1,
string memory str2,
string memory str3
) internal returns (string memory) {
return string(bytes.concat(bytes.concat(bytes(str0), bytes(str1)), bytes.concat(bytes(str2), bytes(str3))));
}

function floatPointToUint(string memory str) internal returns (uint256) {
bytes memory asBytes = bytes(str);
uint256 k;

for (uint256 i = 0; i < asBytes.length; i++) {
if (asBytes[i] != ".") k++;
}

bytes memory noPointBytes = new bytes(k);
k = 0;
for (uint256 i = 0; i < asBytes.length; i++) {
if (asBytes[i] != ".") noPointBytes[k++] = asBytes[i];
}

return vm.parseUint(string(noPointBytes));
}
}
2 changes: 1 addition & 1 deletion lib/forge-std