From 208499dffe880c350e8600cf7cc23efbd4daf34f Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Fri, 7 Jan 2022 15:43:00 -0500 Subject: [PATCH 01/19] going to lend position should set selected base to position's base (consistent with other position views) Fixes #769 --- src/views/LendPosition.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/views/LendPosition.tsx b/src/views/LendPosition.tsx index 0ff2af04a..7dcbcb58d 100644 --- a/src/views/LendPosition.tsx +++ b/src/views/LendPosition.tsx @@ -37,7 +37,7 @@ const LendPosition = () => { /* STATE FROM CONTEXT */ const { userState, - userActions: { setSelectedSeries }, + userActions: { setSelectedSeries, setSelectedBase }, } = useContext(UserContext) as IUserContext; const { selectedSeries, seriesMap, assetMap, seriesLoading } = userState; @@ -129,8 +129,12 @@ const LendPosition = () => { useEffect(() => { const _series = seriesMap.get(idFromUrl) || null; - idFromUrl && setSelectedSeries(_series); - }, [idFromUrl, seriesMap, setSelectedSeries]); + const _base = assetMap.get(_series?.baseId!); + if (idFromUrl) { + setSelectedSeries(_series); + setSelectedBase(_base!); + } + }, [idFromUrl, seriesMap, setSelectedSeries, assetMap, setSelectedBase]); return ( <> From 064791c5fedb23bbe99d77ae7f687f7809b106af Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 12 Jan 2022 15:07:48 -0500 Subject: [PATCH 02/19] use inputMode numeric for mobile number pad --- src/views/Borrow.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/views/Borrow.tsx b/src/views/Borrow.tsx index 83c3075d1..0007e8fd9 100644 --- a/src/views/Borrow.tsx +++ b/src/views/Borrow.tsx @@ -305,6 +305,7 @@ const Borrow = () => { <TextInput plain type="number" + inputMode="numeric" placeholder="Enter amount" value={borrowInput} onChange={(event: any) => From 9cc45900338c2d75e8718bf613862b0a1ea9d5e5 Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 12 Jan 2022 15:16:47 -0500 Subject: [PATCH 03/19] use inputMode numeric for all textInputs --- src/components/settings/SlippageSetting.tsx | 1 + src/views/Lend.tsx | 6 ++---- src/views/LendPosition.tsx | 1 + src/views/Pool.tsx | 1 + src/views/PoolPosition.tsx | 1 + src/views/VaultPosition.tsx | 3 +++ 6 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/settings/SlippageSetting.tsx b/src/components/settings/SlippageSetting.tsx index 47d6d52a9..2b6a18ed6 100644 --- a/src/components/settings/SlippageSetting.tsx +++ b/src/components/settings/SlippageSetting.tsx @@ -76,6 +76,7 @@ const SlippageSetting = () => { reverse plain type="number" + inputMode="numeric" value={input || ''} onChange={(event: any) => setInput(event.target.value)} /> diff --git a/src/views/Lend.tsx b/src/views/Lend.tsx index a0ea118a5..29f15d7c7 100644 --- a/src/views/Lend.tsx +++ b/src/views/Lend.tsx @@ -63,10 +63,7 @@ const Lend = () => { const { txProcess: lendProcess, resetProcess: resetLendProcess } = useProcess(ActionCodes.LEND, selectedSeries?.id!); /* input validation hooks */ - const { inputError: lendError } = useInputValidation(lendInput, ActionCodes.LEND, selectedSeries, [ - 0, - maxLend_, - ]); + const { inputError: lendError } = useInputValidation(lendInput, ActionCodes.LEND, selectedSeries, [0, maxLend_]); /* LOCAL FNS */ const handleLend = () => { @@ -144,6 +141,7 @@ const Lend = () => { <TextInput plain type="number" + inputMode="numeric" placeholder="Enter amount" value={lendInput || ''} onChange={(event: any) => diff --git a/src/views/LendPosition.tsx b/src/views/LendPosition.tsx index 7dcbcb58d..49b017566 100644 --- a/src/views/LendPosition.tsx +++ b/src/views/LendPosition.tsx @@ -240,6 +240,7 @@ const LendPosition = () => { <TextInput plain type="number" + inputMode="numeric" placeholder="Amount to redeem" value={closeInput || ''} onChange={(event: any) => diff --git a/src/views/Pool.tsx b/src/views/Pool.tsx index 6957fa6f3..d6bf8ae0b 100644 --- a/src/views/Pool.tsx +++ b/src/views/Pool.tsx @@ -127,6 +127,7 @@ function Pool() { <TextInput plain type="number" + inputMode="numeric" placeholder="Enter Amount" value={poolInput || ''} onChange={(event: any) => diff --git a/src/views/PoolPosition.tsx b/src/views/PoolPosition.tsx index 55394e6d0..8438fe3e7 100644 --- a/src/views/PoolPosition.tsx +++ b/src/views/PoolPosition.tsx @@ -257,6 +257,7 @@ const PoolPosition = () => { <TextInput plain type="number" + inputMode="numeric" placeholder="Tokens to remove" value={removeInput || ''} onChange={(event: any) => diff --git a/src/views/VaultPosition.tsx b/src/views/VaultPosition.tsx index 54f4167d2..9a57991bf 100644 --- a/src/views/VaultPosition.tsx +++ b/src/views/VaultPosition.tsx @@ -456,6 +456,7 @@ const VaultPosition = () => { <TextInput plain type="number" + inputMode="numeric" placeholder={`Enter ${vaultBase?.displaySymbol} amount to Repay`} // ref={(el:any) => { el && !repayOpen && !rateLockOpen && !mobile && el.focus(); setInputRef(el); }} value={repayInput || ''} @@ -581,6 +582,7 @@ const VaultPosition = () => { // disabled={removeCollatInput} plain type="number" + inputMode="numeric" placeholder="Collateral to add" value={addCollatInput || ''} onChange={(event: any) => @@ -649,6 +651,7 @@ const VaultPosition = () => { // disabled={addCollatInput} plain type="number" + inputMode="numeric" placeholder="Collateral to remove" value={removeCollatInput || ''} onChange={(event: any) => From edb22b47b021ba3b4ae8faa55738d7198fead86e Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 12 Jan 2022 15:30:29 -0500 Subject: [PATCH 04/19] use decimal instead of numeric --- src/components/settings/SlippageSetting.tsx | 2 +- src/views/Borrow.tsx | 2 +- src/views/Lend.tsx | 2 +- src/views/LendPosition.tsx | 2 +- src/views/Pool.tsx | 2 +- src/views/PoolPosition.tsx | 2 +- src/views/VaultPosition.tsx | 6 +++--- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/settings/SlippageSetting.tsx b/src/components/settings/SlippageSetting.tsx index 2b6a18ed6..ab1437a41 100644 --- a/src/components/settings/SlippageSetting.tsx +++ b/src/components/settings/SlippageSetting.tsx @@ -76,7 +76,7 @@ const SlippageSetting = () => { reverse plain type="number" - inputMode="numeric" + inputMode="decimal" value={input || ''} onChange={(event: any) => setInput(event.target.value)} /> diff --git a/src/views/Borrow.tsx b/src/views/Borrow.tsx index 0007e8fd9..89e950db0 100644 --- a/src/views/Borrow.tsx +++ b/src/views/Borrow.tsx @@ -305,7 +305,7 @@ const Borrow = () => { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Enter amount" value={borrowInput} onChange={(event: any) => diff --git a/src/views/Lend.tsx b/src/views/Lend.tsx index 29f15d7c7..960eeb1ed 100644 --- a/src/views/Lend.tsx +++ b/src/views/Lend.tsx @@ -141,7 +141,7 @@ const Lend = () => { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Enter amount" value={lendInput || ''} onChange={(event: any) => diff --git a/src/views/LendPosition.tsx b/src/views/LendPosition.tsx index 49b017566..556125be8 100644 --- a/src/views/LendPosition.tsx +++ b/src/views/LendPosition.tsx @@ -240,7 +240,7 @@ const LendPosition = () => { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Amount to redeem" value={closeInput || ''} onChange={(event: any) => diff --git a/src/views/Pool.tsx b/src/views/Pool.tsx index d6bf8ae0b..cf387e637 100644 --- a/src/views/Pool.tsx +++ b/src/views/Pool.tsx @@ -127,7 +127,7 @@ function Pool() { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Enter Amount" value={poolInput || ''} onChange={(event: any) => diff --git a/src/views/PoolPosition.tsx b/src/views/PoolPosition.tsx index 8438fe3e7..cd47ca87f 100644 --- a/src/views/PoolPosition.tsx +++ b/src/views/PoolPosition.tsx @@ -257,7 +257,7 @@ const PoolPosition = () => { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Tokens to remove" value={removeInput || ''} onChange={(event: any) => diff --git a/src/views/VaultPosition.tsx b/src/views/VaultPosition.tsx index 9a57991bf..f55877223 100644 --- a/src/views/VaultPosition.tsx +++ b/src/views/VaultPosition.tsx @@ -456,7 +456,7 @@ const VaultPosition = () => { <TextInput plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder={`Enter ${vaultBase?.displaySymbol} amount to Repay`} // ref={(el:any) => { el && !repayOpen && !rateLockOpen && !mobile && el.focus(); setInputRef(el); }} value={repayInput || ''} @@ -582,7 +582,7 @@ const VaultPosition = () => { // disabled={removeCollatInput} plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Collateral to add" value={addCollatInput || ''} onChange={(event: any) => @@ -651,7 +651,7 @@ const VaultPosition = () => { // disabled={addCollatInput} plain type="number" - inputMode="numeric" + inputMode="decimal" placeholder="Collateral to remove" value={removeCollatInput || ''} onChange={(event: any) => From 20e61d5ee424f3845713ce86dd3982ccec120c93 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Sun, 16 Jan 2022 09:59:34 +0000 Subject: [PATCH 05/19] address update --- package.json | 2 +- src/contexts/yieldEnv.json | 28 +++++++++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 318532ad6..a76d808f9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "app-v2", - "version": "2.0.60", + "version": "2.0.61", "private": true, "dependencies": { "@multiavatar/multiavatar": "^1.0.6", diff --git a/src/contexts/yieldEnv.json b/src/contexts/yieldEnv.json index 9b22231d1..8281072b5 100644 --- a/src/contexts/yieldEnv.json +++ b/src/contexts/yieldEnv.json @@ -6,29 +6,29 @@ "CompoundMultiOracle" : "0x53FBa816BD69a7f2a096f58687f87dd3020d0d5c", "ChainlinkMultiOracle": "0xcDCe5C87f691058B61f3A65913f1a3cBCbAd9F52", "CompositeMultiOracle": "0xA81414a544D0bd8a28257F4038D3D24B08Dd9Bb4", - "YearnVaultMultiOracle": "0xC597E9cA52Afc13F7F5EDdaC9e53DEF569236016", "Witch": "0x53C3760670f6091E1eC76B4dd27f73ba4CAd5061", - "lidoWrapHandler": "0x491aB93faa921C8E634F891F96512Be14fD3DbB1" + "lidoWrapHandler": "0x491aB93faa921C8E634F891F96512Be14fD3DbB1", + "YearnVaultMultiOracle": "0xC597E9cA52Afc13F7F5EDdaC9e53DEF569236016" }, "4": { "Cauldron": "0x8390Cd98C116F269a6E6A3b50Fb03B0931423164", - "Ladle": "0xbC0200F0AAD7C1c0bBB1CC7885E1e796DFFac3e0", + "Ladle": "0x45f502A7358ec631c7a422270922d7dFA142cb9C", "CompoundMultiOracle" : "0xeCA876c39DF7b75281Ea78eB35912fa1CC8f9482", "ChainlinkMultiOracle": "0xBDBF01Ee32485aF94e316C395765F5Af2bf4b4dB", "CompositeMultiOracle": "0x8482BF1e17ceF57109F34C259d39d5B9BB9A4e13", - "YearnVaultMultiOracle": "0x88175a3e14F905ecCe464aF2A8E99f26159d4Cb8", - "Witch": "0x458098338c136D1c4410910e6F893316D86Ba837", - "lidoWrapHandler": "0x80d0CfceE7dfa135189e44d617032d5A7Cc2705b" + "Witch": "0x5cEe0925647722e7ed32dcaf5A2DeFD3872Edb6F", + "lidoWrapHandler": "0x80d0CfceE7dfa135189e44d617032d5A7Cc2705b", + "YearnVaultMultiOracle": "0x88175a3e14F905ecCe464aF2A8E99f26159d4Cb8" }, "42": { - "Cauldron": "0xacb1fb5E88ba69E12BDE76A4c373F1935d9fe912", + "Cauldron": "0xb2cf78ccA863675Ef61a0c696885DED0C833F8f3", "Ladle": "0xe7bae0445B9a1DBE834a7379fbE23C6d2Bd61C59", "CompoundMultiOracle" : "0x23F5F7a17117ba794Fcc5F46Fbf635edD2596Bb6", "ChainlinkMultiOracle": "0xBcdc8bad83ca8053DFA6B1CBAEB7E71699254a3b", "CompositeMultiOracle": "0xaF45D0277399E79Bf599d72fF4521f8Dc4A060E1", - "YearnVaultMultiOracle": "0xaF45D0277399E79Bf599d72fF4521f8Dc4A060E1", "Witch": "0xA421B81481e0d150740f8F137FB10b1b747974a8", - "lidoWrapHandler": "0x491aB93faa921C8E634F891F96512Be14fD3DbB1" + "lidoWrapHandler": "0x491aB93faa921C8E634F891F96512Be14fD3DbB1", + "YearnVaultMultiOracle": "0xaF45D0277399E79Bf599d72fF4521f8Dc4A060E1" }, "421611": { "Cauldron": "0x589fb605D165D0cd32608cC44dAcB87151d3B913", @@ -46,12 +46,10 @@ "0x8e8D6aB093905C400D583EfD37fbeEB1ee1c0c39" ], "4": [ - "0x1B496E901263b1EFa32880D6d13e325A8cb2D5c1", - "0x3a333FEc691DFCa0907Bc80024C27d0bbe7D022f", - "0x455F45e240436B75218E71B72472fad1a28Bc7A9", - "0xE40D644017BfC8b233bEf9cE2Eab55b512B964B4", - "0x4ABe302854c59546E37f46a8aa5DF482cFC683B0", - "0xccf1374c3d12AE3aAd51419e2CDed785C39d47A7" + "0x8468B9154D777Dc4520fDA275d30D8D663841C1d", + "0x6020a656a0421aFd96b02523D17fA23E206CD453", + "0x1a017DBc08317b105E266139F888c7ae6eDd6a0F", + "0xc946e0b5e815a7b22F563a569895663BE54f152A" ], "42": [ "0xad1fBd05f4FDB81C1bAE82172E9913a7c4152154", From e26c951c029b6efb3bdfa15cb9b2b6c59e7aa4d5 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Sun, 16 Jan 2022 10:51:51 +0000 Subject: [PATCH 06/19] update cauldron addr --- src/contexts/yieldEnv.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/contexts/yieldEnv.json b/src/contexts/yieldEnv.json index 8281072b5..e2183fe93 100644 --- a/src/contexts/yieldEnv.json +++ b/src/contexts/yieldEnv.json @@ -11,7 +11,7 @@ "YearnVaultMultiOracle": "0xC597E9cA52Afc13F7F5EDdaC9e53DEF569236016" }, "4": { - "Cauldron": "0x8390Cd98C116F269a6E6A3b50Fb03B0931423164", + "Cauldron": "0xb2cf78ccA863675Ef61a0c696885DED0C833F8f3", "Ladle": "0x45f502A7358ec631c7a422270922d7dFA142cb9C", "CompoundMultiOracle" : "0xeCA876c39DF7b75281Ea78eB35912fa1CC8f9482", "ChainlinkMultiOracle": "0xBDBF01Ee32485aF94e316C395765F5Af2bf4b4dB", @@ -21,7 +21,7 @@ "YearnVaultMultiOracle": "0x88175a3e14F905ecCe464aF2A8E99f26159d4Cb8" }, "42": { - "Cauldron": "0xb2cf78ccA863675Ef61a0c696885DED0C833F8f3", + "Cauldron": "0x82Dfaef826C8c190E9b472896a9a99165Ccee35a", "Ladle": "0xe7bae0445B9a1DBE834a7379fbE23C6d2Bd61C59", "CompoundMultiOracle" : "0x23F5F7a17117ba794Fcc5F46Fbf635edD2596Bb6", "ChainlinkMultiOracle": "0xBcdc8bad83ca8053DFA6B1CBAEB7E71699254a3b", From 6cdef38656fad5cad829366c83a5aa156303f387 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Mon, 17 Jan 2022 10:32:52 +0000 Subject: [PATCH 07/19] remove slippage multiplier --- src/hooks/actionHooks/useAddLiquidity.ts | 3 +-- src/hooks/viewHelperHooks/usePoolHelpers.ts | 2 +- src/utils/yieldMath.ts | 10 ++++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/hooks/actionHooks/useAddLiquidity.ts b/src/hooks/actionHooks/useAddLiquidity.ts index ddcf15739..a10a31ca6 100644 --- a/src/hooks/actionHooks/useAddLiquidity.ts +++ b/src/hooks/actionHooks/useAddLiquidity.ts @@ -73,11 +73,10 @@ export const useAddLiquidity = () => { series.ts, series.g1, series.decimals, - slippageTolerance + // slippageTolerance ); console.log(cachedBaseReserves.toString(), cachedRealReserves.toString()) - const [minRatio, maxRatio] = calcPoolRatios(cachedBaseReserves, cachedRealReserves); const [_baseToPool, _baseToFyToken] = splitLiquidity( diff --git a/src/hooks/viewHelperHooks/usePoolHelpers.ts b/src/hooks/viewHelperHooks/usePoolHelpers.ts index 05725460d..3f33419f1 100644 --- a/src/hooks/viewHelperHooks/usePoolHelpers.ts +++ b/src/hooks/viewHelperHooks/usePoolHelpers.ts @@ -129,7 +129,7 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b strategySeries.ts, strategySeries.g1, strategySeries.decimals, - slippageTolerance + // slippageTolerance ); /* Check if buy and pool option is allowed */ diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index fe0a9ca10..f3ad46a2b 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -150,6 +150,7 @@ const _computeA = ( return [a, invA]; /* returns a and inverse of a */ }; + const _computeB = ( timeToMaturity: BigNumber | string, ts: BigNumber | string, @@ -710,7 +711,7 @@ export function fyTokenForMint( const fyDaiRealReserves_ = new Decimal(fyTokenRealReserves18.toString()); const base_ = new Decimal(base18.toString()); const timeTillMaturity_ = new Decimal(timeTillMaturity.toString()); - const slippage_ = new Decimal(slippage).mul(new Decimal(10)); /* multiply the user slippage by 10 */ + const slippage_ = new Decimal(slippage) // .mul(new Decimal(10)); /* multiply the user slippage by 10 */ let min = ZERO; let max = base_.mul(TWO); @@ -721,7 +722,7 @@ export function fyTokenForMint( while (true) { /* NB return ZERO when not converging > not mintable */ // eslint-disable-next-line no-plusplus - if (i++ > 100) return ZERO_BN; + if (i++ > 1000) return ZERO_BN; // if (i++ > 100) throw 'Not converging' zIn = new Decimal( @@ -735,6 +736,7 @@ export function fyTokenForMint( 18 ).toString() ); + const Z_1 = baseReserves_.add(zIn); // New base balance const z_1 = base_.sub(zIn); // My remaining base const Y_1 = fyDaiRealReserves_.sub(yOut); // New fyToken balance @@ -760,6 +762,10 @@ export function fyTokenForMint( } } + console.log( 'yOut : ', yOut.floor().toFixed()); + + console.log( 'buyFyTOKEN : ', zIn.toString() ); + return decimal18ToDecimalN( // (converted back to original decimals) BigNumber.from(yOut.floor().toFixed()), From 7859529e0e9e0262be18b663881759fe5e997c38 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Mon, 17 Jan 2022 11:46:32 +0000 Subject: [PATCH 08/19] revert to 100 iterations before failing --- src/utils/yieldMath.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index f3ad46a2b..aef7a7a6d 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -722,7 +722,7 @@ export function fyTokenForMint( while (true) { /* NB return ZERO when not converging > not mintable */ // eslint-disable-next-line no-plusplus - if (i++ > 1000) return ZERO_BN; + if (i++ > 100) return ZERO_BN; // if (i++ > 100) throw 'Not converging' zIn = new Decimal( @@ -762,9 +762,8 @@ export function fyTokenForMint( } } - console.log( 'yOut : ', yOut.floor().toFixed()); - - console.log( 'buyFyTOKEN : ', zIn.toString() ); +// console.log( 'yOut : ', yOut.toFixed()); +// console.log( 'buyFyTOKEN: ', zIn.toString() ); return decimal18ToDecimalN( // (converted back to original decimals) From 40dba0cf267349b55de4077a3214b43dca472ae3 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Mon, 17 Jan 2022 15:18:14 +0000 Subject: [PATCH 09/19] use slippage in poolHelper --- src/hooks/viewHelperHooks/usePoolHelpers.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/hooks/viewHelperHooks/usePoolHelpers.ts b/src/hooks/viewHelperHooks/usePoolHelpers.ts index 3f33419f1..fadfb5ced 100644 --- a/src/hooks/viewHelperHooks/usePoolHelpers.ts +++ b/src/hooks/viewHelperHooks/usePoolHelpers.ts @@ -10,6 +10,7 @@ import { maxFyTokenOut, burnFromStrategy, burn, + calculateSlippage, } from '../../utils/yieldMath'; import { SettingsContext } from '../../contexts/SettingsContext'; @@ -124,7 +125,7 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b strategySeries.baseReserves, strategySeries.fyTokenRealReserves, strategySeries.fyTokenReserves, - _input, + calculateSlippage(_input, slippageTolerance, true), strategySeries.getTimeTillMaturity(), strategySeries.ts, strategySeries.g1, From ecb797a2b734b0946d16b498d06f5bb964efbffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Cuesta=20Ca=C3=B1ada?= <mail@albertocuesta.es> Date: Tue, 18 Jan 2022 12:51:40 +0000 Subject: [PATCH 10/19] draft: fyTokenForMint that looks at surplus --- src/utils/yieldMath.ts | 78 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index fe0a9ca10..4c9bf2b33 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -261,7 +261,6 @@ export function mintWithBase( baseReserves: BigNumber | string, fyTokenReservesVirtual: BigNumber | string, fyTokenReservesReal: BigNumber | string, - supply: BigNumber | string, fyToken: BigNumber | string, timeTillMaturity: BigNumber | string, ts: BigNumber | string, @@ -270,7 +269,7 @@ export function mintWithBase( ): [BigNumber, BigNumber] { const Z = new Decimal(baseReserves.toString()); const YR = new Decimal(fyTokenReservesReal.toString()); - const S = new Decimal(supply.toString()); + const supply = fyTokenReservesVirtual.sub(fyTokenReservesReal); const y = new Decimal(fyToken.toString()); // buyFyToken: const z1 = new Decimal( @@ -302,13 +301,13 @@ export function burnForBase( baseReserves: BigNumber, fyTokenReservesVirtual: BigNumber, fyTokenReservesReal: BigNumber, - supply: BigNumber, lpTokens: BigNumber, timeTillMaturity: BigNumber | string, ts: BigNumber | string, g2: BigNumber | string, decimals: number ): BigNumber { + const supply = fyTokenReservesVirtual.sub(fyTokenReservesReal); // Burn FyToken const [z1, y] = burn(baseReserves, fyTokenReservesReal, supply, lpTokens); // Sell FyToken for base @@ -689,7 +688,80 @@ export function maxFyTokenOut( return decimal18ToDecimalN(toBn(safeRes), decimals); } +/** + * Calculate the amount of fyToken that should be bought when providing liquidity with only underlying. + * The amount bought leaves a bit of unused underlying, to allow for the pool reserves to change between + * the calculation and the mint. The pool returns any unused underlying. + * + * @param baseReserves + * @param fyTokenRealReserves + * @param fyTokenVirtualReserves + * @param base + * @param timeTillMaturity + * @param ts + * @param g1 + * @param decimals + * @param slippage How far from the optimum we want to be + * @param precision How wide the range in which we will accept a value + * @returns fyTokenToBuy, surplus + */ export function fyTokenForMint( + baseReserves: BigNumber | string, + fyTokenRealReserves: BigNumber | string, + fyTokenVirtualReserves: BigNumber | string, + base: BigNumber | string, + timeTillMaturity: BigNumber | string, + ts: BigNumber | string, + g1: BigNumber | string, + decimals: number, + slippage: number = 0.01, // 1% default + precision: number = 0.0001 // 0.01% default +): [BigNumber, BigNumber] { + const minSurplus = base.mul(slippage) + const maxSurplus = minSurplus.add(base.mul(precision)) + let maxFYToken = maxFyTokenOut( + baseReserves, + fyTokenRealReserves, + timeTillMaturity, + ts, + g1, + decimals + ) + let minFYToken = ZERO_BN + + let i = 0; + while (true) { + /* NB return ZERO when not converging > not mintable */ + // eslint-disable-next-line no-plusplus + if (i++ > 100) return ZERO_BN; + // if (i++ > 100) throw 'Not converging' + + const fyTokenToBuy = (minFYToken.add(maxFYToken)).div(2) + + const baseIn = mintWithBase( + baseReserves, + fyTokenVirtualReserves, + fyTokenRealReserves, + fyTokenToBuy, + timeTillMaturity, + ts, + g1, + decimals + )[1] + const surplus = base.sub(baseIn) + + // Just right + if (minSurplus < surplus && surplus < maxSurplus) return [fyTokenToBuy, surplus] + + // Bought too much, lower the max and the buy + if (baseIn > base || surplus < minSurplus) maxFYToken = fyTokenToBuy + + // Bought too little, raise the min and the buy + if (surplus > maxSurplus) minFYToken = fyTokenToBuy + } +} + +export function fyTokenForMintOld( baseReserves: BigNumber | string, fyTokenRealReserves: BigNumber | string, fyTokenVirtualReserves: BigNumber | string, From 62e98f0d57755c25f6a1991ff8b55e90828acdd9 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Tue, 18 Jan 2022 14:19:06 +0000 Subject: [PATCH 11/19] handle fyToken for mint returning array --- src/hooks/actionHooks/useAddLiquidity.ts | 3 +-- src/hooks/viewHelperHooks/usePoolHelpers.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/hooks/actionHooks/useAddLiquidity.ts b/src/hooks/actionHooks/useAddLiquidity.ts index ddcf15739..d125fa2fa 100644 --- a/src/hooks/actionHooks/useAddLiquidity.ts +++ b/src/hooks/actionHooks/useAddLiquidity.ts @@ -64,7 +64,7 @@ export const useAddLiquidity = () => { const [cachedBaseReserves, cachedFyTokenReserves] = await series?.poolContract.getCache()!; const cachedRealReserves = cachedFyTokenReserves.sub(series?.totalSupply!); - const _fyTokenToBeMinted = fyTokenForMint( + const [_fyTokenToBeMinted, ] = fyTokenForMint( cachedBaseReserves, cachedRealReserves, cachedFyTokenReserves, @@ -93,7 +93,6 @@ export const useAddLiquidity = () => { const alreadyApproved = (await base.getAllowance(account!, ladleAddress)).gt(_input); /* DIAGNOSITCS */ - console.log( 'input: ', _input.toString(), diff --git a/src/hooks/viewHelperHooks/usePoolHelpers.ts b/src/hooks/viewHelperHooks/usePoolHelpers.ts index 05725460d..6d56bda2e 100644 --- a/src/hooks/viewHelperHooks/usePoolHelpers.ts +++ b/src/hooks/viewHelperHooks/usePoolHelpers.ts @@ -120,7 +120,7 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b strategySeries.decimals ); - _fyTokenToBuy = fyTokenForMint( + [_fyTokenToBuy, ] = fyTokenForMint( strategySeries.baseReserves, strategySeries.fyTokenRealReserves, strategySeries.fyTokenReserves, From d2650ce9ef53e9557c71769f2c0d74e8a6b358a7 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Tue, 18 Jan 2022 14:33:24 +0000 Subject: [PATCH 12/19] conversion to decimals --- src/utils/yieldMath.ts | 56 ++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index 4c9bf2b33..7c9491e86 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -245,10 +245,9 @@ export function burnFromStrategy( } /** - * @param { BigNumber | string } baseReserves - * @param { BigNumber | string } fyTokenReservesVirtual - * @param { BigNumber | string } fyTokenReservesReal - * @param { BigNumber | string } totalSupply + * @param { BigNumber } baseReserves + * @param { BigNumber } fyTokenReservesVirtual + * @param { BigNumber } fyTokenReservesReal * @param { BigNumber | string } fyToken * @param { BigNumber | string } timeTillMaturity * @param { BigNumber | string } ts @@ -258,9 +257,9 @@ export function burnFromStrategy( * @returns {[BigNumber, BigNumber]} */ export function mintWithBase( - baseReserves: BigNumber | string, - fyTokenReservesVirtual: BigNumber | string, - fyTokenReservesReal: BigNumber | string, + baseReserves: BigNumber, + fyTokenReservesVirtual: BigNumber, + fyTokenReservesReal: BigNumber, fyToken: BigNumber | string, timeTillMaturity: BigNumber | string, ts: BigNumber | string, @@ -269,7 +268,7 @@ export function mintWithBase( ): [BigNumber, BigNumber] { const Z = new Decimal(baseReserves.toString()); const YR = new Decimal(fyTokenReservesReal.toString()); - const supply = fyTokenReservesVirtual.sub(fyTokenReservesReal); + const supply = fyTokenReservesVirtual.sub( fyTokenReservesReal ); const y = new Decimal(fyToken.toString()); // buyFyToken: const z1 = new Decimal( @@ -666,6 +665,7 @@ export function maxFyTokenOut( g1: BigNumber | string, decimals: number ): BigNumber { + /* convert to 18 decimals, if required */ const baseReserves18 = decimalNToDecimal18(BigNumber.from(baseReserves), decimals); const fyTokenReserves18 = decimalNToDecimal18(BigNumber.from(fyTokenReserves), decimals); @@ -706,9 +706,9 @@ export function maxFyTokenOut( * @returns fyTokenToBuy, surplus */ export function fyTokenForMint( - baseReserves: BigNumber | string, - fyTokenRealReserves: BigNumber | string, - fyTokenVirtualReserves: BigNumber | string, + baseReserves: BigNumber, + fyTokenRealReserves: BigNumber, + fyTokenVirtualReserves: BigNumber, base: BigNumber | string, timeTillMaturity: BigNumber | string, ts: BigNumber | string, @@ -717,41 +717,48 @@ export function fyTokenForMint( slippage: number = 0.01, // 1% default precision: number = 0.0001 // 0.01% default ): [BigNumber, BigNumber] { - const minSurplus = base.mul(slippage) - const maxSurplus = minSurplus.add(base.mul(precision)) - let maxFYToken = maxFyTokenOut( + + const base_ = new Decimal(base.toString()); + const minSurplus = base_.mul(slippage) + const maxSurplus = minSurplus.add(base_.mul(precision)) + + console.log( base_.toString(), minSurplus.toString(), maxSurplus.toString()) + + let maxFYToken = new Decimal( maxFyTokenOut( baseReserves, fyTokenRealReserves, timeTillMaturity, ts, g1, decimals - ) - let minFYToken = ZERO_BN + ).toString() ) + + let minFYToken = ZERO_DEC + console.log('maxFyToken :', maxFYToken.toString()) let i = 0; while (true) { /* NB return ZERO when not converging > not mintable */ // eslint-disable-next-line no-plusplus - if (i++ > 100) return ZERO_BN; - // if (i++ > 100) throw 'Not converging' - + if (i++ > 100) return [ ZERO_BN, ZERO_BN ]; const fyTokenToBuy = (minFYToken.add(maxFYToken)).div(2) - + const baseIn = mintWithBase( baseReserves, fyTokenVirtualReserves, fyTokenRealReserves, - fyTokenToBuy, + toBn(fyTokenToBuy), timeTillMaturity, ts, g1, decimals - )[1] - const surplus = base.sub(baseIn) + )[1]; + const surplus = base_.sub( new Decimal( baseIn.toString())) // Just right - if (minSurplus < surplus && surplus < maxSurplus) return [fyTokenToBuy, surplus] + if (minSurplus < surplus && surplus < maxSurplus) console.log('fyToken to buy: ', fyTokenToBuy.toString(), 'surplus: ', surplus.toString()); + + if (minSurplus < surplus && surplus < maxSurplus) return [toBn(fyTokenToBuy), BigNumber.from(surplus) ] // Bought too much, lower the max and the buy if (baseIn > base || surplus < minSurplus) maxFYToken = fyTokenToBuy @@ -761,6 +768,7 @@ export function fyTokenForMint( } } + export function fyTokenForMintOld( baseReserves: BigNumber | string, fyTokenRealReserves: BigNumber | string, From 9a5aa6132009cd3fda592ecaeadb48e92d6df78a Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Wed, 19 Jan 2022 14:07:30 +0000 Subject: [PATCH 13/19] re-include slippage tolerance --- src/hooks/actionHooks/useAddLiquidity.ts | 2 +- src/hooks/viewHelperHooks/usePoolHelpers.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hooks/actionHooks/useAddLiquidity.ts b/src/hooks/actionHooks/useAddLiquidity.ts index f57915be6..cc4c26aa7 100644 --- a/src/hooks/actionHooks/useAddLiquidity.ts +++ b/src/hooks/actionHooks/useAddLiquidity.ts @@ -73,7 +73,7 @@ export const useAddLiquidity = () => { series.ts, series.g1, series.decimals, - // slippageTolerance + slippageTolerance ); console.log(cachedBaseReserves.toString(), cachedRealReserves.toString()) diff --git a/src/hooks/viewHelperHooks/usePoolHelpers.ts b/src/hooks/viewHelperHooks/usePoolHelpers.ts index 66d92fb87..e6a5a7a6e 100644 --- a/src/hooks/viewHelperHooks/usePoolHelpers.ts +++ b/src/hooks/viewHelperHooks/usePoolHelpers.ts @@ -130,7 +130,7 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b strategySeries.ts, strategySeries.g1, strategySeries.decimals, - // slippageTolerance + slippageTolerance ); /* Check if buy and pool option is allowed */ From 9193a2e49e6bc2ee37a9a73cc2a545c7377efb0a Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Wed, 19 Jan 2022 14:09:18 +0000 Subject: [PATCH 14/19] working fyTokenForMint algo --- src/utils/yieldMath.ts | 138 +++++++++++++++++++++-------------------- 1 file changed, 71 insertions(+), 67 deletions(-) diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index 11e4ae31a..839b8f6c2 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -28,8 +28,6 @@ const g1_default = new Decimal(950 / 1000).mul(2 ** 64); const g2_default = new Decimal(1000 / 950).mul(2 ** 64); const precisionFee = new Decimal(1000000000000); -// console.log(g1_default.div(2 ** 64).toString()); -// console.log(g2_default.div(2 ** 64).toString()); /** ************************* Support functions @@ -150,7 +148,6 @@ const _computeA = ( return [a, invA]; /* returns a and inverse of a */ }; - const _computeB = ( timeToMaturity: BigNumber | string, ts: BigNumber | string, @@ -267,9 +264,10 @@ export function mintWithBase( g1: BigNumber | string, decimals: number ): [BigNumber, BigNumber] { + const Z = new Decimal(baseReserves.toString()); const YR = new Decimal(fyTokenReservesReal.toString()); - const supply = fyTokenReservesVirtual.sub( fyTokenReservesReal ); + const supply = fyTokenReservesVirtual.sub(fyTokenReservesReal); const y = new Decimal(fyToken.toString()); // buyFyToken: const z1 = new Decimal( @@ -279,9 +277,9 @@ export function mintWithBase( const YR2 = YR.sub(y); // FYToken reserves after the trade // Mint specifying how much fyToken to take in. Reverse of `mint`. - const [m, z2] = mint(Z2.floor().toFixed(), YR2.floor().toFixed(), supply, fyToken, false); + const [minted, z2] = mint( toBn(Z2), toBn(YR2), supply, fyToken, false); - return [m, toBn(z1).add(z2)]; + return [minted, toBn(z1).add(z2)]; } /** @@ -506,8 +504,8 @@ export function buyFYToken( * */ export function maxBaseIn( - baseReserves: BigNumber | string, - fyTokenReserves: BigNumber | string, + baseReserves: BigNumber, + fyTokenReserves: BigNumber, timeTillMaturity: BigNumber | string, ts: BigNumber | string, g1: BigNumber | string, @@ -517,9 +515,9 @@ export function maxBaseIn( const fyTokenAmountOut = maxFyTokenOut(baseReserves, fyTokenReserves, timeTillMaturity, ts, g1, decimals); /* convert to 18 decimals, if required */ - const baseReserves18 = decimalNToDecimal18(BigNumber.from(baseReserves), decimals); - const fyTokenReserves18 = decimalNToDecimal18(BigNumber.from(fyTokenReserves), decimals); - const fyTokenAmountOut18 = decimalNToDecimal18(BigNumber.from(fyTokenAmountOut), decimals); + const baseReserves18 = decimalNToDecimal18(baseReserves, decimals); + const fyTokenReserves18 = decimalNToDecimal18(fyTokenReserves, decimals); + const fyTokenAmountOut18 = decimalNToDecimal18(fyTokenAmountOut, decimals); const baseReserves_ = new Decimal(baseReserves18.toString()); const fyTokenReserves_ = new Decimal(fyTokenReserves18.toString()); @@ -564,8 +562,8 @@ export function maxBaseIn( * */ export function maxBaseOut( - baseReserves: BigNumber | string, - fyTokenReserves: BigNumber | string, + baseReserves: BigNumber, + fyTokenReserves: BigNumber, timeTillMaturity: BigNumber | string, ts: BigNumber | string, g2: BigNumber | string, @@ -575,9 +573,9 @@ export function maxBaseOut( const fyTokenAmountIn = maxFyTokenIn(baseReserves, fyTokenReserves, timeTillMaturity, ts, g2, decimals); /* convert to 18 decimals, if required */ - const baseReserves18 = decimalNToDecimal18(BigNumber.from(baseReserves), decimals); - const fyTokenReserves18 = decimalNToDecimal18(BigNumber.from(fyTokenReserves), decimals); - const fyTokenAmountIn18 = decimalNToDecimal18(BigNumber.from(fyTokenAmountIn), decimals); + const baseReserves18 = decimalNToDecimal18(baseReserves, decimals); + const fyTokenReserves18 = decimalNToDecimal18(fyTokenReserves, decimals); + const fyTokenAmountIn18 = decimalNToDecimal18(fyTokenAmountIn, decimals); const baseReserves_ = new Decimal(baseReserves18.toString()); const fyTokenReserves_ = new Decimal(fyTokenReserves18.toString()); @@ -617,16 +615,16 @@ export function maxBaseOut( * @returns { BigNumber } */ export function maxFyTokenIn( - baseReserves: BigNumber | string, - fyTokenReserves: BigNumber | string, + baseReserves: BigNumber, + fyTokenReserves: BigNumber, timeTillMaturity: BigNumber | string, ts: BigNumber | string, g2: BigNumber | string, decimals: number ): BigNumber { /* convert to 18 decimals, if required */ - const baseReserves18 = decimalNToDecimal18(BigNumber.from(baseReserves), decimals); - const fyTokenReserves18 = decimalNToDecimal18(BigNumber.from(fyTokenReserves), decimals); + const baseReserves18 = decimalNToDecimal18(baseReserves, decimals); + const fyTokenReserves18 = decimalNToDecimal18(fyTokenReserves, decimals); const baseReserves_ = new Decimal(baseReserves18.toString()); const fyTokenReserves_ = new Decimal(fyTokenReserves18.toString()); @@ -659,27 +657,28 @@ export function maxFyTokenIn( * @returns { BigNumber } */ export function maxFyTokenOut( - baseReserves: BigNumber | string, - fyTokenReserves: BigNumber | string, + baseReserves: BigNumber, + fyTokenReserves: BigNumber, timeTillMaturity: BigNumber | string, ts: BigNumber | string, g1: BigNumber | string, - decimals: number + decimals: number, ): BigNumber { - /* convert to 18 decimals, if required */ - const baseReserves18 = decimalNToDecimal18(BigNumber.from(baseReserves), decimals); - const fyTokenReserves18 = decimalNToDecimal18(BigNumber.from(fyTokenReserves), decimals); + const baseReserves18 = decimalNToDecimal18(baseReserves, decimals); + const fyTokenReserves18 = decimalNToDecimal18(fyTokenReserves, decimals); + + /* convert to decimal for the math */ const baseReserves_ = new Decimal(baseReserves18.toString()); const fyTokenReserves_ = new Decimal(fyTokenReserves18.toString()); const [a, invA] = _computeA(timeTillMaturity, ts, g1); const xa = baseReserves_.pow(a); - const ya = fyTokenReserves_.pow(a); + const ya = fyTokenReserves_.pow(a) const xy = xa.add(ya); - const inaccessible = xy.div(2).pow(invA); + const inaccessible = (xy.div(2)).pow(invA); const res = inaccessible.gt(fyTokenReserves_) ? ZERO : fyTokenReserves_.sub(inaccessible); /* Handle precision variations */ @@ -693,19 +692,20 @@ export function maxFyTokenOut( * Calculate the amount of fyToken that should be bought when providing liquidity with only underlying. * The amount bought leaves a bit of unused underlying, to allow for the pool reserves to change between * the calculation and the mint. The pool returns any unused underlying. - * - * @param baseReserves - * @param fyTokenRealReserves - * @param fyTokenVirtualReserves - * @param base - * @param timeTillMaturity - * @param ts - * @param g1 - * @param decimals + * + * @param baseReserves + * @param fyTokenRealReserves + * @param fyTokenVirtualReserves + * @param base + * @param timeTillMaturity + * @param ts + * @param g1 + * @param decimals * @param slippage How far from the optimum we want to be * @param precision How wide the range in which we will accept a value * @returns fyTokenToBuy, surplus */ + export function fyTokenForMint( baseReserves: BigNumber, fyTokenRealReserves: BigNumber, @@ -720,29 +720,25 @@ export function fyTokenForMint( ): [BigNumber, BigNumber] { const base_ = new Decimal(base.toString()); - const minSurplus = base_.mul(slippage) - const maxSurplus = minSurplus.add(base_.mul(precision)) + const minSurplus = base_.mul(slippage); + const maxSurplus = minSurplus.add(base_.mul(precision)); - console.log( base_.toString(), minSurplus.toString(), maxSurplus.toString()) - - let maxFYToken = new Decimal( maxFyTokenOut( - baseReserves, - fyTokenRealReserves, - timeTillMaturity, - ts, - g1, - decimals - ).toString() ) - - let minFYToken = ZERO_DEC - console.log('maxFyToken :', maxFYToken.toString()) + let maxFYToken = new Decimal( + maxFyTokenOut(baseReserves, fyTokenVirtualReserves, timeTillMaturity, ts, g1, decimals).toString() + ); + let minFYToken = ZERO_DEC; let i = 0; while (true) { /* NB return ZERO when not converging > not mintable */ // eslint-disable-next-line no-plusplus - if (i++ > 100) return [ ZERO_BN, ZERO_BN ]; - const fyTokenToBuy = (minFYToken.add(maxFYToken)).div(2) + if (i++ > 100) { + console.log('No solution') + return [ZERO_BN, ZERO_BN] + }; + + const fyTokenToBuy = minFYToken.add(maxFYToken).div(2); + // console.log('fyToken tobuy', fyTokenToBuy.toFixed() ) const baseIn = mintWithBase( baseReserves, @@ -754,22 +750,30 @@ export function fyTokenForMint( g1, decimals )[1]; - const surplus = base_.sub( new Decimal( baseIn.toString())) - + + const surplus = base_.sub(new Decimal(baseIn.toString())); + // console.log( 'min:', minSurplus.toFixed() , 'max:', maxSurplus.toFixed() , 'surplus: ', surplus.toFixed() ) + // Just right - if (minSurplus < surplus && surplus < maxSurplus) console.log('fyToken to buy: ', fyTokenToBuy.toString(), 'surplus: ', surplus.toString()); - - if (minSurplus < surplus && surplus < maxSurplus) return [toBn(fyTokenToBuy), BigNumber.from(surplus) ] - + if (minSurplus.lt(surplus) && surplus.lt(maxSurplus)) { + // console.log('fyToken to buy: ', fyTokenToBuy.toFixed(), 'surplus: ', surplus.toFixed()); + return [ toBn(fyTokenToBuy), toBn(surplus) ]; + } + // Bought too much, lower the max and the buy - if (baseIn > base || surplus < minSurplus) maxFYToken = fyTokenToBuy - + if ( baseIn.gt(base) || surplus.lt(minSurplus) ) { + // console.log('Bought too much'); + maxFYToken = fyTokenToBuy; + } + // Bought too little, raise the min and the buy - if (surplus > maxSurplus) minFYToken = fyTokenToBuy + if (surplus.gt(maxSurplus)) { + // console.log('Bought too little'); + minFYToken = fyTokenToBuy; + } } } - export function fyTokenForMintOld( baseReserves: BigNumber | string, fyTokenRealReserves: BigNumber | string, @@ -791,7 +795,7 @@ export function fyTokenForMintOld( const fyDaiRealReserves_ = new Decimal(fyTokenRealReserves18.toString()); const base_ = new Decimal(base18.toString()); const timeTillMaturity_ = new Decimal(timeTillMaturity.toString()); - const slippage_ = new Decimal(slippage) // .mul(new Decimal(10)); /* multiply the user slippage by 10 */ + const slippage_ = new Decimal(slippage); // .mul(new Decimal(10)); /* multiply the user slippage by 10 */ let min = ZERO; let max = base_.mul(TWO); @@ -842,8 +846,8 @@ export function fyTokenForMintOld( } } -// console.log( 'yOut : ', yOut.toFixed()); -// console.log( 'buyFyTOKEN: ', zIn.toString() ); + // console.log( 'yOut : ', yOut.toFixed()); + // console.log( 'buyFyTOKEN: ', zIn.toString() ); return decimal18ToDecimalN( // (converted back to original decimals) From 231070443708b59e2d2a0699a953f873b4738fdb Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 19 Jan 2022 10:18:14 -0500 Subject: [PATCH 15/19] add settings context type --- src/hooks/actionHooks/useAddLiquidity.ts | 63 ++++++++++++------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/src/hooks/actionHooks/useAddLiquidity.ts b/src/hooks/actionHooks/useAddLiquidity.ts index cc4c26aa7..26772858f 100644 --- a/src/hooks/actionHooks/useAddLiquidity.ts +++ b/src/hooks/actionHooks/useAddLiquidity.ts @@ -14,6 +14,7 @@ import { IUserContext, IUserContextActions, IUserContextState, + ISettingsContext, } from '../../types'; import { cleanValue, getTxCode } from '../../utils/appUtils'; import { BLANK_VAULT } from '../../utils/constants'; @@ -27,12 +28,12 @@ import { ChainContext } from '../../contexts/ChainContext'; export const useAddLiquidity = () => { const { settingsState: { slippageTolerance, diagnostics, approveMax }, - } = useContext(SettingsContext); + } = useContext(SettingsContext) as ISettingsContext; const { chainState: { contractMap }, } = useContext(ChainContext); - const { userState, userActions }: { userState: IUserContextState; userActions: IUserContextActions } = useContext( + const { userState, userActions }: { userState: IUserContextState; userActions: IUserContextActions } = useContext( UserContext ) as IUserContext; const { activeAccount: account, assetMap, seriesMap } = userState; @@ -59,12 +60,12 @@ export const useAddLiquidity = () => { const cleanInput = cleanValue(input, base?.decimals!); const _input = ethers.utils.parseUnits(cleanInput, base?.decimals); - const _inputLessSlippage = calculateSlippage(_input, slippageTolerance, true); + const _inputLessSlippage = calculateSlippage(_input, slippageTolerance.toString(), true); const [cachedBaseReserves, cachedFyTokenReserves] = await series?.poolContract.getCache()!; const cachedRealReserves = cachedFyTokenReserves.sub(series?.totalSupply!); - const [_fyTokenToBeMinted, ] = fyTokenForMint( + const [_fyTokenToBeMinted] = fyTokenForMint( cachedBaseReserves, cachedRealReserves, cachedFyTokenReserves, @@ -76,7 +77,7 @@ export const useAddLiquidity = () => { slippageTolerance ); - console.log(cachedBaseReserves.toString(), cachedRealReserves.toString()) + console.log(cachedBaseReserves.toString(), cachedRealReserves.toString()); const [minRatio, maxRatio] = calcPoolRatios(cachedBaseReserves, cachedRealReserves); const [_baseToPool, _baseToFyToken] = splitLiquidity( @@ -86,36 +87,36 @@ export const useAddLiquidity = () => { true ) as [BigNumber, BigNumber]; - const _baseToPoolWithSlippage = BigNumber.from(calculateSlippage(_baseToPool, slippageTolerance)); + const _baseToPoolWithSlippage = BigNumber.from(calculateSlippage(_baseToPool, slippageTolerance.toString())); /* if approveMAx, check if signature is still required */ const alreadyApproved = (await base.getAllowance(account!, ladleAddress)).gt(_input); /* DIAGNOSITCS */ - console.log( - 'input: ', - _input.toString(), - 'inputLessSlippage: ', - _inputLessSlippage.toString(), - 'base: ', - cachedBaseReserves.toString(), - 'real: ', - cachedRealReserves.toString(), - 'virtual: ', - cachedFyTokenReserves.toString(), - '>> baseSplit: ', - _baseToPool.toString(), - '>> fyTokenSplit: ', - _baseToFyToken.toString(), - '>> baseSplitWithSlippage: ', - _baseToPoolWithSlippage.toString(), - '>> minRatio', - minRatio.toString(), - '>> maxRatio', - maxRatio.toString(), - 'matching vault id', - matchingVaultId - ); + console.log( + 'input: ', + _input.toString(), + 'inputLessSlippage: ', + _inputLessSlippage.toString(), + 'base: ', + cachedBaseReserves.toString(), + 'real: ', + cachedRealReserves.toString(), + 'virtual: ', + cachedFyTokenReserves.toString(), + '>> baseSplit: ', + _baseToPool.toString(), + '>> fyTokenSplit: ', + _baseToFyToken.toString(), + '>> baseSplitWithSlippage: ', + _baseToPoolWithSlippage.toString(), + '>> minRatio', + minRatio.toString(), + '>> maxRatio', + maxRatio.toString(), + 'matching vault id', + matchingVaultId + ); /** * GET SIGNTURE/APPROVAL DATA @@ -126,7 +127,7 @@ export const useAddLiquidity = () => { target: base, spender: 'LADLE', amount: _input, - ignoreIf: alreadyApproved===true, + ignoreIf: alreadyApproved === true, }, ], txCode From f7ba2fa4c756a2182cabc7fb1f6c0baab32f93a9 Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 19 Jan 2022 10:25:14 -0500 Subject: [PATCH 16/19] fix constricted strategy item --- src/views/Pool.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/views/Pool.tsx b/src/views/Pool.tsx index cf387e637..6fea36e8a 100644 --- a/src/views/Pool.tsx +++ b/src/views/Pool.tsx @@ -249,7 +249,7 @@ function Pool() { {stepPosition === 1 && poolProcess?.stage === ProcessStage.PROCESS_COMPLETE && poolProcess?.tx.status === TxState.SUCCESSFUL && ( - <Box pad="large" gap="small"> + <Box pad="small" gap="small" height="auto"> <Text size="small"> View strategy Position: </Text> <StrategyItem strategy={strategyMap.get(selectedStrategy?.id!)!} index={0} condensed /> </Box> From cffa0e43a6b7a42e927d860df960daaeb10f9a94 Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 19 Jan 2022 10:26:52 -0500 Subject: [PATCH 17/19] add settings context type --- src/hooks/viewHelperHooks/usePoolHelpers.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/hooks/viewHelperHooks/usePoolHelpers.ts b/src/hooks/viewHelperHooks/usePoolHelpers.ts index e6a5a7a6e..043527e31 100644 --- a/src/hooks/viewHelperHooks/usePoolHelpers.ts +++ b/src/hooks/viewHelperHooks/usePoolHelpers.ts @@ -1,7 +1,7 @@ import { useContext, useEffect, useState } from 'react'; import { ethers, BigNumber } from 'ethers'; import { UserContext } from '../../contexts/UserContext'; -import { IAsset, ISeries, IStrategy, IVault } from '../../types'; +import { IAsset, ISeries, ISettingsContext, IStrategy, IVault } from '../../types'; import { cleanValue } from '../../utils/appUtils'; import { fyTokenForMint, @@ -18,7 +18,7 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b /* STATE FROM CONTEXT */ const { settingsState: { slippageTolerance, diagnostics }, - } = useContext(SettingsContext); + } = useContext(SettingsContext) as ISettingsContext; const { userState: { selectedSeries, selectedBase, selectedStrategy, seriesMap, vaultMap, assetMap, activeAccount }, @@ -121,11 +121,11 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b strategySeries.decimals ); - [_fyTokenToBuy, ] = fyTokenForMint( + [_fyTokenToBuy] = fyTokenForMint( strategySeries.baseReserves, strategySeries.fyTokenRealReserves, strategySeries.fyTokenReserves, - calculateSlippage(_input, slippageTolerance, true), + calculateSlippage(_input, slippageTolerance.toString(), true), strategySeries.getTimeTillMaturity(), strategySeries.ts, strategySeries.g1, @@ -218,7 +218,6 @@ export const usePoolHelpers = (input: string | undefined, removeLiquidityView: b setRemoveBaseReceived_(ethers.utils.formatUnits(_baseVal, strategySeries.decimals)); setRemoveFyTokenReceived(_fyTokenVal); setRemoveFyTokenReceived_(ethers.utils.formatUnits(_fyTokenVal, strategySeries.decimals)); - } else { /* CASE> fytokenReceived less than debt : USE REMOVE OPTION 1 */ diagnostics && From 1417482695c40f28e74c314b8f7259b824f6409c Mon Sep 17 00:00:00 2001 From: marcomariscal <marco.a.mariscal@gmail.com> Date: Wed, 19 Jan 2022 10:27:26 -0500 Subject: [PATCH 18/19] formatting --- src/utils/yieldMath.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/utils/yieldMath.ts b/src/utils/yieldMath.ts index 839b8f6c2..65082be44 100644 --- a/src/utils/yieldMath.ts +++ b/src/utils/yieldMath.ts @@ -28,7 +28,6 @@ const g1_default = new Decimal(950 / 1000).mul(2 ** 64); const g2_default = new Decimal(1000 / 950).mul(2 ** 64); const precisionFee = new Decimal(1000000000000); - /** ************************* Support functions *************************** */ @@ -264,7 +263,6 @@ export function mintWithBase( g1: BigNumber | string, decimals: number ): [BigNumber, BigNumber] { - const Z = new Decimal(baseReserves.toString()); const YR = new Decimal(fyTokenReservesReal.toString()); const supply = fyTokenReservesVirtual.sub(fyTokenReservesReal); @@ -277,7 +275,7 @@ export function mintWithBase( const YR2 = YR.sub(y); // FYToken reserves after the trade // Mint specifying how much fyToken to take in. Reverse of `mint`. - const [minted, z2] = mint( toBn(Z2), toBn(YR2), supply, fyToken, false); + const [minted, z2] = mint(toBn(Z2), toBn(YR2), supply, fyToken, false); return [minted, toBn(z1).add(z2)]; } @@ -662,12 +660,12 @@ export function maxFyTokenOut( timeTillMaturity: BigNumber | string, ts: BigNumber | string, g1: BigNumber | string, - decimals: number, + decimals: number ): BigNumber { /* convert to 18 decimals, if required */ const baseReserves18 = decimalNToDecimal18(baseReserves, decimals); const fyTokenReserves18 = decimalNToDecimal18(fyTokenReserves, decimals); - + /* convert to decimal for the math */ const baseReserves_ = new Decimal(baseReserves18.toString()); const fyTokenReserves_ = new Decimal(fyTokenReserves18.toString()); @@ -675,10 +673,10 @@ export function maxFyTokenOut( const [a, invA] = _computeA(timeTillMaturity, ts, g1); const xa = baseReserves_.pow(a); - const ya = fyTokenReserves_.pow(a) + const ya = fyTokenReserves_.pow(a); const xy = xa.add(ya); - const inaccessible = (xy.div(2)).pow(invA); + const inaccessible = xy.div(2).pow(invA); const res = inaccessible.gt(fyTokenReserves_) ? ZERO : fyTokenReserves_.sub(inaccessible); /* Handle precision variations */ @@ -718,7 +716,6 @@ export function fyTokenForMint( slippage: number = 0.01, // 1% default precision: number = 0.0001 // 0.01% default ): [BigNumber, BigNumber] { - const base_ = new Decimal(base.toString()); const minSurplus = base_.mul(slippage); const maxSurplus = minSurplus.add(base_.mul(precision)); @@ -732,10 +729,10 @@ export function fyTokenForMint( while (true) { /* NB return ZERO when not converging > not mintable */ // eslint-disable-next-line no-plusplus - if (i++ > 100) { - console.log('No solution') - return [ZERO_BN, ZERO_BN] - }; + if (i++ > 100) { + console.log('No solution'); + return [ZERO_BN, ZERO_BN]; + } const fyTokenToBuy = minFYToken.add(maxFYToken).div(2); // console.log('fyToken tobuy', fyTokenToBuy.toFixed() ) @@ -755,19 +752,19 @@ export function fyTokenForMint( // console.log( 'min:', minSurplus.toFixed() , 'max:', maxSurplus.toFixed() , 'surplus: ', surplus.toFixed() ) // Just right - if (minSurplus.lt(surplus) && surplus.lt(maxSurplus)) { + if (minSurplus.lt(surplus) && surplus.lt(maxSurplus)) { // console.log('fyToken to buy: ', fyTokenToBuy.toFixed(), 'surplus: ', surplus.toFixed()); - return [ toBn(fyTokenToBuy), toBn(surplus) ]; + return [toBn(fyTokenToBuy), toBn(surplus)]; } // Bought too much, lower the max and the buy - if ( baseIn.gt(base) || surplus.lt(minSurplus) ) { + if (baseIn.gt(base) || surplus.lt(minSurplus)) { // console.log('Bought too much'); maxFYToken = fyTokenToBuy; } // Bought too little, raise the min and the buy - if (surplus.gt(maxSurplus)) { + if (surplus.gt(maxSurplus)) { // console.log('Bought too little'); minFYToken = fyTokenToBuy; } From b75796fcbbd8fa2e046e3b731c91d903800cb456 Mon Sep 17 00:00:00 2001 From: brucedonovan <bruce.donovan@gmail.com> Date: Thu, 20 Jan 2022 12:43:28 +0000 Subject: [PATCH 19/19] version bump 2.0.62 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index a76d808f9..541ea95eb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "app-v2", - "version": "2.0.61", + "version": "2.0.62", "private": true, "dependencies": { "@multiavatar/multiavatar": "^1.0.6",