0

Instead of using truffle deploy, I want to create my own deploy.js script and run it using node deploy.js.

How can I import the classes/functions artifacts, describe and it, so we can do like so:

var Foo = artifacts.require('./Foo');

describe('Deploying', async () => {
    it('Deploying Foo', async () => {
        let foo = await Foo.new()   
    })  
})

1 Answer 1

1

You don't need describe, it and so forth, for this specific purpose.

These entities are part of the Mocha Framework, designated for testing, not for production.

For deployment, you can simply write your code and run it with NodeJS.

Note that you will have to require(web3) and instantiate it yourself, since Truffle will no longer "be there" to do it for you.

Also note that you will have to add web3 to your npm installation file (package.json), so that you can import it.

Also note that while you'd probably want to install the latest version of this module (currently [email protected]), Truffle still uses [email protected]. And unfortunately, there are many breaking changes between the two versions, so you will have to interact with web3 different from what you're probably used to (unless you stick to [email protected] in your npm installation file).

Here is an example of a deployment script using [email protected]:

let fs = require("fs");
let Web3 = require("web3");

let CLIENT_URL     = process.argv[2];
let GAS_PRICE      = process.argv[3];
let OWNER_ADDRESS  = process.argv[4];
let OWNER_PASSWORD = process.argv[5];

let web3 = new Web3(new Web3.providers.HttpProvider(CLIENT_URL));

async function call(transaction) {
    let success = await web3.eth.personal.unlockAccount(OWNER_ADDRESS, OWNER_PASSWORD);
    let gas = await transaction.estimateGas({from: OWNER_ADDRESS, gasPrice: GAS_PRICE});
    return await transaction.call({from: OWNER_ADDRESS, gasPrice: GAS_PRICE, gas: gas});
}

async function send(transaction) {
    let success = await web3.eth.personal.unlockAccount(OWNER_ADDRESS, OWNER_PASSWORD);
    let gas = await transaction.estimateGas({from: OWNER_ADDRESS, gasPrice: GAS_PRICE});
    return await transaction.send({from: OWNER_ADDRESS, gasPrice: GAS_PRICE, gas: gas});
}

async function deploy(contractName, contractArgs) {
    let abi = fs.readFileSync("../build/binaries/" + contractName + ".abi").toString();
    let bin = fs.readFileSync("../build/binaries/" + contractName + ".bin").toString();
    let contract = new web3.eth.Contract(JSON.parse(abi));
    let handle = await send(contract.deploy({data: "0x" + bin, arguments: contractArgs}));
    console.log(`${contractName} contract deployed at address ${handle._address}`);
    return handle;
}

async function run() {
    let arg1 = ...;
    let arg2 = ...;
    let arg3 = ...;
    let myContract = await deploy("MyContract", [arg1, arg2]);
    let retval  = await call(myContract.methods.func1(arg3));
    let receipt = await send(myContract.methods.func2(arg3));
}

run();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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