Skip to content

Commit

Permalink
Update contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
dineshpinto committed Jun 6, 2024
1 parent 62f016a commit 90a23c5
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
41 changes: 26 additions & 15 deletions code_examples/developer-hub-solidity/FtsoV2ChangeQuoteFeed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ interface IFastUpdater {
)
external
view
returns (uint256[] memory _feedValues, int8[] memory _decimals, int64 _timestamp);
returns (
uint256[] memory _feedValues,
int8[] memory _decimals,
int64 _timestamp
);
}

/**
* THIS IS AN EXAMPLE CONTRACT USING HARDCODED VALUES.
* DO NOT USE THIS CODE IN PRODUCTION.
*/

contract FtsoV2ChangeQuoteFeed {
IFastUpdater internal ftsoV2;

/**
* Network: Coston
* Network: Songbird Testnet Coston
* Address: 0x9B931f5d3e24fc8C9064DB35bDc8FB4bE0E862f9
*/
constructor() {
Expand All @@ -32,10 +40,14 @@ contract FtsoV2ChangeQuoteFeed {
) internal pure returns (uint256) {
if (_baseFeedDecimals < _quoteDecimals) {
// Scale up if base feed decimals are less than quote feed decimals
return _baseFeedValue * 10 ** uint256(_quoteDecimals - _baseFeedDecimals);
return
_baseFeedValue *
10 ** uint256(_quoteDecimals - _baseFeedDecimals);
} else if (_baseFeedDecimals > _quoteDecimals) {
// Scale down if base feed decimals are more than quote feed decimals
return _baseFeedValue / 10 ** uint256(_baseFeedDecimals - _quoteDecimals);
return
_baseFeedValue /
10 ** uint256(_baseFeedDecimals - _quoteDecimals);
} else {
// No scaling needed if decimals are equal
return _baseFeedValue;
Expand All @@ -47,29 +59,28 @@ contract FtsoV2ChangeQuoteFeed {
* @param _baseAndQuoteFeedIndexes Array containing the indexes of the base and quote feeds.
* @return The computed new quote feed value.
*/
function getNewQuoteFeedValue(uint256[] calldata _baseAndQuoteFeedIndexes)
external
view
returns (uint256)
{
function getNewQuoteFeedValue(
uint256[] calldata _baseAndQuoteFeedIndexes
) external view returns (uint256) {
require(
_baseAndQuoteFeedIndexes.length == 2,
"Invalid feed indexes. Please provide exactly two indexes."
);

(uint256[] memory feedValues, int8[] memory decimals, ) = ftsoV2.fetchCurrentFeeds(_baseAndQuoteFeedIndexes);

// Fetch current feeds
(uint256[] memory feedValues, int8[] memory decimals, ) = ftsoV2
.fetchCurrentFeeds(_baseAndQuoteFeedIndexes);
uint8 newQuoteDecimals = uint8(decimals[1]);
// Scale the base feed value to match the quote feed decimals
uint256 scaledBaseFeedValue = _scaleBaseFeedValue(
feedValues[0],
uint8(decimals[0]),
newQuoteDecimals
);

// Prevent division by zero
require(feedValues[1] != 0, "Division by zero");

uint256 newQuoteFeedValue = (scaledBaseFeedValue * 10**uint256(newQuoteDecimals)) / feedValues[1];
// Compute the new quote feed value
uint256 newQuoteFeedValue = (scaledBaseFeedValue *
10 ** uint256(newQuoteDecimals)) / feedValues[1];
return newQuoteFeedValue;
}
}
20 changes: 15 additions & 5 deletions code_examples/developer-hub-solidity/FtsoV2FeedConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ interface IFastUpdater {
)
external
view
returns (uint256[] memory _feedValues, int8[] memory _decimals, int64 _timestamp);
returns (
uint256[] memory _feedValues,
int8[] memory _decimals,
int64 _timestamp
);
}

/**
Expand All @@ -21,7 +25,7 @@ contract FtsoV2FeedConsumer {
uint256[] public feedIndexes = [0, 2, 9];

/**
* Network: Coston
* Network: Songbird Testnet Coston
* Address: 0x9B931f5d3e24fc8C9064DB35bDc8FB4bE0E862f9
*/
constructor() {
Expand All @@ -34,9 +38,15 @@ contract FtsoV2FeedConsumer {
function getFtsoV2CurrentFeedValues()
external
view
returns (uint256[] memory _feedValues, int8[] memory _decimals, int64 _timestamp)
returns (uint256[] memory _feedValues, int8[] memory _decimals)
{
return ftsoV2.fetchCurrentFeeds(feedIndexes);
(
uint256[] memory feedValues,
int8[] memory decimals,
/* uint64 timestamp */
) = ftsoV2.fetchCurrentFeeds(feedIndexes);
/* Your custom feed consumption logic. */
/* In this example the feed values and decimals are just returned. */
return (feedValues, decimals);
}
}

20 changes: 12 additions & 8 deletions docs/ftso/1-getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,29 @@ contract FtsoV2FeedConsumer {
uint256[] public feedIndexes = [0, 2, 9];
/**
* Network: Flare Testnet Coston
* Network: Songbird Testnet Coston
* Address: 0x9B931f5d3e24fc8C9064DB35bDc8FB4bE0E862f9
*/
constructor() {
ftsoV2 = IFastUpdater(0x9B931f5d3e24fc8C9064DB35bDc8FB4bE0E862f9);
}
/**
* Get the current values of the specified feeds.
* @return _feedValues The current values of the specified feeds.
* @return _decimals The number of decimal places for each feed value.
* @return _timestamp The timestamp at which the feed values were fetched.
* Get the current value of the feeds.
*/
function getFtsoV2CurrentFeedValues()
public
external
view
returns (uint256[] memory _feedValues, int8[] memory _decimals, int64 _timestamp)
returns (uint256[] memory _feedValues, int8[] memory _decimals)
{
return ftsoV2.fetchCurrentFeeds(feedIndexes);
(
uint256[] memory feedValues,
int8[] memory decimals,
/* uint64 timestamp */
) = ftsoV2.fetchCurrentFeeds(feedIndexes);
/* Your custom feed consumption logic. */
/* In this example the feed values and decimals are just returned. */
return (feedValues, decimals);
}
}
```
Expand Down
44 changes: 22 additions & 22 deletions docs/ftso/guides/change-quote-feed.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ This guide will show you how to fetch the latest feed values for two feeds and c
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IFastUpdater {
function fetchCurrentFeeds(
uint256[] calldata _feedIndexes
)
external
view
returns (uint256[] memory _feedValues, int8[] memory _decimals, int64 _timestamp);
}
import { IFastUpdater } from "@flarenetwork/flare-periphery-contracts/flare/ftso/userInterfaces/IFastUpdater.sol";
/**
* THIS IS AN EXAMPLE CONTRACT USING HARDCODED VALUES.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract FtsoV2ChangeQuoteFeed {
IFastUpdater internal ftsoV2;
/**
* Network: Coston
* Network: Songbird Testnet Coston
* Address: 0x9B931f5d3e24fc8C9064DB35bDc8FB4bE0E862f9
*/
constructor() {
Expand All @@ -45,10 +42,14 @@ contract FtsoV2ChangeQuoteFeed {
) internal pure returns (uint256) {
if (_baseFeedDecimals < _quoteDecimals) {
// Scale up if base feed decimals are less than quote feed decimals
return _baseFeedValue * 10 ** uint256(_quoteDecimals - _baseFeedDecimals);
return
_baseFeedValue *
10 ** uint256(_quoteDecimals - _baseFeedDecimals);
} else if (_baseFeedDecimals > _quoteDecimals) {
// Scale down if base feed decimals are more than quote feed decimals
return _baseFeedValue / 10 ** uint256(_baseFeedDecimals - _quoteDecimals);
return
_baseFeedValue /
10 ** uint256(_baseFeedDecimals - _quoteDecimals);
} else {
// No scaling needed if decimals are equal
return _baseFeedValue;
Expand All @@ -60,29 +61,28 @@ contract FtsoV2ChangeQuoteFeed {
* @param _baseAndQuoteFeedIndexes Array containing the indexes of the base and quote feeds.
* @return The computed new quote feed value.
*/
function getNewQuoteFeedValue(uint256[] calldata _baseAndQuoteFeedIndexes)
external
view
returns (uint256)
{
function getNewQuoteFeedValue(
uint256[] calldata _baseAndQuoteFeedIndexes
) external view returns (uint256) {
require(
_baseAndQuoteFeedIndexes.length == 2,
"Invalid feed indexes. Please provide exactly two indexes."
);
(uint256[] memory feedValues, int8[] memory decimals, ) = ftsoV2.fetchCurrentFeeds(_baseAndQuoteFeedIndexes);
// Fetch current feeds
(uint256[] memory feedValues, int8[] memory decimals, ) = ftsoV2
.fetchCurrentFeeds(_baseAndQuoteFeedIndexes);
uint8 newQuoteDecimals = uint8(decimals[1]);
// Scale the base feed value to match the quote feed decimals
uint256 scaledBaseFeedValue = _scaleBaseFeedValue(
feedValues[0],
uint8(decimals[0]),
newQuoteDecimals
);
// Prevent division by zero
require(feedValues[1] != 0, "Division by zero");
uint256 newQuoteFeedValue = (scaledBaseFeedValue * 10**uint256(newQuoteDecimals)) / feedValues[1];
// Compute the new quote feed value
uint256 newQuoteFeedValue = (scaledBaseFeedValue *
10 ** uint256(newQuoteDecimals)) / feedValues[1];
return newQuoteFeedValue;
}
}
Expand Down

0 comments on commit 90a23c5

Please sign in to comment.