Skip to content

Commit fc60407

Browse files
authored
Merge branch 'develop' into feat/integrate-debridge-swapper
2 parents 4267156 + 7495f02 commit fc60407

File tree

13 files changed

+117
-80
lines changed

13 files changed

+117
-80
lines changed

packages/swap-widget/src/hooks/useMarketData.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { adapters } from '@shapeshiftoss/caip'
2-
import { bn, fromBaseUnit } from '@shapeshiftoss/utils'
2+
import { BigAmount, bn } from '@shapeshiftoss/utils'
33
import { useQuery } from '@tanstack/react-query'
44

55
import type { AssetId } from '../types'
@@ -157,7 +157,7 @@ export const formatUsdValue = (
157157
): string => {
158158
if (!usdPrice || usdPrice === '0') return '$0.00'
159159

160-
const amount = fromBaseUnit(cryptoAmount, precision)
160+
const amount = BigAmount.fromBaseUnit({ value: cryptoAmount, precision }).toPrecision()
161161
const usdValue = bn(amount).times(usdPrice).toNumber()
162162

163163
if (usdValue < 0.01) return '< $0.01'

packages/swap-widget/src/types/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
thorchainChainId,
2727
} from '@shapeshiftoss/caip'
2828
import type { TransactionData } from '@shapeshiftoss/types'
29-
import { fromBaseUnit, toBaseUnit } from '@shapeshiftoss/utils'
29+
import { BigAmount } from '@shapeshiftoss/utils'
3030
import type { WalletClient } from 'viem'
3131
import { erc20Abi } from 'viem'
3232

@@ -274,7 +274,9 @@ export const getChainType = (chainId: string): 'evm' | 'utxo' | 'cosmos' | 'sola
274274

