-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathgetQueryCallsAttributes.ts
More file actions
132 lines (123 loc) · 4.56 KB
/
Copy pathgetQueryCallsAttributes.ts
File metadata and controls
132 lines (123 loc) · 4.56 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { Address, PoolType } from '@/types';
import { BaseToken } from '@/entities/baseToken';
import {
ZERO_ADDRESS,
BALANCER_RELAYER,
getPoolAddress,
ChainId,
} from '@/utils';
import {
AddLiquidityNestedCallAttributes,
AddLiquidityNestedInputV2,
} from './types';
import { NestedPoolV2, PoolKind } from '@/entities/types';
import { Relayer } from '@/entities/relayer';
export const getQueryCallsAttributes = (
{ amountsIn, chainId, fromInternalBalance }: AddLiquidityNestedInputV2,
pools: NestedPoolV2[],
): AddLiquidityNestedCallAttributes[] => {
/**
* Overall logic to build sequence of add liquidity calls:
* 1. Go from bottom pool to up filling out input amounts and output refs
* 2. Inputs will be amountsIn provided, output of the previous level or 0n
* 3. Output at max level is the bptOut
*/
const poolsSortedByLevel = pools.sort((a, b) => a.level - b.level);
const accountAddressPlaceholder = ZERO_ADDRESS;
const calls: AddLiquidityNestedCallAttributes[] = [];
for (const pool of poolsSortedByLevel) {
const sortedTokens = pool.tokens
.sort((a, b) => a.index - b.index)
.map((t) => new BaseToken(chainId, t.address, t.decimals));
const maxAmountsIn = getMaxAmountsIn(sortedTokens, amountsIn, calls);
if (maxAmountsIn.every((a) => a.amount === 0n && !a.isRef)) {
continue;
}
calls.push({
chainId: chainId,
sortedTokens,
poolId: pool.id,
poolAddress: pool.address,
poolType: pool.type,
kind:
pool.type === PoolType.ComposableStable
? PoolKind.COMPOSABLE_STABLE_V2
: PoolKind.WEIGHTED,
sender: getSender(maxAmountsIn, accountAddressPlaceholder, chainId),
recipient: '0x', // set as placeholder - will be updated after all calls are created
maxAmountsIn,
minBptOut: 0n, // limits set to zero for query calls
fromInternalBalance: fromInternalBalance ?? false,
outputReference: Relayer.toChainedReference(
BigInt(poolsSortedByLevel.indexOf(pool)),
),
});
}
updateRecipients(calls, accountAddressPlaceholder);
return calls;
};
const getMaxAmountsIn = (
sortedTokens: BaseToken[],
amountsIn: { address: Address; rawAmount: bigint }[],
calls: AddLiquidityNestedCallAttributes[],
): { amount: bigint; isRef: boolean }[] => {
return sortedTokens.map((token) => {
/**
* There are 3 possible scenarios:
* 1. token has amountIn provided by the user -> return amount
* 2. token is the output of a previous add liquidity call -> return outputRef
* 3. otherwise -> return zero
*/
// 1. token has amountIn provided by the user -> return amount
const amountIn = amountsIn.find((a) => token.isSameAddress(a.address));
if (amountIn !== undefined) {
return {
amount: amountIn.rawAmount,
isRef: false,
};
}
// 2. token is the output of a previous add liquidity call -> return outputRef
const previousCall = calls.find(
(call) => getPoolAddress(call.poolId) === token.address,
);
if (previousCall !== undefined) {
return {
amount: previousCall.outputReference,
isRef: true,
};
}
// 3. otherwise -> return zero
return {
amount: 0n,
isRef: false,
};
});
};
// Sender's logic: if there is at least one amountIn that is not a reference,
// then the sender is the user, otherwise it's the relayer.
const getSender = (
maxAmountsIn: { amount: bigint; isRef: boolean }[],
accountAddress: Address,
chainId: ChainId,
): Address => {
return maxAmountsIn.some((a) => !a.isRef && a.amount > 0n)
? accountAddress
: BALANCER_RELAYER[chainId];
};
// Recipient's logic: if there is a following call, then the recipient is the
// sender of that call, otherwise it's the user.
const updateRecipients = (
calls: AddLiquidityNestedCallAttributes[],
accountAddress: Address,
) => {
for (const call of calls) {
const followingCall = calls.find((_call) =>
_call.maxAmountsIn.some((a) => a.amount === call.outputReference),
);
if (followingCall !== undefined) {
call.recipient = followingCall.sender;
} else {
call.recipient = accountAddress;
}
}
};