-
-
Notifications
You must be signed in to change notification settings - Fork 864
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
237 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"viem": patch | ||
--- | ||
|
||
Added `readContract` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* TODO: Heaps more test cases :D | ||
* - Complex calldata types | ||
* - Complex return types (tuple/structs) | ||
* - Calls against blocks | ||
*/ | ||
|
||
import { describe, expect, test } from 'vitest' | ||
import { | ||
accounts, | ||
publicClient, | ||
testClient, | ||
wagmiContractConfig, | ||
walletClient, | ||
} from '../../_test' | ||
import { baycContractConfig } from '../../_test/abis' | ||
import { encodeFunctionData } from '../../utils' | ||
import { mine } from '../test' | ||
import { sendTransaction } from '../wallet' | ||
|
||
import { deployContract } from './deployContract' | ||
import { getTransactionReceipt } from './getTransactionReceipt' | ||
import { readContract } from './readContract' | ||
|
||
describe('wagmi', () => { | ||
test('default', async () => { | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'balanceOf', | ||
args: ['0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC'], | ||
}), | ||
).toEqual(3n) | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'getApproved', | ||
args: [420n], | ||
}), | ||
).toEqual('0x0000000000000000000000000000000000000000') | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'isApprovedForAll', | ||
args: [ | ||
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC', | ||
'0x0000000000000000000000000000000000000000', | ||
], | ||
}), | ||
).toEqual(false) | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'name', | ||
}), | ||
).toEqual('wagmi') | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'ownerOf', | ||
args: [420n], | ||
}), | ||
).toEqual('0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC') | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'supportsInterface', | ||
args: ['0x1a452251'], | ||
}), | ||
).toEqual(false) | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'symbol', | ||
}), | ||
).toEqual('WAGMI') | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'tokenURI', | ||
args: [420n], | ||
}), | ||
).toMatchInlineSnapshot( | ||
'"data:application/json;base64,eyJuYW1lIjogIndhZ21pICM0MjAiLCAiaW1hZ2UiOiAiZGF0YTppbWFnZS9zdmcreG1sO2Jhc2U2NCxQSE4yWnlCNGJXeHVjejBpYUhSMGNEb3ZMM2QzZHk1M015NXZjbWN2TWpBd01DOXpkbWNp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ZeTAyTGprME15QXdMVEV5TGpVM01TQTFMall4TnkweE1pNDFOekVnTVRJdU5UUTFkamMxTGpJM00zcHRNemN1TnpFMExUWXlMamN5TjJNdE5pNDVORE1nTUMweE1pNDFOekVnTlM0Mk1UY3RNVEl1TlRjeElERXlMalUwTlhZeU5TNHdPVEZqTUNBMkxqa3lPU0ExTGpZeU9DQXhNaTQxTkRZZ01USXVOVGN4SURFeUxqVTBObWd4TURBdU5UY3lZell1T1RReklEQWdNVEl1TlRjeExUVXVOakUzSURFeUxqVTNNUzB4TWk0MU5EWjJMVEkxTGpBNU1XTXdMVFl1T1RJNExUVXVOakk0TFRFeUxqVTBOUzB4TWk0MU56RXRNVEl1TlRRMVNEVXpOQzQzTVRSNklpQm1hV3hzTFhKMWJHVTlJbVYyWlc1dlpHUWlJQzgrUEM5blBqd3ZjM1puUGc9PSJ9"', | ||
) | ||
expect( | ||
await readContract(publicClient, { | ||
...wagmiContractConfig, | ||
functionName: 'totalSupply', | ||
}), | ||
).toEqual(558n) | ||
}) | ||
}) | ||
|
||
test('fake contract address', async () => { | ||
await expect(() => | ||
readContract(publicClient, { | ||
abi: wagmiContractConfig.abi, | ||
address: '0x0000000000000000000000000000000000000069', | ||
functionName: 'totalSupply', | ||
}), | ||
).rejects.toThrowErrorMatchingInlineSnapshot(` | ||
"The contract method \\"totalSupply\\" returned no data (\\"0x\\"). This could be due to any of the following: | ||
- The contract does not have the function \\"totalSupply\\", | ||
- The parameters passed to the contract function may be invalid, or | ||
- The address is not a contract. | ||
Contract: 0x0000000000000000000000000000000000000000 | ||
Function: totalSupply() | ||
> \\"0x\\" | ||
Version: viem@1.0.2" | ||
`) | ||
}) | ||
|
||
// Deploy BAYC Contract | ||
async function deployBAYC() { | ||
const hash = await deployContract(walletClient, { | ||
...baycContractConfig, | ||
args: ['Bored Ape Wagmi Club', 'BAYC', 69420n, 0n], | ||
from: accounts[0].address, | ||
}) | ||
await mine(testClient, { blocks: 1 }) | ||
const { contractAddress } = await getTransactionReceipt(publicClient, { | ||
hash, | ||
}) | ||
return { contractAddress } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { Abi } from 'abitype' | ||
|
||
import type { Chain, Formatter } from '../../chains' | ||
import type { PublicClient } from '../../clients' | ||
import type { | ||
Address, | ||
ExtractArgsFromAbi, | ||
ExtractResultFromAbi, | ||
ExtractFunctionNameFromAbi, | ||
} from '../../types' | ||
import { | ||
EncodeFunctionDataArgs, | ||
decodeFunctionResult, | ||
encodeFunctionData, | ||
getContractError, | ||
} from '../../utils' | ||
import { call, CallArgs, FormattedCall } from './call' | ||
|
||
export type FormattedReadContract< | ||
TFormatter extends Formatter | undefined = Formatter, | ||
> = FormattedCall<TFormatter> | ||
|
||
export type ReadContractArgs< | ||
TAbi extends Abi | readonly unknown[] = Abi, | ||
TFunctionName extends string = any, | ||
> = Omit< | ||
CallArgs, | ||
| 'accessList' | ||
| 'chain' | ||
| 'from' | ||
| 'gas' | ||
| 'gasPrice' | ||
| 'maxFeePerGas' | ||
| 'maxPriorityFeePerGas' | ||
| 'nonce' | ||
| 'to' | ||
| 'data' | ||
| 'value' | ||
> & { | ||
address: Address | ||
abi: TAbi | ||
functionName: ExtractFunctionNameFromAbi<TAbi, TFunctionName, 'pure' | 'view'> | ||
} & ExtractArgsFromAbi<TAbi, TFunctionName> | ||
|
||
export type ReadContractResponse< | ||
TAbi extends Abi | readonly unknown[] = Abi, | ||
TFunctionName extends string = string, | ||
> = ExtractResultFromAbi<TAbi, TFunctionName> | ||
|
||
export async function readContract< | ||
TAbi extends Abi = Abi, | ||
TFunctionName extends string = any, | ||
>( | ||
client: PublicClient, | ||
{ | ||
abi, | ||
address, | ||
args, | ||
functionName, | ||
...callRequest | ||
}: ReadContractArgs<TAbi, TFunctionName>, | ||
): Promise<ReadContractResponse<TAbi, TFunctionName>> { | ||
const calldata = encodeFunctionData({ | ||
abi, | ||
args, | ||
functionName, | ||
} as unknown as EncodeFunctionDataArgs<TAbi, TFunctionName>) | ||
try { | ||
const { data } = await call(client, { | ||
data: calldata, | ||
to: address, | ||
...callRequest, | ||
} as unknown as CallArgs) | ||
return decodeFunctionResult({ | ||
abi, | ||
functionName, | ||
data: data || '0x', | ||
}) | ||
} catch (err) { | ||
throw getContractError(err, { | ||
abi, | ||
address, | ||
args, | ||
functionName, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7afdee8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viem-playground – ./playgrounds/dev
viem-playground-git-main-wagmi-dev.vercel.app
viem-playground-wagmi-dev.vercel.app
viem-playground.vercel.app
7afdee8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viem-benchmark – ./playgrounds/benchmark
viem-benchmark.vercel.app
viem-benchmark-wagmi-dev.vercel.app
viem-benchmark-git-main-wagmi-dev.vercel.app
7afdee8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
viem-site – ./site
viem-site-git-main-wagmi-dev.vercel.app
viem-site-wagmi-dev.vercel.app
viem-site.vercel.app