본문 바로가기
블록체인/[파공이]파이썬으로 공부하는 이더리움(Web3)

[web3]PYTHON web3를 활용하여 Smart Contract 배포하기!!(1) - Hello World 프린트

by 일등박사 2022. 8. 17.

2022.08.13 - [일등박사의 생각/데이터분석] - [이더리움] Private Block Chain vs Public Block Chain

 

[이더리움] Private Block Chain vs Public Block Chain

게임 좋아하시나요?? 저는 예~전에 디아블로2를 재미있게했었습니다!! 디아블로2에는  "싱글 플레이"와 "멀티 플레이(베틀넷)"가 있었는데요! 싱글플레이는 내 컴퓨터의 환경에서 나혼자 게임을

drfirst.tistory.com

 


지금까지의 포스팅을 정리하면 다음과 같습니다!\

 

1. 나만의 이더리움환경(Private Blockchain Network) 만들기

2022.08.13 - [일등박사의 생각/블록체인] - [GANACHE] 가나슈 설치하기!!! (Python api 연결준비)

 

[GANACHE] 가나슈 설치하기!!! (Python api 연결준비)

2022.08.13 - [일등박사의 생각/데이터분석] - [이더리움] Private Block Chain vs Public Block Chain 지난 포스팅을 통하여 Private Ethereum Network 에 대하여 알아보았는데요!! 이번 포스팅에서는 Window환경..

drfirst.tistory.com

 

2. Python request를 활용하여 이더리움환경과 데이터 주고받기

2022.08.14 - [일등박사의 생각/블록체인] - [GETH]API를 활용, PYTHON으로 ganache의 데이터 불러오기!!

 

[GETH]API를 활용, PYTHON으로 ganache의 데이터 불러오기!!

2022.08.13 - [일등박사의 생각/데이터분석] - [이더리움] Private Block Chain vs Public Block Chain 2022.08.13 - [일등박사의 생각/데이터분석] - [GANACHE] 가나슈 설치하기!!! (Python api 연결준비) 최근!!..

drfirst.tistory.com

 

3. Python 패키지 Web3를 활요하여 이더리움환경과 데이터 주고받기!!

2022.08.17 - [일등박사의 생각/블록체인] - [GETH] Python Web3 패키지를 활용하여 데이터 주고받기!

 

 

 

 

이제 남은것읕 이더리움 네트워크의 꽃!!

바로 Smart Contract 배포하기 입니다!!

 

 

함께 알아보아요~!!!

 

smart contract 에서는 solidity의 배경지식이 필요한데요!@!

이번에는 배경지식이 없어도 이해할 아주 간단한 함수를 배포해볼것입니다!!

pragma solidity ^0.8.13;

contract HelloWorld {
    string public greet = "Hello World!";
}

위의 솔리디티 함수, 참 쉽죠?? 그냥 Hello World를 프린트하는 함수입니다!

그리고  web3에 이어 또다른 파이썬 페키지가 필요합니다!!

파이썬에서 solidity를 컴퓨터 언어로 전화해주는 패키지!! 아래와 같이 설치합니다!!

pip install py-solc-x

 

설치가 끝났음 시작해보아요!!

 

## 기본 페키지 및 주소 불러오기
from web3 import Web3
from solcx import compile_standard, install_solc
import json

BLOCKCHAIN_ADR = "HTTP://127.0.0.1:8545"
chain_id = 1337

# solc 버젼이 맞게 설치
install_solc('0.8.13')

contract_func  = '''
pragma solidity ^0.8.13;

contract HelloWorld {
    string public greet = "Hello World!";
}
'''

## solidity 언어를 컴퍼일하기!!!
compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"Hello.sol": {"content": contract_func}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"] # output needed to interact with and deploy contract 
                }
            }
        },
    },
    solc_version="0.8.13",
)

# 프린트를 보면 내용을 알 수 있다
print(compiled_sol)
## OPCODE가 나오는데 다음 포스팅에서 알아보자!!

 

위까지 진행하면 json과 알수없는 글들이 섞인 아래와 같은 결과물이 print 됩니다

Solidity 언어가 잘 complie 된것이지요~!

