Skip to content

Commit

Permalink
feat: improve AssetOracle
Browse files Browse the repository at this point in the history
  • Loading branch information
bhargavaparoksham committed Dec 29, 2024
1 parent 0d58c29 commit 4211a4a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
7 changes: 6 additions & 1 deletion script/DeployOracle.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ contract DeployScript is Script {
uint256 deployerPrivateKey = vm.envUint("PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

string memory source = "const yahooFinanceUrl = `https://query1.finance.yahoo.com/v8/finance/chart/TSLA?interval=1h`; const response = await Functions.makeHttpRequest({ url: yahooFinanceUrl }); if (!response || response.status !== 200) throw new Error(\"Failed to fetch asset data\"); const data = response.data.chart.result[0]; const currentPrice = data.meta.regularMarketPrice; return Functions.encodeUint256(Math.round(currentPrice * 100));";

// Calculate the source hash
bytes32 sourceHash = keccak256(abi.encodePacked(source));

// Deploy your contract
AssetOracle assetOracle = new AssetOracle(0xf9B8fc078197181C841c296C876945aaa425B278,"TSLA");
AssetOracle assetOracle = new AssetOracle(0xf9B8fc078197181C841c296C876945aaa425B278,"TSLA", sourceHash);

console.log("Deployed Oracle contract at:", address(assetOracle));

Expand Down
3 changes: 1 addition & 2 deletions script/fetchAssetPrice.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Asset price function to be used in the AssetOracle contract to fetch data from Yahoo Finance using Chainlink Functions.

const assetSymbol = args[0];
const yahooFinanceUrl = `https://query1.finance.yahoo.com/v8/finance/chart/${assetSymbol}?interval=1d`;
const yahooFinanceUrl = `https://query1.finance.yahoo.com/v8/finance/chart/TSLA?interval=1h`;

const response = await Functions.makeHttpRequest({ url: yahooFinanceUrl });
if (!response || response.status !== 200)
Expand Down
31 changes: 25 additions & 6 deletions src/protocol/AssetOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,25 @@ contract AssetOracle is FunctionsClient, ConfirmedOwner {
bytes32 public s_lastRequestId;
bytes public lastResponse;
bytes public lastError;
bytes32 public sourceHash; // Hash of the valid source

string public assetSymbol; // Asset symbol (e.g., "TSLA")
uint256 public assetPrice; // Price in cents
uint256 public lastUpdated; // Timestamp of last update


event AssetSymbolUpdated(string newAssetSymbol);
event AssetPriceUpdated(uint256 price, uint256 timestamp);
event SourceHashUpdated(bytes32 newSourceHash);

error UnexpectedRequestID(bytes32 requestId);
error InvalidSource();

constructor(address router, string memory _assetSymbol)
constructor(address router, string memory _assetSymbol, bytes32 _sourceHash)
FunctionsClient(router)
ConfirmedOwner(msg.sender)
{
assetSymbol = _assetSymbol;
sourceHash = _sourceHash;
}

// Request new asset price data
Expand All @@ -34,12 +40,13 @@ contract AssetOracle is FunctionsClient, ConfirmedOwner {
uint64 subscriptionId,
uint32 gasLimit,
bytes32 donID
) public onlyOwner {
) public {
// Verify source integrity using its hash
if (keccak256(abi.encodePacked(source)) != sourceHash) {
revert InvalidSource();
}
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(source);
string[] memory args = new string[](1);
args[0] = assetSymbol;
req.setArgs(args);
s_lastRequestId = _sendRequest(req.encodeCBOR(), subscriptionId, gasLimit, donID);
}

Expand All @@ -54,4 +61,16 @@ contract AssetOracle is FunctionsClient, ConfirmedOwner {
lastUpdated = block.timestamp;
emit AssetPriceUpdated(assetPrice, block.timestamp);
}

// Update the source hash
function updateSourceHash(bytes32 newSourceHash) external onlyOwner {
sourceHash = newSourceHash;
emit SourceHashUpdated(newSourceHash);
}

// Update the asset symbol
function updateAssetSymbol(string memory newAssetSymbol) external onlyOwner {
assetSymbol = newAssetSymbol;
emit AssetSymbolUpdated(newAssetSymbol);
}
}

0 comments on commit 4211a4a

Please sign in to comment.