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

feat: bump viem to v2 #94

Draft
wants to merge 4 commits into
base: v1
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
"devDependencies": {
"@offchainlabs/prettier-config": "0.2.1",
"@wagmi/cli": "^1.5.2",
"@wagmi/cli": "^2.1.4",
"dotenv": "^16.3.1",
"patch-package": "^8.0.0",
"postinstall-postinstall": "^2.1.0",
Expand Down
40 changes: 0 additions & 40 deletions patches/@wagmi+cli+1.5.2.patch

This file was deleted.

116 changes: 116 additions & 0 deletions src/abi/erc20ABI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
export const erc20ABI = [
{
type: 'event',
inputs: [
{ name: 'owner', type: 'address', indexed: true },
{ name: 'spender', type: 'address', indexed: true },
{ name: 'value', type: 'uint256', indexed: false },
],
name: 'Approval',
},
{
type: 'event',
inputs: [
{ name: 'from', type: 'address', indexed: true },
{ name: 'to', type: 'address', indexed: true },
{ name: 'value', type: 'uint256', indexed: false },
],
name: 'Transfer',
},
{
stateMutability: 'view',
type: 'function',
inputs: [
{ name: 'owner', type: 'address' },
{ name: 'spender', type: 'address' },
],
name: 'allowance',
outputs: [{ type: 'uint256' }],
},
{
stateMutability: 'nonpayable',
type: 'function',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
name: 'approve',
outputs: [{ type: 'bool' }],
},
{
stateMutability: 'view',
type: 'function',
inputs: [{ name: 'account', type: 'address' }],
name: 'balanceOf',
outputs: [{ type: 'uint256' }],
},
{
stateMutability: 'view',
type: 'function',
inputs: [],
name: 'decimals',
outputs: [{ type: 'uint8' }],
},
{
stateMutability: 'view',
type: 'function',
inputs: [],
name: 'name',
outputs: [{ type: 'string' }],
},
{
stateMutability: 'view',
type: 'function',
inputs: [],
name: 'symbol',
outputs: [{ type: 'string' }],
},
{
stateMutability: 'view',
type: 'function',
inputs: [],
name: 'totalSupply',
outputs: [{ type: 'uint256' }],
},
{
stateMutability: 'nonpayable',
type: 'function',
inputs: [
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
name: 'transfer',
outputs: [{ type: 'bool' }],
},
{
stateMutability: 'nonpayable',
type: 'function',
inputs: [
{ name: 'sender', type: 'address' },
{ name: 'recipient', type: 'address' },
{ name: 'amount', type: 'uint256' },
],
name: 'transferFrom',
outputs: [{ type: 'bool' }],
},
{
stateMutability: 'nonpayable',
type: 'function',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'addedValue', type: 'uint256' },
],
name: 'increaseAllowance',
outputs: [{ type: 'bool' }],
},
{
stateMutability: 'nonpayable',
type: 'function',
inputs: [
{ name: 'spender', type: 'address' },
{ name: 'subtractedValue', type: 'uint256' },
],
name: 'decreaseAllowance',
outputs: [{ type: 'bool' }],
},
] as const;
36 changes: 23 additions & 13 deletions src/arbGasInfoReadContract.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
import { Chain, GetFunctionArgs, PublicClient, ReadContractReturnType, Transport } from 'viem';
import {
Chain,
PublicClient,
Transport,
ContractFunctionArgs,
ReadContractParameters,
ReadContractReturnType,
} from 'viem';

import { arbGasInfo } from './contracts';
import { GetFunctionName } from './types/utils';
import { GetReadContractFunctionName } from './types/utils';

export type ArbGasInfoAbi = typeof arbGasInfo.abi;
export type ArbGasInfoFunctionName = GetFunctionName<ArbGasInfoAbi>;
export type ArbGasInfoFunctionName = GetReadContractFunctionName<ArbGasInfoAbi>;
type ArbGasInfoReadContractArgs<TFunctionName extends ArbGasInfoFunctionName> =
ContractFunctionArgs<ArbGasInfoAbi, 'pure' | 'view', TFunctionName>;

export type ArbGasInfoReadContractParameters<TFunctionName extends ArbGasInfoFunctionName> = {
functionName: TFunctionName;
} & GetFunctionArgs<ArbGasInfoAbi, TFunctionName>;

export type ArbGasInfoReadContractReturnType<TFunctionName extends ArbGasInfoFunctionName> =
ReadContractReturnType<ArbGasInfoAbi, TFunctionName>;
export type ArbGasInfoReadContractParameters<
TFunctionName extends ArbGasInfoFunctionName,
TArgs extends ArbGasInfoReadContractArgs<TFunctionName> = ArbGasInfoReadContractArgs<TFunctionName>,
> = Omit<ReadContractParameters<ArbGasInfoAbi, TFunctionName, TArgs>, 'abi' | 'address'>;
export type ArbGasInfoReadContractReturnType<
TFunctionName extends ArbGasInfoFunctionName,
TArgs extends ArbGasInfoReadContractArgs<TFunctionName> = ArbGasInfoReadContractArgs<TFunctionName>,
> = ReadContractReturnType<ArbGasInfoAbi, TFunctionName, TArgs>;

export function arbGasInfoReadContract<
TChain extends Chain | undefined,
TFunctionName extends ArbGasInfoFunctionName,
TChain extends Chain | undefined,
>(
client: PublicClient<Transport, TChain>,
params: ArbGasInfoReadContractParameters<TFunctionName>,
): Promise<ArbGasInfoReadContractReturnType<TFunctionName>> {
// @ts-ignore (todo: fix viem type issue)
) {
return client.readContract({
address: arbGasInfo.address,
abi: arbGasInfo.abi,
functionName: params.functionName,
args: params.args,
});
} as ReadContractParameters<ArbGasInfoAbi, TFunctionName, ArbGasInfoReadContractArgs<TFunctionName>>);
}
73 changes: 45 additions & 28 deletions src/arbOwnerPrepareTransactionRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,57 @@ import {
Address,
Chain,
Transport,
PrepareTransactionRequestReturnType,
EncodeFunctionDataReturnType,
ContractFunctionArgs,
} from 'viem';

