Skip to content

Commit 5b9ab31

Browse files
committed
added deployContract command
1 parent 593073d commit 5b9ab31

File tree

9 files changed

+6450
-420
lines changed

9 files changed

+6450
-420
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ node_modules
88
!.env.example
99
vite.config.js.timestamp-*
1010
vite.config.ts.timestamp-*
11+
12+
/artifacts
13+
/cache

contracts/Example.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
contract Example {
5+
string public name;
6+
7+
function setName(string memory _name) public {
8+
name = _name;
9+
}
10+
}

hardhat.config.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/** @type import('hardhat/config').HardhatUserConfig */
2+
module.exports = {
3+
solidity: "0.8.19",
4+
};

package-lock.json

Lines changed: 6342 additions & 417 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"eslint": "^8.28.0",
2222
"eslint-config-prettier": "^8.5.0",
2323
"eslint-plugin-svelte": "^2.30.0",
24+
"hardhat": "^2.17.2",
2425
"prettier": "^2.8.0",
2526
"prettier-plugin-svelte": "^2.10.1",
2627
"svelte": "^4.0.5",

resources/Example.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"_format": "hh-sol-artifact-1",
3+
"contractName": "Example",
4+
"sourceName": "contracts/Example.sol",
5+
"abi": [
6+
{
7+
"inputs": [],
8+
"name": "name",
9+
"outputs": [
10+
{
11+
"internalType": "string",
12+
"name": "",
13+
"type": "string"
14+
}
15+
],
16+
"stateMutability": "view",
17+
"type": "function"
18+
},
19+
{
20+
"inputs": [
21+
{
22+
"internalType": "string",
23+
"name": "_name",
24+
"type": "string"
25+
}
26+
],
27+
"name": "setName",
28+
"outputs": [],
29+
"stateMutability": "nonpayable",
30+
"type": "function"
31+
}
32+
],
33+
"bytecode": "0x608060405234801561001057600080fd5b50610678806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806306fdde031461003b578063c47f002714610059575b600080fd5b610043610075565b60405161005091906101a6565b60405180910390f35b610073600480360381019061006e9190610311565b610103565b005b6000805461008290610389565b80601f01602080910402602001604051908101604052809291908181526020018280546100ae90610389565b80156100fb5780601f106100d0576101008083540402835291602001916100fb565b820191906000526020600020905b8154815290600101906020018083116100de57829003601f168201915b505050505081565b80600090816101129190610570565b5050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610150578082015181840152602081019050610135565b60008484015250505050565b6000601f19601f8301169050919050565b600061017882610116565b6101828185610121565b9350610192818560208601610132565b61019b8161015c565b840191505092915050565b600060208201905081810360008301526101c0818461016d565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61021e8261015c565b810181811067ffffffffffffffff8211171561023d5761023c6101e6565b5b80604052505050565b60006102506101c8565b905061025c8282610215565b919050565b600067ffffffffffffffff82111561027c5761027b6101e6565b5b6102858261015c565b9050602081019050919050565b82818337600083830152505050565b60006102b46102af84610261565b610246565b9050828152602081018484840111156102d0576102cf6101e1565b5b6102db848285610292565b509392505050565b600082601f8301126102f8576102f76101dc565b5b81356103088482602086016102a1565b91505092915050565b600060208284031215610327576103266101d2565b5b600082013567ffffffffffffffff811115610345576103446101d7565b5b610351848285016102e3565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806103a157607f821691505b6020821081036103b4576103b361035a565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b60006008830261041c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826103df565b61042686836103df565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061046d6104686104638461043e565b610448565b61043e565b9050919050565b6000819050919050565b61048783610452565b61049b61049382610474565b8484546103ec565b825550505050565b600090565b6104b06104a3565b6104bb81848461047e565b505050565b5b818110156104df576104d46000826104a8565b6001810190506104c1565b5050565b601f821115610524576104f5816103ba565b6104fe846103cf565b8101602085101561050d578190505b610521610519856103cf565b8301826104c0565b50505b505050565b600082821c905092915050565b600061054760001984600802610529565b1980831691505092915050565b60006105608383610536565b9150826002028217905092915050565b61057982610116565b67ffffffffffffffff811115610592576105916101e6565b5b61059c8254610389565b6105a78282856104e3565b600060209050601f8311600181146105da57600084156105c8578287015190505b6105d28582610554565b86555061063a565b601f1984166105e8866103ba565b60005b82811015610610578489015182556001820191506020850194506020810190506105eb565b8683101561062d5784890151610629601f891682610536565b8355505b6001600288020188555050505b50505050505056fea264697066735822122036db2d63b70dd76ddcfb08c9203b3913b8aa6f6aa4af467529f20e315cd86fd164736f6c63430008130033",
34+
"deployedBytecode": "0x608060405234801561001057600080fd5b50600436106100365760003560e01c806306fdde031461003b578063c47f002714610059575b600080fd5b610043610075565b60405161005091906101a6565b60405180910390f35b610073600480360381019061006e9190610311565b610103565b005b6000805461008290610389565b80601f01602080910402602001604051908101604052809291908181526020018280546100ae90610389565b80156100fb5780601f106100d0576101008083540402835291602001916100fb565b820191906000526020600020905b8154815290600101906020018083116100de57829003601f168201915b505050505081565b80600090816101129190610570565b5050565b600081519050919050565b600082825260208201905092915050565b60005b83811015610150578082015181840152602081019050610135565b60008484015250505050565b6000601f19601f8301169050919050565b600061017882610116565b6101828185610121565b9350610192818560208601610132565b61019b8161015c565b840191505092915050565b600060208201905081810360008301526101c0818461016d565b905092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b61021e8261015c565b810181811067ffffffffffffffff8211171561023d5761023c6101e6565b5b80604052505050565b60006102506101c8565b905061025c8282610215565b919050565b600067ffffffffffffffff82111561027c5761027b6101e6565b5b6102858261015c565b9050602081019050919050565b82818337600083830152505050565b60006102b46102af84610261565b610246565b9050828152602081018484840111156102d0576102cf6101e1565b5b6102db848285610292565b509392505050565b600082601f8301126102f8576102f76101dc565b5b81356103088482602086016102a1565b91505092915050565b600060208284031215610327576103266101d2565b5b600082013567ffffffffffffffff811115610345576103446101d7565b5b610351848285016102e3565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806103a157607f821691505b6020821081036103b4576103b361035a565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b60006008830261041c7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826103df565b61042686836103df565b95508019841693508086168417925050509392505050565b6000819050919050565b6000819050919050565b600061046d6104686104638461043e565b610448565b61043e565b9050919050565b6000819050919050565b61048783610452565b61049b61049382610474565b8484546103ec565b825550505050565b600090565b6104b06104a3565b6104bb81848461047e565b505050565b5b818110156104df576104d46000826104a8565b6001810190506104c1565b5050565b601f821115610524576104f5816103ba565b6104fe846103cf565b8101602085101561050d578190505b610521610519856103cf565b8301826104c0565b50505b505050565b600082821c905092915050565b600061054760001984600802610529565b1980831691505092915050565b60006105608383610536565b9150826002028217905092915050565b61057982610116565b67ffffffffffffffff811115610592576105916101e6565b5b61059c8254610389565b6105a78282856104e3565b600060209050601f8311600181146105da57600084156105c8578287015190505b6105d28582610554565b86555061063a565b601f1984166105e8866103ba565b60005b82811015610610578489015182556001820191506020850194506020810190506105eb565b8683101561062d5784890151610629601f891682610536565b8355505b6001600288020188555050505b50505050505056fea264697066735822122036db2d63b70dd76ddcfb08c9203b3913b8aa6f6aa4af467529f20e315cd86fd164736f6c63430008130033",
35+
"linkReferences": {},
36+
"deployedLinkReferences": {}
37+
}

