5

I have an existing node/graphql/mongodb/react/redux project that is in beta and I would like to integrate Ethereum smart contracts to be able to save transaction information into the block chain. Is this possible? If so, how do I do it?

I am already have a working knowledge of Solidity and Truffle and I have built a couple of sample projects and deployed those to a private network.

Is it better to start from scratch?

2

The easiest way to manage a JavaScript application that talks to the blockchain is to use a development framework and deployment tool like Truffle.

Truffle takes care of giving you a way to run tests and migrations, and manage your contract definitions, including which addresses they are deployed to on each network. It also provides good tutorials that will help you learn to write applications that talk to the blockchain.

The most common examples of using Truffle will use its own build library for your whole application, but if you already have an existing React application, you probably already have your own build process using browserify, webpack or some other tool. In theory this can be integrated seamlessly with truffle, but it may be simpler, having got used to how Truffle works, to simply put it in a sub-directory of your own project and reference its libraries and the contract json files it builds directly.

See this project for an example. The original truffle directory contains contracts, migrations and contract tests, and running truffle migrate from inside that directory will deploy the contracts to the blockchain and store their definitions under its build directory. The truffle.js file defines the network it will deploy to, but not (unlike most Truffle tutorials) the web application HTML and JavaScript build process, since this is handled by the application's original system as defined in its package.json. The main front-end JavaScript loads the truffle-generated contract definitions, and interacts with them using tools found in the truffle-contract library.

  • I've accepted this as the answer because it uses truffle, which I'm familiar with to integrate an existing code base with smart contracts. Thank you for your feedback. – Jason Cochran Aug 16 '17 at 2:57
2

Example below is not exactly for saving transaction info on blockchain. But for invoking a transfer function written in smart contract from nodejs code. Hope this helps though.

1> Contract:

    contract SmartToken {
//store mapping of address and tokens (like a hashmap)
        mapping(address => uint) tokens;

        event OnamountChanged(address indexed _fromAddress, uint amount);

//transfer amount from one account to another account
        function transfer(address fromAddress, address toAddress, uint amount) returns (bool success) {
            //Cannot transfer to same account
            if (fromAddress == toAddress) {
                return false;
            }

//change balance of accounts
            if ((tokens[fromAddress] - amount) < 0) {
                //Transfer entire amount
                token[toAddress] += tokens[fromAddress];
                tokens[fromAddress] = 0;
            } else {
                token[toAddress] += amount;
                tokens[fromAddress] -= amount;
            }

            OnamountChanged(fromAddress, tokens[fromAddress]);
            return true;
        }
    }

2> nodejs:

    exports.transferTokens = function (transferData, next) {
    var rfr = require('rfr');
    //Copy abi into a json file and put it somewhere within nodejs app folder somewhere
    var abiJson = rfr('/app/account/queries/SmartToken.json');
    //Copy your contract address here
    var contAddr = "Your Contract Address";
    var bcHelper = rfr('/app/utils/bcHelper.js');

//Get connection to your blockchain - see helper code in #3 below
        var bcConn = bcHelper.myBcConnection();
//Reference to ABI JSON
        var contractAbi = bcConn.eth.contract(abiJson);
//Reference to contract address
        var contractRef = contractAbi.at(contAddr);
//fromAddress, toAddress, password and amount are passed from my UI in json format
        var fromAddress = transferData.fromAddress;
        var toAddress = transferData.toAddress;
        var password = transferData.password; //password of fromAccount
        var transferAmount = transferData.amount;

//Default account is required for smart contract write
        bcConn.eth.defaultAccount = bcConn.eth.coinbase;
//unlockAccount so that it is open to transfer required tokens
        bcConn.personal.unlockAccount (fromAddress, password, 15000, function(err, result) {
            if (err) {
                return next(err, null);
            }
    //invoke transfer function on smart contract in #1
            contractRef.transfer(fromAddress, toAddress, transferAmount, function (err, result) {
                if (err) return next(err, null);

                return next(null, result);
            });
        });
    };

3> helper file: bcHelper.js

    var Web3 = require('web3');
    var myBcInstance;

    exports.myBcConnection  = function() {
        if (myBcInstance) {
            return myBcInstance;
        } else {
//Connect to web3 instance
            myBcInstance = new Web3(new Web3.providers.HttpProvider('http://localhost:8042');
            return myBcInstance;
        }
    };
  • Thank you for your answer. "Example below is not exactly for saving transaction info on blockchain." Are you saying that it is not possible to save data into the blockchain from node? Also could you go into detail above each section of code, please? – Jason Cochran Aug 15 '17 at 21:41
  • It is possible to save data from node in one of the two ways. 1) by invoking a function on smart contract that will perform some write operation (see example above). 2) directly invoking a send transaction between accounts. What is is that you are trying to do? – Dhanesh Aug 15 '17 at 22:08
  • I have added some inline comments. Hope it will help. This covers pretty much the way one would invoke a smart contract from nodejs code for any function that writes data to blockchain. – Dhanesh Aug 15 '17 at 22:23
  • Thank you for the comments and code examples. I accepted the other answer as it takes advantage of truffle, which I'm already familiar with. Both answers are correct; the other was closer to what I was looking for. Thank you for taking the time answer. – Jason Cochran Aug 16 '17 at 2:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.