Skip to content

Commit 8e94d90

Browse files
committed
fix: pass account address instead of wallet client
1 parent e0e9a8f commit 8e94d90

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

src/GearboxSDK.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import { AddressMap, formatBN } from "./utils";
2828
import { createAnvilClient } from "./utils/viem";
2929

3030
export interface SDKAttachOptions {
31+
/**
32+
* Account address for contract write simulations
33+
*/
34+
account?: Address;
3135
/**
3236
* RPC URL to use
3337
*/

src/accounts/CreditAccountsService.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Address, ContractFunctionArgs, WalletClient } from "viem";
1+
import type { Address, ContractFunctionArgs } from "viem";
22
import { decodeFunctionData, encodeFunctionData } from "viem";
33

44
import {
@@ -41,14 +41,12 @@ export interface CreditAccountFilter {
4141

4242
export class CreditAccountsService extends SDKConstruct {
4343
#compressor: Address;
44-
#wallet: WalletClient;
4544

46-
constructor(sdk: GearboxSDK, wallet: WalletClient) {
45+
constructor(sdk: GearboxSDK) {
4746
super(sdk);
4847
this.#compressor = sdk.addressProvider.getLatestVersion(
4948
AP_CREDIT_ACCOUNT_COMPRESSOR,
5049
);
51-
this.#wallet = wallet;
5250
}
5351

5452
/**
@@ -81,7 +79,8 @@ export class CreditAccountsService extends SDKConstruct {
8179
// if (this.provider.testnet && priceUpdateTxs.length > 0) {
8280
// await this.provider.warpSafe(timestamp);
8381
// }
84-
const resp = await simulateMulticall(this.#wallet, {
82+
const resp = await simulateMulticall(this.provider.publicClient, {
83+
account: this.provider.account,
8584
contracts: [
8685
...priceUpdateTxs.map(rawTxToMulticallPriceUpdate),
8786
{
@@ -166,7 +165,7 @@ export class CreditAccountsService extends SDKConstruct {
166165
*/
167166
public async fullyLiquidate(
168167
account: CreditAccountData,
169-
to: Address,
168+
to?: Address,
170169
slippage = 50n,
171170
): Promise<RawTx> {
172171
const cm = this.sdk.marketRegister.findCreditManager(account.creditManager);
@@ -183,11 +182,15 @@ export class CreditAccountsService extends SDKConstruct {
183182
cm.creditFacade.address,
184183
priceUpdates,
185184
);
185+
const recipient = to ?? this.sdk.provider.account;
186+
if (!recipient) {
187+
throw new Error("liquidate account: assets recipient not specied");
188+
}
186189
return cm.creditFacade.createRawTx({
187190
functionName: "liquidateCreditAccount",
188191
args: [
189192
account.creditAccount,
190-
to,
193+
recipient,
191194
[...priceUpdateCalls, ...preview.calls],
192195
],
193196
});
@@ -206,15 +209,19 @@ export class CreditAccountsService extends SDKConstruct {
206209
operation: "close" | "zeroDebt",
207210
ca: CreditAccountData,
208211
assetsToKeep: Address[],
209-
to: Address,
212+
to?: Address,
210213
slippage = 50n,
211214
): Promise<RawTx> {
212215
const cm = this.sdk.marketRegister.findCreditManager(ca.creditManager);
216+
const recipient = to ?? this.sdk.provider.account;
217+
if (!recipient) {
218+
throw new Error("close account: assets recipient not specied");
219+
}
213220
const calls = await this.#prepareCloseCreditAccount(
214221
ca,
215222
cm,
216223
assetsToKeep,
217-
to,
224+
recipient,
218225
slippage,
219226
);
220227
return cm.creditFacade.createRawTx({
@@ -236,7 +243,8 @@ export class CreditAccountsService extends SDKConstruct {
236243
args: GetCreditAccountsArgs;
237244
}): Promise<[accounts: CreditAccountData[], newOffset: bigint]> {
238245
if (priceUpdateTxs?.length) {
239-
const resp = await simulateMulticall(this.#wallet, {
246+
const resp = await simulateMulticall(this.provider.publicClient, {
247+
account: this.provider.account,
240248
contracts: [
241249
...priceUpdateTxs.map(rawTxToMulticallPriceUpdate),
242250
{

src/chain/Provider.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Chain, PublicClient } from "viem";
1+
import type { Address, Chain, PublicClient } from "viem";
22
import { createPublicClient, defineChain, http } from "viem";
33

44
import { AddressLabeller } from "../base/AddressLabeller";
@@ -7,21 +7,39 @@ import type { NetworkType } from "./chains";
77
import { chains } from "./chains";
88

99
export interface ProviderOptions {
10+
/**
11+
* Account address for contract write simulations
12+
*/
13+
account?: Address;
14+
/**
15+
* RPC URL to use
16+
*/
1017
rpcURL: string;
18+
/**
19+
* RPC client timeout in milliseconds
20+
*/
1121
timeout?: number;
22+
/**
23+
* Chain Id needs to be set, because we sometimemes use forked testnets with different chain ids
24+
*/
1225
chainId: number;
26+
/**
27+
* NetworkType needs to be set, because we sometimemes use forked testnets with different chain ids
28+
*/
1329
networkType: NetworkType;
1430
}
1531

1632
export class Provider {
33+
public readonly account?: Address;
1734
public readonly chainId: number;
1835
public readonly chain: Chain;
1936
public readonly networkType: NetworkType;
2037
public readonly publicClient: PublicClient;
2138
public readonly addressLabels: IAddressLabeller;
2239

2340
constructor(opts: ProviderOptions) {
24-
const { chainId, networkType, rpcURL, timeout = 120_000 } = opts;
41+
const { account, chainId, networkType, rpcURL, timeout = 120_000 } = opts;
42+
this.account = account;
2543
this.chainId = chainId;
2644
this.networkType = networkType;
2745

src/utils/viem/simulateMulticall.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export type MulticallParameters<
3939
} = {},
4040
> = Pick<
4141
CallParameters,
42-
"blockNumber" | "blockTag" | "stateOverride" | "gas"
42+
"blockNumber" | "blockTag" | "stateOverride" | "gas" | "account"
4343
> & {
4444
allowFailure?: allowFailure | boolean | undefined;
4545
batchSize?: number | undefined;
@@ -85,13 +85,14 @@ export async function simulateMulticall<
8585
parameters: MulticallParameters<contracts, allowFailure>,
8686
): Promise<MulticallReturnType<contracts, allowFailure>> {
8787
const {
88+
account,
8889
allowFailure = true,
8990
batchSize: batchSize_,
9091
blockNumber,
9192
blockTag,
93+
gas,
9294
multicallAddress: multicallAddress_,
9395
stateOverride,
94-
gas,
9596
} = parameters;
9697
const contracts = parameters.contracts as ContractFunctionParameters[];
9798

@@ -179,6 +180,7 @@ export async function simulateMulticall<
179180
simulateContract,
180181
"simulateContract",
181182
)({
183+
account,
182184
abi: multicall3Abi,
183185
address: multicallAddress!,
184186
args: [calls],

0 commit comments

Comments
 (0)