diff --git a/packages/nibijs/package.json b/packages/nibijs/package.json index b6fa09b4..430ea602 100644 --- a/packages/nibijs/package.json +++ b/packages/nibijs/package.json @@ -23,6 +23,7 @@ "@cosmjs/stargate": "^0.28.11", "@cosmjs/tendermint-rpc": "^0.28.11", "@nibiruchain/protojs": "^0.19.0", + "bignumber.js": "^9.1.1", "cross-fetch": "^3.1.5", "long": "^5.2.0" }, diff --git a/packages/nibijs/src/stableswap/stableswap.ts b/packages/nibijs/src/stableswap/stableswap.ts index 9c3f7efd..cb16295d 100644 --- a/packages/nibijs/src/stableswap/stableswap.ts +++ b/packages/nibijs/src/stableswap/stableswap.ts @@ -1,25 +1,6 @@ -/* global BigInt */ +import { BigNumber } from "bignumber.js" -/** - * bigIntExponentiation() takes a base bigint and an exponent bigint and does math similiar to `**` in JS - * BigInt does not support `**` until ES7 - * - * @param {bigint} base - * @param {bigint} exponent - */ -export const bigIntExponentiation = (base: bigint, exponent: bigint) => { - let result = BigInt(1) - - while (exponent > BigInt(0)) { - if (exponent % BigInt(2) === BigInt(1)) { - result *= base - } - base *= base - exponent /= BigInt(2) - } - - return result -} +BigNumber.set({ ROUNDING_MODE: BigNumber.ROUND_FLOOR }) /** * StableSwap contains the logic for exchanging tokens @@ -27,30 +8,30 @@ export const bigIntExponentiation = (base: bigint, exponent: bigint) => { * Based on: https://github.com/NibiruChain/nibiru/blob/master/contrib/scripts/testing/stableswap_model.py * * Constructor: - * @param {bigint} Amplification - * @param {bigint[]} totalTokenSupply - * @param {bigint[]} tokenPrices - * @param {bigint} fee + * @param {BigNumber} Amplification + * @param {BigNumber[]} totalTokenSupply + * @param {BigNumber[]} tokenPrices + * @param {BigNumber} fee * * @export * @class StableSwap */ export class StableSwap { - public Amplification: bigint - public totalTokenSupply: bigint[] - public totalTokensInPool: bigint - public tokenPrices: bigint[] - public fee: bigint + public Amplification: BigNumber + public totalTokenSupply: BigNumber[] + public totalTokensInPool: BigNumber + public tokenPrices: BigNumber[] + public fee: BigNumber constructor( - Amplification: bigint, - totalTokenSupply: bigint[], - tokenPrices: bigint[], - fee: bigint, + Amplification: BigNumber, + totalTokenSupply: BigNumber[], + tokenPrices: BigNumber[], + fee: BigNumber, ) { this.Amplification = Amplification this.totalTokenSupply = totalTokenSupply - this.totalTokensInPool = BigInt(totalTokenSupply.length) + this.totalTokensInPool = BigNumber(totalTokenSupply.length) this.tokenPrices = tokenPrices this.fee = fee } @@ -61,10 +42,10 @@ export class StableSwap { * @memberof StableSwap */ xp() { - return this.totalTokenSupply.map( - (x, i) => - (BigInt(x) * this.tokenPrices[i]) / - bigIntExponentiation(BigInt(10), BigInt(18)), + return this.totalTokenSupply.map((x, i) => + BigNumber(x) + .multipliedBy(this.tokenPrices[i]) + .dividedBy(BigNumber(10).exponentiatedBy(BigNumber(18))), ) } @@ -78,23 +59,34 @@ export class StableSwap { * @memberof StableSwap */ D() { - let Dprev = BigInt(0) + let Dprev = BigNumber(0) const xp = this.xp() - const S = xp.reduce((a, b) => a + b, BigInt(0)) + const S = xp.reduce((a, b) => a.plus(b), BigNumber(0)) let D = S - const Ann = - this.Amplification * - bigIntExponentiation(this.totalTokensInPool, this.totalTokensInPool) - while (Math.abs(Number((D - Dprev).toString())) > 1) { + + const Ann = this.Amplification.multipliedBy( + this.totalTokensInPool.exponentiatedBy(this.totalTokensInPool), + ) + + while (D.minus(Dprev).abs().isGreaterThan(BigNumber(1))) { let D_P = D + for (const x of xp) { - D_P = (D_P * D) / (this.totalTokensInPool * x) + D_P = D_P.multipliedBy(D).dividedBy(this.totalTokensInPool.multipliedBy(x)) } + Dprev = D - D = - ((Ann * S + D_P * this.totalTokensInPool) * D) / - ((Ann - BigInt(1)) * D + (this.totalTokensInPool + BigInt(1)) * D_P) + + D = Ann.multipliedBy(S) + .plus(D_P.multipliedBy(this.totalTokensInPool)) + .multipliedBy(D) + .dividedBy( + Ann.minus(BigNumber(1)) + .multipliedBy(D) + .plus(this.totalTokensInPool.plus(BigNumber(1)).multipliedBy(D_P)), + ) } + return D } @@ -111,53 +103,61 @@ export class StableSwap { * * @param {number} fromIndex * @param {number} toIndex - * @param {bigint} x + * @param {BigNumber} x * @memberof StableSwap */ - y(fromIndex: number, toIndex: number, x: bigint) { + y(fromIndex: number, toIndex: number, x: BigNumber) { const D = this.D() let xx = this.xp() xx[fromIndex] = x xx = xx.filter((_, idx) => idx !== toIndex) - const Ann = - this.Amplification * - bigIntExponentiation(this.totalTokensInPool, this.totalTokensInPool) + const Ann = this.Amplification.multipliedBy( + this.totalTokensInPool.exponentiatedBy(this.totalTokensInPool), + ) let c = D for (const y of xx) { - c = (c * D) / (y * this.totalTokensInPool) + c = c.multipliedBy(D).dividedBy(y.multipliedBy(this.totalTokensInPool)) } - c = (c * D) / (this.totalTokensInPool * Ann) - const b = xx.reduce((a, b) => a + b, BigInt(0)) + D / Ann - D - let yPrev = BigInt(0) + c = c.multipliedBy(D).dividedBy(this.totalTokensInPool.multipliedBy(Ann)) + const b = xx + .reduce((a, b) => a.plus(b), BigNumber(0)) + .plus(D.dividedBy(Ann)) + .minus(D) + let yPrev = BigNumber(0) let yVal = D - while (Math.abs(Number(yVal - yPrev)) > 1) { + while (yVal.minus(yPrev).abs().isGreaterThan(BigNumber(1))) { yPrev = yVal - yVal = (bigIntExponentiation(yVal, BigInt(2)) + c) / (BigInt(2) * yVal + b) + yVal = yVal + .exponentiatedBy(BigNumber(2)) + .plus(c) + .dividedBy(BigNumber(2).multipliedBy(yVal).plus(b)) } return yVal } /** - * exchange() runs a theorhetical Curve StableSwap model to determine impact on token price/impact + * exchange() runs a theorhetical Curve StableSwap model to determine impact on token price * * @param {number} fromIndex * @param {number} toIndex - * @param {bigint} dx + * @param {BigNumber} dx * @memberof StableSwap */ - exchange(fromIndex: number, toIndex: number, dx: bigint) { + exchange(fromIndex: number, toIndex: number, dx: BigNumber) { const xp = this.xp() - const x = xp[fromIndex] + dx + const x = xp[fromIndex].plus(dx) const y = this.y(fromIndex, toIndex, x) - const dy = xp[toIndex] - y - const fee = (dy * this.fee) / bigIntExponentiation(BigInt(10), BigInt(10)) + const dy = xp[toIndex].minus(y) + const fee = dy + .multipliedBy(this.fee) + .dividedBy(BigNumber(10).exponentiatedBy(BigNumber(10))) - if (dy <= 0) { + if (dy.isLessThanOrEqualTo(BigNumber(0))) { throw new Error("Invalid exchange operation") } - return dy - fee + return dy.minus(fee) } } diff --git a/packages/nibijs/src/test/mocks/stabletests.csv b/packages/nibijs/src/test/mocks/stabletests.csv index df63669e..85a08202 100644 --- a/packages/nibijs/src/test/mocks/stabletests.csv +++ b/packages/nibijs/src/test/mocks/stabletests.csv @@ -1,16 +1,16 @@ -"[27775542291595154, 97122479520742482]",3569,1,0,14569132846928708,14558669000207900 +"[27775542291595154, 97122479520742482]",3569,1,0,14569132846928708,14558669000207899 "[99811231953029219, 67867258133352948, 4548504687635641, 22643988721136401]",1866,3,0,74314087998730798,74314370435091235 "[79490968623278253, 2416473075872119, 16201992876681232, 26563606891100389, 31550918159678239]",1,2,3,20029180402698560,19630034405572792 "[20390077612988015, 14143931325787607]",2874,0,1,646125170094190,646076474640607 "[29862292342512526, 67464721015487113, 87029435446288613, 84748092193374166]",1661,1,0,19106998798163720,19105983646625076 -"[26314181423329306, 72232425287195858]",895,1,0,7017055246943759,7008536248313182 +"[26314181423329306, 72232425287195858]",895,1,0,7017055246943759,7008536248313181 "[50063648172290677, 88478493455845741, 56498015592382508]",434,0,2,56175044123085933,55631680015957697 "[85974277623017018, 75621698555726081, 29660923586357885]",1769,1,0,56285720871465698,56282169988152274 "[1448549668833367, 70220925182942167, 42193669609889726]",2702,2,1,30952835910452511,30952209807541944 "[52760182169704204, 80560578505928626, 38370011571631126, 10523567718382376]",221,0,1,6243981777191623,6244229760683159 -"[21751115335094304, 2269537422422848, 79169137488199321, 62781109611104749, 70868520138484385]",906,1,4,4012347630123592,4013055467856176 +"[21751115335094304, 2269537422422848, 79169137488199321, 62781109611104749, 70868520138484385]",906,1,4,4012347630123592,4013055467856175 "[33812769090233324, 48275794826844955, 89859018195028312, 27622838635968022]",2623,1,0,29988889904145794,29986866603935209 -"[38550790744242251, 35749808204325994, 93688086287146772, 34503077430670297]",2892,1,0,34838346497693955,34836630239365079 +"[38550790744242251, 35749808204325994, 93688086287146772, 34503077430670297]",2892,1,0,34838346497693955,34836630239365078 "[35417314410960968, 4102881661698347, 50055922087158045, 41884105950994686, 21101364706028050]",1186,1,2,3257903809020377,3257972188930204 "[81129188249394063, 2584813155461682, 44804906888421064, 95264202217524109]",1,0,3,13940226839652919,13942817493440413 "[72198520015474283, 98969806731775461, 7611649180436155]",3122,1,2,202275634204473,202067889175795 @@ -30,18 +30,18 @@ "[74283885983215027, 97376907892131235, 44701059091011433, 75038989483601953]",3572,1,0,61077282754379821,61076374079398594 "[61809191794567159, 65268210983736695, 13041955921362012]",3334,2,0,1367651454523366,1367875804543958 "[29305950030977337, 50529747115163700, 87408501924636118]",1893,0,2,57500308755670506,57500367166862904 -"[81555735945943859, 92446513201273467, 26820392358889621]",745,1,0,5649991870092771,5649806044917715 +"[81555735945943859, 92446513201273467, 26820392358889621]",745,1,0,5649991870092771,5649806044917714 "[55312357586388897, 85507925667443520]",476,1,0,15461825549485662,15448256102708432 "[3324774254953230, 7444302682607094, 73818588715037600]",2423,0,1,1036600598831895,1038409415489951 -"[70866111453900446, 987034925133774, 5551501056990013, 33827754450273291]",1,0,2,440991689322851,163173975913946 +"[70866111453900446, 987034925133774, 5551501056990013, 33827754450273291]",1,0,2,440991689322851,163173975913945 "[69260898739623064, 2407120271845792]",2114,1,0,27975191526590264,28031647097048791 "[78989492850850109, 34085017560697864, 17098872479483990, 82101123756003477, 75942316151386295]",2151,0,2,832459722815124,832455899715611 -"[43105400266091190, 30907212812585241, 99592755261735635, 68038321017851058, 75636781777032787]",3041,1,4,3966410030435909,3966413284798787 -"[72480217367559860, 29633693498020282, 39797991003336311, 23528064722640901]",749,0,3,13344798053600377,13343604186498670 +"[43105400266091190, 30907212812585241, 99592755261735635, 68038321017851058, 75636781777032787]",3041,1,4,3966410030435909,3966413284798786 +"[72480217367559860, 29633693498020282, 39797991003336311, 23528064722640901]",749,0,3,13344798053600377,13343604186498669 "[7573870148015712, 96425949453897932, 12213914565225419, 29817160901860679]",3816,1,3,15641214014477820,15640405643999879 "[39446768558086319, 33060509822102699, 47308413038664680]",3683,2,1,26651994504238407,26649135052115203 "[59765006200169844, 32729340087571085, 5459283337981383, 72833741419973763]",1958,3,1,15353465961140485,15352657088325650 -"[11687948178407447, 70735263447974241, 19396727197336045, 60339348678984523]",1929,2,3,57279688711334547,57276037997951275 +"[11687948178407447, 70735263447974241, 19396727197336045, 60339348678984523]",1929,2,3,57279688711334547,57276037997951274 "[76423498163807014, 61843295785370953, 65130276996772654, 27310582754688037, 18460489944575185]",1763,3,2,55654197258731139,55654084187322882 "[46510092262672225, 35088848864762949, 85877728950773668]",495,0,2,25241287191968211,25242561667555042 "[94489200612776920, 12892523631993082]",1703,0,1,6897644195895437,6860662857295104 @@ -50,7 +50,7 @@ "[63491607504450131, 6957907101513874, 52641848063164062]",2856,2,0,28866767659336552,28865894138520401 "[42575766492233533, 75944493215873760, 69477013071683111, 20575071825017380]",1806,2,1,623786338925258,623786837033419 "[489218592538267, 14662597764290572, 23765886037043450]",2123,0,1,14537721546710050,14517774321461833 -"[13885765464616729, 3550969430265598]",1,1,0,5224753861040049,6500102664148832 +"[13885765464616729, 3550969430265598]",1,1,0,5224753861040049,6500102664148831 "[79661913387008706, 91524687892670355, 84888699883808398, 56172071485651526]",1,2,0,39201593055227540,38768818852997409 "[67556033156043755, 5228023594229166, 9567881125457033]",877,0,2,4783368122087361,4765066821372804 "[74993254895716474, 82141866006727728, 71846419421012636, 91708209715761089]",3726,1,0,28098385920714617,28098320361060157 @@ -66,7 +66,7 @@ "[8898007662468760, 92882563089559083]",3079,1,0,6842128161385877,6767336355160415 "[37572692320844011, 78194216157707620]",3403,1,0,24005344006305440,23994508948323056 "[63296299392391998, 15393876906431156, 14305587782713097, 68056283702550734]",1931,3,2,11647681896302999,11644441698114468 -"[18495841046767555, 82801749401579506, 61367066674882964]",2401,1,2,50688306115900746,50677407249052123 +"[18495841046767555, 82801749401579506, 61367066674882964]",2401,1,2,50688306115900746,50677407249052122 "[78569114356943248, 15034128553414328, 96020554733746314]",1906,0,1,10107230131538149,10093317230348115 "[62995662163319645, 34510490826081701, 65056543656776077, 78725328043591716, 19580263133955812]",3215,2,1,11056603170104980,11056591306192758 "[46867583551858212, 37361486211856719, 80530344373805838, 35279570203019942, 96169359256080527]",3052,3,1,34823191232863459,34822887767130375 @@ -101,20 +101,20 @@ "[74194789012746491, 23111217050480249]",1925,0,1,7678002541916038,7671784007785916 "[35761839419113165, 43177675271669727, 2793876178502950, 74236746662306530]",3324,3,1,23408516255585964,23407642540826963 "[2389935293412759, 90722194103360944, 33554657062247059, 75413708292200801]",1412,3,0,348841718471271,347747403470350 -"[81160498135213554, 99142268833380375, 90345012890697159]",2747,2,1,85420679438876243,85410686872367065 -"[32137332442702858, 40678323701569584, 27468385539121105]",1925,2,0,13106400667278981,13106113266529047 +"[81160498135213554, 99142268833380375, 90345012890697159]",2747,2,1,85420679438876243,85410686872367064 +"[32137332442702858, 40678323701569584, 27468385539121105]",1925,2,0,13106400667278981,13106113266529046 "[60139786451371726, 16377001684986043, 42151498053701624, 15076187330797665]",3865,1,0,50708734466897840,50708584204081401 "[3621235742757581, 55363775943291536, 85394299419424733, 67699336762421870, 28482776932231394]",1727,4,1,95361111436334,95361674349960 "[92433040794899419, 17164989863623070]",2536,0,1,7886119318682126,7871771144730641 "[55214885829406499, 50335319328185454]",849,0,1,3627690833258196,3627341317050072 "[595195262273315, 23874611527563193]",1643,0,1,6758896817926198,6793532110645690 -"[38638239458495434, 45749377268118255]",1,0,1,41907717755308016,31182448816213348 +"[38638239458495434, 45749377268118255]",1,0,1,41907717755308016,31182448816213350 "[99558605390903370, 55089900255704010, 21746075552214046, 50722180288720687]",1005,2,3,30507627849563204,30507560771416899 "[50922729579375163, 96720072430232666, 5131591505294711]",1180,1,0,34397549730425743,34365776712075701 "[70187425709284979, 78301460518417366, 42914247765978870, 52445731913189562, 54079792593765903]",1044,2,4,20932647044074441,20932637138043229 "[91796285297293685, 22985387394391223, 68765093669833582, 56015703436774448]",3328,0,2,41396767105996307,41396441439442088 "[78682156810606273, 60288819532447583, 48606253300325634, 874425726434693, 75169432195480183]",317,2,1,18086202328356698,18085908045182504 -"[6260137618940906, 94592802995981447, 36171078644600848]",3647,2,0,2457151399736167,2453808745051091 +"[6260137618940906, 94592802995981447, 36171078644600848]",3647,2,0,2457151399736167,2453808745051090 "[90437735388118521, 44941741779181066, 21012424410658660, 37722872677786489, 41294431478651274]",1180,2,4,9339930315177791,9339942012964508 "[57055247100972084, 97411041131490238, 88049437050618239, 28624004497812491, 66751901520401601]",2842,4,1,40418268224225143,40418264520740681 "[80129410612599784, 83152019722453770]",1482,0,1,7286711630499487,7286582584842827 @@ -123,7 +123,7 @@ "[61875212826932831, 72593040558011432, 19011646412279374]",3251,2,1,56205039384725983,56204711457319102 "[15684112510939275, 96348627053556424]",3255,1,0,6367437054719561,6357124094279745 "[10577563463353731, 52365820866282587, 29743177535602727, 41464246715654358]",2358,2,1,2125474505652479,2125485704542424 -"[45797231490346310, 19798482141736898]",1559,1,0,17115911409820152,17117702615031928 +"[45797231490346310, 19798482141736898]",1559,1,0,17115911409820152,17117702615031927 "[71563162133678205, 81067597843976089, 70096022688615157, 99605299596835179]",1591,3,1,14585836363953879,14585783933681730 "[29778208710118849, 64003338911050551, 45683809660143699]",3366,2,1,23470192025837031,23470115661671601 "[19061029274162701, 88861042867103360]",1838,1,0,10380564887668160,10356328610769880 @@ -139,11 +139,11 @@ "[77449783951525627, 96622527617405901]",124,1,0,17597919266290529,17564511534571724 "[31472711499304794, 28537794900047728, 64590509440965888]",2278,1,0,3199818035633924,3199815672426861 "[85127147804183038, 10907451024955490]",3101,0,1,5172140096670793,5156993162774455 -"[78869112819379136, 65683563674540773, 20450721902044273]",1,0,2,9580067507543145,6681840906321364 +"[78869112819379136, 65683563674540773, 20450721902044273]",1,0,2,9580067507543145,6681840906321363 "[76355003527890715, 89423248722724658]",445,0,1,32884188770912793,32874319386910856 "[58350897179380223, 25320109331183988, 47409765415590316, 45884797510615309, 12771227731243619]",3348,4,3,669497091074848,669498333633530 "[31629082547321912, 66962487253351599]",984,1,0,23526055061542009,23472331528288205 -"[61339069701175488, 57031595555609106, 27628293955272026]",1959,1,2,12393582463904563,12391970802708548 +"[61339069701175488, 57031595555609106, 27628293955272026]",1959,1,2,12393582463904563,12391970802708547 "[90723223197964049, 92126516973348926, 25135966310466049, 6468863141559427]",3848,1,3,3502939308201254,3501483276834226 "[30643815872495940, 99444958907123488, 60753933151148470, 45488579207343040, 36009953155972845]",487,2,3,11864673148291478,11864635854948134 "[50543316487943597, 40313943101469725]",763,0,1,6352546382595016,6350904205737744 @@ -160,7 +160,7 @@ "[8218396563534725, 57737575296515419]",3166,0,1,34780030163893337,34786225555935240 "[20447099011249796, 85406630653105891]",1,1,0,5743132972156642,2840742068206874 "[91674619594269523, 29729701492058737, 2764287487501530, 10378124496155158]",3685,3,1,15942654438851238,15943004075115926 -"[98644950910472577, 58900491983607430, 36131813150848573]",2043,2,0,93437448520396446,93418264263692998 +"[98644950910472577, 58900491983607430, 36131813150848573]",2043,2,0,93437448520396446,93418264263692997 "[17900199594434773, 9442670433755745, 27115402511241802, 76251636243027103, 66221708467367021]",2391,2,4,27286261816823239,27286278252910860 "[99528408031324165, 66063536800192963]",1,0,1,54560755318735428,36277482536949316 "[53673848541149130, 97714409424635281, 96721723451169947, 58155389654406246]",2326,1,2,60488830579513017,60488441844712628 @@ -168,8 +168,8 @@ "[29132228183822363, 87258711327926155]",1147,0,1,31309221243789011,31317607263211598 "[88499875721648702, 18532749390880803, 61623274093981801, 48895221084664315]",2461,1,2,24836036948053844,24836210684551946 "[37918616704488552, 1978275343636407]",1185,1,0,30652228690824828,30682612007314091 -"[39566760046981365, 72165888636327214, 20984355492852881, 69732831981287588, 26644647874223458]",542,0,3,25787314673575233,25787324319619429 -"[33231171828127595, 70031823269237423, 29203587248870731, 46542297859429344]",1,3,1,826924181157457,831903979460410 +"[39566760046981365, 72165888636327214, 20984355492852881, 69732831981287588, 26644647874223458]",542,0,3,25787314673575233,25787324319619428 +"[33231171828127595, 70031823269237423, 29203587248870731, 46542297859429344]",1,3,1,826924181157457,831903979460409 "[24168732790973415, 9891884832884577, 95888118345683431, 70447213823396764, 34778290967823452]",3364,2,0,18576443409495213,18576203163745039 "[43460789793974808, 65358405945975190, 27686557946342977, 3730885400396796]",2477,2,1,52214733323434910,52213910103877138 "[54867547995683828, 12486378424318257, 86932311741415380, 55718479759640327]",550,1,0,51587976361293124,51580284382596332 @@ -187,14 +187,14 @@ "[10386380277462794, 23652365759781377]",2588,0,1,8543868360422247,8544415115796825 "[57452763039578501, 25602886812442815, 57161394447653617, 59027345742473696, 28222779128014982]",184,3,0,42555399562147592,42554716657434372 "[95688279586172992, 81357300020638325]",2875,1,0,44376644563882978,44373443081853912 -"[48086772769243042, 43738509811407018, 95806070340604096, 54383155620608511]",3697,0,3,7745771226916083,7745769878714023 +"[48086772769243042, 43738509811407018, 95806070340604096, 54383155620608511]",3697,0,3,7745771226916083,7745769878714022 "[48466550134706494, 62458489539405430, 41001260515711365, 84672518612262309, 66952519627874424]",193,1,0,179130399358799,179129904259862 "[12467222030895314, 53297354208761319, 9745688825539827, 83158153602115150, 18829217014123659]",3914,1,0,8414326373462587,8414192704810180 -"[87373514757295389, 36776518566996441]",1,0,1,14534609232868448,9396059756397486 +"[87373514757295389, 36776518566996441]",1,0,1,14534609232868448,9396059756397485 "[20871959848374229, 92008916703325096, 13218142135668071, 89619195637431353, 68632514579833846]",129,4,1,65265537174093928,65263485816100742 "[98931597104436506, 33132493678017210, 27768934273835381, 92972857848664691, 90429529573619284]",60,4,0,83499627800950500,83491126823768986 "[86153591584134967, 3499032560766213, 87788771609402210, 95665988072887715]",1756,3,0,18753147219416474,18752788840532866 "[75038892220520647, 67581506476477568]",3521,1,0,10603995148523590,10603927906542831 -"[71600789539212919, 63529769797209224, 25127151339962766, 70085352502498970, 6565694423980876]",443,0,2,9942752460235186,9942400476625734 +"[71600789539212919, 63529769797209224, 25127151339962766, 70085352502498970, 6565694423980876]",443,0,2,9942752460235186,9942400476625733 "[19025005827936588, 97753026265519923]",910,0,1,86011328318824956,85981698734805866 "[72959864838082277, 44748119077116580, 52088565520242482, 6454284556605846, 53320655481448340]",1802,1,0,15221030816245318,15221038933088359 diff --git a/packages/nibijs/src/test/stableswap.test.ts b/packages/nibijs/src/test/stableswap.test.ts index 940954f1..0662c3ab 100644 --- a/packages/nibijs/src/test/stableswap.test.ts +++ b/packages/nibijs/src/test/stableswap.test.ts @@ -1,8 +1,7 @@ -/* global BigInt */ - import fs from "fs" import path from "path" -import { StableSwap, bigIntExponentiation } from "../stableswap" +import { BigNumber } from "bignumber.js" +import { StableSwap } from "../stableswap" describe("stableswap tests", () => { test("stabletests", () => { @@ -17,85 +16,22 @@ describe("stableswap tests", () => { throw new Error("Invalid line format") } - const balances = JSON.parse(match[1]) as bigint[] - const amplification = parseFloat(match[2]) + const balances = JSON.parse(match[1]) as BigNumber[] + const amplification = BigNumber(match[2]) const send = parseFloat(match[3]) const recv = parseFloat(match[4]) - const dx = BigInt(match[5]) - const expectedDy = BigInt(match[7]) + const dx = BigNumber(match[5]) + const expectedDy = BigNumber(match[7]) const curveModel = new StableSwap( - BigInt(amplification), + amplification, balances, - Array(balances.length).fill(bigIntExponentiation(BigInt(10), BigInt(18))), - BigInt(0), + Array(balances.length).fill(BigNumber(10).exponentiatedBy(BigNumber(18))), + BigNumber(0), ) const dy = curveModel.exchange(send, recv, dx) - expect(Math.abs(Number(dy - expectedDy))).toBeLessThanOrEqual(1) + expect(dy.minus(expectedDy).abs().isLessThanOrEqualTo(BigNumber(1))).toEqual(true) }) }) }) - -describe("power", () => { - interface TestCase { - base: bigint - exponent: bigint - out: bigint - } - - const testCases: TestCase[] = [ - { - base: BigInt(0), - exponent: BigInt(0), - out: BigInt(1), - }, - { - base: BigInt(1), - exponent: BigInt(0), - out: BigInt(1), - }, - { - base: BigInt(-10), - exponent: BigInt(0), - out: BigInt(1), - }, - { - base: BigInt(0), - exponent: BigInt(1), - out: BigInt(0), - }, - { - base: BigInt(0), - exponent: BigInt(-10), - out: BigInt(1), - }, - { - base: BigInt(-10), - exponent: BigInt(-10), - out: BigInt(1), - }, - { - base: BigInt(-10), - exponent: BigInt(10), - out: BigInt(10000000000), - }, - { - base: BigInt(10), - exponent: BigInt(-10), - out: BigInt(1), - }, - { - base: BigInt(10), - exponent: BigInt(100), - out: BigInt( - "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", - ), - }, - ] - - test.each(testCases)("%o", (tc) => { - const out = bigIntExponentiation(tc.base, tc.exponent) - expect(out).toEqual(tc.out) - }) -}) diff --git a/yarn.lock b/yarn.lock index c5ff642a..16185ac2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2878,6 +2878,11 @@ before-after-hook@^2.2.0: resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c" integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ== +bignumber.js@^9.1.1: + version "9.1.1" + resolved "https://registry.yarnpkg.com/bignumber.js/-/bignumber.js-9.1.1.tgz#c4df7dc496bd849d4c9464344c1aa74228b4dac6" + integrity sha512-pHm4LsMJ6lzgNGVfZHjMoO8sdoRhOzOH4MLmY65Jg70bpxCKu5iOHNJyfF6OyvYw7t8Fpf35RuzUyqnQsj8Vig== + bin-links@^3.0.0: version "3.0.3" resolved "https://registry.yarnpkg.com/bin-links/-/bin-links-3.0.3.tgz#3842711ef3db2cd9f16a5f404a996a12db355a6e"