Skip to content

Commit

Permalink
applied Decimal pow and tested
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Dec 14, 2023
1 parent d4a3def commit 1147cfe
Show file tree
Hide file tree
Showing 163 changed files with 7,443 additions and 14,906 deletions.
82 changes: 82 additions & 0 deletions packages/math/__tests__/swap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from "../src/utils";
import {
calcAmountWithSlippage,
calcInGivenOut,
calcOutGivenIn,
calcPriceImpactGivenIn,
calcPriceImpactGivenOut,
getRoutesForTrade,
Expand Down Expand Up @@ -251,4 +253,84 @@ describe("Test swap calculations", () => {
},
]
);

cases('calcOutGivenIn', opts => {
const result = calcOutGivenIn(
new BigNumber(opts.tokenBalanceIn),
new BigNumber(opts.tokenWeightIn),
new BigNumber(opts.tokenBalanceOut),
new BigNumber(opts.tokenWeightOut),
new BigNumber(opts.tokenAmountIn),
new BigNumber(opts.swapFee)
);
expect(result.toNumber()).toBeCloseTo(new BigNumber(opts.expected).toNumber(), 5);
}, {
"Exponent not an integer(0.5)": {
tokenBalanceIn: '1000',
tokenWeightIn: '10',
tokenBalanceOut: '500',
tokenWeightOut: '20',
tokenAmountIn: '100',
swapFee: '0.01',
expected: '23.051861474872545'
},
"Exponent not an integer(1.5)": {
tokenBalanceIn: '1500',
tokenWeightIn: '30',
tokenBalanceOut: '700',
tokenWeightOut: '20',
tokenAmountIn: '200',
swapFee: '0',
expected: '119.8216126711115'
},
"regular": {
tokenBalanceIn: '2000',
tokenWeightIn: '40',
tokenBalanceOut: '1000',
tokenWeightOut: '10',
tokenAmountIn: '300',
swapFee: '0.05',
expected: '413.0850595232377'
}
});

