Git Product home page Git Product logo

ether-deploy_contract's Introduction

Contract Deployment in Web3.py

Deploying contract in Rinkeby Test Network through Infura API

Requirements:

  • web3.py 4.7.2
  • API Key from infura.io
  • Source account and its private key
infuraApi = 'API'
account = 'SRC ACCOUNT'
key = 'PRIVATE KEY'

Contract:

pragma solidity ^0.4.22;

contract HelloWorld {
    string public message;

    constructor() public {
        message = "Hello, World!";

    }

    function setMessage(string text) public payable{
        require(msg.value >= 0.01 ether);
        message = text;
    }

}

A simple "Hello, World" program that allows to change its message which call be called with ".message().call()".

Compiling the Contract:

from utils import contractSource
from solc import compile_source

contractCompiled = compile_source(contractSource("HelloWorld"))
contractInterface = contractCompiled['<stdin>:HelloWorld']
Note: contractSource is a function defined in src/utils.py and its read the HelloWorld.sol file's content.

Creating the Contract:

helloWorld = w3.eth.contract(abi=contractInterface['abi'], bytecode=contractInterface['bin'])

contractInterface returns a dict with the Contracts inside of .sol file. In this cenario there is only one "HelloWorld".

Building the transaction and sending it:

Defining the transaction's parameters:
txParams = {
    'gas': 2000000,
    'gasPrice': w3.eth.gasPrice,
    'nonce': w3.eth.getTransactionCount(account),
    'chainId': 4,  # Rinkeby Test Network
    'from': account
}
Associating transaction's parameters with the Contract:
tx = helloWorld.constructor().buildTransaction(txParams)
Signing the transaction with creator account's private key:
signedTx = w3.eth.account.signTransaction(tx, key)
Sending the transaction to the Rinkeby Test Network:
txReceipt = w3.eth.sendRawTransaction(signedTx.rawTransaction)

If sendRawTransaction is successful it returns a hash: 0xea1bb31684fa63f13008f11c3ddbfed444835e9e8d0a279ff806e52b8e527b95

which can be used to get the transaction's detail. Check in rinkeby.etherscan.io.

Getting transaction's detail:
receipt = w3.eth.waitForTransactionReceipt(txReceipt)

The function above returns the following dictionary:

AttributeDict({'blockHash': HexBytes('0x68dc9bc28a5e8fa2f5ff6325f13e8bd2f0560ca543bf011d71f9e1ed00f86e5c'),
 'blockNumber': 3224432,
 'contractAddress': '0x327fE98DaF48836e982396cF3cf4Bf468Ca11Ea8',
 'cumulativeGasUsed': 5460633,
 'from': '0xf31a44312c4cc8cfd207e5baa654f5597e461552',
 'gasUsed': 282543,
 'logs': [],
 'logsBloom': HexBytes('a lot of zeros'),
 'status': 1,
 'to': None,
 'transactionHash': HexBytes('0xea1bb31684fa63f13008f11c3ddbfed444835e9e8d0a279ff806e52b8e527b95'),
 'transactionIndex': 9})

The 'contractAddress' field will be used to interact with the deployed contract in the next section.

Defining the interface and interacting with it:

helloWorldInstance = w3.eth.contract(
   receipt.contractAddress,
   abi=contractInterface['abi'],
)
helloWorldInstance.functions.message().call() # returns "Hello, World!"

ether-deploy_contract's People

Contributors

varugasu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.