-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPurchaseBundler.ts
80 lines (70 loc) · 2.11 KB
/
PurchaseBundler.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
import { Address, decodeAbiParameters, Hex } from 'viem';
import { Wallet } from '@/contracts';
import { MslV5 } from '@/contracts/MslV5';
import { MslV6 } from '@/contracts/MslV6';
import { purchaseBundlerAbi } from '@/generated/blockchain/v6';
import { BaseContract } from './BaseContract';
export class PurchaseBundler extends BaseContract<typeof purchaseBundlerAbi> {
msl: MslV5 | MslV6;
constructor({
contractAddress,
walletClient,
msl,
}: {
contractAddress: Address;
msl: MslV5 | MslV6;
walletClient: Wallet;
}) {
super({
walletClient,
address: contractAddress,
abi: purchaseBundlerAbi,
});
this.msl = msl;
}
static EXECUTION_INFO = {
name: 'executionInfo',
type: 'tuple',
components: [
{
name: 'reservoirExecutionInfo',
type: 'tuple',
components: [
{ name: 'module', type: 'address' },
{ name: 'data', type: 'bytes' },
{ name: 'value', type: 'uint256' },
],
},
{ name: 'contractMustBeOwner', type: 'bool' },
],
} as const;
async executeSell({ repaymentCalldata, price }: { repaymentCalldata: Hex; price: bigint }) {
const repaymentArgs = this.msl.decodeRepaymentCalldata(repaymentCalldata);
const callbackData = decodeAbiParameters(
[PurchaseBundler.EXECUTION_INFO],
repaymentArgs.data.callbackData,
);
const { principalAddress, nftCollateralAddress, nftCollateralTokenId } = repaymentArgs.loan;
const txHash = await this.safeContractWrite.executeSell([
[principalAddress],
[price],
[nftCollateralAddress],
[nftCollateralTokenId],
callbackData[0].reservoirExecutionInfo.module,
[repaymentCalldata],
]);
return {
txHash,
waitTxInBlock: async () => {
const receipt = await this.bcClient.waitForTransactionReceipt({
hash: txHash,
});
const events = this.parseEventLogs('SellAndRepayExecuted', receipt.logs);
if (events.length !== 1) {
throw new Error('Sell and Repay not executed');
}
return receipt;
},
};
}
}