The Modern Scientist

The Modern Scientist aspires to connect builders & the curious to forward-thinking ideas. Either…

Follow publication

Deploy multi files smart contract with Python

The easiest way of deploy your smart contract is Remix. But if you want to deploy your smart contracts with Python, I hope this guide will help you.

Assume we have a smart contract man.sol, and it contains two contract in a file, like this:

pragma solidity ^0.8.0;
import "./SafeERC20.sol";
contract mainContract {
... (Any code can be here ...)
}
contract childContract {
... (Other code here)
}

So we have a directory like this:

  • main.sol
    -SafeERC20.sol
    -deploy.py

it doesn’t matter what is SafeERC20.sol, it can be any contract and our main.sol can import any other contract.

Know we need a python file to compile our smart contracts. Deploy.py is here:

import json
import os

import web3.eth
from web3 import Web3, HTTPProvider

from solcx import install_solc, set_solc_version,compile_standard
from dotenv import load_dotenv
#here install solidity version
install_solc('v0.8.0')
set_solc_version('v0.8.0')


file_path = "."
name = "main.sol"
input = {
'language': 'Solidity',
'sources': {
name: {'urls': [file_path + "/" + name]}},
'settings': {
'outputSelection': {
'*': {
'*': ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"],
},
'def': {name: ["abi", "evm.bytecode.opcodes"]},
}
}
}

output = compile_standard(input, allow_paths=file_path)

contracts = output["contracts"]

with open('compiled_code.json', "w") as file:
json.dump(output, file)

bytecode = contracts["SC-.sol"]["mainContract"]["evm"]["bytecode"]["object"]

abi = contracts["main.sol"]["mainContract"]["abi"]


# Deploy on local ganache
# w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
# chainId = 1337
# myAddress = "0x6235207DE426B0E3739529F1c53c14aaA271D..."
# privateKey = "0xdbe7f5a9c95ea2df023ad9......."

#Deploy on rinkeby infura rinkeby
w3 = Web3(Web3.HTTPProvider("https://rinkeby.infura.io/v3/......"))
chainId = 4

myAddress = "0xBa842323C4747609CeCEd164d61896d2Cf4..."
privateKey ="0x99de2de028a52668d3e94a00d47c4500db0afed3fe8e40..."

SCOnline = w3.eth.contract(abi=abi, bytecode=bytecode)

nonce = w3.eth.getTransactionCount(myAddress)

transaction = SCOnline.constructor().buildTransaction({
"gasPrice": w3.eth.gas_price, "chainId": chainId, "from": myAddress, "nonce": nonce
})

signedTrx = w3.eth.account.sign_transaction(transaction, private_key= privateKey)



txHash = w3.eth.send_raw_transaction(signedTrx.rawTransaction)

txReceipt = w3.eth.wait_for_transaction_receipt(txHash)

Done!
If there was a function in our main smart contract(getAddress(uint id)), we can use it in our python code like this:

deployedSC =w3.eth.contract(address=txReceipt.contractAddress, abi=abi)

newAddress = deployedSC.functions.getAddress(1).call()

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

The Modern Scientist
The Modern Scientist

Published in The Modern Scientist

The Modern Scientist aspires to connect builders & the curious to forward-thinking ideas. Either you are novice or expert, TMS will share contents that fulfils your ambition and interest. Write with us: shorturl.at/hjO39

Hamid Naghipour
Hamid Naghipour

Written by Hamid Naghipour

I am a developer and I love challenges

No responses yet

Write a response