import { arbOwner } from './contracts';
import { upgradeExecutorEncodeFunctionData } from './upgradeExecutor';
import { GetFunctionName } from './types/utils';
import { GetPrepareTransactionRequestParams } from './types/utils';

type ArbOwnerAbi = typeof arbOwner.abi;
export type ArbOwnerPrepareTransactionRequestFunctionName = GetFunctionName<ArbOwnerAbi>;
export type ArbOwnerEncodeFunctionDataParameters<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
> = EncodeFunctionDataParameters<ArbOwnerAbi, TFunctionName>;
export type ArbOwnerFunctionName = GetPrepareTransactionRequestParams<ArbOwnerAbi>;
type ArbOwnerPrepareTransactionRequestArgs<TFunctionName extends ArbOwnerFunctionName> =
ContractFunctionArgs<ArbOwnerAbi, 'nonpayable' | 'payable', TFunctionName>;

function arbOwnerEncodeFunctionData<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
>({ functionName, abi, args }: ArbOwnerEncodeFunctionDataParameters<TFunctionName>) {
type ArbOwnerEncodeFunctionDataParameters<
TFunctionName extends ArbOwnerFunctionName,
TArgs extends ArbOwnerPrepareTransactionRequestArgs<TFunctionName> = ArbOwnerPrepareTransactionRequestArgs<TFunctionName>,
> = EncodeFunctionDataParameters<ArbOwnerAbi, TFunctionName> & {
args: TArgs;
};

function arbOwnerEncodeFunctionData<TFunctionName extends ArbOwnerFunctionName>({
functionName,
abi,
args,
}: ArbOwnerEncodeFunctionDataParameters<TFunctionName>): EncodeFunctionDataReturnType {
return encodeFunctionData({
abi,
functionName,
args,
});
} as EncodeFunctionDataParameters);
}

type ArbOwnerPrepareFunctionDataParameters<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
> = ArbOwnerEncodeFunctionDataParameters<TFunctionName> & {
upgradeExecutor: Address | false;
abi: ArbOwnerAbi;
type ArbOwnerPrepareFunctionDataReturnType = {
to: Address;
data: `0x${string}`;
value: BigInt;
};
function arbOwnerPrepareFunctionData<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
>(params: ArbOwnerPrepareFunctionDataParameters<TFunctionName>) {
type ArbOwnerPrepareFunctionDataParameters<TFunctionName extends ArbOwnerFunctionName> =
ArbOwnerEncodeFunctionDataParameters<TFunctionName> & {
upgradeExecutor: Address | false;
};
function arbOwnerPrepareFunctionData<TFunctionName extends ArbOwnerFunctionName>(
params: ArbOwnerPrepareFunctionDataParameters<TFunctionName>,
): ArbOwnerPrepareFunctionDataReturnType {
const { upgradeExecutor } = params;

if (!upgradeExecutor) {
return {
to: arbOwner.address,
data: arbOwnerEncodeFunctionData(
params as ArbOwnerEncodeFunctionDataParameters<TFunctionName>,
),
data: arbOwnerEncodeFunctionData(params),
value: BigInt(0),
};
}
Expand All @@ -54,31 +66,33 @@ function arbOwnerPrepareFunctionData<
functionName: 'executeCall',
args: [
arbOwner.address, // target
arbOwnerEncodeFunctionData(params as ArbOwnerEncodeFunctionDataParameters<TFunctionName>), // targetCallData
arbOwnerEncodeFunctionData(params), // targetCallData
],
}),
value: BigInt(0),
};
}

export type ArbOwnerPrepareTransactionRequestParameters<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
> = Omit<ArbOwnerPrepareFunctionDataParameters<TFunctionName>, 'abi'> & {
TFunctionName extends ArbOwnerFunctionName,
> = Omit<ArbOwnerPrepareFunctionDataParameters<TFunctionName>, 'abi' | 'functionName'> & {
account: Address;
functionName: TFunctionName;
};

export type ArbOwnerPrepareTransactionRequestReturnType<TChain extends Chain | undefined> =
PrepareTransactionRequestReturnType<TChain>;
export async function arbOwnerPrepareTransactionRequest<
TFunctionName extends ArbOwnerPrepareTransactionRequestFunctionName,
TChain extends Chain | undefined,
TFunctionName extends ArbOwnerFunctionName,
TChain extends Chain | undefined = Chain | undefined,
>(
client: PublicClient<Transport, TChain>,
params: ArbOwnerPrepareTransactionRequestParameters<TFunctionName>,
) {
): Promise<ArbOwnerPrepareTransactionRequestReturnType<TChain>> {
if (typeof client.chain === 'undefined') {
throw new Error('[arbOwnerPrepareTransactionRequest] client.chain is undefined');
}

// params is extending ArbOwnerPrepareFunctionDataParameters, it's safe to cast
const { to, data, value } = arbOwnerPrepareFunctionData({
...params,
abi: arbOwner.abi,
Expand All @@ -93,5 +107,8 @@ export async function arbOwnerPrepareTransactionRequest<
account: params.account,
});

return { ...request, chainId: client.chain.id };
return {
...request,
chainId: client.chain.id,
} as unknown as Promise<ArbOwnerPrepareTransactionRequestReturnType<TChain>>;
}
Loading
Loading