Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ | add viem playground #5

Merged
merged 1 commit into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions viem-playground/contracts/Storage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Storage {
string message;

function store(string memory _message) public {
message = _message;
}

function retrieve() public view returns (string memory) {
return message;
}
}
4 changes: 4 additions & 0 deletions viem-playground/hardhat.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: '0.8.28',
}
72 changes: 72 additions & 0 deletions viem-playground/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { createTestClient, http, publicActions, walletActions } from 'viem'
import { hardhat } from 'viem/chains'
import { readFileSync } from 'fs'
import solc from 'solc'

const client = createTestClient({
chain: hardhat,
mode: 'hardhat',
transport: http(),
})
.extend(publicActions)
.extend(walletActions)

const [account] = await client.getAddresses()
console.log('Address:', account)

console.log('Chain ID:', client.chain.id)

const CONTRACT_FILE = 'contracts/Storage.sol'

const content = readFileSync(CONTRACT_FILE).toString()

const input = {
language: 'Solidity',
sources: {
[CONTRACT_FILE]: {
content: content,
},
},
settings: {
outputSelection: {
'*': {
'*': ['*'],
},
},
},
}

const compiled = solc.compile(JSON.stringify(input))
const output = JSON.parse(compiled)

const abi = output.contracts[CONTRACT_FILE].Storage.abi
const bytecode = output.contracts[CONTRACT_FILE].Storage.evm.bytecode.object

let hash = await client.deployContract({ abi, account, bytecode, args: [] })
console.log('Transaction Hash:', hash)

const receipt = await client.getTransactionReceipt({
hash,
})

const { request } = await client.simulateContract({
abi,
account,
address: receipt.contractAddress!,
functionName: 'store',
args: ['Hello, KBA!'],
})

hash = await client.writeContract(request)
console.log('Transaction Hash:', hash)

const message = await client.readContract({
abi,
address: receipt.contractAddress!,
functionName: 'retrieve',
args: [],
})
console.log('Message:', message)

const latestBlock = await client.getBlockNumber()
console.log('Latest Block:', latestBlock)
14 changes: 14 additions & 0 deletions viem-playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "viem-playground",
"module": "index.ts",
"type": "module",
"scripts": {
"node": "hardhat node"
},
"dependencies": {
"hardhat": "^2.22.17",
"solc": "^0.8.28",
"tsx": "^4.19.2",
"viem": "^2.21.57"
}
}
1 change: 1 addition & 0 deletions viem-playground/solc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'solc'
26 changes: 26 additions & 0 deletions viem-playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
// Enable latest features
"lib": ["ESNext", "DOM"],
"target": "ESNext",
"module": "ESNext",
"moduleDetection": "force",
"allowJs": true,

// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,

// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,

// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}