Skip to content

Commit 8bdcdcf

Browse files
committed
[Chainlink] Build-Token-Shop-With-Price-Feeds
1 parent 2348a53 commit 8bdcdcf

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎documents/Chainlink/Build-Token-Shop-With-Price-Feeds.md‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,36 @@ receive() external payable {
102102
```
103103

104104
> **Note:** TokenShop 계약에 `MyERC20::mint` 호출 권한을 부여해야 합니다.
105+
106+
### amountToMint Function
107+
108+
ETH/USD 가격을 조회하여 전송된 ETH를 USD로 변환하고, 해당 USD 금액에 해당하는 토큰 수량을 계산합니다:
109+
110+
```solidity
111+
function amountToMint(uint256 amountInETH) public view returns (uint256) {
112+
// ETH를 USD로 변환
113+
uint256 ethUsd = uint256(getChainlinkDataFeedLatestAnswer()) * 10 ** 10; // 8 decimals -> 18 decimals
114+
uint256 ethAmountInUSD = amountInETH * ethUsd / 10 ** 18; // ETH = 18 decimals
115+
return (ethAmountInUSD * 10 ** TOKEN_DECIMALS) / TOKEN_USD_PRICE;
116+
}
117+
```
118+
119+
### Chainlink Data Feeds 구현
120+
121+
ETH/USD 가격을 가져오는 함수입니다:
122+
123+
```solidity
124+
/**
125+
* Returns the latest answer
126+
*/
127+
function getChainlinkDataFeedLatestAnswer() public view returns (int) {
128+
(
129+
/*uint80 roundID*/,
130+
int price,
131+
/*uint startedAt*/,
132+
/*uint timeStamp*/,
133+
/*uint80 answeredInRound*/
134+
) = i_priceFeed.latestRoundData();
135+
return price;
136+
}
137+
```

0 commit comments

Comments
 (0)