Skip to content

Commit 68baa18

Browse files
committed
fix: clean up logic for USTB gsm converter and add interface
1 parent 376863c commit 68baa18

File tree

2 files changed

+47
-14
lines changed

2 files changed

+47
-14
lines changed

src/contracts/facilitators/gsm/converter/USTBGsmConverter.sol

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';
99
import {IGhoToken} from '../../../gho/interfaces/IGhoToken.sol';
1010
import {IGsm} from '../interfaces/IGsm.sol';
1111
import {IGsmConverter} from './interfaces/IGsmConverter.sol';
12-
import {IRedemption} from '../dependencies/circle/IRedemption.sol';
13-
// TODO: replace with proper issuance implementation later
12+
// TODO: replace with proper issuance implementation/interface later from USTB
13+
import {ISubscriptionRedemption} from '../dependencies/USTB/ISubscriptionRedemption.sol';
1414
import {MockBUIDLSubscription} from '../../../../test/mocks/MockBUIDLSubscription.sol';
1515

1616
import 'forge-std/console2.sol';
@@ -169,7 +169,7 @@ contract USTBGsmConverter is Ownable, EIP712, IGsmConverter {
169169

170170
/**
171171
* @notice Buys the GSM underlying asset in exchange for selling GHO, after asset redemption
172-
* @param minAmount The minimum amount of the underlying asset to buy (ie USTB)
172+
* @param minAmount The minimum amount of the underlying asset to buy via conversion (USDC)
173173
* @param receiver Recipient address of the underlying asset being purchased
174174
* @return The amount of underlying asset bought, after asset redemption
175175
* @return The amount of GHO sold by the user
@@ -183,26 +183,20 @@ contract USTBGsmConverter is Ownable, EIP712, IGsmConverter {
183183
uint256 initialIssuedAssetBalance = IERC20(ISSUED_ASSET).balanceOf(address(this));
184184
uint256 initialRedeemedAssetBalance = IERC20(REDEEMED_ASSET).balanceOf(address(this));
185185

186-
(, uint256 ghoAmount, , ) = IGsm(GSM).getGhoAmountForBuyAsset(minAmount);
186+
uint256 minUSTBAmount = ISubscriptionRedemption(REDEMPTION_CONTRACT).calculateUstbIn(minAmount);
187+
188+
(, uint256 ghoAmount, , ) = IGsm(GSM).getGhoAmountForBuyAsset(minUSTBAmount);
187189

188190
IGhoToken(GHO_TOKEN).transferFrom(originator, address(this), ghoAmount);
189191
IGhoToken(GHO_TOKEN).approve(address(GSM), ghoAmount);
190-
(uint256 boughtAssetAmount, uint256 ghoSold) = IGsm(GSM).buyAsset(minAmount, address(this));
192+
(uint256 boughtAssetAmount, uint256 ghoSold) = IGsm(GSM).buyAsset(minUSTBAmount, address(this));
191193
require(ghoAmount == ghoSold, 'INVALID_GHO_SOLD');
192194
IGhoToken(GHO_TOKEN).approve(address(GSM), 0);
193195

194196
IERC20(ISSUED_ASSET).approve(address(REDEMPTION_CONTRACT), boughtAssetAmount);
195197
IRedemption(REDEMPTION_CONTRACT).redeem(boughtAssetAmount);
196-
// TODO: adjust require depending on how much USDC is redeemed
197-
// because USTB increases in price, redemption will NOT be in 1:1 ratio
198-
uint256 redeemedAmount = boughtAssetAmount;
199-
// require(
200-
// IERC20(REDEEMED_ASSET).balanceOf(address(this)) ==
201-
// initialRedeemedAssetBalance + ,
202-
// 'INVALID_REDEMPTION'
203-
// );
204198
IERC20(ISSUED_ASSET).approve(address(REDEMPTION_CONTRACT), 0);
205-
IERC20(REDEEMED_ASSET).safeTransfer(receiver, redeemedAmount);
199+
IERC20(REDEEMED_ASSET).safeTransfer(receiver, minAmount);
206200

207201
require(
208202
IGhoToken(GHO_TOKEN).balanceOf(address(this)) == initialGhoBalance,
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Simple interface for subscriptions/redemptions of USTB/USDC
2+
pragma solidity ^0.8.10;
3+
4+
/**
5+
* @title ISubscriptionRedemption
6+
*/
7+
interface ISubscriptionRedemption {
8+
/**
9+
* @notice Subscribes an amount of USTB in exchange for USDC
10+
* @param amount The amount of USDC to subscribe
11+
*/
12+
function subscribe(uint256 amount) external;
13+
14+
/**
15+
* @notice Redeems an amount of USDC in exchange for USTB
16+
* @param amount The amount of the USTB to redeem
17+
*/
18+
function redeem(uint256 amount) external;
19+
20+
/**
21+
* @notice Calculates the amount of USTB required to redeem to get a given amount of USDC
22+
* @param usdcOutAmount The amount of USDC to receive
23+
* @return ustbInAmount The amount of USTB required to redeem to get usdcOutAmount
24+
* @return usdPerUstbChainlinkRaw The price of USTB in USD, in Chainlink raw format
25+
*/
26+
function calculateUstbIn(
27+
uint256 usdcOutAmount
28+
) external view returns (uint256 ustbInAmount, uint256 usdPerUstbChainlinkRaw);
29+
30+
/**
31+
* @notice Calculates the amount of USDC that will be received when redeeming a given amount of USTB
32+
* @param superstateTokenInAmount The amount of USTB to redeem
33+
* @return usdcOutAmount The amount of USDC to receive
34+
* @return usdPerUstbChainlinkRaw The price of USTB in USD, in Chainlink raw format
35+
*/
36+
function calculateUsdcOut(
37+
uint256 superstateTokenInAmount
38+
) external view returns (uint256 usdcOutAmount, uint256 usdPerUstbChainlinkRaw);
39+
}

0 commit comments

Comments
 (0)