Geth - 콘솔에서 컨트랙트 배포

CHOYEAH·2023년 12월 1일
0

Go ethreume

목록 보기
9/11

컨트랙트 작성


// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.4.16 <0.9.0;

contract SimpleStorage {
    uint storeData;

    function set(uint x) public {
        storeData = x;
    }

    function get() public view returns (uint) {
        return storeData;
    }
}

solc 다운로드

npm i -g solc

added 9 packages, and audited 10 packages in 405ms

1 package is looking for funding
  run `npm fund` for details

found 0 vulnerabilities

컴파일

 solcjs --bin --abi ./contracts/storate.sol 

--bin: 컨트랙트 바이너리(바이트코드) 버전을 생성 지시, 바이너리는 블록체인에 배포되기 위해 필요한 형식
--abi: 컨트랙트 ABI(Application Binary Interface)를 생성하도록 지시, ABI는 컨트랙트 메소드와 구조를 정의하는 JSON 형식의 인터페이스, 컨트랙트와 상호 작용할 때 필요.

결과물

바이트코드

608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c806360fe47b1146100385780636d4ce63c14610054575b5f80fd5b610052600480360381019061004d91906100ba565b610072565b005b61005c61007b565b60405161006991906100f4565b60405180910390f35b805f8190555050565b5f8054905090565b5f80fd5b5f819050919050565b61009981610087565b81146100a3575f80fd5b50565b5f813590506100b481610090565b92915050565b5f602082840312156100cf576100ce610083565b5b5f6100dc848285016100a6565b91505092915050565b6100ee81610087565b82525050565b5f6020820190506101075f8301846100e5565b9291505056fea2646970667358221220c1a6378a39fc1cfb08ea70e02860188ef29c1e50cca9f898f84698ba5eff96b064736f6c63430008170033

abi

[
  {
    "inputs": [],
    "name": "get",
    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [{ "internalType": "uint256", "name": "x", "type": "uint256" }],
    "name": "set",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  }
]

콘솔에서 컨트랙트 배포

바이트코드, abi 변수에 대입

> let bin = "0x608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c806360fe47b1146100385780636d4ce63c14610054575b5f80fd5b610052600480360381019061004d91906100ba565b610072565b005b61005c61007b565b60405161006991906100f4565b60405180910390f35b805f8190555050565b5f8054905090565b5f80fd5b5f819050919050565b61009981610087565b81146100a3575f80fd5b50565b5f813590506100b481610090565b92915050565b5f602082840312156100cf576100ce610083565b5b5f6100dc848285016100a6565b91505092915050565b6100ee81610087565b82525050565b5f6020820190506101075f8301846100e5565b9291505056fea264697066735822122002818b59e42af5a612fc56c1c2ce267bf923c6c777a8bf2b85a77317681ea10564736f6c63430008160033";

> let abi = [{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}]

> bin
"0x608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c806360fe47b1146100385780636d4ce63c14610054575b5f80fd5b610052600480360381019061004d91906100ba565b610072565b005b61005c61007b565b60405161006991906100f4565b60405180910390f35b805f8190555050565b5f8054905090565b5f80fd5b5f819050919050565b61009981610087565b81146100a3575f80fd5b50565b5f813590506100b481610090565b92915050565b5f602082840312156100cf576100ce610083565b5b5f6100dc848285016100a6565b91505092915050565b6100ee81610087565b82525050565b5f6020820190506101075f8301846100e5565b9291505056fea2646970667358221220c1a6378a39fc1cfb08ea70e02860188ef29c1e50cca9f898f84698ba5eff96b064736f6c63430008170033"

> abi
[{
    inputs: [],
    name: "get",
    outputs: [{
        internalType: "uint256",
        name: "",
        type: "uint256"
    }],
    stateMutability: "view",
    type: "function"
}, {
    inputs: [{
        internalType: "uint256",
        name: "x",
        type: "uint256"
    }],
    name: "set",
    outputs: [],
    stateMutability: "nonpayable",
    type: "function"
}]

바이트코드 앞에 0x를 붙여주었음

컨트랙트 배포 & 트랜잭션 확인

> let contractInterface = eth.contract(abi)
undefined

> contractInterface.new({from: eth.accounts[0], data: bin, gas:1000000})
{
  abi: [{
      inputs: [],
      name: "get",
      outputs: [{...}],
      stateMutability: "view",
      type: "function"
  }, {
      inputs: [{...}],
      name: "set",
      outputs: [],
      stateMutability: "nonpayable",
      type: "function"
  }],
  address: undefined,
  transactionHash: "0x78b88804db919c4f24899aaf0eaae7f8ff68db82c03d009b21897e81b61e7c4e"
}

> eth.getTransaction("0x78b88804db919c4f24899aaf0eaae7f8ff68db82c03d009b21897e81b61e7c4e")
{
  accessList: [],
  blockHash: "0xace388be3c49e049ec3c74f6df01f5c1fbcb58b16912fe4f32a6611df20c3de5",
  blockNumber: 3104,
  chainId: "0x3039",
  from: "0x2da28e108530892c660dca151762ec3304b63127",
  gas: 1000000,
  gasPrice: 1000000007,
  hash: "0x78b88804db919c4f24899aaf0eaae7f8ff68db82c03d009b21897e81b61e7c4e",
  input: "0x608060405234801561000f575f80fd5b506101438061001d5f395ff3fe608060405234801561000f575f80fd5b5060043610610034575f3560e01c806360fe47b1146100385780636d4ce63c14610054575b5f80fd5b610052600480360381019061004d91906100ba565b610072565b005b61005c61007b565b60405161006991906100f4565b60405180910390f35b805f8190555050565b5f8054905090565b5f80fd5b5f819050919050565b61009981610087565b81146100a3575f80fd5b50565b5f813590506100b481610090565b92915050565b5f602082840312156100cf576100ce610083565b5b5f6100dc848285016100a6565b91505092915050565b6100ee81610087565b82525050565b5f6020820190506101075f8301846100e5565b9291505056fea264697066735822122002818b59e42af5a612fc56c1c2ce267bf923c6c777a8bf2b85a77317681ea10564736f6c63430008160033",
  maxFeePerGas: 1000000014,
  maxPriorityFeePerGas: 1000000000,
  nonce: 6,
  r: "0x69078a6a7931ec1e072232d0ad257810957a5ac5e57436732b729af605ac1c53",
  s: "0x4054c79b0ba289138894c18e62f70f4d9e367272af8ecd6b33a10bf307d8e8ed",
  to: null,
  transactionIndex: 0,
  type: "0x2",
  v: "0x0",
  value: 0
}

컨트랙트 주소 확인

eth.getTransactionReceipt("0x78b88804db919c4f24899aaf0eaae7f8ff68db82c03d009b21897e81b61e7c4e")

{
  blockHash: "0xace388be3c49e049ec3c74f6df01f5c1fbcb58b16912fe4f32a6611df20c3de5",
  blockNumber: 3104,
  contractAddress: "0x77e7637b4ee9f5fbb8cb69b5f3de506b52c4a619",
  cumulativeGasUsed: 1000000,
  effectiveGasPrice: 1000000007,
  from: "0x2da28e108530892c660dca151762ec3304b63127",
  gasUsed: 1000000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  status: "0x0",
  to: null,
  transactionHash: "0x78b88804db919c4f24899aaf0eaae7f8ff68db82c03d009b21897e81b61e7c4e",
  transactionIndex: 0,
  type: "0x2"
}
profile
Move fast & break things

0개의 댓글