-
Notifications
You must be signed in to change notification settings - Fork 180
/
addAggregatorAndDestinationToMemo.ts
115 lines (100 loc) · 4.13 KB
/
addAggregatorAndDestinationToMemo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import type { ChainId } from '@shapeshiftoss/caip'
import { bchChainId, CHAIN_NAMESPACE, type ChainNamespace } from '@shapeshiftoss/caip'
import { bn } from '@shapeshiftoss/chain-adapters'
import assert from 'assert'
import BigNumber from 'bignumber.js'
import type { Address } from 'viem'
import { fromBaseUnit } from 'lib/math'
import { subtractBasisPointAmount } from 'state/slices/tradeQuoteSlice/utils'
import { BCH_MAXIMUM_BYTES_LENGTH, UTXO_MAXIMUM_BYTES_LENGTH } from '../constants'
import { MEMO_PART_DELIMITER } from './constants'
import { shortenedNativeAssetNameByNativeAssetName } from './longTailHelpers'
export const addAggregatorAndDestinationToMemo = ({
sellChainId,
quotedMemo,
aggregator,
finalAssetAddress,
minAmountOut,
slippageBps,
finalAssetPrecision,
chainNamespace,
}: {
sellChainId: ChainId
slippageBps: BigNumber.Value
quotedMemo: string | undefined
aggregator: Address
finalAssetAddress: Address
minAmountOut: string
finalAssetPrecision: number
chainNamespace: ChainNamespace
}) => {
if (!quotedMemo) throw new Error('no memo provided')
const [
prefix,
nativeAssetName,
address,
nativeAssetLimitWithManualSlippage,
affiliate,
affiliateBps,
] = quotedMemo.split(MEMO_PART_DELIMITER)
const finalAssetLimitWithManualSlippage = subtractBasisPointAmount(
bn(minAmountOut).toFixed(0, BigNumber.ROUND_DOWN),
slippageBps,
BigNumber.ROUND_DOWN,
)
const maximumPrecision = 6
const endingExponential = finalAssetPrecision - maximumPrecision
const finalAssetLimitCryptoPrecision = fromBaseUnit(
finalAssetLimitWithManualSlippage,
finalAssetPrecision,
maximumPrecision,
)
const shouldPrependZero = endingExponential < 10
const thorAggregatorExponential = shouldPrependZero
? `0${endingExponential > 0 ? endingExponential : '1'}`
: endingExponential
// The THORChain aggregators expects this amount to be an exponent, we need to add two numbers at the end which are used at exponents in the contract
// We trim 10 of precisions to make sure the THORChain parser can handle the amount without precisions and rounding issues
// If the finalAssetPrecision is under 5, the THORChain parser won't fail and we add one exponent at the end so the aggregator contract won't multiply the amount
const finalAssetLimitWithTwoLastNumbersAsExponent = `${
finalAssetPrecision < maximumPrecision
? finalAssetLimitWithManualSlippage
: finalAssetLimitCryptoPrecision.replace('.', '')
}${thorAggregatorExponential}`
// Paranoia assertion - expectedAmountOut should never be 0 as it would likely lead to a loss of funds.
assert(
BigInt(finalAssetLimitWithTwoLastNumbersAsExponent) > 0n,
'expected finalAssetLimitWithManualSlippage to be a positive amount',
)
const aggregatorLastTwoChars = aggregator.slice(aggregator.length - 2, aggregator.length)
const shortenedNativeAssetName =
shortenedNativeAssetNameByNativeAssetName[
nativeAssetName as keyof typeof shortenedNativeAssetNameByNativeAssetName
]
assert(shortenedNativeAssetName, 'cannot find shortened native asset name')
// Thorchain memo format:
// SWAP:ASSET:DESTADDR:LIM:AFFILIATE:FEE:DEX Aggregator Addr:Final Asset Addr:MinAmountOut
// see https://gitlab.com/thorchain/thornode/-/merge_requests/2218 for reference
const memo = [
prefix,
shortenedNativeAssetName,
address,
nativeAssetLimitWithManualSlippage,
affiliate,
affiliateBps,
aggregatorLastTwoChars,
finalAssetAddress,
finalAssetLimitWithTwoLastNumbersAsExponent,
].join(MEMO_PART_DELIMITER)
const memoBytesLength = new Blob([memo]).size
// Dogecoin, BTC and LTC only supports 80 bytes memo and we don't want to lose more precision for now
// We already check it in the L1ToLongtail handler but paranoia double security as this function is reusable.
if (chainNamespace === CHAIN_NAMESPACE.Utxo && sellChainId !== bchChainId) {
assert(memoBytesLength < UTXO_MAXIMUM_BYTES_LENGTH, 'memo is too long')
}
// BCH supports 220 bytes of data
if (sellChainId === bchChainId) {
assert(memoBytesLength < BCH_MAXIMUM_BYTES_LENGTH, 'memo is too long')
}
return memo
}