-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
497 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import SafeApiKit, { SafeInfoResponse } from "@safe-global/api-kit"; | ||
import { Safe4337Pack } from "@safe-global/relay-kit"; | ||
import { ethers } from "ethers"; | ||
|
||
const SAFE_4337_MODULE = "0x75cf11467937ce3F2f357CE24ffc3DBF8fD5c226"; | ||
const isRelevantSafe = (safe: SafeInfoResponse) => | ||
safe.threshold === 1 && safe.fallbackHandler === SAFE_4337_MODULE; | ||
const sanitizeAddress = (dirty: string) => ethers.getAddress(dirty); | ||
|
||
export async function existingSafe( | ||
signer: string, | ||
chainId: bigint, | ||
): 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)); | ||
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 signer = sanitizeAddress(nearSigner); | ||
const provider = new ethers.JsonRpcProvider(rpcUrl); | ||
const safeAddress = await existingSafe( | ||
signer, | ||
(await provider.getNetwork()).chainId, | ||
); | ||
let options = safeAddress | ||
? { safeAddress } | ||
: { | ||
owners: [signer], | ||
threshold: 1, | ||
}; | ||
const safe4337Pack = await Safe4337Pack.init({ | ||
provider: rpcUrl, | ||
bundlerUrl, | ||
options, | ||
customContracts: { | ||
// Why do I need to specify this? | ||
safe4337ModuleAddress: SAFE_4337_MODULE | ||
} | ||
// ... | ||
}); | ||
return safe4337Pack; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.