Bytecode Caller viem extension provides an easy way to use the Bytecode Caller in your viem client.
For information about the Bytecode Caller itself, please refer to the Bytecode Caller documentation.
yarn
yarn add @bytecodecaller/viem
npm
npm install @bytecodecaller/viem
Contract that you want to read:
contract FriendWhoWantsMangoes {
function askHowManyMangoes() external pure returns(uint256) {
return 10;
}
}
contract MangoSeller {
function askForMangoesPrice(uint256 numberOfMangoes) external pure returns(uint256) {
return numberOfMangoes * 20;
}
}
Query contract to use: (No need to deploy this smart contract)
contract MangoPriceReader {
function estimateExpenses(address friend, address seller) public pure returns (uint256) {
uint256 mangoesToBuy = FriendWhoWantsMangoes(friend).askHowManyMangoes();
return MangoSeller(seller).askForMangoesPrice(mangoesToBuy);
}
}
const client = createPublicClient({
transport: http('https://mainnet.base.org/'),
}).extend(callBytecodeExtension)
or if you use more extensions:
const client = createPublicClient({
transport: http('https://mainnet.base.org/'),
}).extend((client) => ({
otherExtension,
...callBytecodeExtension(client),
}))
import { bytecode as mangoPriceReaderBytecode, abi as mangoPriceReaderAbi } from 'build/MangoPriceReader.sol/MangoPriceReader.json'
const friendContractAddress = '0xE930Eb2004e09f6492F49f58A2F35C0B1382c68C'
const sellerContractAddress = '0x2d5b56ee345698c000061B81755eB5E70eA8DEa1'
const result = await client.callBytecode({
bytecode: mangoPriceReaderBytecode,
abi: mangoPriceReaderAbi,
method: 'estimateExpenses',
args: [friendContractAddress, sellerContractAddress],
})
MIT License