src/commands/cmdFuncMap.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { encodeAbiParameters, encodePacked, keccak256, parseAbiParameters, toHex, toRlp } from 'viem';
22
import { array, byteArray, findInSerialize, fromProperty } from './array'
3-
import { contractEvents, readContract, writeContract } from './contract'
3+
import { contractEvents, deployContract, readContract, writeContract } from './contract'
44
import { date, isoDate, now } from './date'
55
import { asyncEcho, echo } from './echo'
66
import { asyncLog, log } from './log'
@@ -431,6 +431,19 @@ const cmdFuncMap: Record<string, CmdFunc> = {
431431
'Params:',
432432
'value => The wei value',
433433
].join('<br>')
434+
},
435+
['deployContract']: {
436+
exec: deployContract,
437+
help: [
438+
'Deploys a contract to the network, given bytecode & constructor arguments.',
439+
'Usage: deployContract account=0x… abi=$abiJson bytecode=0x… [chainId=id] [args=$args]',
440+
'Params:',
441+
'account => Owner of the contract',
442+
`abi => Contract's Abi as JSON`,
443+
"bytecode => The contract's bytecode as 0x hex string",
444+
'[chainId] => Forces a specific chain id for the request',
445+
'[args] => List of arguments to pass to the constructor',
446+
].join('<br>')
434447
}
435448
}
436449

