Replies: 6 comments 5 replies
-
Hi @Vodvalex could you please provide some more context as to what it is you're stuck with? Are you just needing to get the price feed in pounds and not dollars? |
Beta Was this translation helpful? Give feedback.
-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
/**
Network: Sepolia
Base: BTC/USD
Base Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
Quote: EUR/USD
Quote Address: 0x1a81afB8146aeFfCFc5E50e8479e826E7D55b910
Decimals: 8
*/
/**
THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
DO NOT USE THIS CODE IN PRODUCTION.
*/
contract PriceConverter {
function getDerivedPrice(
address _base,
address _quote,
uint8 _decimals
) public view returns (int256) {
require(
_decimals > uint8(0) && _decimals <= uint8(18),
"Invalid _decimals"
);
int256 decimals = int256(10 ** uint256(_decimals));
(, int256 basePrice, , , ) = AggregatorV3Interface(_base)
.latestRoundData();
uint8 baseDecimals = AggregatorV3Interface(_base).decimals();
basePrice = scalePrice(basePrice, baseDecimals, _decimals);
(, int256 quotePrice, , , ) = AggregatorV3Interface(_quote)
.latestRoundData();
uint8 quoteDecimals = AggregatorV3Interface(_quote).decimals();
quotePrice = scalePrice(quotePrice, quoteDecimals, _decimals);
return (basePrice * decimals) / quotePrice;
}
function scalePrice(
int256 _price,
uint8 _priceDecimals,
uint8 _decimals
) internal pure returns (int256) {
if (_priceDecimals < _decimals) {
return _price * int256(10 ** uint256(_decimals - _priceDecimals));
} else if (_priceDecimals > _decimals) {
return _price / int256(10 ** uint256(_priceDecimals - _decimals));
}
return _price;
}
} |
Beta Was this translation helpful? Give feedback.
-
Hi Everyone.Thanks for your replies.My deepest appologies I have been offshore for 1 week and could not answer.I have done the following : // SPDX-License-Identifier: MIT /**
/**
library PriceConverterGBP {
} FundMeGBP.sol / /SPDX-License-Identifier: MIT contract FundMeGBP{
} At this stage I am the most interested in the "require ( msg.value >= SERVICE_FEE, "You need to spend more ETH!"); " in function fund() in order for me to create my Smart contract in GBP. I am sure a lot of you have been much longer in this game than me.If anyone could possibly kindly check what I have done and perhaps point out on my mistakes that would be great.Then I will start testing it in Foundry.Thank you. |
Beta Was this translation helpful? Give feedback.
-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
/**
Network: Sepolia
Base: ETH/USD
Base Address: 0x694AA1769357215DE4FAC081bf1f309aDC325306
Quote: EUR/USD
Quote Address: 0x635A86F9fdD16Ff09A0701C305D3a845F1758b8E
Decimals: 8
*/
/**
THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
DO NOT USE THIS CODE IN PRODUCTION.
*/
library PriceConverterGBP {
function getGbpPrice(
address _base,
address _quote,
uint8 _decimals
) public view returns (int256) {
require(
_decimals > uint8(0) && _decimals <= uint8(18),
"Invalid _decimals"
);
int256 decimals = int256(10 ** uint256(_decimals));
(, int256 basePrice, , , ) = AggregatorV3Interface(_base)
.latestRoundData();
uint8 baseDecimals = AggregatorV3Interface(_base).decimals();
basePrice = scalePrice(basePrice, baseDecimals, _decimals);
(, int256 quotePrice, , , ) = AggregatorV3Interface(_quote)
.latestRoundData();
uint8 quoteDecimals = AggregatorV3Interface(_quote).decimals();
quotePrice = scalePrice(quotePrice, quoteDecimals, _decimals);
return (((basePrice * decimals) / quotePrice)*1e10);
}
function scalePrice(
int256 _price,
uint8 _priceDecimals,
uint8 _decimals
) internal pure returns (int256) {
if (_priceDecimals < _decimals) {
return _price * int256(10 ** uint256(_decimals - _priceDecimals));
} else if (_priceDecimals > _decimals) {
return _price / int256(10 ** uint256(_priceDecimals - _decimals));
}
return _price;
}
function getConversionRate(int ethamount) internal view returns (int){
address _base= 0x694AA1769357215DE4FAC081bf1f309aDC325306;
address _quote=0x635A86F9fdD16Ff09A0701C305D3a845F1758b8E;
uint8 _decimals=8;
int ethPriceGbp= (getGbpPrice (_base, _quote, _decimals));
int ethAmountGbp= (ethPriceGbp*ethamount)/1e18;
return ethAmountGbp;
}
} |
Beta Was this translation helpful? Give feedback.
-
/ /SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
import {PriceConverterGBP} from "PriceConverterGBP.sol";
contract FundMeGBP{
using PriceConverterGBP for uint;
uint public constant SERVICE_FEE =5e18;
address [] private s_customers;
address private immutable i_owner;
AggregatorV3Interface private s_priceFeed;
mapping( address => uint) private s_addressToAmountPay;
function Fund () public payable {
require ( msg.value >= SERVICE_FEE, "You need to spend more ETH!");
s_customers.push(msg.sender);
s_addressToAmountPaid[msg.sender]+= msg.value;
}
}
|
Beta Was this translation helpful? Give feedback.
-
So what is wrong with the line |
Beta Was this translation helpful? Give feedback.
-
Dear All.Since I live in the UK I am trying to build a smart contract FundMe in British Pounds(GBP).but I am stuck.I have used the contract from chainlink data feed ''Getting a different price denomination:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";
/**
*/
/**
*/
contract PriceConverter {
function getDerivedPrice(
address _base,
address _quote,
uint8 _decimals
) public view returns (int256) {
require(
_decimals > uint8(0) && _decimals <= uint8(18),
"Invalid _decimals"
);
int256 decimals = int256(10 ** uint256(_decimals));
(, int256 basePrice, , , ) = AggregatorV3Interface(_base)
.latestRoundData();
uint8 baseDecimals = AggregatorV3Interface(_base).decimals();
basePrice = scalePrice(basePrice, baseDecimals, _decimals);
}
Now how to move to the function getConversionRate() from this point??? I suspect there should be some conversion function from int to uint or perhaps I am moving to the wrong direction. Sorry I am a beginner. Your help will be much apriciated.Thank you.
Beta Was this translation helpful? Give feedback.
All reactions