0

I am following this (python ethereum interface tutorial) and contracts to compile and deploy in ganache cli.

Till now i am able to compile and deploy contracts with web3.py,py-solc and ganache node. I have generated abi and contract address. Now i am writing an API which will save data in ethereum through abi JSON and address JSON as mentioned below.

{
    "contract_address": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "contract_abi": [
        {
            "constant": true,
            "inputs": [],
            "name": "creationBlock",
            "outputs": [
                {
                    "name": "",
                    "type": "uint256"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "name": "_greeting",
                    "type": "bytes32"
                }
            ],
            "name": "setGreeting",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [],
            "name": "destroy",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "owner",
            "outputs": [
                {
                    "name": "",
                    "type": "address"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": false,
            "inputs": [
                {
                    "name": "_newOwner",
                    "type": "address"
                }
            ],
            "name": "changeOwner",
            "outputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "greet",
            "outputs": [
                {
                    "name": "",
                    "type": "bytes32"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "constant": true,
            "inputs": [],
            "name": "greeting",
            "outputs": [
                {
                    "name": "",
                    "type": "bytes32"
                }
            ],
            "payable": false,
            "stateMutability": "view",
            "type": "function"
        },
        {
            "inputs": [],
            "payable": false,
            "stateMutability": "nonpayable",
            "type": "constructor"
        },
        {
            "anonymous": false,
            "inputs": [
                {
                    "indexed": true,
                    "name": "changer",
                    "type": "address"
                },
                {
                    "indexed": true,
                    "name": "_from",
                    "type": "bytes32"
                },
                {
                    "indexed": true,
                    "name": "_to",
                    "type": "bytes32"
                }
            ],
            "name": "GreetingChange",
            "type": "event"
        }
    ]
}

I want to save now data in blockchain but i have no idea how to save it as lack of ideal and easy tutorial with python interface supporting. I dont know how to pass data through smart contract from api so it would reflect in blockchain? Any advice please?

API to save data

class TestAPI(APIView):
    def post(self,*arg):
        w3 = Web3(HTTPProvider('http://127.0.0.1:7545'))
        w3.eth.defaultAccount = w3.eth.accounts[1]
        self.file = None
        try:
            self.file =  open("./static/file/deployment_variables.json", 'r')
        except FileNotFoundError:
            return HttpResponse("No contract is deployed")
        if self.file is not None:
            datastore = json.load(self.file)
            contract_abi = datastore["contract_abi"]
            contract_address = datastore["contract_address"]
            user = w3.eth.contract(address=contract_address, abi=contract_abi)
            pass

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.