go-ethereum/docs/interacting-with-geth/javascript-console-contracts.md

134 lines
6.4 KiB
Markdown
Raw Permalink Normal View History

2022-10-27 15:27:55 +01:00
---
2022-11-13 22:40:42 -07:00
title: 'JavaScript Console 2: Contracts'
2022-10-27 15:27:55 +01:00
description: Instructions for working with contracts in the Javascript console.
---
2022-11-17 12:50:52 -07:00
The [Introduction to the Javascript console](/docs/interacting-with-geth/javascript-console) page outlined how a Javascript console can be attached to Geth to provide a more user-friendly interface to Ethereum than interacting directly with the JSON-RPC API. This page will describe how to deploy contracts and interact with contracts using the attached console. This page will assume the Javascript console is attached to a running Geth instance using IPC. Clef should be used to manage accounts.
2022-10-27 15:27:55 +01:00
2022-11-25 17:34:46 +00:00
## Deploying a contract {#deploying-a-contract}
2022-10-27 15:27:55 +01:00
First we need a contract to deploy. We can use the well-known `Storage.sol` contract written in Solidity. The following Solidity code can be copied and pasted into a text editor and saved as `go-ethereum/storage-contract/Storage.sol`.
2022-11-30 10:38:00 +00:00
```js
2022-10-27 15:27:55 +01:00
// SPDX License-Identifier: GPL 3.0
pragma solidity ^0.8.0;
contract Storage{
uint256 value = 5;
2022-11-13 22:03:06 -07:00
2022-10-27 15:27:55 +01:00
function set(uint256 number) public{
value = number;
}
function retrieve() public view returns (uint256){
return value;
}
}
```
2022-11-17 12:50:52 -07:00
The contract needs to be compiled before Geth can understand it. Compiling the contract creates an [Application Binary Interface](/docs/tools/abigen) and the contract bytecode. This requires a Solidity compiler (e.g. `solc`) to be installed on the local machine. Then, compile and save the ABI and bytecode to a new `build` subdirectory using the following terminal commands:
2022-10-27 15:27:55 +01:00
```sh
cd ~/go-ethereum/storage-contract
solc --bin Storage.sol -o build
solc --abi Storage.sol -o build
```
The outputs look as follows:
```sh
# build/Storage.bin
608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea264697066735822122031443f2fb748bdb27e539fdbeb0c6f575aec50508baaa7e4dbeb08577ef19b3764736f6c63430008110033
```
```json
# Storage.abi
[{"inputs":[],"name":"retrieve","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"number","type":"uint256"}],"name":"store","outputs":[],"stateMutability":"nonpayable","type":"function"}]
```
2022-11-13 22:03:06 -07:00
These are all the data required to deploy the contract using the Geth Javascript console. Open the Javascript console using `./geth attach geth.ipc`.
2022-10-27 15:27:55 +01:00
Now, for convenice we can store the abi and bytecode in variables in the console:
```js
2022-11-13 22:03:06 -07:00
var abi = [
{
inputs: [],
name: 'retrieve',
outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
stateMutability: 'view',
type: 'function'
},
{
inputs: [{ internalType: 'uint256', name: 'number', type: 'uint256' }],
name: 'store',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
}
];
var bytecode =
'608060405234801561001057600080fd5b50610150806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80632e64cec11461003b5780636057361d14610059575b600080fd5b610043610075565b60405161005091906100a1565b60405180910390f35b610073600480360381019061006e91906100ed565b61007e565b005b60008054905090565b8060008190555050565b6000819050919050565b61009b81610088565b82525050565b60006020820190506100b66000830184610092565b92915050565b600080fd5b6100ca81610088565b81146100d557600080fd5b50565b6000813590506100e7816100c1565b92915050565b600060208284031215610103576101026100bc565b5b6000610111848285016100d8565b9150509291505056fea264697066735822122031443f2fb748bdb27e539fdbeb0c6f575aec50508baaa7e4dbeb08577ef19b3764736f6c63430008110033';
2022-10-27 15:27:55 +01:00
```
The ABI can be used to create an instance of the contract:
```js
2022-11-13 22:03:06 -07:00
var contract = eth.contract(abi);
2022-10-27 15:27:55 +01:00
```
This contract instance can then be deployed to the blockchain. This is done using `eth.sendTransaction`, passing the contract bytecode in the `data` field. For convenience we can create a transaction JSON object first, then pass it to `eth.sendTransaction` later. Let's use the first account in `eth.accounts` as the sender. The amount of gas to include can be determined using `eth.estimateGas`:
```js
2022-11-13 22:03:06 -07:00
var gas = eth.estimateGas({ data: bytecode });
2022-10-27 15:27:55 +01:00
```
2022-10-27 16:39:53 +01:00
Note that each command that touches accounts will require **approval in Clef** unless a custom rule has been implemented. The bytecode, gas and address of the sender can be bundled together into an object that will be passed to the contract's `new()` method which deploys the contract.
2022-10-27 15:27:55 +01:00
```js
2022-11-13 22:03:06 -07:00
var tx = { from: eth.accounts[0], data: bytecode, gas: gas };
var deployed_contract = contract.new(tx);
2022-10-27 15:27:55 +01:00
```
The transaction hash and deployment address can now been viewed in the console by entering the variable name (in this case `deployed_contract`):
```js
{
abi:[{
inputs: [],
name: "retrieve",
outputs: [{...}],
stateMutability: "view",
type: "function"
},{
inputs: [],
name: "store",
outputs: [{...}],
stateMutability: "nonpayable",
type: "function"
}],
address: "0x2d6505f8b1130a22a5998cd31788bf6c751247f",
transactionHash: "0x5040a8916b23b76696ea9eba5b072546e1112cc481995219081fc86f5b911bf3",
allEvents: function bound(),
retrieve: function bound(),
store: function bound()
}
```
Passing the transaction hash to `eth.getTransaction()` returns the deployment transaction details including the contract bytecode in the `input` field. To interact with the contract, create an instance by passing the deployment address to `contract.at` then call the methods.
```js
2022-11-13 22:03:06 -07:00
var instance = contract.at('0x2d6505f8b1130a22a5998cd31788bf6c751247f');
2022-10-27 15:27:55 +01:00
// store() alters the state and therefore requires sendTransaction()
2022-11-13 22:03:06 -07:00
contract.set.sendTransaction(42, { from: eth.accounts[0], gas: 1000000 });
2022-10-27 15:27:55 +01:00
// retrieve does not alter state so it can be executed using call()
2022-11-13 22:03:06 -07:00
contract.retrieve().call() >> 2;
2022-10-27 15:27:55 +01:00
```
2022-11-25 17:34:46 +00:00
## Summary {#summary}
2022-11-13 22:03:06 -07:00
2022-10-27 16:39:53 +01:00
This page demonstrated how to create, compile, deploy and interact with an Ethereum smart contract using Geth's Javascript console.