Skip to content

Commit

Permalink
alt safe
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Aug 14, 2024
1 parent dc0787b commit 05eadcc
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions src/lib/alt-safe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import SafeApiKit, { SafeInfoResponse } from "@safe-global/api-kit";
import { Safe4337Pack } from "@safe-global/relay-kit";
import EthSafeOperation from "@safe-global/relay-kit/dist/src/packs/safe-4337/SafeOperation";
import { calculateSafeUserOperationHash } from "@safe-global/relay-kit/dist/src/packs/safe-4337/utils";
import { ethers } from "ethers";
import { getDeployment } from "./safe";
import { getSafeModuleSetupDeployment } from "@safe-global/safe-modules-deployments";

const isRelevantSafe = (safe: SafeInfoResponse, moduleAddress: string) =>

Check failure on line 9 in src/lib/alt-safe.ts

View workflow job for this annotation

GitHub Actions / types

Missing return type on function
safe.threshold === 1 && safe.fallbackHandler === moduleAddress;

export async function existingSafe(
signer: string,
chainId: bigint,
moduleAddress: string
): Promise<string | undefined> {
const apiKit = new SafeApiKit({
chainId,
});
const safes = (await apiKit.getSafesByOwner(signer)).safes;
const safeInfos = await Promise.all(
safes.map((safeAddress) => apiKit.getSafeInfo(safeAddress))
);
const relevantSafes = safeInfos.filter((info) =>
isRelevantSafe(info, moduleAddress)
);
console.log("Relevant Safes", relevantSafes);
if (relevantSafes.length > 0) {
if (relevantSafes.length > 1) {
console.warn(
`Found multiple relevant Safes for ${signer} - using the first`
);
}
return relevantSafes[0].address;
}
}

export async function loadSafeKit(
rpcUrl: string,
bundlerUrl: string,
nearSigner: string
): Promise<Safe4337Pack> {
const provider = new ethers.JsonRpcProvider(rpcUrl);
const [safeModuleAddress, network] = await Promise.all([
getSafeModuleAddress(provider),
provider.getNetwork(),
]);
const safeAddress = await existingSafe(
nearSigner,
network.chainId,
safeModuleAddress
);
let options = safeAddress
? { safeAddress }
: {
owners: [nearSigner],
threshold: 1,
};
const safe4337Pack = await Safe4337Pack.init({
provider: rpcUrl,
bundlerUrl,
// Error: Incompatibility detected: Safe modules version 0.3.0 is not supported.
// The SDK can use 0.2.0 only.
// safeModulesVersion: "0.3.0",
options,
});
return safe4337Pack;
}

async function getSafeModuleAddress(
provider: ethers.JsonRpcProvider
): Promise<string> {
return (
await getDeployment(getSafeModuleSetupDeployment, {
provider,
version: "0.2.0",
})
).getAddress();
}

// This should be part of the kit!
export async function getSafeOpHash(

Check failure on line 82 in src/lib/alt-safe.ts

View workflow job for this annotation

GitHub Actions / types

Missing return type on function
provider: ethers.JsonRpcProvider,
safeKit: Safe4337Pack,
safeOp: EthSafeOperation
) {
return calculateSafeUserOperationHash(
safeOp.data,
BigInt(await safeKit.getChainId()),
// The safe isn't deployed so we have to fetch the fallback Handler on our own:
// await safeKit.protocolKit.getFallbackHandler()
await getSafeModuleAddress(provider)
);
}

0 comments on commit 05eadcc

Please sign in to comment.