-
Notifications
You must be signed in to change notification settings - Fork 111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Custom Redeem Algo #1035
Draft
akshatmittal
wants to merge
1
commit into
master
Choose a base branch
from
redeem-algo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
WIP: Custom Redeem Algo #1035
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: BlueOak-1.0.0 | ||
pragma solidity 0.8.19; | ||
|
||
import "../interfaces/IRToken.sol"; | ||
import "../libraries/Fixed.sol"; | ||
import "../p1/BasketHandler.sol"; | ||
import "../p1/BackingManager.sol"; | ||
|
||
contract FacadeRedeem { | ||
function getBaseInformation(IRToken rToken) | ||
external | ||
returns ( | ||
uint48 currentNonce, | ||
uint192 basketsNeeded, | ||
IERC20[] memory allErc20s, | ||
uint256[] memory allBalances, | ||
IERC20[][] memory erc20s, | ||
uint256[][] memory quantities | ||
) | ||
{ | ||
// Either hell freezes over, or it's a static call. | ||
require(msg.sender == address(0), unicode"ಠ_ಠ"); | ||
|
||
rToken.main().poke(); | ||
|
||
BackingManagerP1 bm = BackingManagerP1(address(rToken.main().backingManager())); | ||
BasketHandlerP1 bh = BasketHandlerP1(address(rToken.main().basketHandler())); | ||
|
||
currentNonce = bh.nonce(); | ||
basketsNeeded = rToken.basketsNeeded(); | ||
|
||
allErc20s = rToken.main().assetRegistry().erc20s(); | ||
allBalances = new uint256[](allErc20s.length); | ||
|
||
for (uint256 i = 0; i < allErc20s.length; ++i) { | ||
allBalances[i] = allErc20s[i].balanceOf(address(bm)); | ||
} | ||
|
||
quantities = new uint256[][](currentNonce); | ||
erc20s = new IERC20[][](currentNonce); | ||
|
||
for (uint48 nonce = 1; nonce <= currentNonce; nonce++) { | ||
(IERC20[] memory erc20ForNonce, uint256[] memory qtys) = bh.getHistoricalBasket(nonce); | ||
|
||
quantities[nonce - 1] = qtys; | ||
erc20s[nonce - 1] = erc20ForNonce; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { BigNumber } from 'ethers' | ||
import hre, { ethers } from 'hardhat' | ||
|
||
const bigIntMax = (...args: BigNumber[]) => args.reduce((m, e) => (e.gt(m) ? e : m)) | ||
const bigIntMin = (...args: BigNumber[]) => args.reduce((m, e) => (e.lt(m) ? e : m)) | ||
|
||
const rTokenAddress = '0xA0d69E286B938e21CBf7E51D71F6A4c8918f482F' // eUSD | ||
// const rTokenAddress = '0xaCdf0DBA4B9839b96221a8487e9ca660a48212be' // hyUSD | ||
|
||
async function main() { | ||
const FacadeRedeemFactory = await hre.ethers.getContractFactory('FacadeRedeem') | ||
const FacadeRedeem = await FacadeRedeemFactory.deploy().then((e) => e.deployed()) | ||
|
||
const baseData = await FacadeRedeem.connect(hre.ethers.provider).callStatic.getBaseInformation( | ||
rTokenAddress, | ||
{ | ||
from: ethers.constants.AddressZero, | ||
} | ||
) | ||
|
||
console.log({ currentNonce: baseData.currentNonce }) | ||
console.log({ basketsNeeded: baseData.basketsNeeded }) | ||
|
||
// Nonces are mapped to their _indexes_ aka, nonce 3 becomes 2, nonce 4 becomes 3 and so on. | ||
const allValidNonces = Array.from({ length: baseData.currentNonce }, (_, i) => i).filter((e) => { | ||
// If basket has an erc20 which was unregistered. | ||
if ( | ||
baseData.erc20s[e].filter((f) => baseData.allErc20s.includes(f)).length !== | ||
baseData.erc20s[e].length | ||
) { | ||
return false | ||
} | ||
|
||
// If basket has no erc20s, aka before 3.1.0 release. | ||
if (baseData.erc20s[e].length === 0) { | ||
return false | ||
} | ||
|
||
return true | ||
}) | ||
|
||
console.log({ allValidNonces }) | ||
|
||
// Final picked nonces | ||
const pickedNonces: Record<number, BigNumber> = {} | ||
|
||
// Just temp storage | ||
const nonceFractions: Record<number, BigNumber> = {} | ||
const activeBalances = [...baseData.allBalances] | ||
|
||
function getBalanceFor(erc20: string) { | ||
return activeBalances[baseData.allErc20s.indexOf(erc20)] | ||
} | ||
|
||
// Worst case, we'll need some fraction of each basket. | ||
let validNonces = [...allValidNonces] | ||
while (validNonces.length > 0) { | ||
for (const nonce of validNonces) { | ||
const basketFraction = baseData.erc20s[nonce].map((e, i) => | ||
baseData.quantities[nonce][i].eq(0) | ||
? BigNumber.from(0) | ||
: getBalanceFor(e).mul(BigNumber.from(10).pow(36)).div(baseData.quantities[nonce][i]) | ||
) | ||
|
||
const basketFractionRatio = basketFraction.map((e) => e.div(baseData.basketsNeeded)) | ||
|
||
// console.log(basketFraction, basketFractionRatio) | ||
|
||
nonceFractions[nonce] = bigIntMin(...basketFractionRatio) | ||
|
||
// if (nonceFractions[nonce].eq(0)) { | ||
// // remove fraction if it gives Infinity, it just means the list was empty. | ||
// validNonces = validNonces.filter((e) => e !== nonce) | ||
// } | ||
|
||
// console.log(basketFraction, basketFractionRatio, nonceFractions[nonce]) | ||
} | ||
|
||
// Let's pick the nonce with the max redemption value. | ||
const chosenNonce = validNonces.find( | ||
(e) => nonceFractions[e] == bigIntMax(...validNonces.map((e) => nonceFractions[e])) | ||
)! | ||
pickedNonces[chosenNonce] = nonceFractions[chosenNonce] | ||
|
||
// console.log({ chosenNonce }) | ||
// console.log(baseData.quantities[chosenNonce]) | ||
|
||
baseData.erc20s[chosenNonce].forEach((e, i) => { | ||
// console.log(getBalanceFor(e)) | ||
// console.log( | ||
// baseData.quantities[chosenNonce][i] | ||
// .mul(nonceFractions[chosenNonce]) | ||
// .mul(baseData.basketsNeeded) | ||
// .div(BigNumber.from(10).pow(36)) | ||
// ) | ||
|
||
activeBalances[baseData.allErc20s.indexOf(e)] = getBalanceFor(e).sub( | ||
baseData.quantities[chosenNonce][i] | ||
.mul(nonceFractions[chosenNonce]) | ||
.mul(baseData.basketsNeeded) | ||
.div(BigNumber.from(10).pow(36)) | ||
) | ||
|
||
if (getBalanceFor(e).lt(0)) { | ||
activeBalances[baseData.allErc20s.indexOf(e)] = BigNumber.from(0) | ||
} | ||
|
||
// console.log(activeBalances[baseData.allErc20s.indexOf(e)]) | ||
// console.log('next') | ||
}) | ||
|
||
validNonces = validNonces.filter((e) => e !== chosenNonce) | ||
|
||
// console.log(chosenNonce, nonceFractions[chosenNonce]) | ||
} | ||
|
||
console.log({ pickedNonces }) | ||
} | ||
|
||
main() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error(error) | ||
process.exit(1) | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not know about this trick, but makes sense!