-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.ts
More file actions
89 lines (78 loc) · 2.93 KB
/
index.ts
File metadata and controls
89 lines (78 loc) · 2.93 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
import type { Delegation, DelegationExecution } from 'universal-types';
import {
decodeERC20BalanceGteWrapEnforcerTerms,
decodeEnforcerERC20TransferAmount,
encodeDelegation,
encodeExternalCallEnforcerArgs,
encodeSingleExecution,
getERC20BalanceGteWrapEnforcerFromDelegation,
getErc20TransferAmountEnforcerFromDelegation,
getExternalHookEnforcerFromDelegation,
} from './utils.js';
import { getResolverWalletClient } from '../../resolver/resolver-wallet-client.js';
import type { ValidChain } from 'universal-data';
import {
SINGLE_EXECUTION_MODE,
delegationManagerAbi,
multicallAbi,
universalDeployments,
} from 'universal-data';
import { encodeFunctionData, erc20Abi } from 'viem';
import { getHookActions } from './get-actions.js';
export async function processLimitOrderDelegation({
chainId,
delegation,
}: { chainId: ValidChain['id']; delegation: Delegation }) {
const { erc20TransferAmountEnforcer } =
getErc20TransferAmountEnforcerFromDelegation(delegation);
const { token: tokenOut, amount: amountOut } =
decodeEnforcerERC20TransferAmount(erc20TransferAmountEnforcer.terms);
const { erc20BalanceGteWrapEnforcer } =
getERC20BalanceGteWrapEnforcerFromDelegation(delegation);
const { token: tokenIn, amount: amountIn } =
decodeERC20BalanceGteWrapEnforcerTerms(erc20BalanceGteWrapEnforcer.terms);
const { externalHookEnforcer, index: externalHookEnforcerIndex } =
getExternalHookEnforcerFromDelegation(delegation);
const { withdrawActions, depositActions } = getHookActions({
amountIn,
amountOut,
delegator: delegation.delegator,
tokenIn,
tokenOut,
});
delegation.caveats[externalHookEnforcerIndex] = {
...externalHookEnforcer,
// Update the args of the external hook enforcer to include the data for the Aave V3 deposit
args: encodeExternalCallEnforcerArgs({
target: universalDeployments.Multicall,
callData: encodeFunctionData({
abi: multicallAbi,
functionName: 'multicall',
args: [[...withdrawActions, ...depositActions]],
}),
}),
};
// Get resolver wallet client
const resolverWalletClient = getResolverWalletClient(chainId);
// Set the delegation execution to transfer the delegator tokens to the multicall contract
const execution: DelegationExecution = {
value: 0n,
target: tokenOut,
calldata: encodeFunctionData({
abi: erc20Abi,
functionName: 'transfer',
args: [universalDeployments.Multicall, amountOut],
}),
};
const permissionContexts = [encodeDelegation(delegation)];
const executionCallData = [encodeSingleExecution(execution)];
const executionModes = SINGLE_EXECUTION_MODE;
// Redeem the delegation
const txHash = await resolverWalletClient.writeContract({
address: universalDeployments.DelegationManager,
abi: delegationManagerAbi,
functionName: 'redeemDelegations',
args: [permissionContexts, executionModes, executionCallData],
});
return txHash;
}