src/commands/contract.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { getPublicClient, readContract as wagmiReadContract, writeContract as wagmiWriteContract } from '@wagmi/core'
22
import checkConnected from '../utils/checkConnected'
3-
import type { Abi, Address } from 'viem'
3+
import type { Abi, Address, Hex } from 'viem'
44
import { checkSupportedChain } from '../web3/wagmi'
5+
import getConnectedWallet from '../utils/getConnectedWallet'
6+
import { get } from 'svelte/store'
7+
import { network } from '../stores'
58

69
type ContractOptions = {
710
abi: Abi
@@ -20,6 +23,14 @@ type ContractEventFilterArgs = {
2023
toBlock?: bigint,
2124
}
2225

26+
type DeployOptions = {
27+
abi: Abi,
28+
account: Address, // owner of the contract
29+
bytecode: Hex,
30+
chainId?: number,
31+
args?: unknown[],
32+
}
33+
2334
function checkOptions(options: ContractOptions) {
2435
if (!options?.address) {
2536
throw new Error('Address is required. Type "help writeContract" for more details.')
@@ -75,3 +86,28 @@ export async function contractEvents(args: ContractEventFilterArgs) {
7586

7687
return client.getFilterLogs({ filter });
7788
}
89+
90+
export async function deployContract(options: DeployOptions) {
91+
if (!options?.account) {
92+
throw new Error('Account is required. Type "help deployContract" for more details.')
93+
}
94+
95+
if (!options?.abi) {
96+
throw new Error('ABI is required. Type "help deployContract" for more details.')
97+
}
98+
99+
if (!options?.bytecode) {
100+
throw new Error(`Contract's bytecode is required. Type "help deployContract" for more details.`)
101+
}
102+
103+
// If no chainId is passed, we use the current network
104+
const chainId = options?.chainId || get(network)?.id
105+
106+
if (!chainId) {
107+
throw new Error('No chain ID found. Type "help deployContract" for more details.')
108+
}
109+
110+
const wallet = await getConnectedWallet(chainId)
111+
112+
return wallet.deployContract(options)
113+
}

src/web3/wagmi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { w3mConnectors, w3mProvider } from '@web3modal/ethereum'
22
import { configureChains, createConfig, type Chain } from '@wagmi/core'
3-
import { arbitrum, mainnet, polygon, sepolia } from '@wagmi/core/chains'
3+
import { arbitrum, mainnet, polygon, sepolia, goerli } from '@wagmi/core/chains'
44
import { PUBLIC_WALLET_CONNECT_PROJECT_ID } from '$env/static/public'
55
import { CHAIN_STORAGE_KEY } from '../utils/chain'
66

@@ -14,6 +14,7 @@ export const chains: Chain[] = [
1414
mainnet,
1515
polygon,
1616
sepolia,
17+
goerli,
1718
...customChains,
1819
]
1920

0 commit comments

Comments
 (0)