[Deployment] deploying contract with viem #465
-
EnvironmentTestnet zkSolc Versionany zksync-ethers Versionnot relevant Hardhat.config.tsnot relevant Deployment Script (WITHOUT PRIVATE KEY)import { createWalletClient, custom, http } from 'viem';
import { privateKeyToAccount, generatePrivateKey } from 'viem/accounts';
import { zkSyncSepoliaTestnet } from 'viem/chains';
import { eip712WalletActions } from 'viem/zksync';
import Greeter from './Greeter';
async function setupWithWindowEthereum() {
const transport = custom(window.ethereum); //http('https://sepolia.era.zksync.dev');
const walletClient = createWalletClient({
chain: zkSyncSepoliaTestnet,
transport,
}).extend(eip712WalletActions());
await walletClient.request({ method: 'eth_requestAccounts' });
const [account] = await walletClient.getAddresses();
return { transport, account, walletClient };
}
async function setupWithLocalWallet() {
const transport = http('https://sepolia.era.zksync.dev');
const account = privateKeyToAccount(generatePrivateKey());
const walletClient = createWalletClient({
account,
transport,
chain: zkSyncSepoliaTestnet,
}).extend(eip712WalletActions());
return { transport, account, walletClient };
}
async function testWithLocalAccount() {
const { walletClient, account } = await setupWithLocalWallet();
const txHash = await walletClient.deployContract({
account,
abi: Greeter.abi,
bytecode: Greeter.bytecode,
args: ['hello'],
});
return `txHash : ${txHash}`;
}
async function testWithWindowEthereum() {
const { walletClient, account } = await setupWithWindowEthereum();
const txHash = await walletClient.deployContract({
account,
abi: Greeter.abi,
bytecode: Greeter.bytecode,
args: ['hello'],
});
return `txHash : ${txHash}`;
}
export async function execute() {
await testWithWindowEthereum();
// await testWithLocalAccount();
} Package.json{
"name": "viem-example",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite"
},
"dependencies": {
"viem": "2.9.15"
},
"devDependencies": {
"typescript": "^5.4.5",
"vite": "^5.2.8"
}
} Contract Codeagma solidity ^0.8.0;
import "hardhat/console.sol";
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
console.log("Deploying a Greeter with greeting:", _greeting);
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
console.log("Changing greeting from '%s' to '%s'", greeting, _greeting);
greeting = _greeting;
}
} Does this work on other EVMs? (If yes, please list at least 1 of them)yes Description of What Your Contract Doesnot relevant Repo Link (Optional)https://stackblitz.com/edit/viem-getting-started-ydrgsr?file=index.ts Additional DetailsI can't get viem to deploy a simple contract See issue comment on viem repo : wevm/viem#1887 (comment) Their doc does not seems to be correct: https://viem.sh/zksync/actions/deployContract Reproduction : https://stackblitz.com/edit/viem-getting-started-ydrgsr?file=index.ts the doc here does not describe how to deploy either : https://docs.zksync.io/build/tutorials/tooling-guides/viem.html |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @wighawag have you seen the other answered discussions regarding viem deployment: #433 We are actively working to make deployment more convenient with viem to improve the experience. |
Beta Was this translation helpful? Give feedback.
Hey @wighawag have you seen the other answered discussions regarding viem deployment: #433
We are actively working to make deployment more convenient with viem to improve the experience.