Truffle: Does truffle not support backend programming

Created on 24 Jun 2016  路  4Comments  路  Source: trufflesuite/truffle

I can't find anywhere to write my backend code. Is truffle not support this? If that, build a damp based on truffle can be real hard just through javascript. Is there anyway can fix it ?

Most helpful comment

This issue is the only relevant search result for "truffle backend" so I'm leaving this example here, in case anyone stumbles upon this:

var contract = require('truffle-contract');

var Web3 = require('web3');
var provider = new Web3.providers.HttpProvider('http://localhost:8545');

// pass provider as an argument to use web3.eth.accounts etc.
var web3 = new Web3(provider);

var fs = require('fs');
var SilverCoin = contract(JSON.parse(fs.readFileSync('build/contracts/SilverCoin.json', 'utf8')));
SilverCoin.setProvider(provider);

function getBalance (callback) {
    SilverCoin.deployed().then(function(instance) {
        instance.balanceOf.call(web3.eth.accounts[0]).then(function(balance) {
            callback(balance.valueOf());
        });
    });
}

var http = require('http');
http.createServer(function(req, res) {
    getBalance(function(balance) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(String(balance));
    })
}).listen(8000);

npm run build bundles your files and saves them to build/ (build/app.js for example) and you can serve this to the browser through whatever framework you're using for the server.

All 4 comments

Hi @rjl493456442. This is web3.0. The contracts _are_ your backend. :)

Will close this because it doesn't fit into the Ethereum paradigm. You can read more about it in the Truffle docs as well as many resources online.

@tcoulter - Having a regular backend could definitely be useful since you don't want all interactions to happen on chain. Or maybe I'm missing something and you could shed some light on it.

Let's say you have a dapp that implements a standard buy-sell escrow contract. Wouldn't it be better if you could negotiate prices with the seller off chain, or any kind of communication really, and then place the final order using the contract? You don't want to be spending gas for discussions.

You could have a REST API on another server and use responses from there but it'd be great if there was an option to do this straight from truffle.

@rohithpr That was an old commit - things have changed a lot since then.

You can do this easily with Truffle 3. You'll use Truffle to write, deploy and test your contracts. Doing so will create .json files within your contracts ./build/contracts directory. In your backend, if it's Javascript, you can use those .json files in conjunction with truffle-contract to interact with your contracts from the backend. If you write your backend in a different language, those .json files have everything you need in order to interact with them.

We don't have examples that show this, but if you build something that's public we'd love to see it. Cheers!

This issue is the only relevant search result for "truffle backend" so I'm leaving this example here, in case anyone stumbles upon this:

var contract = require('truffle-contract');

var Web3 = require('web3');
var provider = new Web3.providers.HttpProvider('http://localhost:8545');

// pass provider as an argument to use web3.eth.accounts etc.
var web3 = new Web3(provider);

var fs = require('fs');
var SilverCoin = contract(JSON.parse(fs.readFileSync('build/contracts/SilverCoin.json', 'utf8')));
SilverCoin.setProvider(provider);

function getBalance (callback) {
    SilverCoin.deployed().then(function(instance) {
        instance.balanceOf.call(web3.eth.accounts[0]).then(function(balance) {
            callback(balance.valueOf());
        });
    });
}

var http = require('http');
http.createServer(function(req, res) {
    getBalance(function(balance) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(String(balance));
    })
}).listen(8000);

npm run build bundles your files and saves them to build/ (build/app.js for example) and you can serve this to the browser through whatever framework you're using for the server.

Was this page helpful?
0 / 5 - 0 ratings