-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce boilerplate for base SimpleAccount config (#124)
- Loading branch information
Showing
5 changed files
with
69 additions
and
44 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
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 |
---|---|---|
@@ -1,22 +1,67 @@ | ||
import { Address, PublicClient } from "viem"; | ||
import { JsonRpcProvider } from "ethers"; | ||
import { | ||
Account, | ||
Address, | ||
Chain, | ||
PublicClient, | ||
Transport, | ||
WalletClient, | ||
} from "viem"; | ||
import { JsonRpcProvider, Signer } from "ethers"; | ||
import { AccountAbi, FactoryAbi } from "./abi/simpleAccount"; | ||
import { RequiredAccountOpts } from "../types"; | ||
import { RequestSignatureFunc } from "../hooks"; | ||
import { RequestSignature } from "../hooks"; | ||
|
||
export const base = ( | ||
owner: Address, | ||
requestSignature: RequestSignatureFunc, | ||
const FACTORY_ADDRESS = "0x9406Cc6185a346906296840746125a0E44976454"; | ||
|
||
type SimpleAccountArgsWithViemEOA<E extends WalletClient> = [ | ||
ethClient: PublicClient | JsonRpcProvider, | ||
eoa: E, | ||
]; | ||
type SimpleAccountArgsWithViemEOANoHoist<E extends WalletClient> = [ | ||
ethClient: PublicClient | JsonRpcProvider, | ||
eoa: E, | ||
account: Account, | ||
]; | ||
type SimpleAccountArgsWithEthersSigner<E extends Signer> = [ | ||
ethClient: PublicClient | JsonRpcProvider, | ||
eoa: E, | ||
]; | ||
|
||
export const base = <E extends WalletClient | Signer>( | ||
...args: E extends WalletClient | ||
? E["account"] extends Account | ||
? SimpleAccountArgsWithViemEOA<E> | ||
: SimpleAccountArgsWithViemEOANoHoist<E> | ||
: E extends Signer | ||
? SimpleAccountArgsWithEthersSigner<E> | ||
: never | ||
): RequiredAccountOpts<typeof AccountAbi, typeof FactoryAbi> => { | ||
const [ethClient, eoa, account] = args; | ||
|
||
return { | ||
accountAbi: AccountAbi, | ||
factoryAbi: FactoryAbi, | ||
factoryAddress: "0x9406Cc6185a346906296840746125a0E44976454", | ||
factoryAddress: FACTORY_ADDRESS, | ||
ethClient, | ||
setFactoryData(salt, encoder) { | ||
return encoder("createAccount", [owner, salt]); | ||
async setFactoryData(salt, encoder) { | ||
if (account !== undefined) { | ||
return encoder("createAccount", [account.address, salt]); | ||
} else if ("getAddresses" in eoa) { | ||
return encoder("createAccount", [(await eoa.getAddresses())[0], salt]); | ||
} else { | ||
return encoder("createAccount", [ | ||
(await eoa.getAddress()) as Address, | ||
salt, | ||
]); | ||
} | ||
}, | ||
requestSignature, | ||
requestSignature: | ||
"getAddresses" in eoa | ||
? account !== undefined | ||
? RequestSignature.withViemWalletClient(eoa, account) | ||
: RequestSignature.withViemWalletClient( | ||
eoa as WalletClient<Transport, Chain | undefined, Account>, | ||
) | ||
: RequestSignature.withEthersSigner(eoa), | ||
}; | ||
}; |
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