Skip to content

Commit 032af55

Browse files
committed
feat: build Safe API kit instance based on network
Without this patch the instantiation of the Safe API Kit object fails due to the Filecoin networks requiring a specific Transaction Service URL. Usually these are determined by the SDK by providing it with just the chain id. Since the TX Service on Filecoin and Filecoin Calibration are hosted by a third party the SDK can't infer them. This adds a strategy that constructs the Safe API Kit instance differently based on the current chain id.
1 parent 1c45546 commit 032af55

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

hooks/useSafeAccounts.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useEffect, useState } from "react";
22
import { useAccount } from "wagmi";
3-
import SafeApiKit from "@safe-global/api-kit";
3+
import { SafeApiStrategyFactory } from "@/safe/SafeApiKitStrategy";
44

55
import { useEnsStore } from "@/lib/ens-store";
66
import { Account } from "@/lib/account-store";
@@ -17,9 +17,8 @@ export function useSafeAccounts() {
1717

1818
setIsLoading(true);
1919
try {
20-
const safeService = new SafeApiKit({
21-
chainId: BigInt(chainId),
22-
});
20+
const safeService =
21+
SafeApiStrategyFactory.getStrategy(chainId).createInstance();
2322

2423
const { safes } = await safeService.getSafesByOwner(address);
2524
setSafeAccounts(

safe/SafeApiKitStrategy.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import SafeApiKit from "@safe-global/api-kit";
2+
3+
export interface SafeApiKitStrategy {
4+
createInstance(): SafeApiKit;
5+
}
6+
7+
export class FilecoinMainnetStrategy implements SafeApiKitStrategy {
8+
createInstance(): SafeApiKit {
9+
return new SafeApiKit({
10+
chainId: BigInt(314),
11+
txServiceUrl: "https://transaction.safe.filecoin.io/api",
12+
});
13+
}
14+
}
15+
16+
export class FilecoinTestnetStrategy implements SafeApiKitStrategy {
17+
createInstance(): SafeApiKit {
18+
console.log("chainId", BigInt(314159), typeof BigInt(314159));
19+
return new SafeApiKit({
20+
chainId: BigInt(314159),
21+
txServiceUrl: "https://transaction-testnet.safe.filecoin.io/api",
22+
});
23+
}
24+
}
25+
26+
export class DefaultSafeApiStrategy implements SafeApiKitStrategy {
27+
constructor(private chainId: number) {}
28+
29+
createInstance(): SafeApiKit {
30+
return new SafeApiKit({
31+
chainId: BigInt(this.chainId),
32+
});
33+
}
34+
}
35+
36+
export class SafeApiStrategyFactory {
37+
static getStrategy(chainId: number): SafeApiKitStrategy {
38+
switch (chainId) {
39+
case 314:
40+
return new FilecoinMainnetStrategy();
41+
case 314159:
42+
return new FilecoinTestnetStrategy();
43+
default:
44+
return new DefaultSafeApiStrategy(chainId);
45+
}
46+
}
47+
}

settings/SafeSettingsSigningStrategy.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { TypedDataEncoder } from "ethers";
2-
import SafeApiKit from "@safe-global/api-kit";
32
import { type EIP712TypedData } from "@safe-global/types-kit";
43
import Safe, {
54
buildSignatureBytes,
@@ -12,6 +11,7 @@ import {
1211
hypercertApiSigningDomainSafe,
1312
type SafeApiSigningDomain,
1413
} from "@/configs/constants";
14+
import { SafeApiStrategyFactory } from "@/safe/SafeApiKitStrategy";
1515

1616
import { SettingsSigningStrategy } from "./SettingsSigningStrategy";
1717

@@ -72,7 +72,9 @@ export class SafeSettingsSigningStrategy extends SettingsSigningStrategy {
7272
provider: this.walletClient as unknown as Eip1193Provider,
7373
safeAddress: this.address,
7474
});
75-
const apiKit = new SafeApiKit({ chainId: BigInt(this.chainId) });
75+
const apiKit = SafeApiStrategyFactory.getStrategy(
76+
this.chainId,
77+
).createInstance();
7678

7779
const typedData = {
7880
domain: hypercertApiSigningDomainSafe(this.chainId, this.address),

0 commit comments

Comments
 (0)