Skip to content

Commit c736e89

Browse files
committed
fix: start withdrawal
1 parent a8ec670 commit c736e89

File tree

2 files changed

+18
-72
lines changed

2 files changed

+18
-72
lines changed

src/sdk/accounts/AbstractCreditAccountsService.ts

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -652,30 +652,35 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
652652
* @param props - {@link StartDelayedWithdrawalProps}
653653
* @returns
654654
*/
655-
public async startDelayedWithdrawal_Mellow({
655+
public async startDelayedWithdrawal({
656656
creditAccount,
657657
minQuota,
658658
averageQuota,
659659

660-
instantWithdrawals,
661-
delayedWithdrawals,
662-
sourceAmount,
663-
sourceToken,
660+
preview,
664661
}: StartDelayedWithdrawalProps): Promise<CreditAccountOperationResult> {
665662
const cm = this.sdk.marketRegister.findCreditManager(
666663
creditAccount.creditManager,
667664
);
668665

669-
const balances = [...instantWithdrawals, ...delayedWithdrawals].filter(
670-
a => a.balance > 0,
671-
);
666+
const record = preview.outputs.reduce<Record<Address, bigint>>((acc, o) => {
667+
const token = o.token.toLowerCase() as Address;
668+
acc[token] = (acc[token] || 0n) + o.amount;
669+
670+
return acc;
671+
}, {});
672+
const balances = Object.entries(record);
673+
672674
const storeExpectedBalances: MultiCall = {
673675
target: cm.creditFacade.address,
674676
callData: encodeFunctionData({
675677
abi: iCreditFacadeV300MulticallAbi,
676678
functionName: "storeExpectedBalances",
677679
args: [
678-
balances.map(a => ({ token: a.token, amount: a.balance - 10n })),
680+
balances.map(([token, amount]) => ({
681+
token: token as Address,
682+
amount: amount - 10n,
683+
})),
679684
],
680685
}),
681686
};
@@ -694,43 +699,10 @@ export abstract class AbstractCreditAccountService extends SDKConstruct {
694699
desiredQuotas: averageQuota,
695700
});
696701

697-
const mellowAdapter = cm.creditManager.adapters.mustGet(sourceToken);
698-
const redeem: MultiCall = {
699-
target: mellowAdapter.address,
700-
callData: encodeFunctionData({
701-
abi: ierc4626AdapterAbi,
702-
functionName: "redeem",
703-
args: [sourceAmount, ADDRESS_0X0, ADDRESS_0X0],
704-
}),
705-
};
706-
707-
const CLAIMER = "0x25024a3017B8da7161d8c5DCcF768F8678fB5802";
708-
const mellowClaimerAdapter = cm.creditManager.adapters.mustGet(CLAIMER);
709-
710-
const multiAcceptContract = getContract({
711-
address: mellowClaimerAdapter.address,
712-
abi: iMellowClaimerAdapterAbi,
713-
client: this.client,
714-
});
715-
716-
const indices = await multiAcceptContract.read.getMultiVaultSubvaultIndices(
717-
[sourceToken],
718-
);
719-
720-
const multiaccept: MultiCall = {
721-
target: mellowClaimerAdapter.address,
722-
callData: encodeFunctionData({
723-
abi: iMellowClaimerAdapterAbi,
724-
functionName: "multiAccept",
725-
args: [sourceToken, ...indices],
726-
}),
727-
};
728-
729702
const calls: Array<MultiCall> = [
730703
...priceUpdatesCalls,
731704
storeExpectedBalances,
732-
redeem,
733-
multiaccept,
705+
...preview.requestCalls,
734706
compareBalances,
735707
...this.prepareUpdateQuotas(creditAccount.creditFacade, {
736708
minQuota,
@@ -1435,17 +1407,3 @@ const iMellowClaimerAdapterAbi = [
14351407
stateMutability: "nonpayable",
14361408
},
14371409
] as const;
1438-
1439-
const ierc4626AdapterAbi = [
1440-
{
1441-
type: "function",
1442-
inputs: [
1443-
{ name: "shares", internalType: "uint256", type: "uint256" },
1444-
{ name: "", internalType: "address", type: "address" },
1445-
{ name: "", internalType: "address", type: "address" },
1446-
],
1447-
name: "redeem",
1448-
outputs: [{ name: "useSafePrices", internalType: "bool", type: "bool" }],
1449-
stateMutability: "nonpayable",
1450-
},
1451-
] as const;

src/sdk/accounts/types.ts

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -235,21 +235,9 @@ export type PreviewDelayedWithdrawalResult = Awaited<
235235

236236
export interface StartDelayedWithdrawalProps extends PrepareUpdateQuotasProps {
237237
/**
238-
* Amount of source token (ex. cp0xlrt)
239-
*/
240-
sourceAmount: bigint;
241-
/**
242-
* Address of source token (ex. cp0xlrt)
243-
*/
244-
sourceToken: Address;
245-
/**
246-
* Array of token which can be instantly withdrawn
247-
*/
248-
instantWithdrawals: Array<Asset>;
249-
/**
250-
* Array of token which will be withdrawn with a delay
238+
* Withdrawal preview
251239
*/
252-
delayedWithdrawals: Array<Asset>;
240+
preview: PreviewDelayedWithdrawalResult;
253241
/**
254242
* Minimal credit account data on which operation is performed
255243
*/
@@ -591,7 +579,7 @@ export interface ICreditAccountsService extends SDKConstruct {
591579
* @param props - {@link StartDelayedWithdrawalProps}
592580
* @returns All necessary data to execute the transaction (call, credit facade)
593581
*/
594-
startDelayedWithdrawal_Mellow(
582+
startDelayedWithdrawal(
595583
props: StartDelayedWithdrawalProps,
596584
): Promise<CreditAccountOperationResult>;
597585

0 commit comments

Comments
 (0)