cases('calcInGivenOut', opts => {
const result = calcInGivenOut(
new BigNumber(opts.tokenBalanceIn),
new BigNumber(opts.tokenWeightIn),
new BigNumber(opts.tokenBalanceOut),
new BigNumber(opts.tokenWeightOut),
new BigNumber(opts.tokenAmountOut),
new BigNumber(opts.swapFee)
);
expect(result.toNumber()).toBeCloseTo(new BigNumber(opts.expected).toNumber(), 5);
}, {
"Exponent not an integer(0.5)": {
tokenBalanceIn: '1000',
tokenWeightIn: '20',
tokenBalanceOut: '500',
tokenWeightOut: '10',
tokenAmountOut: '100',
swapFee: '0.01',
expected: '119.22625126252005'
},
"Exponent not an integer(1.5)": {
tokenBalanceIn: '1500',
tokenWeightIn: '20',
tokenBalanceOut: '700',
tokenWeightOut: '30',
tokenAmountOut: '200',
swapFee: '0',
expected: '984.7535089018387'
},
"regular": {
tokenBalanceIn: '2000',
tokenWeightIn: '10',
tokenBalanceOut: '1000',
tokenWeightOut: '40',
tokenAmountOut: '300',
swapFee: '0.05',
expected: '6663.013218176637'
}
});
});
1 change: 1 addition & 0 deletions packages/math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"dependencies": {
"@chain-registry/types": "0.16.0",
"@chain-registry/utils": "1.13.1",
"decimal.js-light": "^2.5.1",
"bignumber.js": "9.1.1",
"long": "5.2.3",
"osmojs": "^16.5.1"
Expand Down
14 changes: 8 additions & 6 deletions packages/math/src/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Asset } from "@chain-registry/types";
import { BigNumber } from "bignumber.js";
import { CoinDenom, Trade, PrettyPair } from "./types";
import { symbolToOsmoDenom } from "./utils";
import Long from "long";
import { Decimal } from "decimal.js-light";
import { SwapAmountInRoute } from "osmojs/dist/codegen/osmosis/poolmanager/v1beta1/swap_route";
import { Pool } from "osmojs/dist/codegen/osmosis/gamm/pool-models/balancer/balancerPool";
import { Coin } from "osmojs/dist/codegen/cosmos/base/v1beta1/coin";
Expand Down Expand Up @@ -107,9 +107,7 @@ const getPoolAsset = (pool: Pool, denom: string) => {
(asset) => asset?.token && asset.token.denom === denom
);
if (!poolAsset) {
throw new Error(
`Pool ${pool.id} doesn't have the pool asset for ${denom}`
);
throw new Error(`Pool ${pool.id} doesn't have the pool asset for ${denom}`);
}
return { denom, weight: poolAsset.weight, amount: poolAsset.token!.amount };
};
Expand Down Expand Up @@ -140,7 +138,7 @@ export const calcOutGivenIn = (
let adjustedIn = one.minus(swapFee);
adjustedIn = tokenAmountIn.multipliedBy(adjustedIn);
const y = tokenBalanceIn.div(tokenBalanceIn.plus(adjustedIn));
const foo = y.pow(weightRatio);
const foo = pow(y, weightRatio);
const bar = one.minus(foo);
return tokenBalanceOut.multipliedBy(bar);
};
Expand All @@ -156,7 +154,7 @@ export const calcInGivenOut = (
const weightRatio = tokenWeightOut.div(tokenWeightIn);
const diff = tokenBalanceOut.minus(tokenAmountOut);
const y = tokenBalanceOut.div(diff);
let foo = y.pow(weightRatio);
let foo = pow(y, weightRatio);
foo = foo.minus(one);
const tokenAmountIn = one.minus(swapFee);
return tokenBalanceIn.multipliedBy(foo).div(tokenAmountIn);
Expand Down Expand Up @@ -227,3 +225,7 @@ export const calcPriceImpactGivenOut = (

return priceImpact.toString();
};

export const pow = (x: BigNumber, y: BigNumber): BigNumber => {
return new BigNumber(new Decimal(x.toString()).pow(new Decimal(y.toString())).toString())
};
2 changes: 1 addition & 1 deletion packages/osmojs/cosmos-sdk
Submodule cosmos-sdk updated 373 files
2 changes: 1 addition & 1 deletion packages/osmojs/osmosis
Submodule osmosis updated 1106 files
24 changes: 12 additions & 12 deletions packages/osmojs/src/codegen/binary.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* This file and any referenced files were automatically generated by @cosmology/telescope@0.99.12
* This file and any referenced files were automatically generated by @cosmology/telescope@0.102.0
* DO NOT MODIFY BY HAND. Instead, download the latest proto files for your chain
* and run the transpile command or yarn proto command to regenerate this bundle.
*/
Expand Down Expand Up @@ -348,12 +348,12 @@ export class BinaryWriter {
(value = value >>> 0) < 128
? 1
: value < 16384
? 2
: value < 2097152
? 3
: value < 268435456
? 4
: 5,
? 2
: value < 2097152
? 3
: value < 268435456
? 4
: 5,
value
)).len;
return this;
Expand Down Expand Up @@ -463,10 +463,10 @@ function pool(
function indexOutOfRange(reader: BinaryReader, writeLength?: number) {
return RangeError(
"index out of range: " +
reader.pos +
" + " +
(writeLength || 1) +
" > " +
reader.len
reader.pos +
" + " +
(writeLength || 1) +
" > " +
reader.len
);
}
8 changes: 4 additions & 4 deletions packages/osmojs/src/codegen/capability/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as _42 from "./v1/capability";
import * as _43 from "./v1/genesis";
import * as _53 from "./v1/capability";
import * as _54 from "./v1/genesis";
export namespace capability {
export const v1 = {
..._42,
..._43
..._53,
..._54
};
}
2 changes: 1 addition & 1 deletion packages/osmojs/src/codegen/cosmos/auth/v1beta1/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function createBaseBaseAccount(): BaseAccount {
return {
$typeUrl: "/cosmos.auth.v1beta1.BaseAccount",
address: "",
pubKey: undefined,
pubKey: Any.fromPartial({}),
accountNumber: BigInt(0),
sequence: BigInt(0)
};
Expand Down
2 changes: 1 addition & 1 deletion packages/osmojs/src/codegen/cosmos/auth/v1beta1/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export const QueryAccountRequest = {
};
function createBaseQueryAccountResponse(): QueryAccountResponse {
return {
account: undefined
account: Any.fromPartial({})
};
}
export const QueryAccountResponse = {
Expand Down
8 changes: 4 additions & 4 deletions packages/osmojs/src/codegen/cosmos/authz/v1beta1/authz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ export const GenericAuthorization = {
};
function createBaseGrant(): Grant {
return {
authorization: undefined,
expiration: undefined
authorization: Any.fromPartial({}),
expiration: new Date()
};
}
export const Grant = {
Expand Down Expand Up @@ -262,8 +262,8 @@ function createBaseGrantAuthorization(): GrantAuthorization {
return {
granter: "",
grantee: "",
authorization: undefined,
expiration: undefined
authorization: Any.fromPartial({}),
expiration: new Date()
};
}
export const GrantAuthorization = {
Expand Down
6 changes: 3 additions & 3 deletions packages/osmojs/src/codegen/cosmos/bank/v1beta1/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ export const QueryBalanceRequest = {
};
function createBaseQueryBalanceResponse(): QueryBalanceResponse {
return {
balance: undefined
balance: Coin.fromPartial({})
};
}
export const QueryBalanceResponse = {
Expand Down Expand Up @@ -1072,7 +1072,7 @@ export const QuerySupplyOfRequest = {
};
function createBaseQuerySupplyOfResponse(): QuerySupplyOfResponse {
return {
amount: undefined
amount: Coin.fromPartial({})
};
}
export const QuerySupplyOfResponse = {
Expand Down Expand Up @@ -1354,7 +1354,7 @@ export const QuerySupplyOfWithoutOffsetRequest = {
};
function createBaseQuerySupplyOfWithoutOffsetResponse(): QuerySupplyOfWithoutOffsetResponse {
return {
amount: undefined
amount: Coin.fromPartial({})
};
}
export const QuerySupplyOfWithoutOffsetResponse = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ function createBaseTxResponse(): TxResponse {
info: "",
gasWanted: BigInt(0),
gasUsed: BigInt(0),
tx: undefined,
tx: Any.fromPartial({}),
timestamp: "",
events: []
};
Expand Down
Loading

0 comments on commit 1147cfe

Please sign in to comment.