Popcorn-Limited / vaultcraft-sdk

0 stars 2 forks source link

use wallet client for simulation #13

Open 0xruhum opened 1 year ago

0xruhum commented 1 year ago

We currently execute the simulation using the public client. But, we can do it with the wallet client as well. That way the user isn't forced to provide the account in the call options:

vault.ts L131-140

    async deposit(amount: bigint, receiver: Address, options: WriteOptions): Promise<Hash> {
        const { request } = await this.publicClient.simulateContract({
            ...options,
            ...this.baseObj,
            functionName: "deposit",
            args: [amount, receiver],
        });

        return this.walletClient.writeContract(request);
    };

Can be changed to:

    async deposit(amount: bigint, receiver: Address, options?: WriteOptions): Promise<Hash> {
        const { request } = await this.walletClient.simulateContract({
            ...options,
            ...this.baseObj,
            functionName: "deposit",
            args: [amount, receiver],
        });

        return this.walletClient.writeContract(request);
    };

That way, you don't have to call deposit() like this anymore since the account will be embedded in the wallet client:

deposit(1e18, "0x", {account: "0x..."});

This will need some changes to the walletClient parameter's type as well.

0xruhum commented 11 months ago

The separation between wallet and public clients is deliberate. Forcing the usage of only one wallet could be an issue if the wallet client in a frontend project doesn't support public methods:

https://viem.sh/docs/faq.html#why-doesn-t-wallet-client-support-public-actions