Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix paymaster when using rainbowkit connector #17

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 30 additions & 14 deletions packages/agw-react/src/abstractWalletConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type { WalletDetailsParams } from '@rainbow-me/rainbowkit';
import { type CreateConnectorFn } from '@wagmi/core';
import {
type Chain,
createPublicClient,
createWalletClient,
custom,
Expand All @@ -15,12 +16,18 @@
type EIP1193RequestFn,
type EIP1474Methods,
http,
toHex,
} from 'viem';
import { toAccount } from 'viem/accounts';
import { abstractTestnet } from 'viem/chains';

import { AGW_APP_ID, ICON_URL } from './constants.js';

// TODO: support Abstract mainnet
const VALID_CHAINS: Record<number, Chain> = {
[abstractTestnet.id]: abstractTestnet,
};

/**
* Create a wagmi connector for the Abstract Global Wallet.
*
Expand Down Expand Up @@ -73,9 +80,14 @@
const getAbstractProvider = async (
parameters?: { chainId?: number | undefined } | undefined,
) => {
const chainId = parameters?.chainId ?? abstractTestnet.id;
const chain = VALID_CHAINS[chainId];
if (!chain) {
throw new Error('Unsupported chain');
}
const provider = await connector.getProvider(parameters);
const providerHandleRequest = provider.request;
const handler: EIP1193RequestFn<EIP1474Methods> = async (e: any) => {

Check warning on line 90 in packages/agw-react/src/abstractWalletConnector.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
const { method, params } = e;
switch (method) {
case 'eth_accounts': {
Expand All @@ -97,20 +109,20 @@
case 'eth_signTransaction':
case 'eth_sendTransaction': {
const accounts = await connector.getAccounts();

if (accounts[0] == undefined) {
const account = accounts[0];
if (!account) {
throw new Error('Account not found');
}
const transaction = params[0];

const transport = custom(provider);

const wallet = createWalletClient({
account: accounts[0],
account,
transport,
});

const signer = toAccount({
address: accounts[0],
address: account,
signMessage: wallet.signMessage,
signTransaction:
wallet.signTransaction as CustomSource['signTransaction'],
Expand All @@ -119,21 +131,25 @@
});

const abstractClient = await createAbstractClient({
chain: abstractTestnet,
chain,
signer,
transport,
});

// Undo the automatic formatting applied by Wagmi's eth_signTransaction
// Formatter: https://github.com/wevm/viem/blob/main/src/zksync/formatters.ts#L114
if (transaction.eip712Meta) {
transaction.paymaster =
transaction.eip712Meta.paymasterParams.paymaster;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have eip712Meta but no paymaster params? Might need a null check here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true

transaction.paymasterInput = toHex(
transaction.eip712Meta.paymasterParams.paymasterInput,
);
}

if (method === 'eth_signTransaction') {
console.trace('Signing transaction with abstract client', params);
return (await abstractClient.signTransaction({
...params[0],
})) as any;
return (await abstractClient.signTransaction(transaction)) as any;

Check warning on line 150 in packages/agw-react/src/abstractWalletConnector.ts

View workflow job for this annotation

GitHub Actions / publish

Unexpected any. Specify a different type
} else if (method === 'eth_sendTransaction') {
console.trace('Sending transaction with abstract client', params);
return (await abstractClient.sendTransaction({
...params[0],
})) as any;
return await abstractClient.sendTransaction(transaction);
}
throw new Error('Should not have reached this point');
}
Expand Down
Loading
Loading