{'contracts': {'Hello.sol': {'HelloWorld': {'abi': [{'inputs': [], 'name': 'greet', 'outputs': [{'internalType': 'string', 'name': '', 'type': 'string'}], 'stateMutability': 'view', 'type': 'function'}], 'evm': {'bytecode': {'functionDebugData': {'extract_byte_array_length': {'entryPoint': 308, 'id': None, 'parameterSlots': 1, 'returnSlots': 1}, 'panic_error_0x22': {'entryPoint': 261, 'id': None, 'parameterSlots': 0, 'returnSlots': 0}}, 'generatedSources': [{'ast': {'nodeType': 'YulBlock', 'src': '0:516:1', 'statements': [{'body': {'nodeType': 'YulBlock', 'src': '35:152:1', 'statements': [{'expression': {'arguments': [{'kind': 'number', 'nodeType': 'YulLiteral', 'src': '52:1:1', 'type': '', 'value': '0'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '55:77:1', 'type': '', 'value': '35408467139433450592217433187231851964531694900788300625387963629091585785856'}], 'functionName': {'name': 'mstore', 'nodeType': 'YulIdentifier', 'src': '45:6:1'}, 'nodeType': 'YulFunctionCall', 'src': '45:88:1'}, 'nodeType': 'YulExpressionStatement', 'src': '45:88:1'}, {'expression': {'arguments': [{'kind': 'number', 'nodeType': 'YulLiteral', 'src': '149:1:1', 'type': '', 'value': '4'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '152:4:1', 'type': '', 'value': '0x22'}], 'functionName': {'name': 'mstore', 'nodeType': 'YulIdentifier', 'src': '142:6:1'}, 'nodeType': 'YulFunctionCall', 'src': '142:15:1'}, 'nodeType': 'YulExpressionStatement', 'src': '142:15:1'}, {'expression': {'arguments': [{'kind': 'number', 'nodeType': 'YulLiteral', 'src': '173:1:1', 'type': '', 'value': '0'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '176:4:1', 'type': '', 'value': '0x24'}], 'functionName': {'name': 'revert', 'nodeType': 'YulIdentifier', 'src': '166:6:1'}, 'nodeType': 'YulFunctionCall', 'src': '166:15:1'}, 'nodeType': 'YulExpressionStatement', 'src': '166:15:1'}]}, 'name': 'panic_error_0x22', 'nodeType': 'YulFunctionDefinition', 'src': '7:180:1'}, {'body': {'nodeType': 'YulBlock', 'src': '244:269:1', 'statements': [{'nodeType': 'YulAssignment', 'src': '254:22:1', 'value': {'arguments': [{'name': 'data', 'nodeType': 'YulIdentifier', 'src': '268:4:1'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '274:1:1', 'type': '', 'value': '2'}], 'functionName': {'name': 'div', 'nodeType': 'YulIdentifier', 'src': '264:3:1'}, 'nodeType': 'YulFunctionCall', 'src': '264:12:1'}, 'variableNames': [{'name': 'length', 'nodeType': 'YulIdentifier', 'src': '254:6:1'}]}, {'nodeType': 'YulVariableDeclaration', 'src': '285:38:1', 'value': {'arguments': [{'name': 'data', 'nodeType': 'YulIdentifier', 'src': '315:4:1'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '321:1:1', 'type': '', 'value': '1'}], 'functionName': {'name': 'and', 'nodeType': 'YulIdentifier', 'src': '311:3:1'}, 'nodeType': 'YulFunctionCall', 'src': '311:12:1'}, 'variables': [{'name': 'outOfPlaceEncoding', 'nodeType': 'YulTypedName', 'src': '289:18:1', 'type': ''}]}, {'body': {'nodeType': 'YulBlock', 'src': '362:51:1', 'statements': [{'nodeType': 'YulAssignment', 'src': '376:27:1', 'value': {'arguments': [{'name': 'length', 'nodeType': 'YulIdentifier', 'src': '390:6:1'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '398:4:1', 'type': '', 'value': '0x7f'}], 'functionName': {'name': 'and', 'nodeType': 'YulIdentifier', 'src': '386:3:1'}, 'nodeType': 'YulFunctionCall', 'src': '386:17:1'}, 'variableNames': [{'name': 'length', 'nodeType': 'YulIdentifier', 'src': '376:6:1'}]}]}, 'condition': {'arguments': [{'name': 'outOfPlaceEncoding', 'nodeType': 'YulIdentifier', 'src': '342:18:1'}], 'functionName': {'name': 'iszero', 'nodeType': 'YulIdentifier', 'src': '335:6:1'}, 'nodeType': 'YulFunctionCall', 'src': '335:26:1'}, 'nodeType': 'YulIf', 'src': '332:81:1'}, {'body': {'nodeType': 'YulBlock', 'src': '465:42:1', 'statements': [{'expression': {'arguments': [], 'functionName': {'name': 'panic_error_0x22', 'nodeType': 'YulIdentifier', 'src': '479:16:1'}, 'nodeType': 'YulFunctionCall', 'src': '479:18:1'}, 'nodeType': 'YulExpressionStatement', 'src': '479:18:1'}]}, 'condition': {'arguments': [{'name': 'outOfPlaceEncoding', 'nodeType': 'YulIdentifier', 'src': '429:18:1'}, {'arguments': [{'name': 'length', 'nodeType': 'YulIdentifier', 'src': '452:6:1'}, {'kind': 'number', 'nodeType': 'YulLiteral', 'src': '460:2:1', 'type': '', 'value': '32'}], 'functionName': {'name': 'lt', 'nodeType': 'YulIdentifier', 'src': '449:2:1'}, 'nodeType': 'YulFunctionCall', 'src': '449:14:1'}], 'functionName': {'name': 'eq', 'nodeType': 'YulIdentifier', 'src': '426:2:1'}, 'nodeType': 'YulFunctionCall', 'src': '426:38:1'}, 'nodeType': 'YulIf', 'src': '423:84:1'}]}, 'name': 'extract_byte_array_length', 'nodeType': 'YulFunctionDefinition', 'parameters': [{'name': 'data', 'nodeType': 'YulTypedName', 'src': '228:4:1', 'type': ''}], 'returnVariables': [{'name': 'length', 'nodeType': 'YulTypedName', 'src': '237:6:1', 'type': ''}], 'src': '193:320:1'}]}, 'contents': '{\n\n    function panic_error_0x22() {\n        mstore(0, 35408467139433450592217433187231851964531694900788300625387963629091585785856)\n        mstore(4, 0x22)\n        revert(0, 0x24)\n    }\n\n    function extract_byte_array_length(data) -> length {\n        length := div(data, 2)\n        let outOfPlaceEncoding := and(data, 1)\n        if iszero(outOfPlaceEncoding) {\n            length := and(length, 0x7f)\n        }\n\n        if eq(outOfPlaceEncoding, lt(length, 32)) {\n            panic_error_0x22()\n        }\n    }\n\n}\n', 'id': 1, 'language': 'Yul', 'name': '#utility.yul'}], 'linkReferences': {}, 'object': '60806040526040518060400160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b50610165565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b60208210810361015f5761015e610105565b5b50919050565b61022d806101746000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063cfae321714610030575b600080fd5b61003861004e565b6040516100459190610175565b60405180910390f35b6000805461005b906101c6565b80601f0160208091040260200160405190810160405280929190818152602001828054610087906101c6565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600081519050919050565b600082825260208201905092915050565b60005b838110156101165780820151818401526020810190506100fb565b83811115610125576000848401525b50505050565b6000601f19601f8301169050919050565b6000610147826100dc565b61015181856100e7565b93506101618185602086016100f8565b61016a8161012b565b840191505092915050565b6000602082019050818103600083015261018f818461013c565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101de57607f821691505b6020821081036101f1576101f0610197565b5b5091905056fea2646970667358221220b25a949ba8cdcd4dce5ff2cf8574fd60f742022c5b670d9a896109b4728bca2964736f6c634300080d0033', 'opcodes': 'PUSH1 0x80 PUSH1 0x40 MSTORE PUSH1 0x40 MLOAD DUP1 PUSH1 0x40 ADD PUSH1 0x40 MSTORE DUP1 PUSH1 0xC DUP2 MSTORE PUSH1 0x20 ADD PUSH32 0x48656C6C6F20576F726C64210000000000000000000000000000000000000000 DUP2 MSTORE POP PUSH1 0x0 SWAP1 DUP1 MLOAD SWAP1 PUSH1 0x20 ADD SWAP1 PUSH2 0x4F SWAP3 SWAP2 SWAP1 PUSH2 0x62 JUMP JUMPDEST POP CALLVALUE DUP1 ISZERO PUSH2 0x5C JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH2 0x165 JUMP JUMPDEST DUP3 DUP1 SLOAD PUSH2 0x6E SWAP1 PUSH2 0x134 JUMP JUMPDEST SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 PUSH1 0x1F ADD PUSH1 0x20 SWAP1 DIV DUP2 ADD SWAP3 DUP3 PUSH2 0x90 JUMPI PUSH1 0x0 DUP6 SSTORE PUSH2 0xD7 JUMP JUMPDEST DUP3 PUSH1 0x1F LT PUSH2 0xA9 JUMPI DUP1 MLOAD PUSH1 0xFF NOT AND DUP4 DUP1 ADD OR DUP6 SSTORE PUSH2 0xD7 JUMP JUMPDEST DUP3 DUP1 ADD PUSH1 0x1 ADD DUP6 SSTORE DUP3 ISZERO PUSH2 0xD7 JUMPI SWAP2 DUP3 ADD JUMPDEST DUP3 DUP2 GT ISZERO PUSH2 0xD6 JUMPI DUP3 MLOAD DUP3 SSTORE SWAP2 PUSH1 0x20 ADD SWAP2 SWAP1 PUSH1 0x1 ADD SWAP1 PUSH2 0xBB JUMP JUMPDEST JUMPDEST POP SWAP1 POP PUSH2 0xE4 SWAP2 SWAP1 PUSH2 0xE8 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST JUMPDEST DUP1 DUP3 GT ISZERO PUSH2 0x101 JUMPI PUSH1 0x0 DUP2 PUSH1 0x0 SWAP1 SSTORE POP PUSH1 0x1 ADD PUSH2 0xE9 JUMP JUMPDEST POP SWAP1 JUMP JUMPDEST PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x22 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST PUSH1 0x0 PUSH1 0x2 DUP3 DIV SWAP1 POP PUSH1 0x1 DUP3 AND DUP1 PUSH2 0x14C JUMPI PUSH1 0x7F DUP3 AND SWAP2 POP JUMPDEST PUSH1 0x20 DUP3 LT DUP2 SUB PUSH2 0x15F JUMPI PUSH2 0x15E PUSH2 0x105 JUMP JUMPDEST JUMPDEST POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH2 0x22D DUP1 PUSH2 0x174 PUSH1 0x0 CODECOPY PUSH1 0x0 RETURN INVALID PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0x10 JUMPI PUSH1 0x0 DUP1 REVERT JUMPDEST POP PUSH1 0x4 CALLDATASIZE LT PUSH2 0x2B JUMPI PUSH1 0x0 CALLDATALOAD PUSH1 0xE0 SHR DUP1 PUSH4 0xCFAE3217 EQ PUSH2 0x30 JUMPI JUMPDEST PUSH1 0x0 DUP1 REVERT JUMPDEST PUSH2 0x38 PUSH2 0x4E JUMP JUMPDEST PUSH1 0x40 MLOAD PUSH2 0x45 SWAP2 SWAP1 PUSH2 0x175 JUMP JUMPDEST PUSH1 0x40 MLOAD DUP1 SWAP2 SUB SWAP1 RETURN JUMPDEST PUSH1 0x0 DUP1 SLOAD PUSH2 0x5B SWAP1 PUSH2 0x1C6 JUMP JUMPDEST DUP1 PUSH1 0x1F ADD PUSH1 0x20 DUP1 SWAP2 DIV MUL PUSH1 0x20 ADD PUSH1 0x40 MLOAD SWAP1 DUP2 ADD PUSH1 0x40 MSTORE DUP1 SWAP3 SWAP2 SWAP1 DUP2 DUP2 MSTORE PUSH1 0x20 ADD DUP3 DUP1 SLOAD PUSH2 0x87 SWAP1 PUSH2 0x1C6 JUMP JUMPDEST DUP1 ISZERO PUSH2 0xD4 JUMPI DUP1 PUSH1 0x1F LT PUSH2 0xA9 JUMPI PUSH2 0x100 DUP1 DUP4 SLOAD DIV MUL DUP4 MSTORE SWAP2 PUSH1 0x20 ADD SWAP2 PUSH2 0xD4 JUMP JUMPDEST DUP3 ADD SWAP2 SWAP1 PUSH1 0x0 MSTORE PUSH1 0x20 PUSH1 0x0 KECCAK256 SWAP1 JUMPDEST DUP2 SLOAD DUP2 MSTORE SWAP1 PUSH1 0x1 ADD SWAP1 PUSH1 0x20 ADD DUP1 DUP4 GT PUSH2 0xB7 JUMPI DUP3 SWAP1 SUB PUSH1 0x1F AND DUP3 ADD SWAP2 JUMPDEST POP POP POP POP POP DUP2 JUMP JUMPDEST PUSH1 0x0 DUP2 MLOAD SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 DUP3 DUP3 MSTORE PUSH1 0x20 DUP3 ADD SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 JUMPDEST DUP4 DUP2 LT ISZERO PUSH2 0x116 JUMPI DUP1 DUP3 ADD MLOAD DUP2 DUP5 ADD MSTORE PUSH1 0x20 DUP2 ADD SWAP1 POP PUSH2 0xFB JUMP JUMPDEST DUP4 DUP2 GT ISZERO PUSH2 0x125 JUMPI PUSH1 0x0 DUP5 DUP5 ADD MSTORE JUMPDEST POP POP POP POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x1F NOT PUSH1 0x1F DUP4 ADD AND SWAP1 POP SWAP2 SWAP1 POP JUMP JUMPDEST PUSH1 0x0 PUSH2 0x147 DUP3 PUSH2 0xDC JUMP JUMPDEST PUSH2 0x151 DUP2 DUP6 PUSH2 0xE7 JUMP JUMPDEST SWAP4 POP PUSH2 0x161 DUP2 DUP6 PUSH1 0x20 DUP7 ADD PUSH2 0xF8 JUMP JUMPDEST PUSH2 0x16A DUP2 PUSH2 0x12B JUMP JUMPDEST DUP5 ADD SWAP2 POP POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH1 0x0 PUSH1 0x20 DUP3 ADD SWAP1 POP DUP2 DUP2 SUB PUSH1 0x0 DUP4 ADD MSTORE PUSH2 0x18F DUP2 DUP5 PUSH2 0x13C JUMP JUMPDEST SWAP1 POP SWAP3 SWAP2 POP POP JUMP JUMPDEST PUSH32 0x4E487B7100000000000000000000000000000000000000000000000000000000 PUSH1 0x0 MSTORE PUSH1 0x22 PUSH1 0x4 MSTORE PUSH1 0x24 PUSH1 0x0 REVERT JUMPDEST PUSH1 0x0 PUSH1 0x2 DUP3 DIV SWAP1 POP PUSH1 0x1 DUP3 AND DUP1 PUSH2 0x1DE JUMPI PUSH1 0x7F DUP3 AND SWAP2 POP JUMPDEST PUSH1 0x20 DUP3 LT DUP2 SUB PUSH2 0x1F1 JUMPI PUSH2 0x1F0 PUSH2 0x197 JUMP JUMPDEST JUMPDEST POP SWAP2 SWAP1 POP JUMP INVALID LOG2 PUSH5 0x6970667358 0x22 SLT KECCAK256 0xB2 GAS SWAP5 SWAP12 0xA8 0xCD 0xCD 0x4D 0xCE 0x5F CALLCODE 0xCF DUP6 PUSH21 0xFD60F742022C5B670D9A896109B4728BCA2964736F PUSH13 0x634300080D0033000000000000 ', 'sourceMap': '27:65:0:-:0;;;53:36;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;27:65;;;;;;;;;;;;;;;;;;;:::i;:::-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;:::i;:::-;;;:::o;:::-;;;;;;;;;;;;;;;;;;;;;:::o;7:180:1:-;55:77;52:1;45:88;152:4;149:1;142:15;176:4;173:1;166:15;193:320;237:6;274:1;268:4;264:12;254:22;;321:1;315:4;311:12;342:18;332:81;;398:4;390:6;386:17;376:27;;332:81;460:2;452:6;449:14;429:18;426:38;423:84;;479:18;;:::i;:::-;423:84;244:269;193:320;;;:::o;27:65:0:-;;;;;;;'}}, 'metadata': '{"compiler":{"version":"0.8.13+commit.abaa5c0e"},"language":"Solidity","output":{"abi":[{"inputs":[],"name":"greet","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}],"devdoc":{"kind":"dev","methods":{},"version":1},"userdoc":{"kind":"user","methods":{},"version":1}},"settings":{"compilationTarget":{"Hello.sol":"HelloWorld"},"evmVersion":"london","libraries":{},"metadata":{"bytecodeHash":"ipfs"},"optimizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"Hello.sol":{"keccak256":"0xe722d2446c8a30994e713029a3b78cfbb6bdd6ecdc591cb23521ed2e8237a724","urls":["bzz-raw://13b17482ee2e8205cd2b947a0c7e2a763c817e6b5c87ee21a46f9b0106113bb9","dweb:/ipfs/Qma4nrhaK6LAT6cMHjx2DE9y6TX26gE5GKiDpk1gU3HkAt"]}},"version":1}'}}}, 'errors': [{'component': 'general', 'errorCode': '1878', 'formattedMessage': 'Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.\n--> Hello.sol\n\n', 'message': 'SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.', 'severity': 'warning', 'sourceLocation': {'end': -1, 'file': 'Hello.sol', 'start': -1}, 'type': 'Warning'}], 'sources': {'Hello.sol': {'id': 0}}}

 