275275
export const formatAmount = (amount: string, decimals: number, maxDecimals?: number): string => {
276276
const effectiveMaxDecimals = maxDecimals ?? Math.min(decimals, 8)
277-
const result = fromBaseUnit(amount, decimals, effectiveMaxDecimals)
277+
const result = BigAmount.fromBaseUnit({ value: amount, precision: decimals }).toFixed(
278+
effectiveMaxDecimals,
279+
)
278280
const num = Number(result)
279281
if (num === 0) return '0'
280282

@@ -290,7 +292,7 @@ export const formatAmount = (amount: string, decimals: number, maxDecimals?: num
290292
}
291293

292294
export const parseAmount = (amount: string, decimals: number): string => {
293-
return toBaseUnit(amount, decimals)
295+
return BigAmount.fromPrecision({ value: amount, precision: decimals }).toBaseUnit()
294296
}
295297

296298
export const truncateAddress = (address: string, chars = 4): string => {

packages/swapper/src/swappers/ButterSwap/swapperApi/getTradeQuote.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { btcChainId, solanaChainId, tronChainId } from '@shapeshiftoss/caip'
22
import { isEvmChainId } from '@shapeshiftoss/chain-adapters'
33
import {
4+
BigAmount,
45
bnOrZero,
56
chainIdToFeeAssetId,
67
convertDecimalPercentageToBasisPoints,
7-
fromBaseUnit,
8-
toBaseUnit,
98
} from '@shapeshiftoss/utils'
109
import type { Result } from '@sniptt/monads'
1110
import { Err, Ok } from '@sniptt/monads'
@@ -83,10 +82,10 @@ export const getTradeQuote = async (
8382
const routeResult = await getButterRoute({
8483
sellAsset,
8584
buyAsset,
86-
sellAmountCryptoBaseUnit: fromBaseUnit(
87-
sellAmountIncludingProtocolFeesCryptoBaseUnit,
88-
sellAsset.precision,
89-
),
85+
sellAmountCryptoPrecision: BigAmount.fromBaseUnit({
86+
value: sellAmountIncludingProtocolFeesCryptoBaseUnit,
87+
precision: sellAsset.precision,
88+
}).toPrecision(),
9089
slippage,
9190
affiliate: makeButterSwapAffiliate(affiliateBps),
9291
})
@@ -96,10 +95,10 @@ export const getTradeQuote = async (
9695

9796
if (!isRouteSuccess(routeResponse)) {
9897
if (routeResponse.errno === ButterSwapErrorCode.InsufficientAmount) {
99-
const minAmountCryptoBaseUnit = toBaseUnit(
100-
(routeResponse as any).minAmount,
101-
sellAsset.precision,
102-
)
98+
const minAmountCryptoBaseUnit = BigAmount.fromPrecision({
99+
value: (routeResponse as any).minAmount,
100+
precision: sellAsset.precision,
101+
}).toBaseUnit()
103102
return Err(
104103
createTradeAmountTooSmallErr({
105104
minAmountCryptoBaseUnit,
@@ -165,15 +164,21 @@ export const getTradeQuote = async (
165164
}
166165

167166
// Map gasFee.amount to networkFeeCryptoBaseUnit using fee asset precision
168-
const networkFeeCryptoBaseUnit = toBaseUnit(bnOrZero(route.gasFee?.amount), feeAsset.precision)
167+
const networkFeeCryptoBaseUnit = BigAmount.fromPrecision({
168+
value: bnOrZero(route.gasFee?.amount),
169+
precision: feeAsset.precision,
170+
}).toBaseUnit()
169171

170172
// Use destination receive amount as a priority if present and defined
171173
// It won't for same-chain swaps, so we fall back to the source chain receive amount (i.e source chain *is* the destination chain)
172174
const outputAmount = route.dstChain?.totalAmountOut ?? route.srcChain.totalAmountOut
173175

174176
// TODO: affiliate fees not yet here, gut feel is that Butter won't do the swap output - fees logic for us here
175177
// Sanity check me when affiliates are implemented, and do the math ourselves if needed
176-
const buyAmountAfterFeesCryptoBaseUnit = toBaseUnit(outputAmount, buyAsset.precision)
178+
const buyAmountAfterFeesCryptoBaseUnit = BigAmount.fromPrecision({
179+
value: outputAmount,
180+
precision: buyAsset.precision,
181+
}).toBaseUnit()
177182

178183
const rate = getInputOutputRate({
179184
sellAmountCryptoBaseUnit: sellAmountIncludingProtocolFeesCryptoBaseUnit,
@@ -239,7 +244,10 @@ export const getTradeQuote = async (
239244
const solanaTransactionMetadata = maybeSolanaTransactionMetadata?.unwrap()
240245

241246
const step = {
242-
buyAmountBeforeFeesCryptoBaseUnit: toBaseUnit(outputAmount, buyAsset.precision),
247+
buyAmountBeforeFeesCryptoBaseUnit: BigAmount.fromPrecision({
248+
value: outputAmount,
249+
precision: buyAsset.precision,
250+
}).toBaseUnit(),
243251
buyAmountAfterFeesCryptoBaseUnit,
244252
sellAmountIncludingProtocolFeesCryptoBaseUnit,
245253
feeData: {

packages/swapper/src/swappers/ButterSwap/swapperApi/getTradeRate.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { btcChainId, solanaChainId, tronChainId } from '@shapeshiftoss/caip'
22
import { isEvmChainId } from '@shapeshiftoss/chain-adapters'
33
import {
4-
bn,
4+
BigAmount,
55
bnOrZero,
66
chainIdToFeeAssetId,
77
convertDecimalPercentageToBasisPoints,
8-
toBaseUnit,
98
} from '@shapeshiftoss/utils'
109
import type { Result } from '@sniptt/monads'
1110
import { Err, Ok } from '@sniptt/monads'
@@ -53,9 +52,10 @@ export const getTradeRate = async (
5352
)
5453
}
5554

56-
const amount = bn(sellAmountIncludingProtocolFeesCryptoBaseUnit)
57-
.shiftedBy(-sellAsset.precision)
58-
.toString()
55+
const amount = BigAmount.fromBaseUnit({
56+
value: sellAmountIncludingProtocolFeesCryptoBaseUnit,
57+
precision: sellAsset.precision,
58+
}).toPrecision()
5959

6060
const feeAssetId = chainIdToFeeAssetId(sellAsset.chainId)
6161

@@ -69,7 +69,7 @@ export const getTradeRate = async (
6969
const result = await getButterRoute({
7070
sellAsset,
7171
buyAsset,
72-
sellAmountCryptoBaseUnit: amount,
72+
sellAmountCryptoPrecision: amount,
7373
slippage,
7474
affiliate: makeButterSwapAffiliate(affiliateBps),
7575
})
@@ -80,10 +80,10 @@ export const getTradeRate = async (
8080

8181
if (!isRouteSuccess(routeResponse)) {
8282
if (routeResponse.errno === ButterSwapErrorCode.InsufficientAmount) {
83-
const minAmountCryptoBaseUnit = toBaseUnit(
84-
(routeResponse as any).minAmount,
85-
sellAsset.precision,
86-
)
83+
const minAmountCryptoBaseUnit = BigAmount.fromPrecision({
84+
value: (routeResponse as any).minAmount,
85+
precision: sellAsset.precision,
86+
}).toBaseUnit()
8787
return Err(
8888
createTradeAmountTooSmallErr({
8989
minAmountCryptoBaseUnit,
@@ -115,7 +115,10 @@ export const getTradeRate = async (
115115

116116
// TODO: affiliate fees not yet here, gut feel is that Butter won't do the swap output - fees logic for us here
117117
// Sanity check me when affiliates are implemented, and do the math ourselves if needed
118-
const buyAmountAfterFeesCryptoBaseUnit = toBaseUnit(outputAmount, buyAsset.precision)
118+
const buyAmountAfterFeesCryptoBaseUnit = BigAmount.fromPrecision({
119+
value: outputAmount,
120+
precision: buyAsset.precision,
121+
}).toBaseUnit()
119122

120123
const rate = getInputOutputRate({
121124
sellAmountCryptoBaseUnit: sellAmountIncludingProtocolFeesCryptoBaseUnit,
@@ -136,13 +139,19 @@ export const getTradeRate = async (
136139

137140
// Map gasFee.amount to networkFeeCryptoBaseUnit using fee asset precision
138141
const networkFeeCryptoBaseUnit = bnOrZero(route.gasFee?.amount).gt(0)
139-
? toBaseUnit(route.gasFee.amount, feeAsset.precision)
142+
? BigAmount.fromPrecision({
143+
value: route.gasFee.amount,
144+
precision: feeAsset.precision,
145+
}).toBaseUnit()
140146
: '0'
141147

142148
// Always a single step
143149
const step = {
144150
rate,
145-
buyAmountBeforeFeesCryptoBaseUnit: toBaseUnit(outputAmount, buyAsset.precision),
151+
buyAmountBeforeFeesCryptoBaseUnit: BigAmount.fromPrecision({
152+
value: outputAmount,
153+
precision: buyAsset.precision,
154+
}).toBaseUnit(),
146155
buyAmountAfterFeesCryptoBaseUnit,
147156
sellAmountIncludingProtocolFeesCryptoBaseUnit,
148157
feeData: {

packages/swapper/src/swappers/ButterSwap/xhr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type ButterSwapPromise<T> = Promise<Result<T, SwapErrorRight>>
3333
export type GetButterRouteArgs = {
3434
sellAsset: Asset
3535
buyAsset: Asset
36-
sellAmountCryptoBaseUnit: string
36+
sellAmountCryptoPrecision: string
3737
slippage: string
3838
affiliate?: string
3939
}
@@ -44,7 +44,7 @@ const TRON_NATIVE_ADDRESS = 'T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb'
4444
export const getButterRoute = async ({
4545
sellAsset,
4646
buyAsset,
47-
sellAmountCryptoBaseUnit,
47+
sellAmountCryptoPrecision,
4848
slippage,
4949
affiliate,
5050
}: GetButterRouteArgs): ButterSwapPromise<RouteResponse> => {
@@ -88,7 +88,7 @@ export const getButterRoute = async ({
8888
tokenInAddress: sellAssetAddress,
8989
toChainId: butterToChainId,
9090
tokenOutAddress: buyAssetAddress,
91-
amount: sellAmountCryptoBaseUnit,
91+
amount: sellAmountCryptoPrecision,
9292
type: 'exactIn',
9393
slippage,
9494
entrance: 'shapeshift',

packages/swapper/src/swappers/ChainflipSwapper/utils/getQuoteOrRate.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { AssetId } from '@shapeshiftoss/caip'
22
import { CHAIN_NAMESPACE, fromAssetId, solAssetId } from '@shapeshiftoss/caip'
33
import type { GetFeeDataInput } from '@shapeshiftoss/chain-adapters'
44
import type { KnownChainIds } from '@shapeshiftoss/types'
5-
import { assertUnreachable, bnOrZero, toBaseUnit } from '@shapeshiftoss/utils'
5+
import { assertUnreachable, BigAmount, bnOrZero } from '@shapeshiftoss/utils'
66
import type { Result } from '@sniptt/monads'
77
import { Err, Ok } from '@sniptt/monads'
88
import type { AxiosError } from 'axios'
@@ -309,12 +309,12 @@ export const getQuoteOrRate = async (
309309

310310
// This is not really a buyAmount before fees but rather an input/output calculation to get the sell amount
311311
// prorated to the buy asset price to determine price impact
312-
const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit(
313-
bnOrZero(singleQuoteResponse.boostQuote.ingressAmount).times(
312+
const buyAmountBeforeFeesCryptoBaseUnit = BigAmount.fromPrecision({
313+
value: bnOrZero(singleQuoteResponse.boostQuote.ingressAmount).times(
314314
bnOrZero(singleQuoteResponse.estimatedPrice),
315315
),
316-
buyAsset.precision,
317-
)
316+
precision: buyAsset.precision,
317+
}).toBaseUnit()
318318

319319
const boostTradeRateOrQuote = {
320320
id: uuid(),
@@ -372,12 +372,12 @@ export const getQuoteOrRate = async (
372372

373373
// This is not really a buyAmount before fees but rather an input/output calculation to get the sell amount
374374
// prorated to the buy asset price to determine price impact
375-
const buyAmountBeforeFeesCryptoBaseUnit = toBaseUnit(
376-
bnOrZero(singleQuoteResponse.ingressAmount).times(
375+
const buyAmountBeforeFeesCryptoBaseUnit = BigAmount.fromPrecision({
376+
value: bnOrZero(singleQuoteResponse.ingressAmount).times(
377377
bnOrZero(singleQuoteResponse.estimatedPrice),
378378
),
379-
buyAsset.precision,
380-
)
379+
precision: buyAsset.precision,
380+
}).toBaseUnit()
381381

382382
const tradeRateOrQuote = {
383383
id: uuid(),

packages/swapper/src/swappers/ChainflipSwapper/utils/helpers.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { AssetId, ChainId } from '@shapeshiftoss/caip'
22
import { fromAssetId } from '@shapeshiftoss/caip'
33
import type { Asset } from '@shapeshiftoss/types'
4-
import { bn, bnOrZero, fromBaseUnit, isToken } from '@shapeshiftoss/utils'
4+
import { BigAmount, bn, bnOrZero, isToken } from '@shapeshiftoss/utils'
55
import type { Result } from '@sniptt/monads'
66
import { Err, Ok } from '@sniptt/monads'
77
import type { AxiosResponse } from 'axios'
@@ -73,15 +73,15 @@ export const calculateChainflipMinPrice = ({
7373
sellAsset: Asset
7474
buyAsset: Asset
7575
}): string => {
76-
const sellAmountCryptoPrecision = fromBaseUnit(
77-
sellAmountIncludingProtocolFeesCryptoBaseUnit,
78-
sellAsset.precision,
79-
)
80-
81-
const buyAmountCryptoPrecision = fromBaseUnit(
82-
buyAmountAfterFeesCryptoBaseUnit,
83-
buyAsset.precision,
84-
)
76+
const sellAmountCryptoPrecision = BigAmount.fromBaseUnit({
77+
value: sellAmountIncludingProtocolFeesCryptoBaseUnit,
78+
precision: sellAsset.precision,
79+
}).toPrecision()
80+
81+
const buyAmountCryptoPrecision = BigAmount.fromBaseUnit({
82+
value: buyAmountAfterFeesCryptoBaseUnit,
83+
precision: buyAsset.precision,
84+
}).toPrecision()
8585

8686
const estimatedPrice = bn(buyAmountCryptoPrecision).div(sellAmountCryptoPrecision)
8787

packages/swapper/src/swappers/CowSwapper/utils/helpers/helpers.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import type { ChainId } from '@shapeshiftoss/caip'
22
import { fromAssetId } from '@shapeshiftoss/caip'
33
import type { Asset, OrderQuoteResponse } from '@shapeshiftoss/types'
44
import {
5+
BigAmount,
56
bn,
67
bnOrZero,
78
convertBasisPointsToDecimalPercentage,
89
convertPrecision,
9-
fromBaseUnit,
1010
} from '@shapeshiftoss/utils'
1111
import type { Result } from '@sniptt/monads'
1212
import { Err, Ok } from '@sniptt/monads'
@@ -138,15 +138,15 @@ export const getValuesFromQuoteResponse = ({
138138
slippageTolerancePercentageDecimal,
139139
})
140140

141-
const buyAmountAfterAffiliateFeesCryptoPrecision = fromBaseUnit(
142-
buyAmountAfterAffiliateFeesCryptoBaseUnit,
143-
buyAsset.precision,
144-
)
141+
const buyAmountAfterAffiliateFeesCryptoPrecision = BigAmount.fromBaseUnit({
142+
value: buyAmountAfterAffiliateFeesCryptoBaseUnit,
143+
precision: buyAsset.precision,
144+
}).toPrecision()
145145

146-
const sellAmountCryptoPrecision = fromBaseUnit(
147-
sellAmountAfterFeesCryptoBaseUnit,
148-
sellAsset.precision,
149-
)
146+
const sellAmountCryptoPrecision = BigAmount.fromBaseUnit({
147+
value: sellAmountAfterFeesCryptoBaseUnit,
148+
precision: sellAsset.precision,
149+
}).toPrecision()
150150

151151
const rate = bnOrZero(buyAmountAfterAffiliateFeesCryptoPrecision)
152152
.div(sellAmountCryptoPrecision)

packages/swapper/src/swappers/SunioSwapper/utils/getQuoteOrRate.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { tronChainId } from '@shapeshiftoss/caip'
2-
import { bn, contractAddressOrUndefined } from '@shapeshiftoss/utils'
2+
import { BigAmount, bn, contractAddressOrUndefined } from '@shapeshiftoss/utils'
33
import type { Result } from '@sniptt/monads'
44
import { Err, Ok } from '@sniptt/monads'
55
import { TronWeb } from 'tronweb'
@@ -198,9 +198,10 @@ export async function getQuoteOrRate(
198198
}
199199
}
200200

201-
const buyAmountCryptoBaseUnit = bn(bestRoute.amountOut)
202-
.times(bn(10).pow(buyAsset.precision))
203-
.toFixed(0)
201+
const buyAmountCryptoBaseUnit = BigAmount.fromPrecision({
202+
value: bestRoute.amountOut,
203+
precision: buyAsset.precision,
204+
}).toBaseUnit()
204205

205206
// Calculate protocol fees only for quotes
206207
const protocolFeeCryptoBaseUnit = isQuote

0 commit comments

Comments
 (0)