diff --git a/.changeset/moody-gorillas-wait.md b/.changeset/moody-gorillas-wait.md new file mode 100644 index 0000000000..322f91841c --- /dev/null +++ b/.changeset/moody-gorillas-wait.md @@ -0,0 +1,5 @@ +--- +"viem": patch +--- + +Added initial `callContract` implementation diff --git a/src/actions/index.test.ts b/src/actions/index.test.ts index 52b24d6cb3..c77186c8ba 100644 --- a/src/actions/index.test.ts +++ b/src/actions/index.test.ts @@ -7,6 +7,7 @@ test('exports actions', () => { { "addChain": [Function], "call": [Function], + "callContract": [Function], "createBlockFilter": [Function], "createPendingTransactionFilter": [Function], "dropTransaction": [Function], diff --git a/src/actions/index.ts b/src/actions/index.ts index 5c0630f960..dac0049468 100644 --- a/src/actions/index.ts +++ b/src/actions/index.ts @@ -1,5 +1,6 @@ export { call, + callContract, createBlockFilter, createPendingTransactionFilter, estimateGas, @@ -25,6 +26,8 @@ export { } from './public' export type { CallArgs, + CallContractArgs, + CallContractResponse, CallResponse, CreateBlockFilterResponse, CreatePendingTransactionFilterResponse, diff --git a/src/actions/public/call.ts b/src/actions/public/call.ts index 4bcb73cf8a..aaf6bc7ed0 100644 --- a/src/actions/public/call.ts +++ b/src/actions/public/call.ts @@ -2,6 +2,7 @@ import type { Chain, Formatter } from '../../chains' import type { PublicClient } from '../../clients' import { InvalidGasArgumentsError } from '../../errors' import type { + Address, BlockTag, Hex, MergeIntersectionProperties, @@ -21,6 +22,7 @@ export type CallArgs = FormattedCall< TransactionRequestFormatter > & { chain?: TChain + from?: Address } & ( | { /** The balance of the account at a block number. */ diff --git a/src/actions/public/callContract.bench.ts b/src/actions/public/callContract.bench.ts new file mode 100644 index 0000000000..acdda82024 --- /dev/null +++ b/src/actions/public/callContract.bench.ts @@ -0,0 +1,28 @@ +import { Contract } from 'ethers' +import { bench, describe } from 'vitest' + +import { + ethersProvider, + publicClient, + wagmiContractConfig, +} from '../../../test' + +import { callContract } from './callContract' + +describe('Call Contract', () => { + bench('viem: `callContract`', async () => { + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'totalSupply', + }) + }) + + bench('ethers: `callStatic`', async () => { + const wagmi = new Contract( + wagmiContractConfig.address, + wagmiContractConfig.abi, + ethersProvider, + ) + await wagmi.callStatic.mint(1) + }) +}) diff --git a/src/actions/public/callContract.test.ts b/src/actions/public/callContract.test.ts new file mode 100644 index 0000000000..d0121e4f34 --- /dev/null +++ b/src/actions/public/callContract.test.ts @@ -0,0 +1,284 @@ +/** + * TODO: Heaps more test cases :D + * - Complex calldata types + * - Complex return types (tuple/structs) + * - EIP-1559 + * - Calls against blocks + * - Custom chain types + * - Custom nonce + */ + +import { describe, expect, test } from 'vitest' +import { + accounts, + publicClient, + testClient, + wagmiContractConfig, + walletClient, +} from '../../../test' +import { baycContractConfig } from '../../../test/abis' +import { BaseError } from '../../errors' +import { encodeFunctionData, getContractError } from '../../utils' +import { mine } from '../test' +import { sendTransaction } from '../wallet' + +import { callContract } from './callContract' +import { getTransactionReceipt } from './getTransactionReceipt' + +describe('wagmi', () => { + test('default', async () => { + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'isApprovedForAll', + args: [ + '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + ], + }), + ).toEqual(false) + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + functionName: 'mint', + args: [69420n], + }), + ).toEqual(undefined) + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'name', + }), + ).toEqual('wagmi') + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'ownerOf', + args: [1n], + }), + ).toEqual('0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6') + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'safeTransferFrom', + from: '0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6', + args: [ + '0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6', + '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + 1n, + ], + }), + ).toEqual(undefined) + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'tokenURI', + args: [1n], + }), + ).toEqual( + 'data:application/json;base64,eyJuYW1lIjogIndhZ21pICMxIiwgImltYWdlIjogImRhdGE6aW1hZ2Uvc3ZnK3htbDtiYXNlNjQsUEhOMlp5QjRiV3h1Y3owaWFIUjBjRG92TDNkM2R5NTNNeTV2Y21jdk1qQXdNQzl6ZG1jaUlIZHBaSFJvUFNJeE1ESTBJaUJvWldsbmFIUTlJakV3TWpRaUlHWnBiR3c5SW01dmJtVWlQanh3WVhSb0lHWnBiR3c5SW1oemJDZ3hPVFlzSURFd01DVXNJREV3SlNraUlHUTlJazB3SURCb01UQXlOSFl4TURJMFNEQjZJaUF2UGp4bklHWnBiR3c5SW1oemJDZzBOQ3dnTVRBd0pTd2dPVEFsS1NJK1BIQmhkR2dnWkQwaVRUa3dNeUEwTXpjdU5XTXdJRGt1TVRFekxUY3VNemc0SURFMkxqVXRNVFl1TlNBeE5pNDFjeTB4Tmk0MUxUY3VNemczTFRFMkxqVXRNVFl1TlNBM0xqTTRPQzB4Tmk0MUlERTJMalV0TVRZdU5TQXhOaTQxSURjdU16ZzNJREUyTGpVZ01UWXVOWHBOTmprNExqVXlPU0ExTmpaak5pNDVNakVnTUNBeE1pNDFNeTAxTGpVNU5pQXhNaTQxTXkweE1pNDFkaTAxTUdNd0xUWXVPVEEwSURVdU5qQTVMVEV5TGpVZ01USXVOVEk1TFRFeUxqVm9NalV1TURVNVl6WXVPVElnTUNBeE1pNDFNamtnTlM0MU9UWWdNVEl1TlRJNUlERXlMalYyTlRCak1DQTJMamt3TkNBMUxqWXdPU0F4TWk0MUlERXlMalV6SURFeUxqVnpNVEl1TlRJNUxUVXVOVGsySURFeUxqVXlPUzB4TWk0MWRpMDFNR013TFRZdU9UQTBJRFV1TmpBNUxURXlMalVnTVRJdU5UTXRNVEl1TldneU5TNHdOVGxqTmk0NU1pQXdJREV5TGpVeU9TQTFMalU1TmlBeE1pNDFNamtnTVRJdU5YWTFNR013SURZdU9UQTBJRFV1TmpBNUlERXlMalVnTVRJdU5USTVJREV5TGpWb016Y3VOVGc1WXpZdU9USWdNQ0F4TWk0MU1qa3ROUzQxT1RZZ01USXVOVEk1TFRFeUxqVjJMVGMxWXpBdE5pNDVNRFF0TlM0Mk1Ea3RNVEl1TlMweE1pNDFNamt0TVRJdU5YTXRNVEl1TlRNZ05TNDFPVFl0TVRJdU5UTWdNVEl1TlhZMU5pNHlOV0UyTGpJMk5DQTJMakkyTkNBd0lERWdNUzB4TWk0MU1qa2dNRlkwTnpndU5XTXdMVFl1T1RBMExUVXVOakE1TFRFeUxqVXRNVEl1TlRNdE1USXVOVWcyT1RndU5USTVZeTAyTGpreUlEQXRNVEl1TlRJNUlEVXVOVGsyTFRFeUxqVXlPU0F4TWk0MWRqYzFZekFnTmk0NU1EUWdOUzQyTURrZ01USXVOU0F4TWk0MU1qa2dNVEl1TlhvaUlDOCtQSEJoZEdnZ1pEMGlUVEUxTnk0Mk5UVWdOVFF4WXkwMkxqa3pNaUF3TFRFeUxqVTFNaTAxTGpVNU5pMHhNaTQxTlRJdE1USXVOWFl0TlRCak1DMDJMamt3TkMwMUxqWXhPUzB4TWk0MUxURXlMalUxTVMweE1pNDFVekV5TUNBME56RXVOVGsySURFeU1DQTBOemd1TlhZM05XTXdJRFl1T1RBMElEVXVOaklnTVRJdU5TQXhNaTQxTlRJZ01USXVOV2d4TlRBdU5qSmpOaTQ1TXpNZ01DQXhNaTQxTlRJdE5TNDFPVFlnTVRJdU5UVXlMVEV5TGpWMkxUVXdZekF0Tmk0NU1EUWdOUzQyTVRrdE1USXVOU0F4TWk0MU5USXRNVEl1TldneE5EUXVNelExWXpNdU5EWTFJREFnTmk0eU56WWdNaTQzT1RnZ05pNHlOellnTmk0eU5YTXRNaTQ0TVRFZ05pNHlOUzAyTGpJM05pQTJMakkxU0RNeU1DNDRNamhqTFRZdU9UTXpJREF0TVRJdU5UVXlJRFV1TlRrMkxURXlMalUxTWlBeE1pNDFkak0zTGpWak1DQTJMamt3TkNBMUxqWXhPU0F4TWk0MUlERXlMalUxTWlBeE1pNDFhREUxTUM0Mk1tTTJMamt6TXlBd0lERXlMalUxTWkwMUxqVTVOaUF4TWk0MU5USXRNVEl1TlhZdE56VmpNQzAyTGprd05DMDFMall4T1MweE1pNDFMVEV5TGpVMU1pMHhNaTQxU0RJNE15NHhOekpqTFRZdU9UTXlJREF0TVRJdU5UVXhJRFV1TlRrMkxURXlMalUxTVNBeE1pNDFkalV3WXpBZ05pNDVNRFF0TlM0Mk1Ua2dNVEl1TlMweE1pNDFOVElnTVRJdU5XZ3RNalV1TVRBell5MDJMamt6TXlBd0xURXlMalUxTWkwMUxqVTVOaTB4TWk0MU5USXRNVEl1TlhZdE5UQmpNQzAyTGprd05DMDFMall5TFRFeUxqVXRNVEl1TlRVeUxURXlMalZ6TFRFeUxqVTFNaUExTGpVNU5pMHhNaTQxTlRJZ01USXVOWFkxTUdNd0lEWXVPVEEwTFRVdU5qRTVJREV5TGpVdE1USXVOVFV4SURFeUxqVm9MVEkxTGpFd05IcHRNekF4TGpJME1pMDJMakkxWXpBZ015NDBOVEl0TWk0NE1URWdOaTR5TlMwMkxqSTNOaUEyTGpJMVNETXpPUzQyTlRWakxUTXVORFkxSURBdE5pNHlOell0TWk0M09UZ3ROaTR5TnpZdE5pNHlOWE15TGpneE1TMDJMakkxSURZdU1qYzJMVFl1TWpWb01URXlMamsyTm1NekxqUTJOU0F3SURZdU1qYzJJREl1TnprNElEWXVNamMySURZdU1qVjZUVFE1TnlBMU5UTXVPREU0WXpBZ05pNDVNamtnTlM0Mk1qZ2dNVEl1TlRRMklERXlMalUzTVNBeE1pNDFORFpvTVRNeVlUWXVNamdnTmk0eU9DQXdJREFnTVNBMkxqSTROaUEyTGpJM01pQTJMakk0SURZdU1qZ2dNQ0F3SURFdE5pNHlPRFlnTmk0eU56Tm9MVEV6TW1NdE5pNDVORE1nTUMweE1pNDFOekVnTlM0Mk1UWXRNVEl1TlRjeElERXlMalUwTmtFeE1pNDFOaUF4TWk0MU5pQXdJREFnTUNBMU1Ea3VOVGN4SURZd05HZ3hOVEF1T0RVNFl6WXVPVFF6SURBZ01USXVOVGN4TFRVdU5qRTJJREV5TGpVM01TMHhNaTQxTkRWMkxURXhNaTQ1TVdNd0xUWXVPVEk0TFRVdU5qSTRMVEV5TGpVME5TMHhNaTQxTnpFdE1USXVOVFExU0RVd09TNDFOekZqTFRZdU9UUXpJREF0TVRJdU5UY3hJRFV1TmpFM0xURXlMalUzTVNBeE1pNDFORFYyTnpVdU1qY3plbTB6Tnk0M01UUXROakl1TnpJM1l5MDJMamswTXlBd0xURXlMalUzTVNBMUxqWXhOeTB4TWk0MU56RWdNVEl1TlRRMWRqSTFMakE1TVdNd0lEWXVPVEk1SURVdU5qSTRJREV5TGpVME5pQXhNaTQxTnpFZ01USXVOVFEyYURFd01DNDFOekpqTmk0NU5ETWdNQ0F4TWk0MU56RXROUzQyTVRjZ01USXVOVGN4TFRFeUxqVTBObll0TWpVdU1Ea3hZekF0Tmk0NU1qZ3ROUzQyTWpndE1USXVOVFExTFRFeUxqVTNNUzB4TWk0MU5EVklOVE0wTGpjeE5Ib2lJR1pwYkd3dGNuVnNaVDBpWlhabGJtOWtaQ0lnTHo0OEwyYytQQzl6ZG1jKyJ9', + ) + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'getApproved', + args: [420n], + }), + ).toEqual('0x0000000000000000000000000000000000000000') + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'totalSupply', + }), + ).toEqual(558n) + expect( + await callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'balanceOf', + args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], + }), + ).toEqual(3n) + }) + + test('revert', async () => { + await expect(() => + callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'balanceOf', + args: ['0x0000000000000000000000000000000000000000'], + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "ERC721: balance query for the zero address + + Contract: 0x0000000000000000000000000000000000000000 + Function: balanceOf(address owner) + Arguments: (0x0000000000000000000000000000000000000000) + + Details: execution reverted: ERC721: balance query for the zero address + Version: viem@1.0.2" + `) + await expect(() => + callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'approve', + args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', 420n], + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "ERC721: approval to current owner + + Contract: 0x0000000000000000000000000000000000000000 + Function: approve(address to, uint256 tokenId) + Arguments: (0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 420) + + Details: execution reverted: ERC721: approval to current owner + Version: viem@1.0.2" + `) + await expect(() => + callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'mint', + args: [1n], + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "Token ID is taken + + Contract: 0x0000000000000000000000000000000000000000 + Function: mint(uint256 tokenId) + Arguments: (1) + + Details: execution reverted: Token ID is taken + Version: viem@1.0.2" + `) + await expect(() => + callContract(publicClient, { + ...wagmiContractConfig, + functionName: 'safeTransferFrom', + from: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + args: [ + '0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6', + '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', + 1n, + ], + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "ERC721: transfer caller is not owner nor approved + + Sender: 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC + Contract: 0x0000000000000000000000000000000000000000 + Function: safeTransferFrom(address from, address to, uint256 tokenId) + Arguments: (0x1a1E021A302C237453D3D45c7B82B19cEEB7E2e6, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC, 1) + + Details: execution reverted: ERC721: transfer caller is not owner nor approved + Version: viem@1.0.2" + `) + }) +}) + +describe('BAYC', () => { + describe('default', () => { + test('mintApe', async () => { + const { contractAddress } = await deployBAYC() + + // Set sale state to active + // TODO: replace w/ writeContract + await sendTransaction(walletClient, { + data: encodeFunctionData({ + abi: baycContractConfig.abi, + functionName: 'flipSaleState', + }), + from: accounts[0].address, + to: contractAddress!, + }) + await mine(testClient, { blocks: 1 }) + + // Mint an Ape! + expect( + await callContract(publicClient, { + ...baycContractConfig, + address: contractAddress!, + functionName: 'mintApe', + from: accounts[0].address, + args: [1n], + value: 1000000000000000000n, + }), + ).toBe(undefined) + }) + + test('get a free $100k', async () => { + const { contractAddress } = await deployBAYC() + + // Reserve apes + expect( + await callContract(publicClient, { + ...baycContractConfig, + address: contractAddress!, + functionName: 'reserveApes', + from: accounts[0].address, + }), + ).toBe(undefined) + }) + }) + + describe('revert', () => { + test('sale inactive', async () => { + const { contractAddress } = await deployBAYC() + + // Expect mint to fail. + await expect(() => + callContract(publicClient, { + ...baycContractConfig, + address: contractAddress!, + functionName: 'mintApe', + from: accounts[0].address, + args: [1n], + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "Sale must be active to mint Ape + + Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 + Contract: 0x0000000000000000000000000000000000000000 + Function: mintApe(uint256 numberOfTokens) + Arguments: (1) + + Details: execution reverted: Sale must be active to mint Ape + Version: viem@1.0.2" + `) + }) + }) +}) + +test('fake contract address', async () => { + await expect(() => + callContract(publicClient, { + ...wagmiContractConfig, + address: '0x0000000000000000000000000000000000000069', + functionName: 'name', + }), + ).rejects.toThrowErrorMatchingInlineSnapshot(` + "The contract method \\"name\\" returned no data (\\"0x\\"). This could be due to any of the following: + - The contract does not have the function \\"name\\", + - The parameters passed to the contract function may be invalid, or + - The address is not a contract. + + Contract: 0x0000000000000000000000000000000000000000 + Function: name() + > \\"0x\\" + + Version: viem@1.0.2" + `) +}) + +// Deploy BAYC Contract +// TODO: replace w/ deployContract +async function deployBAYC() { + const hash = await sendTransaction(walletClient, { + data: '0x608060405260405180602001604052806000815250600b90805190602001906200002b92919062000484565b506000600f60006101000a81548160ff0219169083151502179055503480156200005457600080fd5b50604051620046d0380380620046d0833981810160405260808110156200007a57600080fd5b81019080805160405193929190846401000000008211156200009b57600080fd5b83820191506020820185811115620000b257600080fd5b8251866001820283011164010000000082111715620000d057600080fd5b8083526020830192505050908051906020019080838360005b8381101562000106578082015181840152602081019050620000e9565b50505050905090810190601f168015620001345780820380516001836020036101000a031916815260200191505b50604052602001805160405193929190846401000000008211156200015857600080fd5b838201915060208201858111156200016f57600080fd5b82518660018202830111640100000000821117156200018d57600080fd5b8083526020830192505050908051906020019080838360005b83811015620001c3578082015181840152602081019050620001a6565b50505050905090810190601f168015620001f15780820380516001836020036101000a031916815260200191505b5060405260200180519060200190929190805190602001909291905050508383620002296301ffc9a760e01b6200037360201b60201c565b81600690805190602001906200024192919062000484565b5080600790805190602001906200025a92919062000484565b50620002736380ac58cd60e01b6200037360201b60201c565b6200028b635b5e139f60e01b6200037360201b60201c565b620002a363780e9d6360e01b6200037360201b60201c565b50506000620002b76200047c60201b60201c565b905080600a60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508073ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35081600e81905550620bdd808101601081905550505050506200052a565b63ffffffff60e01b817bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916141562000410576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601c8152602001807f4552433136353a20696e76616c696420696e746572666163652069640000000081525060200191505060405180910390fd5b6001600080837bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200190815260200160002060006101000a81548160ff02191690831515021790555050565b600033905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620004c757805160ff1916838001178555620004f8565b82800160010185558215620004f8579182015b82811115620004f7578251825591602001919060010190620004da565b5b5090506200050791906200050b565b5090565b5b80821115620005265760008160009055506001016200050c565b5090565b614196806200053a6000396000f3fe60806040526004361061021a5760003560e01c80636c0360eb11610123578063b0f67427116100ab578063e36d64981161006f578063e36d649814610ddf578063e985e9c514610e0a578063e986655014610e91578063eb8d244414610ea8578063f2fde38b14610ed55761021a565b8063b0f6742714610bac578063b88d4fde14610bc3578063bb8a16bd14610cd5578063c87b56dd14610d00578063cb774d4714610db45761021a565b80637d17fcbe116100f25780637d17fcbe14610a395780638da5cb5b14610a5057806395d89b4114610a91578063a22cb46514610b21578063a723533e14610b7e5761021a565b80636c0360eb1461090257806370a0823114610992578063715018a6146109f75780637a3f451e14610a0e5761021a565b80632f745c59116101a65780634f6ccce7116101755780634f6ccce7146106cb57806355f804b31461071a578063571dff3b146107e2578063607e20e31461080d5780636352211e1461089d5761021a565b80632f745c59146105b357806334918dfd146106225780633ccfd60b1461063957806342842e0e146106505761021a565b8063095ea7b3116101ed578063095ea7b3146103bf578063109695231461041a57806318160ddd146104e257806318e20a381461050d57806323b872dd146105385761021a565b8063018a2c371461021f57806301ffc9a71461025a57806306fdde03146102ca578063081812fc1461035a575b600080fd5b34801561022b57600080fd5b506102586004803603602081101561024257600080fd5b8101908080359060200190929190505050610f26565b005b34801561026657600080fd5b506102b26004803603602081101561027d57600080fd5b8101908080357bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19169060200190929190505050610fdf565b60405180821515815260200191505060405180910390f35b3480156102d657600080fd5b506102df611046565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561031f578082015181840152602081019050610304565b50505050905090810190601f16801561034c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561036657600080fd5b506103936004803603602081101561037d57600080fd5b81019080803590602001909291905050506110e8565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156103cb57600080fd5b50610418600480360360408110156103e257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611183565b005b34801561042657600080fd5b506104e06004803603602081101561043d57600080fd5b810190808035906020019064010000000081111561045a57600080fd5b82018360208201111561046c57600080fd5b8035906020019184600183028401116401000000008311171561048e57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506112c7565b005b3480156104ee57600080fd5b506104f7611390565b6040518082815260200191505060405180910390f35b34801561051957600080fd5b506105226113a1565b6040518082815260200191505060405180910390f35b34801561054457600080fd5b506105b16004803603606081101561055b57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506113a7565b005b3480156105bf57600080fd5b5061060c600480360360408110156105d657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919050505061141d565b6040518082815260200191505060405180910390f35b34801561062e57600080fd5b50610637611478565b005b34801561064557600080fd5b5061064e611553565b005b34801561065c57600080fd5b506106c96004803603606081101561067357600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050611651565b005b3480156106d757600080fd5b50610704600480360360208110156106ee57600080fd5b8101908080359060200190929190505050611671565b6040518082815260200191505060405180910390f35b34801561072657600080fd5b506107e06004803603602081101561073d57600080fd5b810190808035906020019064010000000081111561075a57600080fd5b82018360208201111561076c57600080fd5b8035906020019184600183028401116401000000008311171561078e57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050611694565b005b3480156107ee57600080fd5b506107f761174f565b6040518082815260200191505060405180910390f35b34801561081957600080fd5b50610822611754565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610862578082015181840152602081019050610847565b50505050905090810190601f16801561088f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156108a957600080fd5b506108d6600480360360208110156108c057600080fd5b81019080803590602001909291905050506117f2565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34801561090e57600080fd5b50610917611829565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561095757808201518184015260208101905061093c565b50505050905090810190601f1680156109845780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561099e57600080fd5b506109e1600480360360208110156109b557600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506118cb565b6040518082815260200191505060405180910390f35b348015610a0357600080fd5b50610a0c6119a0565b005b348015610a1a57600080fd5b50610a23611b10565b6040518082815260200191505060405180910390f35b348015610a4557600080fd5b50610a4e611b1c565b005b348015610a5c57600080fd5b50610a65611c4c565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b348015610a9d57600080fd5b50610aa6611c76565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610ae6578082015181840152602081019050610acb565b50505050905090810190601f168015610b135780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b348015610b2d57600080fd5b50610b7c60048036036040811015610b4457600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803515159060200190929190505050611d18565b005b610baa60048036036020811015610b9457600080fd5b8101908080359060200190929190505050611ece565b005b348015610bb857600080fd5b50610bc1612127565b005b348015610bcf57600080fd5b50610cd360048036036080811015610be657600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019092919080359060200190640100000000811115610c4d57600080fd5b820183602082011115610c5f57600080fd5b80359060200191846001830284011164010000000083111715610c8157600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061220b565b005b348015610ce157600080fd5b50610cea612283565b6040518082815260200191505060405180910390f35b348015610d0c57600080fd5b50610d3960048036036020811015610d2357600080fd5b8101908080359060200190929190505050612289565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610d79578082015181840152602081019050610d5e565b50505050905090810190601f168015610da65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b348015610dc057600080fd5b50610dc961255a565b6040518082815260200191505060405180910390f35b348015610deb57600080fd5b50610df4612560565b6040518082815260200191505060405180910390f35b348015610e1657600080fd5b50610e7960048036036040811015610e2d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050612566565b60405180821515815260200191505060405180910390f35b348015610e9d57600080fd5b50610ea66125fa565b005b348015610eb457600080fd5b50610ebd612764565b60405180821515815260200191505060405180910390f35b348015610ee157600080fd5b50610f2460048036036020811015610ef857600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050612777565b005b610f2e61296c565b73ffffffffffffffffffffffffffffffffffffffff16610f4c611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614610fd5576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b8060108190555050565b6000806000837bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200190815260200160002060009054906101000a900460ff169050919050565b606060068054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156110de5780601f106110b3576101008083540402835291602001916110de565b820191906000526020600020905b8154815290600101906020018083116110c157829003601f168201915b5050505050905090565b60006110f382612974565b611148576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602c81526020018061408b602c913960400191505060405180910390fd5b6004600083815260200190815260200160002060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050919050565b600061118e826117f2565b90508073ffffffffffffffffffffffffffffffffffffffff168373ffffffffffffffffffffffffffffffffffffffff161415611215576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602181526020018061410f6021913960400191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff1661123461296c565b73ffffffffffffffffffffffffffffffffffffffff16148061126357506112628161125d61296c565b612566565b5b6112b8576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526038815260200180613f956038913960400191505060405180910390fd5b6112c28383612991565b505050565b6112cf61296c565b73ffffffffffffffffffffffffffffffffffffffff166112ed611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611376576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b80600b908051906020019061138c929190613de6565b5050565b600061139c6002612a4a565b905090565b60105481565b6113b86113b261296c565b82612a5f565b61140d576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260318152602001806141306031913960400191505060405180910390fd5b611418838383612b53565b505050565b600061147082600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020612d9690919063ffffffff16565b905092915050565b61148061296c565b73ffffffffffffffffffffffffffffffffffffffff1661149e611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611527576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b600f60009054906101000a900460ff1615600f60006101000a81548160ff021916908315150217905550565b61155b61296c565b73ffffffffffffffffffffffffffffffffffffffff16611579611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611602576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b60004790503373ffffffffffffffffffffffffffffffffffffffff166108fc829081150290604051600060405180830381858888f1935050505015801561164d573d6000803e3d6000fd5b5050565b61166c8383836040518060200160405280600081525061220b565b505050565b600080611688836002612db090919063ffffffff16565b50905080915050919050565b61169c61296c565b73ffffffffffffffffffffffffffffffffffffffff166116ba611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611743576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b61174c81612ddc565b50565b601481565b600b8054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156117ea5780601f106117bf576101008083540402835291602001916117ea565b820191906000526020600020905b8154815290600101906020018083116117cd57829003601f168201915b505050505081565b600061182282604051806060016040528060298152602001613ff7602991396002612df69092919063ffffffff16565b9050919050565b606060098054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156118c15780601f10611896576101008083540402835291602001916118c1565b820191906000526020600020905b8154815290600101906020018083116118a457829003601f168201915b5050505050905090565b60008073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415611952576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602a815260200180613fcd602a913960400191505060405180910390fd5b611999600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020612e15565b9050919050565b6119a861296c565b73ffffffffffffffffffffffffffffffffffffffff166119c6611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611a4f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff16600a60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a36000600a60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550565b67011c37937e08000081565b611b2461296c565b73ffffffffffffffffffffffffffffffffffffffff16611b42611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614611bcb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b6000600d5414611c43576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f5374617274696e6720696e64657820697320616c72656164792073657400000081525060200191505060405180910390fd5b43600c81905550565b6000600a60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905090565b606060078054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015611d0e5780601f10611ce357610100808354040283529160200191611d0e565b820191906000526020600020905b815481529060010190602001808311611cf157829003601f168201915b5050505050905090565b611d2061296c565b73ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415611dc1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260198152602001807f4552433732313a20617070726f766520746f2063616c6c65720000000000000081525060200191505060405180910390fd5b8060056000611dce61296c565b73ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff0219169083151502179055508173ffffffffffffffffffffffffffffffffffffffff16611e7b61296c565b73ffffffffffffffffffffffffffffffffffffffff167f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c318360405180821515815260200191505060405180910390a35050565b600f60009054906101000a900460ff16611f50576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f53616c65206d7573742062652061637469766520746f206d696e74204170650081525060200191505060405180910390fd5b6014811115611faa576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526021815260200180613f746021913960400191505060405180910390fd5b600e54611fc782611fb9611390565b612e2a90919063ffffffff16565b111561201e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260288152602001806140426028913960400191505060405180910390fd5b3461203a8267011c37937e080000612eb290919063ffffffff16565b11156120ae576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601f8152602001807f45746865722076616c75652073656e74206973206e6f7420636f72726563740081525060200191505060405180910390fd5b60005b818110156120ef5760006120c3611390565b9050600e546120d0611390565b10156120e1576120e03382612f38565b5b5080806001019150506120b1565b506000600c541480156121175750600e54612108611390565b148061211657506010544210155b5b156121245743600c819055505b50565b61212f61296c565b73ffffffffffffffffffffffffffffffffffffffff1661214d611c4c565b73ffffffffffffffffffffffffffffffffffffffff16146121d6576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b60006121e0611390565b905060005b601e811015612207576121fa33828401612f38565b80806001019150506121e5565b5050565b61221c61221661296c565b83612a5f565b612271576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260318152602001806141306031913960400191505060405180910390fd5b61227d84848484612f56565b50505050565b600e5481565b606061229482612974565b6122e9576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602f8152602001806140e0602f913960400191505060405180910390fd5b6060600860008481526020019081526020016000208054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156123925780601f1061236757610100808354040283529160200191612392565b820191906000526020600020905b81548152906001019060200180831161237557829003601f168201915b5050505050905060606123a3611829565b90506000815114156123b9578192505050612555565b60008251111561248a5780826040516020018083805190602001908083835b602083106123fb57805182526020820191506020810190506020830392506123d8565b6001836020036101000a03801982511681845116808217855250505050505090500182805190602001908083835b6020831061244c5780518252602082019150602081019050602083039250612429565b6001836020036101000a0380198251168184511680821785525050505050509050019250505060405160208183030381529060405292505050612555565b8061249485612fc8565b6040516020018083805190602001908083835b602083106124ca57805182526020820191506020810190506020830392506124a7565b6001836020036101000a03801982511681845116808217855250505050505090500182805190602001908083835b6020831061251b57805182526020820191506020810190506020830392506124f8565b6001836020036101000a03801982511681845116808217855250505050505090500192505050604051602081830303815290604052925050505b919050565b600d5481565b600c5481565b6000600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16905092915050565b6000600d5414612672576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f5374617274696e6720696e64657820697320616c72656164792073657400000081525060200191505060405180910390fd5b6000600c5414156126eb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f5374617274696e6720696e64657820626c6f636b206d7573742062652073657481525060200191505060405180910390fd5b600e54600c544060001c816126fc57fe5b06600d8190555060ff61271a600c544361310f90919063ffffffff16565b111561273a57600e54600143034060001c8161273257fe5b06600d819055505b6000600d5414156127625761275b6001600d54612e2a90919063ffffffff16565b600d819055505b565b600f60009054906101000a900460ff1681565b61277f61296c565b73ffffffffffffffffffffffffffffffffffffffff1661279d611c4c565b73ffffffffffffffffffffffffffffffffffffffff1614612826576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e657281525060200191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff1614156128ac576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526026815260200180613ed86026913960400191505060405180910390fd5b8073ffffffffffffffffffffffffffffffffffffffff16600a60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a380600a60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b600033905090565b600061298a82600261319290919063ffffffff16565b9050919050565b816004600083815260200190815260200160002060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550808273ffffffffffffffffffffffffffffffffffffffff16612a04836117f2565b73ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92560405160405180910390a45050565b6000612a58826000016131ac565b9050919050565b6000612a6a82612974565b612abf576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602c815260200180613f48602c913960400191505060405180910390fd5b6000612aca836117f2565b90508073ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff161480612b3957508373ffffffffffffffffffffffffffffffffffffffff16612b21846110e8565b73ffffffffffffffffffffffffffffffffffffffff16145b80612b4a5750612b498185612566565b5b91505092915050565b8273ffffffffffffffffffffffffffffffffffffffff16612b73826117f2565b73ffffffffffffffffffffffffffffffffffffffff1614612bdf576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260298152602001806140b76029913960400191505060405180910390fd5b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff161415612c65576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526024815260200180613efe6024913960400191505060405180910390fd5b612c708383836131bd565b612c7b600082612991565b612ccc81600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206131c290919063ffffffff16565b50612d1e81600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206131dc90919063ffffffff16565b50612d35818360026131f69092919063ffffffff16565b50808273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef60405160405180910390a4505050565b6000612da5836000018361322b565b60001c905092915050565b600080600080612dc386600001866132ae565b915091508160001c8160001c9350935050509250929050565b8060099080519060200190612df2929190613de6565b5050565b6000612e09846000018460001b84613347565b60001c90509392505050565b6000612e238260000161343d565b9050919050565b600080828401905083811015612ea8576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601b8152602001807f536166654d6174683a206164646974696f6e206f766572666c6f77000000000081525060200191505060405180910390fd5b8091505092915050565b600080831415612ec55760009050612f32565b6000828402905082848281612ed657fe5b0414612f2d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602181526020018061406a6021913960400191505060405180910390fd5b809150505b92915050565b612f5282826040518060200160405280600081525061344e565b5050565b612f61848484612b53565b612f6d848484846134bf565b612fc2576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526032815260200180613ea66032913960400191505060405180910390fd5b50505050565b60606000821415613010576040518060400160405280600181526020017f3000000000000000000000000000000000000000000000000000000000000000815250905061310a565b600082905060005b6000821461303a578080600101915050600a828161303257fe5b049150613018565b60608167ffffffffffffffff8111801561305357600080fd5b506040519080825280601f01601f1916602001820160405280156130865781602001600182028036833780820191505090505b50905060006001830390508593505b6000841461310257600a84816130a757fe5b0660300160f81b828280600190039350815181106130c157fe5b60200101907effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916908160001a905350600a84816130fa57fe5b049350613095565b819450505050505b919050565b600082821115613187576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601e8152602001807f536166654d6174683a207375627472616374696f6e206f766572666c6f77000081525060200191505060405180910390fd5b818303905092915050565b60006131a4836000018360001b6136d8565b905092915050565b600081600001805490509050919050565b505050565b60006131d4836000018360001b6136fb565b905092915050565b60006131ee836000018360001b6137e3565b905092915050565b6000613222846000018460001b8473ffffffffffffffffffffffffffffffffffffffff1660001b613853565b90509392505050565b60008183600001805490501161328c576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526022815260200180613e846022913960400191505060405180910390fd5b82600001828154811061329b57fe5b9060005260206000200154905092915050565b60008082846000018054905011613310576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260228152602001806140206022913960400191505060405180910390fd5b600084600001848154811061332157fe5b906000526020600020906002020190508060000154816001015492509250509250929050565b6000808460010160008581526020019081526020016000205490506000811415839061340e576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156133d35780820151818401526020810190506133b8565b50505050905090810190601f1680156134005780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5084600001600182038154811061342157fe5b9060005260206000209060020201600101549150509392505050565b600081600001805490509050919050565b613458838361392f565b61346560008484846134bf565b6134ba576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526032815260200180613ea66032913960400191505060405180910390fd5b505050565b60006134e08473ffffffffffffffffffffffffffffffffffffffff16613b23565b6134ed57600190506136d0565b606061365763150b7a0260e01b61350261296c565b888787604051602401808573ffffffffffffffffffffffffffffffffffffffff1681526020018473ffffffffffffffffffffffffffffffffffffffff16815260200183815260200180602001828103825283818151815260200191508051906020019080838360005b8381101561358657808201518184015260208101905061356b565b50505050905090810190601f1680156135b35780820380516001836020036101000a031916815260200191505b5095505050505050604051602081830303815290604052907bffffffffffffffffffffffffffffffffffffffffffffffffffffffff19166020820180517bffffffffffffffffffffffffffffffffffffffffffffffffffffffff8381831617835250505050604051806060016040528060328152602001613ea6603291398773ffffffffffffffffffffffffffffffffffffffff16613b369092919063ffffffff16565b9050600081806020019051602081101561367057600080fd5b8101908080519060200190929190505050905063150b7a0260e01b7bffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916817bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191614925050505b949350505050565b600080836001016000848152602001908152602001600020541415905092915050565b600080836001016000848152602001908152602001600020549050600081146137d7576000600182039050600060018660000180549050039050600086600001828154811061374657fe5b906000526020600020015490508087600001848154811061376357fe5b906000526020600020018190555060018301876001016000838152602001908152602001600020819055508660000180548061379b57fe5b600190038181906000526020600020016000905590558660010160008781526020019081526020016000206000905560019450505050506137dd565b60009150505b92915050565b60006137ef8383613b4e565b61384857826000018290806001815401808255809150506001900390600052602060002001600090919091909150558260000180549050836001016000848152602001908152602001600020819055506001905061384d565b600090505b92915050565b60008084600101600085815260200190815260200160002054905060008114156138fa57846000016040518060400160405280868152602001858152509080600181540180825580915050600190039060005260206000209060020201600090919091909150600082015181600001556020820151816001015550508460000180549050856001016000868152602001908152602001600020819055506001915050613928565b8285600001600183038154811061390d57fe5b90600052602060002090600202016001018190555060009150505b9392505050565b600073ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff1614156139d2576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260208152602001807f4552433732313a206d696e7420746f20746865207a65726f206164647265737381525060200191505060405180910390fd5b6139db81612974565b15613a4e576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601c8152602001807f4552433732313a20746f6b656e20616c7265616479206d696e7465640000000081525060200191505060405180910390fd5b613a5a600083836131bd565b613aab81600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206131dc90919063ffffffff16565b50613ac2818360026131f69092919063ffffffff16565b50808273ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef60405160405180910390a45050565b600080823b905060008111915050919050565b6060613b458484600085613b71565b90509392505050565b600080836001016000848152602001908152602001600020541415905092915050565b606082471015613bcc576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526026815260200180613f226026913960400191505060405180910390fd5b613bd585613b23565b613c47576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601d8152602001807f416464726573733a2063616c6c20746f206e6f6e2d636f6e747261637400000081525060200191505060405180910390fd5b600060608673ffffffffffffffffffffffffffffffffffffffff1685876040518082805190602001908083835b60208310613c975780518252602082019150602081019050602083039250613c74565b6001836020036101000a03801982511681845116808217855250505050505090500191505060006040518083038185875af1925050503d8060008114613cf9576040519150601f19603f3d011682016040523d82523d6000602084013e613cfe565b606091505b5091509150613d0e828286613d1a565b92505050949350505050565b60608315613d2a57829050613ddf565b600083511115613d3d5782518084602001fd5b816040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b83811015613da4578082015181840152602081019050613d89565b50505050905090810190601f168015613dd15780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b9392505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10613e2757805160ff1916838001178555613e55565b82800160010185558215613e55579182015b82811115613e54578251825591602001919060010190613e39565b5b509050613e629190613e66565b5090565b5b80821115613e7f576000816000905550600101613e67565b509056fe456e756d657261626c655365743a20696e646578206f7574206f6620626f756e64734552433732313a207472616e7366657220746f206e6f6e20455243373231526563656976657220696d706c656d656e7465724f776e61626c653a206e6577206f776e657220697320746865207a65726f20616464726573734552433732313a207472616e7366657220746f20746865207a65726f2061646472657373416464726573733a20696e73756666696369656e742062616c616e636520666f722063616c6c4552433732313a206f70657261746f7220717565727920666f72206e6f6e6578697374656e7420746f6b656e43616e206f6e6c79206d696e7420323020746f6b656e7320617420612074696d654552433732313a20617070726f76652063616c6c6572206973206e6f74206f776e6572206e6f7220617070726f76656420666f7220616c6c4552433732313a2062616c616e636520717565727920666f7220746865207a65726f20616464726573734552433732313a206f776e657220717565727920666f72206e6f6e6578697374656e7420746f6b656e456e756d657261626c654d61703a20696e646578206f7574206f6620626f756e6473507572636861736520776f756c6420657863656564206d617820737570706c79206f662041706573536166654d6174683a206d756c7469706c69636174696f6e206f766572666c6f774552433732313a20617070726f76656420717565727920666f72206e6f6e6578697374656e7420746f6b656e4552433732313a207472616e73666572206f6620746f6b656e2074686174206973206e6f74206f776e4552433732314d657461646174613a2055524920717565727920666f72206e6f6e6578697374656e7420746f6b656e4552433732313a20617070726f76616c20746f2063757272656e74206f776e65724552433732313a207472616e736665722063616c6c6572206973206e6f74206f776e6572206e6f7220617070726f766564a2646970667358221220b0e64d1fa6c4dbeb9c6f54607d7e1996943fe27624a80652f57b53fda084621b64736f6c63430007000033000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000006080e6d70000000000000000000000000000000000000000000000000000000000000011426f7265644170655961636874436c756200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000044241594300000000000000000000000000000000000000000000000000000000', + from: accounts[0].address, + }) + await mine(testClient, { blocks: 1 }) + const { contractAddress } = await getTransactionReceipt(publicClient, { + hash, + }) + return { contractAddress } +} diff --git a/src/actions/public/callContract.ts b/src/actions/public/callContract.ts new file mode 100644 index 0000000000..40e7eb2bf6 --- /dev/null +++ b/src/actions/public/callContract.ts @@ -0,0 +1,80 @@ +import { Abi } from 'abitype' + +import type { Chain, Formatter } from '../../chains' +import type { PublicClient } from '../../clients' +import type { + Address, + ExtractArgsFromAbi, + ExtractResultFromAbi, + ExtractFunctionNameFromAbi, + GetValue, +} from '../../types' +import { + EncodeFunctionDataArgs, + decodeFunctionResult, + encodeFunctionData, + getContractError, +} from '../../utils' +import { call, CallArgs, FormattedCall } from './call' + +export type FormattedCallContract< + TFormatter extends Formatter | undefined = Formatter, +> = FormattedCall + +export type CallContractArgs< + TChain extends Chain = Chain, + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = any, +> = Omit, 'from' | 'to' | 'data' | 'value'> & { + address: Address + abi: TAbi + from?: Address + functionName: ExtractFunctionNameFromAbi + value?: GetValue['value']> +} & ExtractArgsFromAbi + +export type CallContractResponse< + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = string, +> = ExtractResultFromAbi + +export async function callContract< + TChain extends Chain, + TAbi extends Abi = Abi, + TFunctionName extends string = any, +>( + client: PublicClient, + { + abi, + address, + args, + functionName, + ...callRequest + }: CallContractArgs, +): Promise> { + const calldata = encodeFunctionData({ + abi, + args, + functionName, + } as unknown as EncodeFunctionDataArgs) + try { + const { data } = await call(client, { + data: calldata, + to: address, + ...callRequest, + } as unknown as CallArgs) + return decodeFunctionResult({ + abi, + functionName, + data: data || '0x', + }) as CallContractResponse + } catch (err) { + throw getContractError(err, { + abi, + address, + args, + functionName, + sender: callRequest.from, + }) + } +} diff --git a/src/actions/public/index.test.ts b/src/actions/public/index.test.ts index 4afd95b1e2..60b6be705b 100644 --- a/src/actions/public/index.test.ts +++ b/src/actions/public/index.test.ts @@ -6,6 +6,7 @@ test('exports actions', () => { expect(actions).toMatchInlineSnapshot(` { "call": [Function], + "callContract": [Function], "createBlockFilter": [Function], "createPendingTransactionFilter": [Function], "estimateGas": [Function], diff --git a/src/actions/public/index.ts b/src/actions/public/index.ts index 3c64589ea3..0d8b4ac243 100644 --- a/src/actions/public/index.ts +++ b/src/actions/public/index.ts @@ -1,6 +1,13 @@ export { call } from './call' export type { CallArgs, CallResponse, FormattedCall } from './call' +export { callContract } from './callContract' +export type { + CallContractArgs, + CallContractResponse, + FormattedCallContract, +} from './callContract' + export { createPendingTransactionFilter } from './createPendingTransactionFilter' export type { CreatePendingTransactionFilterResponse } from './createPendingTransactionFilter' diff --git a/src/errors/abi.ts b/src/errors/abi.ts index 2b6c22690e..67f24e6b0e 100644 --- a/src/errors/abi.ts +++ b/src/errors/abi.ts @@ -43,6 +43,13 @@ export class AbiDecodingDataSizeInvalidError extends BaseError { } } +export class AbiDecodingZeroDataError extends BaseError { + name = 'AbiDecodingZeroDataError' + constructor() { + super('Cannot decode zero data ("0x") with ABI parameters.') + } +} + export class AbiEncodingArrayLengthMismatchError extends BaseError { name = 'AbiEncodingArrayLengthMismatchError' constructor({ @@ -220,15 +227,12 @@ export class InvalidArrayError extends BaseError { export class InvalidDefinitionTypeError extends BaseError { name = 'InvalidDefinitionTypeError' - constructor(type: string, { docsPath }: { docsPath: string }) { + constructor(type: string) { super( [ `"${type}" is not a valid definition type.`, 'Valid types: "function", "event", "error"', ].join('\n'), - { - docsPath, - }, ) } } diff --git a/src/errors/base.ts b/src/errors/base.ts index 9163130737..75ee879cd0 100644 --- a/src/errors/base.ts +++ b/src/errors/base.ts @@ -1,5 +1,6 @@ // @ts-ignore import pkg from '../../package.json' +import { stringify } from '../utils/stringify' /* c8 ignore next */ const version = process.env.TEST ? '1.0.2' : pkg.version @@ -42,7 +43,7 @@ export class BaseError extends Error { ...(args.cause && !(args.cause instanceof BaseError) && Object.keys(args.cause).length > 0 - ? [`Internal Error: ${JSON.stringify(args.cause)}`] + ? [`Internal Error: ${stringify(args.cause)}`] : []), ].join('\n') diff --git a/src/errors/contract.ts b/src/errors/contract.ts new file mode 100644 index 0000000000..98c544965f --- /dev/null +++ b/src/errors/contract.ts @@ -0,0 +1,125 @@ +import { Abi } from 'abitype' +import { Address } from '../types' +import { BaseError } from './base' + +export class ContractMethodExecutionError extends BaseError { + abi?: Abi + args?: unknown[] + contractAddress?: Address + formattedArgs?: string + functionName?: string + reason?: string + sender?: Address + + name = 'ContractMethodExecutionError' + + constructor( + message?: string, + { + abi, + args, + cause, + contractAddress, + formattedArgs, + functionName, + functionWithParams, + sender, + }: { + abi?: Abi + args?: any + cause?: Error + contractAddress?: Address + formattedArgs?: string + functionName?: string + functionWithParams?: string + sender?: Address + } = {}, + ) { + super( + [ + message, + ' ', + sender && `Sender: ${sender}`, + contractAddress && + `Contract: ${ + /* c8 ignore start */ + process.env.TEST + ? '0x0000000000000000000000000000000000000000' + : contractAddress + /* c8 ignore end */ + }`, + functionWithParams && `Function: ${functionWithParams}`, + formattedArgs && + `Arguments: ${[...Array(functionName?.length ?? 0).keys()] + .map(() => ' ') + .join('')}${formattedArgs}`, + ] + .filter(Boolean) + .join('\n'), + { + cause, + }, + ) + if (message) this.reason = message + this.abi = abi + this.args = args + this.contractAddress = contractAddress + this.functionName = functionName + this.sender = sender + } +} + +export class ContractMethodZeroDataError extends BaseError { + abi?: Abi + args?: unknown[] + contractAddress?: Address + functionName?: string + functionWithParams?: string + + name = 'ContractMethodZeroDataError' + + constructor({ + abi, + args, + cause, + contractAddress, + functionName, + functionWithParams, + }: { + abi?: Abi + args?: any + cause?: Error + contractAddress?: Address + functionName?: string + functionWithParams?: string + } = {}) { + super( + [ + `The contract method "${functionName}" returned no data ("0x"). This could be due to any of the following:`, + `- The contract does not have the function "${functionName}",`, + '- The parameters passed to the contract function may be invalid, or', + '- The address is not a contract.', + ' ', + contractAddress && + `Contract: ${ + /* c8 ignore start */ + process.env.TEST + ? '0x0000000000000000000000000000000000000000' + : contractAddress + /* c8 ignore end */ + }`, + functionWithParams && `Function: ${functionWithParams}`, + functionWithParams && ` > "0x"`, + ] + .filter(Boolean) + .join('\n'), + { + cause, + }, + ) + this.abi = abi + this.args = args + this.contractAddress = contractAddress + this.functionName = functionName + } +} diff --git a/src/errors/index.ts b/src/errors/index.ts index d395ab5de7..a9881d62ec 100644 --- a/src/errors/index.ts +++ b/src/errors/index.ts @@ -2,6 +2,7 @@ export { AbiConstructorNotFoundError, AbiConstructorParamsNotFoundError, AbiDecodingDataSizeInvalidError, + AbiDecodingZeroDataError, AbiEncodingArrayLengthMismatchError, AbiEncodingLengthMismatchError, AbiErrorInputsNotFoundError, @@ -23,6 +24,11 @@ export { BaseError } from './base' export { BlockNotFoundError } from './block' +export { + ContractMethodExecutionError, + ContractMethodZeroDataError, +} from './contract' + export { SizeExceedsPaddingSizeError } from './data' export { diff --git a/src/errors/rpc.ts b/src/errors/rpc.ts index 68a453feb6..3ada454f31 100644 --- a/src/errors/rpc.ts +++ b/src/errors/rpc.ts @@ -1,3 +1,4 @@ +import { stringify } from '../utils' import { BaseError } from './base' export class HttpRequestError extends BaseError { @@ -21,7 +22,7 @@ export class HttpRequestError extends BaseError { '', `Status: ${status}`, `URL: ${url}`, - `Request body: ${JSON.stringify(body)}`, + `Request body: ${stringify(body)}`, ].join('\n'), { details, @@ -48,7 +49,7 @@ export class WebSocketRequestError extends BaseError { 'WebSocket request failed.', '', `URL: ${url}`, - `Request body: ${JSON.stringify(body)}`, + `Request body: ${stringify(body)}`, ].join('\n'), { details, @@ -76,7 +77,7 @@ export class RpcError extends BaseError { 'RPC Request failed.', '', `URL: ${url}`, - `Request body: ${JSON.stringify(body)}`, + `Request body: ${stringify(body)}`, ].join('\n'), { cause: error as any, @@ -102,7 +103,7 @@ export class TimeoutError extends BaseError { 'The request took too long to respond.', '', `URL: ${url}`, - `Request body: ${JSON.stringify(body)}`, + `Request body: ${stringify(body)}`, ].join('\n'), { details: 'The request timed out.', diff --git a/src/index.test.ts b/src/index.test.ts index d00d2f4acb..f810c9c52f 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -65,6 +65,7 @@ test('exports actions', () => { "bytesToNumber": [Function], "bytesToString": [Function], "call": [Function], + "callContract": [Function], "createBlockFilter": [Function], "createClient": [Function], "createPendingTransactionFilter": [Function], diff --git a/src/index.ts b/src/index.ts index 014a32e6db..d79b98cf7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ export type { CallArgs, + CallContractArgs, + CallContractResponse, CallResponse, CreateBlockFilterResponse, CreatePendingTransactionFilterResponse, @@ -74,6 +76,7 @@ export type { export { addChain, call, + callContract, createBlockFilter, createPendingTransactionFilter, estimateGas, @@ -222,6 +225,7 @@ export { export type { Address, + AbiItem, AccessList, Block, BlockIdentifier, @@ -260,6 +264,17 @@ export type { } from './types' export type { + DecodeAbiArgs, + DecodeErrorResultArgs, + DecodeFunctionDataArgs, + DecodeFunctionResultArgs, + DecodeFunctionResultResponse, + EncodeAbiArgs, + EncodeDeployDataArgs, + EncodeErrorResultArgs, + EncodeEventTopicsArgs, + EncodeFunctionDataArgs, + EncodeFunctionResultArgs, GetContractAddressOptions, GetCreateAddressOptions, GetCreate2AddressOptions, diff --git a/src/types/solidity.ts b/src/types/contract.ts similarity index 87% rename from src/types/solidity.ts rename to src/types/contract.ts index 45d7baf747..eac2b6b3b6 100644 --- a/src/types/solidity.ts +++ b/src/types/contract.ts @@ -1,23 +1,28 @@ import type { Abi, + AbiError, + AbiEvent, AbiFunction, AbiType, AbiTypeToPrimitiveType, + AbiParameter, AbiParameterToPrimitiveType, AbiParametersToPrimitiveTypes, ExtractAbiFunction, - AbiParameter, ExtractAbiEvent, - AbiEvent, - AbiError, ExtractAbiError, + AbiStateMutability, + ExtractAbiFunctionNames, } from 'abitype' +import { TransactionRequest } from './transaction' import type { Trim } from './utils' ////////////////////////////////////////////////////////////////////// // ABIs +export type AbiItem = Abi[number] + export type AbiEventParametersToPrimitiveTypes< TAbiParameters extends readonly AbiParameter[], TBase = TAbiParameters[0] extends { name: string } ? {} : [], @@ -132,31 +137,6 @@ export type ExtractErrorArgsFromAbi< /** Arguments to pass contract method */ args: TArgs } -export type ExtractResultFromAbi< - TAbi extends Abi | readonly unknown[], - TFunctionName extends string, - TAbiFunction extends AbiFunction & { type: 'function' } = TAbi extends Abi - ? ExtractAbiFunction - : AbiFunction & { type: 'function' }, - TArgs = AbiParametersToPrimitiveTypes, - FailedToParseArgs = - | ([TArgs] extends [never] ? true : false) - | (readonly unknown[] extends TArgs ? true : false), -> = true extends FailedToParseArgs - ? { - /** - * Arguments to pass contract method - * - * Use a [const assertion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions) on {@link abi} for type inference. - */ - result?: readonly unknown[] - } - : TArgs extends readonly [] - ? { result?: never } - : { - /** Arguments to pass contract method */ result: TArgs - } - export type ExtractEventArgsFromAbi< TAbi extends Abi | readonly unknown[], TFunctionName extends string, @@ -182,6 +162,42 @@ export type ExtractEventArgsFromAbi< args?: TArgs } +export type ExtractFunctionNameFromAbi< + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = string, + TAbiStateMutability extends AbiStateMutability = AbiStateMutability, +> = TAbi extends Abi + ? ExtractAbiFunctionNames< + TAbi, + TAbiStateMutability + > extends infer AbiFunctionNames + ? + | AbiFunctionNames + | (TFunctionName extends AbiFunctionNames ? TFunctionName : never) + | (Abi extends TAbi ? string : never) + : never + : TFunctionName + +export type ExtractResultFromAbi< + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = string, + TAbiFunction extends AbiFunction & { + type: 'function' + } = TAbi extends Abi + ? ExtractAbiFunction + : AbiFunction & { type: 'function' }, + TArgs = AbiParametersToPrimitiveTypes, + FailedToParseArgs = + | ([TArgs] extends [never] ? true : false) + | (readonly unknown[] extends TArgs ? true : false), +> = true extends FailedToParseArgs + ? unknown + : TArgs extends readonly [] + ? void + : TArgs extends readonly [infer Arg] + ? Arg + : TArgs + ////////////////////////////////////////////////////////////////////// // Event/Function Definitions @@ -293,3 +309,19 @@ export type ExtractArgsFromFunctionDefinition = ExtractArgsFromDefinition< TDef, { indexedOnly: false } > + +////////////////////////////////////////////////////////////////////// +// Call Args + +export type GetValue< + TAbi extends Abi | readonly unknown[], + TFunctionName extends string, + TValueType = TransactionRequest['value'], + TAbiFunction extends AbiFunction & { type: 'function' } = TAbi extends Abi + ? ExtractAbiFunction + : AbiFunction & { type: 'function' }, +> = TAbiFunction['stateMutability'] extends 'payable' + ? TValueType + : TAbiFunction['payable'] extends true + ? TValueType + : never diff --git a/src/types/index.ts b/src/types/index.ts index 8e01ef44db..827384e71e 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -6,6 +6,20 @@ export type { Uncle, } from './block' +export type { + AbiItem, + AbiEventParametersToPrimitiveTypes, + ExtractArgsFromAbi, + ExtractArgsFromEventDefinition, + ExtractArgsFromFunctionDefinition, + ExtractConstructorArgsFromAbi, + ExtractErrorArgsFromAbi, + ExtractEventArgsFromAbi, + ExtractFunctionNameFromAbi, + ExtractResultFromAbi, + GetValue, +} from './contract' + export type { EstimateGasParameters, FeeHistory, @@ -36,15 +50,6 @@ export type { RpcUncle, } from './rpc' -export type { - ExtractArgsFromAbi, - ExtractArgsFromEventDefinition, - ExtractArgsFromFunctionDefinition, - ExtractConstructorArgsFromAbi, - ExtractErrorArgsFromAbi, - ExtractResultFromAbi, -} from './solidity' - export type { AccessList, TransactionReceipt, diff --git a/src/utils/abi/decodeAbi.test.ts b/src/utils/abi/decodeAbi.test.ts index 0996fc5973..4e44ff9ed9 100644 --- a/src/utils/abi/decodeAbi.test.ts +++ b/src/utils/abi/decodeAbi.test.ts @@ -1586,3 +1586,30 @@ test('invalid type', () => { Version: viem@1.0.2" `) }) + +test('error: zero data', () => { + expect(() => + decodeAbi({ + params: [ + { + inputs: [], + name: 'foo', + outputs: [ + { + internalType: 'uint256', + name: 'x', + type: 'uint256', + }, + ], + stateMutability: 'pure', + type: 'function', + }, + ] as const, + data: '0x', + }), + ).toThrowErrorMatchingInlineSnapshot(` + "Cannot decode zero data (\\"0x\\") with ABI parameters. + + Version: viem@1.0.2" + `) +}) diff --git a/src/utils/abi/decodeAbi.ts b/src/utils/abi/decodeAbi.ts index f5c1789eb7..d22832cda8 100644 --- a/src/utils/abi/decodeAbi.ts +++ b/src/utils/abi/decodeAbi.ts @@ -6,6 +6,7 @@ import { import { AbiDecodingDataSizeInvalidError, + AbiDecodingZeroDataError, InvalidAbiDecodingTypeError, } from '../../errors' import { Hex } from '../../types' @@ -14,10 +15,16 @@ import { size, slice, trim } from '../data' import { hexToBigInt, hexToBool, hexToNumber, hexToString } from '../encoding' import { getArrayComponents } from './encodeAbi' +export type DecodeAbiArgs = { + data: Hex + params: TParams +} + export function decodeAbi({ data, params, -}: { data: Hex; params: TParams }) { +}: DecodeAbiArgs) { + if (data === '0x' && params.length > 0) throw new AbiDecodingZeroDataError() if (size(data) % 32 !== 0) throw new AbiDecodingDataSizeInvalidError(size(data)) const values = decodeParams({ @@ -98,7 +105,7 @@ function decodeParam({ //////////////////////////////////////////////////////////////////// function decodeAddress(value: Hex) { - return { consumed: 32, value: checksumAddress(trim(value)) } + return { consumed: 32, value: checksumAddress(slice(value, -20)) } } function decodeArray( diff --git a/src/utils/abi/decodeDeployData.ts b/src/utils/abi/decodeDeployData.ts index c22bed8f7f..c4da910893 100644 --- a/src/utils/abi/decodeDeployData.ts +++ b/src/utils/abi/decodeDeployData.ts @@ -9,14 +9,21 @@ import { decodeAbi } from './decodeAbi' const docsPath = '/docs/contract/decodeDeployData' +export type DecodeDeployDataArgs = { + abi: TAbi + bytecode: Hex + data: Hex +} +export type DecodeDeployDataResponse = { + args?: readonly unknown[] | undefined + bytecode: Hex +} + export function decodeDeployData({ abi, bytecode, data, -}: { abi: TAbi; bytecode: Hex; data: Hex }): { - args?: readonly unknown[] | undefined - bytecode: Hex -} { +}: DecodeDeployDataArgs): DecodeDeployDataResponse { if (data === bytecode) return { bytecode } const description = abi.find((x) => 'type' in x && x.type === 'constructor') diff --git a/src/utils/abi/decodeErrorResult.ts b/src/utils/abi/decodeErrorResult.ts index c73125487d..e508d1c873 100644 --- a/src/utils/abi/decodeErrorResult.ts +++ b/src/utils/abi/decodeErrorResult.ts @@ -4,12 +4,14 @@ import { Hex } from '../../types' import { slice } from '../data' import { getFunctionSignature } from '../hash' import { decodeAbi } from './decodeAbi' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' -export function decodeErrorResult({ abi, data }: { abi: Abi; data: Hex }) { +export type DecodeErrorResultArgs = { abi: Abi; data: Hex } + +export function decodeErrorResult({ abi, data }: DecodeErrorResultArgs) { const signature = slice(data, 0, 4) const description = abi.find( - (x) => signature === getFunctionSignature(getDefinition(x)), + (x) => signature === getFunctionSignature(formatAbiItemWithParams(x)), ) if (!description) throw new AbiErrorSignatureNotFoundError(signature, { diff --git a/src/utils/abi/decodeFunctionData.ts b/src/utils/abi/decodeFunctionData.ts index 2b012a7152..cba8965719 100644 --- a/src/utils/abi/decodeFunctionData.ts +++ b/src/utils/abi/decodeFunctionData.ts @@ -5,12 +5,14 @@ import { Hex } from '../../types' import { slice } from '../data' import { getFunctionSignature } from '../hash' import { decodeAbi } from './decodeAbi' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' -export function decodeFunctionData({ abi, data }: { abi: Abi; data: Hex }) { +export type DecodeFunctionDataArgs = { abi: Abi; data: Hex } + +export function decodeFunctionData({ abi, data }: DecodeFunctionDataArgs) { const signature = slice(data, 0, 4) const description = abi.find( - (x) => signature === getFunctionSignature(getDefinition(x)), + (x) => signature === getFunctionSignature(formatAbiItemWithParams(x)), ) if (!description) throw new AbiFunctionSignatureNotFoundError(signature, { diff --git a/src/utils/abi/decodeFunctionResult.test.ts b/src/utils/abi/decodeFunctionResult.test.ts index fb2f84cf28..93864179db 100644 --- a/src/utils/abi/decodeFunctionResult.test.ts +++ b/src/utils/abi/decodeFunctionResult.test.ts @@ -13,7 +13,7 @@ test('returns ()', () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'foo', data: '0x', }), @@ -28,7 +28,7 @@ test('returns ()', () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'foo', // @ts-expect-error data: '', @@ -53,7 +53,7 @@ test('returns (address)', () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'foo', data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', }), @@ -111,7 +111,7 @@ test('returns (Bar)', () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'bar', data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000000045', }), @@ -182,7 +182,7 @@ test('returns (Bar, string)', () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'baz', data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac0000000000000000000000000000000000000000000000000000000000010f2c0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac000000000000000000000000000000000000000000000000000000000000004500000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000057761676d69000000000000000000000000000000000000000000000000000000', }), @@ -217,7 +217,8 @@ test("error: function doesn't exist", () => { stateMutability: 'pure', type: 'function', }, - ], + ] as const, + // @ts-expect-error functionName: 'baz', data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', }), @@ -237,14 +238,13 @@ test("error: function doesn't exist", () => { expect(() => decodeFunctionResult({ abi: [ - // @ts-expect-error { inputs: [], name: 'foo', stateMutability: 'pure', type: 'function', }, - ], + ] as const, functionName: 'foo', data: '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', }), diff --git a/src/utils/abi/decodeFunctionResult.ts b/src/utils/abi/decodeFunctionResult.ts index dfcc142872..7abc27694c 100644 --- a/src/utils/abi/decodeFunctionResult.ts +++ b/src/utils/abi/decodeFunctionResult.ts @@ -4,26 +4,50 @@ import { AbiFunctionOutputsNotFoundError, } from '../../errors' -import { Hex } from '../../types' +import { + ExtractFunctionNameFromAbi, + ExtractResultFromAbi, + Hex, +} from '../../types' import { decodeAbi } from './decodeAbi' const docsPath = '/docs/contract/decodeFunctionResult' +export type DecodeFunctionResultArgs< + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = any, +> = { + abi: TAbi + functionName: ExtractFunctionNameFromAbi + data: Hex +} + +export type DecodeFunctionResultResponse< + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = any, +> = ExtractResultFromAbi + export function decodeFunctionResult< - TAbi extends Abi = Abi, - TFunctionName extends ExtractAbiFunctionNames = any, + TAbi extends Abi | readonly unknown[] = Abi, + TFunctionName extends string = any, >({ abi, functionName, data, -}: { abi: TAbi; functionName: TFunctionName; data: Hex }) { - const description = abi.find((x) => 'name' in x && x.name === functionName) +}: DecodeFunctionResultArgs): DecodeFunctionResultResponse< + TAbi, + TFunctionName +> { + const description = (abi as Abi).find( + (x) => 'name' in x && x.name === functionName, + ) if (!description) throw new AbiFunctionNotFoundError(functionName, { docsPath }) if (!('outputs' in description)) throw new AbiFunctionOutputsNotFoundError(functionName, { docsPath }) + const values = decodeAbi({ data, params: description.outputs }) - if (values && values.length > 1) return values - if (values && values.length === 1) return values[0] - return undefined + if (values && values.length > 1) return values as any + if (values && values.length === 1) return values[0] as any + return undefined as any } diff --git a/src/utils/abi/encodeAbi.ts b/src/utils/abi/encodeAbi.ts index cca05deca7..2fe10aee24 100644 --- a/src/utils/abi/encodeAbi.ts +++ b/src/utils/abi/encodeAbi.ts @@ -14,16 +14,18 @@ import { Hex } from '../../types' import { concat, padHex, size } from '../data' import { boolToHex, numberToHex, stringToHex } from '../encoding' +export type EncodeAbiArgs = { + params: TParams + values: AbiParametersToPrimitiveTypes +} + /** * @description Encodes a list of primitive values into an ABI-encoded hex value. */ export function encodeAbi({ params, values, -}: { - params: TParams - values: AbiParametersToPrimitiveTypes -}) { +}: EncodeAbiArgs) { if (params.length !== values.length) throw new AbiEncodingLengthMismatchError({ expectedLength: params.length, diff --git a/src/utils/abi/encodeDeployData.ts b/src/utils/abi/encodeDeployData.ts index 27f8191d85..ad823f64b4 100644 --- a/src/utils/abi/encodeDeployData.ts +++ b/src/utils/abi/encodeDeployData.ts @@ -10,11 +10,16 @@ import { encodeAbi } from './encodeAbi' const docsPath = '/docs/contract/encodeDeployData' +export type EncodeDeployDataArgs = { + abi: TAbi + bytecode: Hex +} & ExtractConstructorArgsFromAbi + export function encodeDeployData({ abi, args, bytecode, -}: { abi: TAbi; bytecode: Hex } & ExtractConstructorArgsFromAbi) { +}: EncodeDeployDataArgs) { if (!args || args.length === 0) return bytecode const description = abi.find((x) => 'type' in x && x.type === 'constructor') diff --git a/src/utils/abi/encodeErrorResult.ts b/src/utils/abi/encodeErrorResult.ts index 501e4ec0cd..cf4e152b87 100644 --- a/src/utils/abi/encodeErrorResult.ts +++ b/src/utils/abi/encodeErrorResult.ts @@ -8,24 +8,26 @@ import { ExtractErrorArgsFromAbi, Hex } from '../../types' import { concatHex } from '../data' import { getFunctionSignature } from '../hash' import { encodeAbi } from './encodeAbi' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' +import { getAbiItem } from './getAbiItem' const docsPath = '/docs/contract/encodeErrorResult' +export type EncodeErrorResultArgs< + TAbi extends Abi = Abi, + TErrorName extends ExtractAbiErrorNames = any, +> = { + abi: TAbi + errorName: TErrorName +} & ExtractErrorArgsFromAbi + export function encodeErrorResult< TAbi extends Abi = Abi, TErrorName extends ExtractAbiErrorNames = any, ->({ - abi, - errorName, - args, -}: { abi: TAbi; errorName: TErrorName } & ExtractErrorArgsFromAbi< - TAbi, - TErrorName ->) { - const description = abi.find((x) => 'name' in x && x.name === errorName) +>({ abi, errorName, args }: EncodeErrorResultArgs) { + const description = getAbiItem({ abi, name: errorName }) if (!description) throw new AbiErrorNotFoundError(errorName, { docsPath }) - const definition = getDefinition(description) + const definition = formatAbiItemWithParams(description) const signature = getFunctionSignature(definition) let data: Hex = '0x' diff --git a/src/utils/abi/encodeEventTopics.ts b/src/utils/abi/encodeEventTopics.ts index 9b47a31c01..d1dd07ba8c 100644 --- a/src/utils/abi/encodeEventTopics.ts +++ b/src/utils/abi/encodeEventTopics.ts @@ -4,43 +4,45 @@ import { AbiParameterToPrimitiveType, ExtractAbiEventNames, } from 'abitype' + import { AbiEventNotFoundError, FilterTypeNotSupportedError, } from '../../errors' -import { Hex } from '../../types' -import { ExtractEventArgsFromAbi } from '../../types/solidity' +import { ExtractEventArgsFromAbi, Hex } from '../../types' import { encodeBytes } from '../encoding' import { keccak256, getEventSignature } from '../hash' import { encodeAbi } from './encodeAbi' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' +import { getAbiItem } from './getAbiItem' -export function encodeEventTopics< +export type EncodeEventTopicsArgs< TAbi extends Abi = Abi, TEventName extends ExtractAbiEventNames = any, ->({ - abi, - eventName, - args, -}: { +> = { abi: TAbi eventName: TEventName -} & ExtractEventArgsFromAbi) { - const description = abi.find((x) => 'name' in x && x.name === eventName) - if (!description) +} & ExtractEventArgsFromAbi + +export function encodeEventTopics< + TAbi extends Abi = Abi, + TEventName extends ExtractAbiEventNames = any, +>({ abi, eventName, args }: EncodeEventTopicsArgs) { + const abiItem = getAbiItem({ abi, name: eventName }) + if (!abiItem) throw new AbiEventNotFoundError(eventName, { docsPath: '/docs/contract/encodeEventTopics', }) - const definition = getDefinition(description) + const definition = formatAbiItemWithParams(abiItem) const signature = getEventSignature(definition as `${string}(${string})`) let topics: Hex[] = [] - if (args && 'inputs' in description) { + if (args && 'inputs' in abiItem) { const args_ = Array.isArray(args) ? args - : description.inputs?.map((x: any) => (args as any)[x.name]) ?? [] + : abiItem.inputs?.map((x: any) => (args as any)[x.name]) ?? [] topics = - description.inputs + abiItem.inputs ?.filter((param) => 'indexed' in param && param.indexed) .map((param, i) => Array.isArray(args_[i]) diff --git a/src/utils/abi/encodeFunctionData.ts b/src/utils/abi/encodeFunctionData.ts index 8f1168139c..c352cce6d4 100644 --- a/src/utils/abi/encodeFunctionData.ts +++ b/src/utils/abi/encodeFunctionData.ts @@ -1,29 +1,31 @@ import { Abi, ExtractAbiFunctionNames } from 'abitype' import { AbiFunctionNotFoundError } from '../../errors' -import { ExtractArgsFromAbi } from '../../types' +import { ExtractArgsFromAbi, ExtractFunctionNameFromAbi } from '../../types' import { concatHex } from '../data' import { getFunctionSignature } from '../hash' import { encodeAbi } from './encodeAbi' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' +import { getAbiItem } from './getAbiItem' + +export type EncodeFunctionDataArgs< + TAbi extends Abi = Abi, + TFunctionName extends string = any, +> = { + abi: TAbi + functionName: ExtractFunctionNameFromAbi +} & ExtractArgsFromAbi export function encodeFunctionData< TAbi extends Abi = Abi, - TFunctionName extends ExtractAbiFunctionNames = any, ->({ - abi, - args, - functionName, -}: { abi: TAbi; functionName: TFunctionName } & ExtractArgsFromAbi< - TAbi, - TFunctionName ->) { - const description = abi.find((x) => 'name' in x && x.name === functionName) + TFunctionName extends string = any, +>({ abi, args, functionName }: EncodeFunctionDataArgs) { + const description = getAbiItem({ abi, name: functionName }) if (!description) throw new AbiFunctionNotFoundError(functionName, { docsPath: '/docs/contract/encodeFunctionData', }) - const definition = getDefinition(description) + const definition = formatAbiItemWithParams(description) const signature = getFunctionSignature(definition) const data = 'inputs' in description && description.inputs diff --git a/src/utils/abi/encodeFunctionResult.test.ts b/src/utils/abi/encodeFunctionResult.test.ts index 6597686287..b38fd097c8 100644 --- a/src/utils/abi/encodeFunctionResult.test.ts +++ b/src/utils/abi/encodeFunctionResult.test.ts @@ -69,7 +69,7 @@ test('returns (address)', () => { }, ] as const, functionName: 'foo', - result: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], + result: '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', }), ).toEqual( '0x000000000000000000000000a5cc3c03994db5b0d9a5eedd10cabab0813678ac', diff --git a/src/utils/abi/encodeFunctionResult.ts b/src/utils/abi/encodeFunctionResult.ts index 5f7f2d37f5..60c4552846 100644 --- a/src/utils/abi/encodeFunctionResult.ts +++ b/src/utils/abi/encodeFunctionResult.ts @@ -4,22 +4,28 @@ import { AbiFunctionOutputsNotFoundError, } from '../../errors' -import { ExtractResultFromAbi } from '../../types' +import { ExtractFunctionNameFromAbi, ExtractResultFromAbi } from '../../types' import { encodeAbi } from './encodeAbi' const docsPath = '/docs/contract/encodeFunctionResult' +export type EncodeFunctionResultArgs< + TAbi extends Abi = Abi, + TFunctionName extends string = any, +> = { + abi: TAbi + functionName: ExtractFunctionNameFromAbi + result?: ExtractResultFromAbi +} + export function encodeFunctionResult< TAbi extends Abi = Abi, - TFunctionName extends ExtractAbiFunctionNames = any, + TFunctionName extends string = any, >({ abi, functionName, result, -}: { abi: TAbi; functionName: TFunctionName } & ExtractResultFromAbi< - TAbi, - TFunctionName ->) { +}: EncodeFunctionResultArgs) { const description = abi.find((x) => 'name' in x && x.name === functionName) if (!description) throw new AbiFunctionNotFoundError(functionName, { docsPath }) diff --git a/src/utils/abi/formatAbiItemWithArgs.test.ts b/src/utils/abi/formatAbiItemWithArgs.test.ts new file mode 100644 index 0000000000..9766565f1a --- /dev/null +++ b/src/utils/abi/formatAbiItemWithArgs.test.ts @@ -0,0 +1,291 @@ +import { expect, test } from 'vitest' + +import { formatAbiItemWithArgs } from './formatAbiItemWithArgs' + +test('default', () => { + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [], + }, + args: [], + }), + ).toEqual('foo()') + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + name: 'x', + type: 'uint256', + }, + { + name: 'sender', + type: 'address', + }, + ], + }, + args: [1n, '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], + }), + ).toEqual('foo(1, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC)') + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + name: 'x', + type: 'uint256', + }, + { + name: 'sender', + type: 'address', + }, + ], + }, + args: [1n, '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], + includeFunctionName: false, + }), + ).toEqual('(1, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC)') + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + type: 'uint256', + }, + { + type: 'address', + }, + ], + }, + args: [1n, '0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], + }), + ).toEqual('foo(1, 0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC)') + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + components: [ + { + internalType: 'uint256[]', + name: 'x', + type: 'uint256[]', + }, + { + internalType: 'bool', + name: 'y', + type: 'bool', + }, + { + internalType: 'string[]', + name: 'z', + type: 'string[]', + }, + ], + internalType: 'struct ABIExample.Baz', + name: 'bar', + type: 'tuple', + }, + { + internalType: 'uint256', + name: 'a', + type: 'uint256', + }, + { + internalType: 'string[]', + name: 'b', + type: 'string[]', + }, + ], + }, + args: [ + { + x: [1n, 2n, 3n, 4n], + y: true, + z: ['hello', 'world'], + }, + 420n, + ['wagmi', 'viem'], + ], + }), + ).toMatchInlineSnapshot( + '"foo({\\"x\\":[\\"1\\",\\"2\\",\\"3\\",\\"4\\"],\\"y\\":true,\\"z\\":[\\"hello\\",\\"world\\"]}, 420, [\\"wagmi\\",\\"viem\\"])"', + ) + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + components: [ + { + internalType: 'uint256[]', + name: 'x', + type: 'uint256[]', + }, + { + internalType: 'bool', + name: 'y', + type: 'bool', + }, + { + internalType: 'string[]', + name: 'z', + type: 'string[]', + }, + ], + internalType: 'struct ABIExample.Baz', + name: 'bar', + type: 'tuple', + }, + { + internalType: 'uint256', + name: 'a', + type: 'uint256', + }, + { + internalType: 'string[]', + name: 'b', + type: 'string[]', + }, + ], + }, + args: [ + { + x: [1n, 2n, 3n, 4n], + y: true, + z: ['hello', 'world'], + }, + 420n, + ['wagmi', 'viem'], + ], + includeFunctionName: false, + }), + ).toMatchInlineSnapshot( + '"({\\"x\\":[\\"1\\",\\"2\\",\\"3\\",\\"4\\"],\\"y\\":true,\\"z\\":[\\"hello\\",\\"world\\"]}, 420, [\\"wagmi\\",\\"viem\\"])"', + ) + + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [ + { + components: [ + { + internalType: 'uint256[]', + name: 'x', + type: 'uint256[]', + }, + { + internalType: 'bool', + name: 'y', + type: 'bool', + }, + { + internalType: 'string[]', + name: 'z', + type: 'string[]', + }, + ], + internalType: 'struct ABIExample.Baz', + name: 'bar', + type: 'tuple', + }, + { + internalType: 'uint256', + name: 'a', + type: 'uint256', + }, + { + internalType: 'string[]', + name: 'b', + type: 'string[]', + }, + ], + }, + args: [ + { + x: [1n, 2n, 3n, 4n], + y: true, + z: ['hello', 'world'], + }, + 420n, + ['wagmi', 'viem'], + ], + includeName: true, + }), + ).toMatchInlineSnapshot( + '"foo(bar: {\\"x\\":[\\"1\\",\\"2\\",\\"3\\",\\"4\\"],\\"y\\":true,\\"z\\":[\\"hello\\",\\"world\\"]}, a: 420, b: [\\"wagmi\\",\\"viem\\"])"', + ) + + expect( + formatAbiItemWithArgs({ + // @ts-expect-error + abiItem: { + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + inputs: [], + }, + args: [], + }), + ).toEqual(undefined) + expect( + formatAbiItemWithArgs({ + // @ts-expect-error + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + args: [], + }), + ).toEqual(undefined) + expect( + formatAbiItemWithArgs({ + abiItem: { + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + // @ts-expect-error + inputs: undefined, + }, + args: [], + }), + ).toEqual(undefined) +}) diff --git a/src/utils/abi/formatAbiItemWithArgs.ts b/src/utils/abi/formatAbiItemWithArgs.ts new file mode 100644 index 0000000000..8028a39ede --- /dev/null +++ b/src/utils/abi/formatAbiItemWithArgs.ts @@ -0,0 +1,27 @@ +import { AbiParameter } from 'abitype' +import { AbiItem } from '../../types' +import { stringify } from '../../utils' + +export function formatAbiItemWithArgs({ + abiItem, + args, + includeFunctionName = true, + includeName = false, +}: { + abiItem: AbiItem + args: readonly unknown[] + includeFunctionName?: boolean + includeName?: boolean +}) { + if (!('name' in abiItem)) return + if (!('inputs' in abiItem)) return + if (!abiItem.inputs) return + return `${includeFunctionName ? abiItem.name : ''}(${abiItem.inputs + .map( + (input: AbiParameter, i: number) => + `${includeName && input.name ? `${input.name}: ` : ''}${ + typeof args[i] === 'object' ? stringify(args[i]) : args[i] + }`, + ) + .join(', ')})` +} diff --git a/src/utils/abi/getDefinition.test.ts b/src/utils/abi/formatAbiItemWithParams.test.ts similarity index 58% rename from src/utils/abi/getDefinition.test.ts rename to src/utils/abi/formatAbiItemWithParams.test.ts index 285d4c7f47..311fd80268 100644 --- a/src/utils/abi/getDefinition.test.ts +++ b/src/utils/abi/formatAbiItemWithParams.test.ts @@ -1,11 +1,11 @@ import { expect, test } from 'vitest' -import { getDefinition } from './getDefinition' +import { formatAbiItemWithParams } from './formatAbiItemWithParams' test('foo()', () => { expect( // @ts-expect-error - getDefinition({ + formatAbiItemWithParams({ name: 'foo', outputs: [], stateMutability: 'nonpayable', @@ -13,7 +13,7 @@ test('foo()', () => { }), ).toEqual('foo()') expect( - getDefinition({ + formatAbiItemWithParams({ inputs: [], name: 'foo', outputs: [], @@ -25,7 +25,7 @@ test('foo()', () => { test('foo(uint256)', () => { expect( - getDefinition({ + formatAbiItemWithParams({ inputs: [ { internalType: 'uint256', @@ -39,11 +39,29 @@ test('foo(uint256)', () => { type: 'function', }), ).toEqual('foo(uint256)') + expect( + formatAbiItemWithParams( + { + inputs: [ + { + internalType: 'uint256', + name: 'a', + type: 'uint256', + }, + ], + name: 'foo', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { includeName: true }, + ), + ).toEqual('foo(uint256 a)') }) test('getVoter((uint256,bool,address,uint256),string[],bytes)', () => { expect( - getDefinition({ + formatAbiItemWithParams({ inputs: [ { components: [ @@ -89,11 +107,63 @@ test('getVoter((uint256,bool,address,uint256),string[],bytes)', () => { type: 'function', }), ).toEqual('getVoter((uint256,bool,address,uint256),string[],bytes)') + expect( + formatAbiItemWithParams( + { + inputs: [ + { + components: [ + { + internalType: 'uint256', + name: 'weight', + type: 'uint256', + }, + { + internalType: 'bool', + name: 'voted', + type: 'bool', + }, + { + internalType: 'address', + name: 'delegate', + type: 'address', + }, + { + internalType: 'uint256', + name: 'vote', + type: 'uint256', + }, + ], + internalType: 'struct Ballot.Voter', + name: 'voter', + type: 'tuple', + }, + { + internalType: 'string[]', + name: 'foo', + type: 'string[]', + }, + { + internalType: 'bytes', + name: 'bar', + type: 'bytes', + }, + ], + name: 'getVoter', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { includeName: true }, + ), + ).toEqual( + 'getVoter((uint256 weight, bool voted, address delegate, uint256 vote), string[] foo, bytes bar)', + ) }) test('VoterEvent((uint256,bool,address,uint256),string[],bytes)', () => { expect( - getDefinition({ + formatAbiItemWithParams({ inputs: [ { components: [ @@ -139,11 +209,63 @@ test('VoterEvent((uint256,bool,address,uint256),string[],bytes)', () => { type: 'event', }), ).toEqual('VoterEvent((uint256,bool,address,uint256),string[],bytes)') + expect( + formatAbiItemWithParams( + { + inputs: [ + { + components: [ + { + internalType: 'uint256', + name: 'weight', + type: 'uint256', + }, + { + internalType: 'bool', + name: 'voted', + type: 'bool', + }, + { + internalType: 'address', + name: 'delegate', + type: 'address', + }, + { + internalType: 'uint256', + name: 'vote', + type: 'uint256', + }, + ], + internalType: 'struct Ballot.Voter', + name: 'voter', + type: 'tuple', + }, + { + internalType: 'string[]', + name: 'foo', + type: 'string[]', + }, + { + internalType: 'bytes', + name: 'bar', + type: 'bytes', + }, + ], + name: 'VoterEvent', + outputs: [], + stateMutability: 'nonpayable', + type: 'event', + }, + { includeName: true }, + ), + ).toEqual( + 'VoterEvent((uint256 weight, bool voted, address delegate, uint256 vote), string[] foo, bytes bar)', + ) }) test('VoterError((uint256,bool,address,uint256),string[],bytes)', () => { expect( - getDefinition({ + formatAbiItemWithParams({ inputs: [ { components: [ @@ -193,7 +315,7 @@ test('VoterError((uint256,bool,address,uint256),string[],bytes)', () => { test('error: invalid type', () => { expect(() => - getDefinition({ + formatAbiItemWithParams({ inputs: [ { internalType: 'bytes32[]', @@ -208,8 +330,6 @@ test('error: invalid type', () => { "\\"constructor\\" is not a valid definition type. Valid types: \\"function\\", \\"event\\", \\"error\\" - Docs: https://viem.sh/docs/contract/getDefinition - Version: viem@1.0.2" `) }) diff --git a/src/utils/abi/formatAbiItemWithParams.ts b/src/utils/abi/formatAbiItemWithParams.ts new file mode 100644 index 0000000000..a779f0be10 --- /dev/null +++ b/src/utils/abi/formatAbiItemWithParams.ts @@ -0,0 +1,41 @@ +import { AbiParameter } from 'abitype' + +import { InvalidDefinitionTypeError } from '../../errors' +import { AbiItem } from '../../types' + +export function formatAbiItemWithParams( + abiItem: AbiItem, + { includeName = false }: { includeName?: boolean } = {}, +) { + if ( + abiItem.type !== 'function' && + abiItem.type !== 'event' && + abiItem.type !== 'error' + ) + throw new InvalidDefinitionTypeError(abiItem.type) + + return `${abiItem.name}(${getParams(abiItem.inputs, { includeName })})` +} + +function getParams( + params: readonly AbiParameter[] | undefined, + { includeName }: { includeName: boolean }, +): string { + if (!params) return '' + return params + .map((param) => getParam(param, { includeName })) + .join(includeName ? ', ' : ',') +} + +function getParam( + param: AbiParameter, + { includeName }: { includeName: boolean }, +): string { + if (param.type.startsWith('tuple')) { + return `(${getParams( + (param as unknown as { components: AbiParameter[] }).components, + { includeName }, + )})${param.type.slice('tuple'.length)}` + } + return param.type + (includeName && param.name ? ` ${param.name}` : '') +} diff --git a/src/utils/abi/getAbiItem.ts b/src/utils/abi/getAbiItem.ts new file mode 100644 index 0000000000..8c41662734 --- /dev/null +++ b/src/utils/abi/getAbiItem.ts @@ -0,0 +1,5 @@ +import { Abi } from 'abitype' + +export function getAbiItem({ abi, name }: { abi: Abi; name: string }) { + return abi.find((x) => 'name' in x && x.name === name) +} diff --git a/src/utils/abi/getDefinition.ts b/src/utils/abi/getDefinition.ts deleted file mode 100644 index 2ec70ab100..0000000000 --- a/src/utils/abi/getDefinition.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { AbiError, AbiEvent, AbiFunction, AbiParameter } from 'abitype' -import { InvalidDefinitionTypeError } from '../../errors' - -export function getDefinition(description: AbiFunction | AbiEvent | AbiError) { - if ( - description.type !== 'function' && - description.type !== 'event' && - description.type !== 'error' - ) - throw new InvalidDefinitionTypeError(description.type, { - docsPath: '/docs/contract/getDefinition', - }) - - return `${description.name}(${getParams(description.inputs)})` -} - -function getParams(params?: readonly AbiParameter[]): string { - if (!params) return '' - return params.map(getParam).join(',') -} - -function getParam(param: AbiParameter): string { - if (param.type.startsWith('tuple')) { - return `(${getParams( - (param as unknown as { components: AbiParameter[] }).components, - )})${param.type.slice('tuple'.length)}` - } - return param.type -} diff --git a/src/utils/abi/index.test.ts b/src/utils/abi/index.test.ts index e32327a74e..c3ed72bbb3 100644 --- a/src/utils/abi/index.test.ts +++ b/src/utils/abi/index.test.ts @@ -15,6 +15,9 @@ test('exports utils', () => { "encodeEventTopics": [Function], "encodeFunctionData": [Function], "encodeFunctionResult": [Function], + "formatAbiItemWithArgs": [Function], + "formatAbiItemWithParams": [Function], + "getAbiItem": [Function], } `) }) diff --git a/src/utils/abi/index.ts b/src/utils/abi/index.ts index 54fc1ee215..060b28b3ef 100644 --- a/src/utils/abi/index.ts +++ b/src/utils/abi/index.ts @@ -1,19 +1,38 @@ +export type { DecodeAbiArgs } from './decodeAbi' export { decodeAbi } from './decodeAbi' +export type { DecodeErrorResultArgs } from './decodeErrorResult' export { decodeErrorResult } from './decodeErrorResult' +export type { DecodeFunctionDataArgs } from './decodeFunctionData' export { decodeFunctionData } from './decodeFunctionData' +export type { + DecodeFunctionResultArgs, + DecodeFunctionResultResponse, +} from './decodeFunctionResult' export { decodeFunctionResult } from './decodeFunctionResult' +export type { EncodeAbiArgs } from './encodeAbi' export { encodeAbi } from './encodeAbi' +export type { EncodeDeployDataArgs } from './encodeDeployData' export { encodeDeployData } from './encodeDeployData' +export type { EncodeErrorResultArgs } from './encodeErrorResult' export { encodeErrorResult } from './encodeErrorResult' +export type { EncodeEventTopicsArgs } from './encodeEventTopics' export { encodeEventTopics } from './encodeEventTopics' +export type { EncodeFunctionDataArgs } from './encodeFunctionData' export { encodeFunctionData } from './encodeFunctionData' +export type { EncodeFunctionResultArgs } from './encodeFunctionResult' export { encodeFunctionResult } from './encodeFunctionResult' + +export { formatAbiItemWithArgs } from './formatAbiItemWithArgs' + +export { formatAbiItemWithParams } from './formatAbiItemWithParams' + +export { getAbiItem } from './getAbiItem' diff --git a/src/utils/address/getAddress.ts b/src/utils/address/getAddress.ts index 55da468c91..36e7c62471 100644 --- a/src/utils/address/getAddress.ts +++ b/src/utils/address/getAddress.ts @@ -11,10 +11,10 @@ export function checksumAddress(address_: Address): Address { let address = hexAddress.split('') for (let i = 0; i < 40; i += 2) { - if (hash?.[i >> 1] >> 4 >= 8) { + if (hash?.[i >> 1] >> 4 >= 8 && address[i]) { address[i] = address[i].toUpperCase() } - if ((hash[i >> 1] & 0x0f) >= 8) { + if ((hash[i >> 1] & 0x0f) >= 8 && address[i + 1]) { address[i + 1] = address[i + 1].toUpperCase() } } diff --git a/src/utils/solidity.test.ts b/src/utils/contract/extractFunctionParts.test.ts similarity index 98% rename from src/utils/solidity.test.ts rename to src/utils/contract/extractFunctionParts.test.ts index 8d6788c770..455b3641ab 100644 --- a/src/utils/solidity.test.ts +++ b/src/utils/contract/extractFunctionParts.test.ts @@ -5,7 +5,7 @@ import { extractFunctionParams, extractFunctionParts, extractFunctionType, -} from './solidity' +} from './extractFunctionParts' test('extractFunctionParts', () => { expect(extractFunctionParts('foo()')).toMatchInlineSnapshot(` diff --git a/src/utils/solidity.ts b/src/utils/contract/extractFunctionParts.ts similarity index 100% rename from src/utils/solidity.ts rename to src/utils/contract/extractFunctionParts.ts diff --git a/src/utils/contract/getContractError.test.ts b/src/utils/contract/getContractError.test.ts new file mode 100644 index 0000000000..96b9a46cb6 --- /dev/null +++ b/src/utils/contract/getContractError.test.ts @@ -0,0 +1,97 @@ +import { describe, expect, test } from 'vitest' + +import { accounts } from '../../../test' +import { baycContractConfig } from '../../../test/abis' +import { BaseError } from '../../errors' +import { getContractError } from './getContractError' + +describe('getContractError', () => { + test('default', () => { + expect( + getContractError( + new BaseError('An RPC error occurred', { + cause: { + code: 3, + message: 'execution reverted: Sale must be active to mint Ape', + data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500', + } as unknown as Error, + }), + { + abi: baycContractConfig.abi, + functionName: 'mintApe', + args: [1n], + sender: accounts[0].address, + }, + ), + ).toMatchInlineSnapshot(` + [ContractMethodExecutionError: Sale must be active to mint Ape + + Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 + Function: mintApe(uint256 numberOfTokens) + Arguments: (1) + + Details: execution reverted: Sale must be active to mint Ape + Version: viem@1.0.2] + `) + }) + + test('default', () => { + expect( + getContractError( + new BaseError('An RPC error occurred', { + cause: { + code: 3, + message: 'execution reverted: Sale must be active to mint Ape', + data: '0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001f53616c65206d7573742062652061637469766520746f206d696e742041706500', + } as unknown as Error, + }), + { + abi: baycContractConfig.abi, + functionName: 'foo', + args: [1n], + sender: accounts[0].address, + }, + ), + ).toMatchInlineSnapshot(` + [ContractMethodExecutionError: Sale must be active to mint Ape + + Sender: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 + + Details: execution reverted: Sale must be active to mint Ape + Version: viem@1.0.2] + `) + }) + + test('unknown error', () => { + expect( + getContractError( + new BaseError('An RPC error occurred', { + cause: new Error('rarararar i am an error lmaoaoo'), + }), + { + abi: baycContractConfig.abi, + functionName: 'foo', + args: [1n], + sender: accounts[0].address, + }, + ), + ).toMatchInlineSnapshot(` + [ViemError: An RPC error occurred + + Details: rarararar i am an error lmaoaoo + Version: viem@1.0.2] + `) + expect( + getContractError(new BaseError('An RPC error occurred'), { + abi: baycContractConfig.abi, + functionName: 'foo', + args: [1n], + sender: accounts[0].address, + }), + ).toMatchInlineSnapshot(` + [ViemError: An RPC error occurred + + Version: viem@1.0.2] + `) + }) +}) diff --git a/src/utils/contract/getContractError.ts b/src/utils/contract/getContractError.ts new file mode 100644 index 0000000000..39bc511ac8 --- /dev/null +++ b/src/utils/contract/getContractError.ts @@ -0,0 +1,70 @@ +import { Abi } from 'abitype' +import { + AbiDecodingZeroDataError, + ContractMethodExecutionError, + ContractMethodZeroDataError, +} from '../../errors' +import { Address } from '../../types' +import { + formatAbiItemWithArgs, + formatAbiItemWithParams, + getAbiItem, +} from '../abi' + +export function getContractError( + err: unknown, + { + abi, + address, + args, + functionName, + sender, + }: { + abi: Abi + args: any + address?: Address + functionName: string + sender?: Address + }, +) { + const { code, message } = + ((err as Error).cause as { code?: number; message?: string }) || {} + + const abiItem = getAbiItem({ abi, name: functionName }) + const formattedArgs = abiItem + ? formatAbiItemWithArgs({ + abiItem, + args, + includeFunctionName: false, + includeName: false, + }) + : undefined + const functionWithParams = abiItem + ? formatAbiItemWithParams(abiItem, { includeName: true }) + : undefined + + if (err instanceof AbiDecodingZeroDataError) { + return new ContractMethodZeroDataError({ + abi, + args, + cause: err as Error, + contractAddress: address, + functionName, + functionWithParams, + }) + } + if (code === 3 || message?.includes('execution reverted')) { + const message_ = message?.replace('execution reverted: ', '') + return new ContractMethodExecutionError(message_, { + abi, + args, + cause: err as Error, + contractAddress: address, + formattedArgs, + functionName, + functionWithParams, + sender, + }) + } + return err +} diff --git a/src/utils/contract/index.test.ts b/src/utils/contract/index.test.ts new file mode 100644 index 0000000000..9e98be925b --- /dev/null +++ b/src/utils/contract/index.test.ts @@ -0,0 +1,15 @@ +import { expect, test } from 'vitest' + +import * as utils from './index' + +test('exports utils', () => { + expect(utils).toMatchInlineSnapshot(` + { + "extractFunctionName": [Function], + "extractFunctionParams": [Function], + "extractFunctionParts": [Function], + "extractFunctionType": [Function], + "getContractError": [Function], + } + `) +}) diff --git a/src/utils/contract/index.ts b/src/utils/contract/index.ts new file mode 100644 index 0000000000..d70232debb --- /dev/null +++ b/src/utils/contract/index.ts @@ -0,0 +1,8 @@ +export { + extractFunctionName, + extractFunctionParams, + extractFunctionParts, + extractFunctionType, +} from './extractFunctionParts' + +export { getContractError } from './getContractError' diff --git a/src/utils/hash/hashFunction.ts b/src/utils/hash/hashFunction.ts index 07baab2afa..6d914fa832 100644 --- a/src/utils/hash/hashFunction.ts +++ b/src/utils/hash/hashFunction.ts @@ -1,5 +1,5 @@ import { encodeBytes } from '../encoding' -import { extractFunctionName, extractFunctionParams } from '../solidity' +import { extractFunctionName, extractFunctionParams } from '../contract' import { keccak256 } from './keccak256' const hash = (value: string) => keccak256(encodeBytes(value)) diff --git a/src/utils/index.test.ts b/src/utils/index.test.ts index ceb61ec3d0..0543c96a83 100644 --- a/src/utils/index.test.ts +++ b/src/utils/index.test.ts @@ -31,16 +31,21 @@ test('exports utils', () => { "encodeRlp": [Function], "extractFunctionName": [Function], "extractFunctionParams": [Function], + "extractFunctionParts": [Function], "extractFunctionType": [Function], "format": [Function], + "formatAbiItemWithArgs": [Function], + "formatAbiItemWithParams": [Function], "formatBlock": [Function], "formatEther": [Function], "formatGwei": [Function], "formatTransaction": [Function], "formatTransactionRequest": [Function], "formatUnit": [Function], + "getAbiItem": [Function], "getAddress": [Function], "getContractAddress": [Function], + "getContractError": [Function], "getCreate2Address": [Function], "getCreateAddress": [Function], "getEventSignature": [Function], @@ -74,6 +79,7 @@ test('exports utils', () => { "sliceHex": [Function], "stringToBytes": [Function], "stringToHex": [Function], + "stringify": [Function], "trim": [Function], } `) diff --git a/src/utils/index.ts b/src/utils/index.ts index 55a05e869c..1c67bc92b4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,3 +1,16 @@ +export type { + DecodeAbiArgs, + DecodeErrorResultArgs, + DecodeFunctionDataArgs, + DecodeFunctionResultArgs, + DecodeFunctionResultResponse, + EncodeAbiArgs, + EncodeDeployDataArgs, + EncodeErrorResultArgs, + EncodeEventTopicsArgs, + EncodeFunctionDataArgs, + EncodeFunctionResultArgs, +} from './abi' export { decodeAbi, decodeErrorResult, @@ -9,6 +22,9 @@ export { encodeEventTopics, encodeFunctionData, encodeFunctionResult, + formatAbiItemWithArgs, + formatAbiItemWithParams, + getAbiItem, } from './abi' export type { @@ -27,6 +43,14 @@ export { export { buildRequest } from './buildRequest' +export { + extractFunctionName, + extractFunctionParams, + extractFunctionType, + extractFunctionParts, + getContractError, +} from './contract' + export { isBytes, isHex, @@ -89,11 +113,7 @@ export { getEventSignature, getFunctionSignature, keccak256 } from './hash' export { rpc } from './rpc' -export { - extractFunctionName, - extractFunctionParams, - extractFunctionType, -} from './solidity' +export { stringify } from './stringify' export { formatEther, diff --git a/src/utils/rpc.ts b/src/utils/rpc.ts index 36009b5367..24d02de3ce 100644 --- a/src/utils/rpc.ts +++ b/src/utils/rpc.ts @@ -6,6 +6,7 @@ import { } from '../errors' import { withRetry } from './promise' import { withTimeout } from './promise/withTimeout' +import { stringify } from './stringify' let id = 0 @@ -78,7 +79,7 @@ async function http( 'Content-Type': 'application/json', }, method: 'POST', - body: JSON.stringify({ jsonrpc: '2.0', id: id++, ...body }), + body: stringify({ jsonrpc: '2.0', id: id++, ...body }), signal: timeout > 0 ? signal : undefined, }) return response @@ -117,7 +118,7 @@ async function http( if (!response.ok) { throw new HttpRequestError({ body, - details: JSON.stringify(data.error) || response.statusText, + details: stringify(data.error) || response.statusText, status: response.status, url, }) diff --git a/src/utils/stringify.test.ts b/src/utils/stringify.test.ts new file mode 100644 index 0000000000..e4b0093456 --- /dev/null +++ b/src/utils/stringify.test.ts @@ -0,0 +1,13 @@ +import { expect, test } from 'vitest' +import { stringify } from './stringify' + +test('default', () => { + expect( + stringify({ + foo: 'bar', + baz: { + value: 69n, + }, + }), + ).toEqual('{"foo":"bar","baz":{"value":"69"}}') +}) diff --git a/src/utils/stringify.ts b/src/utils/stringify.ts new file mode 100644 index 0000000000..92b6df792d --- /dev/null +++ b/src/utils/stringify.ts @@ -0,0 +1,5 @@ +export function stringify(value: unknown) { + return JSON.stringify(value, (_, value) => + typeof value === 'bigint' ? value.toString() : value, + ) +} diff --git a/test/abis.ts b/test/abis.ts new file mode 100644 index 0000000000..94ea6f67ff --- /dev/null +++ b/test/abis.ts @@ -0,0 +1,1223 @@ +export const baycContractConfig = { + address: '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d', + abi: [ + { + inputs: [ + { internalType: 'string', name: 'name', type: 'string' }, + { internalType: 'string', name: 'symbol', type: 'string' }, + { internalType: 'uint256', name: 'maxNftSupply', type: 'uint256' }, + { internalType: 'uint256', name: 'saleStart', type: 'uint256' }, + ], + stateMutability: 'nonpayable', + type: 'constructor', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'owner', + type: 'address', + }, + { + indexed: true, + internalType: 'address', + name: 'approved', + type: 'address', + }, + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Approval', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'owner', + type: 'address', + }, + { + indexed: true, + internalType: 'address', + name: 'operator', + type: 'address', + }, + { + indexed: false, + internalType: 'bool', + name: 'approved', + type: 'bool', + }, + ], + name: 'ApprovalForAll', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'previousOwner', + type: 'address', + }, + { + indexed: true, + internalType: 'address', + name: 'newOwner', + type: 'address', + }, + ], + name: 'OwnershipTransferred', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'from', + type: 'address', + }, + { indexed: true, internalType: 'address', name: 'to', type: 'address' }, + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Transfer', + type: 'event', + }, + { + inputs: [], + name: 'BAYC_PROVENANCE', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'MAX_APES', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'REVEAL_TIMESTAMP', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'apePrice', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'approve', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'address', name: 'owner', type: 'address' }], + name: 'balanceOf', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'baseURI', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'emergencySetStartingIndexBlock', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'flipSaleState', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'getApproved', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'owner', type: 'address' }, + { internalType: 'address', name: 'operator', type: 'address' }, + ], + name: 'isApprovedForAll', + outputs: [{ internalType: 'bool', name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'maxApePurchase', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'uint256', name: 'numberOfTokens', type: 'uint256' }, + ], + name: 'mintApe', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [], + name: 'name', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'owner', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'ownerOf', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'renounceOwnership', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'reserveApes', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'bytes', name: '_data', type: 'bytes' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'saleIsActive', + outputs: [{ internalType: 'bool', name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'operator', type: 'address' }, + { internalType: 'bool', name: 'approved', type: 'bool' }, + ], + name: 'setApprovalForAll', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'string', name: 'baseURI', type: 'string' }], + name: 'setBaseURI', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'string', name: 'provenanceHash', type: 'string' }, + ], + name: 'setProvenanceHash', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'uint256', name: 'revealTimeStamp', type: 'uint256' }, + ], + name: 'setRevealTimestamp', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'setStartingIndex', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'startingIndex', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'startingIndexBlock', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'bytes4', name: 'interfaceId', type: 'bytes4' }], + name: 'supportsInterface', + outputs: [{ internalType: 'bool', name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'symbol', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'index', type: 'uint256' }], + name: 'tokenByIndex', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'owner', type: 'address' }, + { internalType: 'uint256', name: 'index', type: 'uint256' }, + ], + name: 'tokenOfOwnerByIndex', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'tokenURI', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'totalSupply', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'transferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'address', name: 'newOwner', type: 'address' }], + name: 'transferOwnership', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'withdraw', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + ], +} as const + +export const uniswapV3PositionsContractConfig = { + address: '0xC36442b4a4522E871399CD717aBDD847Ab11FE88', + abi: [ + { + inputs: [ + { internalType: 'address', name: '_factory', type: 'address' }, + { internalType: 'address', name: '_WETH9', type: 'address' }, + { internalType: 'address', name: '_tokenDescriptor_', type: 'address' }, + ], + stateMutability: 'nonpayable', + type: 'constructor', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'owner', + type: 'address', + }, + { + indexed: true, + internalType: 'address', + name: 'approved', + type: 'address', + }, + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Approval', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'owner', + type: 'address', + }, + { + indexed: true, + internalType: 'address', + name: 'operator', + type: 'address', + }, + { + indexed: false, + internalType: 'bool', + name: 'approved', + type: 'bool', + }, + ], + name: 'ApprovalForAll', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'address', + name: 'recipient', + type: 'address', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount0', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount1', + type: 'uint256', + }, + ], + name: 'Collect', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint128', + name: 'liquidity', + type: 'uint128', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount0', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount1', + type: 'uint256', + }, + ], + name: 'DecreaseLiquidity', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint128', + name: 'liquidity', + type: 'uint128', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount0', + type: 'uint256', + }, + { + indexed: false, + internalType: 'uint256', + name: 'amount1', + type: 'uint256', + }, + ], + name: 'IncreaseLiquidity', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: 'address', + name: 'from', + type: 'address', + }, + { indexed: true, internalType: 'address', name: 'to', type: 'address' }, + { + indexed: true, + internalType: 'uint256', + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Transfer', + type: 'event', + }, + { + inputs: [], + name: 'DOMAIN_SEPARATOR', + outputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'PERMIT_TYPEHASH', + outputs: [{ internalType: 'bytes32', name: '', type: 'bytes32' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'WETH9', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'approve', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'address', name: 'owner', type: 'address' }], + name: 'balanceOf', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'baseURI', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'pure', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'burn', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'address', name: 'recipient', type: 'address' }, + { internalType: 'uint128', name: 'amount0Max', type: 'uint128' }, + { internalType: 'uint128', name: 'amount1Max', type: 'uint128' }, + ], + internalType: 'struct INonfungiblePositionManager.CollectParams', + name: 'params', + type: 'tuple', + }, + ], + name: 'collect', + outputs: [ + { internalType: 'uint256', name: 'amount0', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1', type: 'uint256' }, + ], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token0', type: 'address' }, + { internalType: 'address', name: 'token1', type: 'address' }, + { internalType: 'uint24', name: 'fee', type: 'uint24' }, + { internalType: 'uint160', name: 'sqrtPriceX96', type: 'uint160' }, + ], + name: 'createAndInitializePoolIfNecessary', + outputs: [{ internalType: 'address', name: 'pool', type: 'address' }], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { + components: [ + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'uint128', name: 'liquidity', type: 'uint128' }, + { internalType: 'uint256', name: 'amount0Min', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1Min', type: 'uint256' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + ], + internalType: + 'struct INonfungiblePositionManager.DecreaseLiquidityParams', + name: 'params', + type: 'tuple', + }, + ], + name: 'decreaseLiquidity', + outputs: [ + { internalType: 'uint256', name: 'amount0', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1', type: 'uint256' }, + ], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [], + name: 'factory', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'getApproved', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + components: [ + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { + internalType: 'uint256', + name: 'amount0Desired', + type: 'uint256', + }, + { + internalType: 'uint256', + name: 'amount1Desired', + type: 'uint256', + }, + { internalType: 'uint256', name: 'amount0Min', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1Min', type: 'uint256' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + ], + internalType: + 'struct INonfungiblePositionManager.IncreaseLiquidityParams', + name: 'params', + type: 'tuple', + }, + ], + name: 'increaseLiquidity', + outputs: [ + { internalType: 'uint128', name: 'liquidity', type: 'uint128' }, + { internalType: 'uint256', name: 'amount0', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1', type: 'uint256' }, + ], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'owner', type: 'address' }, + { internalType: 'address', name: 'operator', type: 'address' }, + ], + name: 'isApprovedForAll', + outputs: [{ internalType: 'bool', name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { + components: [ + { internalType: 'address', name: 'token0', type: 'address' }, + { internalType: 'address', name: 'token1', type: 'address' }, + { internalType: 'uint24', name: 'fee', type: 'uint24' }, + { internalType: 'int24', name: 'tickLower', type: 'int24' }, + { internalType: 'int24', name: 'tickUpper', type: 'int24' }, + { + internalType: 'uint256', + name: 'amount0Desired', + type: 'uint256', + }, + { + internalType: 'uint256', + name: 'amount1Desired', + type: 'uint256', + }, + { internalType: 'uint256', name: 'amount0Min', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1Min', type: 'uint256' }, + { internalType: 'address', name: 'recipient', type: 'address' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + ], + internalType: 'struct INonfungiblePositionManager.MintParams', + name: 'params', + type: 'tuple', + }, + ], + name: 'mint', + outputs: [ + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'uint128', name: 'liquidity', type: 'uint128' }, + { internalType: 'uint256', name: 'amount0', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1', type: 'uint256' }, + ], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [{ internalType: 'bytes[]', name: 'data', type: 'bytes[]' }], + name: 'multicall', + outputs: [{ internalType: 'bytes[]', name: 'results', type: 'bytes[]' }], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [], + name: 'name', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'ownerOf', + outputs: [{ internalType: 'address', name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'spender', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + { internalType: 'uint8', name: 'v', type: 'uint8' }, + { internalType: 'bytes32', name: 'r', type: 'bytes32' }, + { internalType: 'bytes32', name: 's', type: 'bytes32' }, + ], + name: 'permit', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'positions', + outputs: [ + { internalType: 'uint96', name: 'nonce', type: 'uint96' }, + { internalType: 'address', name: 'operator', type: 'address' }, + { internalType: 'address', name: 'token0', type: 'address' }, + { internalType: 'address', name: 'token1', type: 'address' }, + { internalType: 'uint24', name: 'fee', type: 'uint24' }, + { internalType: 'int24', name: 'tickLower', type: 'int24' }, + { internalType: 'int24', name: 'tickUpper', type: 'int24' }, + { internalType: 'uint128', name: 'liquidity', type: 'uint128' }, + { + internalType: 'uint256', + name: 'feeGrowthInside0LastX128', + type: 'uint256', + }, + { + internalType: 'uint256', + name: 'feeGrowthInside1LastX128', + type: 'uint256', + }, + { internalType: 'uint128', name: 'tokensOwed0', type: 'uint128' }, + { internalType: 'uint128', name: 'tokensOwed1', type: 'uint128' }, + ], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'refundETH', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + { internalType: 'bytes', name: '_data', type: 'bytes' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token', type: 'address' }, + { internalType: 'uint256', name: 'value', type: 'uint256' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + { internalType: 'uint8', name: 'v', type: 'uint8' }, + { internalType: 'bytes32', name: 'r', type: 'bytes32' }, + { internalType: 'bytes32', name: 's', type: 'bytes32' }, + ], + name: 'selfPermit', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token', type: 'address' }, + { internalType: 'uint256', name: 'nonce', type: 'uint256' }, + { internalType: 'uint256', name: 'expiry', type: 'uint256' }, + { internalType: 'uint8', name: 'v', type: 'uint8' }, + { internalType: 'bytes32', name: 'r', type: 'bytes32' }, + { internalType: 'bytes32', name: 's', type: 'bytes32' }, + ], + name: 'selfPermitAllowed', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token', type: 'address' }, + { internalType: 'uint256', name: 'nonce', type: 'uint256' }, + { internalType: 'uint256', name: 'expiry', type: 'uint256' }, + { internalType: 'uint8', name: 'v', type: 'uint8' }, + { internalType: 'bytes32', name: 'r', type: 'bytes32' }, + { internalType: 'bytes32', name: 's', type: 'bytes32' }, + ], + name: 'selfPermitAllowedIfNecessary', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token', type: 'address' }, + { internalType: 'uint256', name: 'value', type: 'uint256' }, + { internalType: 'uint256', name: 'deadline', type: 'uint256' }, + { internalType: 'uint8', name: 'v', type: 'uint8' }, + { internalType: 'bytes32', name: 'r', type: 'bytes32' }, + { internalType: 'bytes32', name: 's', type: 'bytes32' }, + ], + name: 'selfPermitIfNecessary', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'operator', type: 'address' }, + { internalType: 'bool', name: 'approved', type: 'bool' }, + ], + name: 'setApprovalForAll', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ internalType: 'bytes4', name: 'interfaceId', type: 'bytes4' }], + name: 'supportsInterface', + outputs: [{ internalType: 'bool', name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'token', type: 'address' }, + { internalType: 'uint256', name: 'amountMinimum', type: 'uint256' }, + { internalType: 'address', name: 'recipient', type: 'address' }, + ], + name: 'sweepToken', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { + inputs: [], + name: 'symbol', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'index', type: 'uint256' }], + name: 'tokenByIndex', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'owner', type: 'address' }, + { internalType: 'uint256', name: 'index', type: 'uint256' }, + ], + name: 'tokenOfOwnerByIndex', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'tokenURI', + outputs: [{ internalType: 'string', name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'totalSupply', + outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { internalType: 'address', name: 'from', type: 'address' }, + { internalType: 'address', name: 'to', type: 'address' }, + { internalType: 'uint256', name: 'tokenId', type: 'uint256' }, + ], + name: 'transferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'uint256', name: 'amount0Owed', type: 'uint256' }, + { internalType: 'uint256', name: 'amount1Owed', type: 'uint256' }, + { internalType: 'bytes', name: 'data', type: 'bytes' }, + ], + name: 'uniswapV3MintCallback', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { internalType: 'uint256', name: 'amountMinimum', type: 'uint256' }, + { internalType: 'address', name: 'recipient', type: 'address' }, + ], + name: 'unwrapWETH9', + outputs: [], + stateMutability: 'payable', + type: 'function', + }, + { stateMutability: 'payable', type: 'receive' }, + ], +} as const + +export const wagmiContractConfig = { + address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2', + abi: [ + { inputs: [], stateMutability: 'nonpayable', type: 'constructor' }, + { + anonymous: false, + inputs: [ + { + indexed: true, + name: 'owner', + type: 'address', + }, + { + indexed: true, + name: 'approved', + type: 'address', + }, + { + indexed: true, + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Approval', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + name: 'owner', + type: 'address', + }, + { + indexed: true, + name: 'operator', + type: 'address', + }, + { + indexed: false, + name: 'approved', + type: 'bool', + }, + ], + name: 'ApprovalForAll', + type: 'event', + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + name: 'from', + type: 'address', + }, + { indexed: true, name: 'to', type: 'address' }, + { + indexed: true, + name: 'tokenId', + type: 'uint256', + }, + ], + name: 'Transfer', + type: 'event', + }, + { + inputs: [ + { name: 'to', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + ], + name: 'approve', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ name: 'owner', type: 'address' }], + name: 'balanceOf', + outputs: [{ name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ name: 'tokenId', type: 'uint256' }], + name: 'getApproved', + outputs: [{ name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { name: 'owner', type: 'address' }, + { name: 'operator', type: 'address' }, + ], + name: 'isApprovedForAll', + outputs: [{ name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ internalType: 'uint256', name: 'tokenId', type: 'uint256' }], + name: 'mint', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [], + name: 'name', + outputs: [{ name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ name: 'tokenId', type: 'uint256' }], + name: 'ownerOf', + outputs: [{ name: '', type: 'address' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { name: 'from', type: 'address' }, + { name: 'to', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { name: 'from', type: 'address' }, + { name: 'to', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + { name: '_data', type: 'bytes' }, + ], + name: 'safeTransferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [ + { name: 'operator', type: 'address' }, + { name: 'approved', type: 'bool' }, + ], + name: 'setApprovalForAll', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + { + inputs: [{ name: 'interfaceId', type: 'bytes4' }], + name: 'supportsInterface', + outputs: [{ name: '', type: 'bool' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [], + name: 'symbol', + outputs: [{ name: '', type: 'string' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [{ name: 'tokenId', type: 'uint256' }], + name: 'tokenURI', + outputs: [{ name: '', type: 'string' }], + stateMutability: 'pure', + type: 'function', + }, + { + inputs: [], + name: 'totalSupply', + outputs: [{ name: '', type: 'uint256' }], + stateMutability: 'view', + type: 'function', + }, + { + inputs: [ + { name: 'from', type: 'address' }, + { name: 'to', type: 'address' }, + { name: 'tokenId', type: 'uint256' }, + ], + name: 'transferFrom', + outputs: [], + stateMutability: 'nonpayable', + type: 'function', + }, + ], +} as const diff --git a/test/bench.ts b/test/bench.ts index 8c54451336..b82d57a4de 100644 --- a/test/bench.ts +++ b/test/bench.ts @@ -4,7 +4,7 @@ import { JsonRpcProvider } from 'essential-eth' import { localhost } from '../src/chains' -export const ethersProvider = new providers.StaticJsonRpcProvider( +export const ethersProvider = new providers.JsonRpcProvider( localhost.rpcUrls.default.http[0], ) diff --git a/test/index.ts b/test/index.ts index 7d2e258ad7..7a1424d0bf 100644 --- a/test/index.ts +++ b/test/index.ts @@ -1,3 +1,5 @@ +export { wagmiContractConfig } from './abis' + export { essentialProvider, ethersProvider, web3Provider } from './bench' export {