|
| 1 | +import { encodeFunctionData, type Abi, type Address, type Hex } from 'viem'; |
| 2 | + |
| 3 | +// copy from view internals |
| 4 | +const getFunctionParameters = (values: unknown[]) => { |
| 5 | + const hasArgs = values.length > 0 && Array.isArray(values[0]); |
| 6 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 7 | + const args = (hasArgs ? values[0]! : []) as unknown[]; |
| 8 | + const options = ((hasArgs ? values[1] : values[0]) ?? {}) as any; |
| 9 | + return { args, options }; |
| 10 | +}; |
| 11 | + |
| 12 | +type ContractType = { |
| 13 | + address: Address; |
| 14 | + abi: Abi; |
| 15 | + read?: { |
| 16 | + [functionName: string]: (...args: any[]) => Promise<any>; |
| 17 | + }; |
| 18 | + simulate?: { |
| 19 | + [functionName: string]: (...args: any[]) => Promise<any>; |
| 20 | + }; |
| 21 | +}; |
| 22 | + |
| 23 | +export type EncodableContract< |
| 24 | + TContract extends ContractType, |
| 25 | + TSimulate extends |
| 26 | + TContract['simulate'] = TContract['simulate'] extends undefined |
| 27 | + ? never |
| 28 | + : TContract['simulate'], |
| 29 | + TRead extends TContract['read'] = TContract['read'] extends undefined |
| 30 | + ? never |
| 31 | + : TContract['read'], |
| 32 | + TMethods extends TSimulate & TRead = TSimulate & TRead, |
| 33 | + TFunctionName extends keyof TMethods = keyof TMethods, |
| 34 | +> = TContract & { |
| 35 | + prepare: { |
| 36 | + [K in TFunctionName]: ( |
| 37 | + ...args: Parameters< |
| 38 | + TMethods[K] extends (...args: any[]) => any ? TMethods[K] : never |
| 39 | + > |
| 40 | + ) => { |
| 41 | + address: TContract['address']; |
| 42 | + abi: TContract['abi']; |
| 43 | + functionName: K; |
| 44 | + args: Parameters< |
| 45 | + TMethods[K] extends (...args: any[]) => any ? TMethods[K] : never |
| 46 | + >[0] extends readonly unknown[] |
| 47 | + ? Parameters< |
| 48 | + TMethods[K] extends (...args: any[]) => any ? TMethods[K] : never |
| 49 | + >[0] |
| 50 | + : undefined; |
| 51 | + }; |
| 52 | + }; |
| 53 | + encode: { |
| 54 | + [K in TFunctionName]: ( |
| 55 | + ...args: Parameters< |
| 56 | + TMethods[K] extends (...args: any[]) => any ? TMethods[K] : never |
| 57 | + > |
| 58 | + ) => { |
| 59 | + to: TContract['address']; |
| 60 | + data: Hex; |
| 61 | + value?: bigint; |
| 62 | + }; |
| 63 | + }; |
| 64 | +}; |
| 65 | + |
| 66 | +export const getEncodableContract = <TContract extends ContractType>( |
| 67 | + contract: TContract, |
| 68 | +) => { |
| 69 | + (contract as any).prepare = new Proxy( |
| 70 | + {}, |
| 71 | + { |
| 72 | + get(_, functionName: string) { |
| 73 | + return (...parameters: unknown[]) => { |
| 74 | + const { args } = getFunctionParameters(parameters); |
| 75 | + return { |
| 76 | + address: contract.address, |
| 77 | + abi: contract.abi, |
| 78 | + functionName, |
| 79 | + args, |
| 80 | + }; |
| 81 | + }; |
| 82 | + }, |
| 83 | + }, |
| 84 | + ); |
| 85 | + (contract as any).encode = new Proxy( |
| 86 | + {}, |
| 87 | + { |
| 88 | + get(_, functionName: string) { |
| 89 | + return (...parameters: unknown[]) => { |
| 90 | + const { args, options } = getFunctionParameters(parameters); |
| 91 | + return { |
| 92 | + to: contract.address, |
| 93 | + data: encodeFunctionData({ |
| 94 | + abi: contract.abi, |
| 95 | + functionName, |
| 96 | + args, |
| 97 | + }), |
| 98 | + value: options.value, |
| 99 | + }; |
| 100 | + }; |
| 101 | + }, |
| 102 | + }, |
| 103 | + ); |
| 104 | + return contract as EncodableContract<TContract>; |
| 105 | +}; |
0 commit comments