이젠 Smart Contract 배표장면입니다!!

## Bytecode와 abi 뽑기

# get bytecode
bytecode = compiled_sol["contracts"]["Hello.sol"]["HelloWorld"]["evm"]["bytecode"]["object"]
# get abia
abi = json.loads(compiled_sol["contracts"]["Hello.sol"]["HelloWorld"]["metadata"])["output"]["abi"]


# Web3로 네트워크에 연결한뒤!! smartcontract 배포하자!!
w3 = Web3(Web3.HTTPProvider(BLOCKCHAIN_ADR))


ContactList = w3.eth.contract(abi=abi, bytecode=bytecode)# Get the number of latest transaction
nonce = w3.eth.getTransactionCount(w3.eth.accounts[-1])


# build transaction
transaction = ContactList.constructor().buildTransaction(
    {
        "chainId": chain_id,
        "gasPrice": w3.eth.gas_price,
        "from": w3.eth.accounts[-1],
        "nonce": nonce,
    }
)
# Sign the transaction
sign_transaction = w3.eth.account.sign_transaction(transaction, private_key="86d33c7ee6acc4400f5e207083cbdb250660630c6e92d14a58f196c740e8bbb7")
print("Deploying Contract!")
# Send the transaction
transaction_hash = w3.eth.send_raw_transaction(sign_transaction.rawTransaction)
# Wait for the transaction to be mined, and get the transaction receipt
print("Waiting for transaction to finish...")
## 다른 python 환경에서  mining을 진행해주어야합니다!!
transaction_receipt = w3.eth.wait_for_transaction_receipt(transaction_hash)
print(f"Done! Contract deployed to {transaction_receipt.contractAddress}")

 

위의 코드로 배포가 완료되었습니다!!!!!

 

이제 아래와 같이 배포된 함수를 불러보면!!!!!!

contact_list = w3.eth.contract(address=transaction_receipt.contractAddress, abi=abi)
print(contact_list.functions.greet().call())

아래와 같이 Hello World 가 잘 프린트 됨을 알수 있습니다!!^^

 

 

 

smart contract 함수 불러오기 성공!!!

 

다음번에는 input 변수를 받는 Smart Contract을 학습해보아요!!

댓글