5

I have been developing a smart contract (ERC-721 compliant) which needs to be populated with lots of various pieces of data.

Approximately 300 ERC721 tokens need to be minted inside the contract at the point of deployment.

I have a json data structure which details all the required initial tokens and their data attributes to be minted inside the contract.

I have currently created a new migrations script e.g. 4_populated_tokens.js which truffle run when deploying to each network.

This always succeeds locally with ganache but when running against any testnet I this always seems to eventually fail, occasionally with 80% or more of the content, usually for the following reason:

Error encountered, bailing. Network state unknown. Review successful transactions manually.

I have tried adjusting the migration with some success by doing the following:

  • Only minting a single token at once i.e. serially instead of firing lots of inserts at once
  • Increasing the gas per transaction

There has been some success with this.

My question :

  • Is it correct to use truffle migrate to populate such large amounts of data, if not what is the preferred solution?

  • Do other people also have this problem using truffle to run in large amounts of data as part of a migration?

All help appreciated, thanks.

  • truffle is a general purpose tool, so if it makes sense to do it in Ethereum in general, it makes sense to do it with truffle too. "Network State unknown" looks like a connection problem. How are you connecting to the network? Via something like Infura or via your own node? – ivicaa Apr 3 '18 at 6:10
  • I am using Infra and HDWalletProvider – Jimbob1987 Apr 3 '18 at 9:09
  • Then I suppose you‘re maybe sending transactions too fast and some quota mechanism at infura side is triggered. Try to add a sleep between sends and see of it helps. – ivicaa Apr 3 '18 at 9:11
1

You can use truffle exec to do extra setup after deployment.

Often you may want to run external scripts that interact with your contracts. Truffle provides an easy way to do this, bootstrapping your contracts based on your desired network and connecting to your Ethereum client automatically per your project configuration.

Source

Below is the example from a Truffle blog post:

1 - Creating a file called sampletx.js and save it in the root of your project (the same directory as your truffle.js file). Then fill it with this code:

var SimpleStorage = artifacts.require("SimpleStorage");

module.exports = function(done) {
    console.log("Getting deployed version of SimpleStorage...")
    SimpleStorage.deployed().then(function(instance) {
        console.log("Setting value to 65...");
        return instance.set(65, {
            privateFor: ["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]
        });
    }).then(function(result) {
        console.log("Transaction:", result.tx);
        console.log("Finished!");
        done();
    }).catch(function(e) {
        console.log(e);
        done();
    });
};

This code does two things: First, it asks Truffle to get our contract abstraction for the SimpleStorage contract. Then, it finds the deployed contract and sets the value managed by SimpleStorage to 65, using the contract's set() function.

The privateFor parameter is not relevant to you here and you can omit that. I copy and paste it from the source.

2 - Run this code using truffle exec:

$ truffle exec sampletx.js

Your output should look something like this (your transaction ID will be different though):

Using network 'development'.

Getting deployed version of SimpleStorage...
Setting value to 65...
Transaction: 0x0a7a661e657f5a706b0c39b4f197038ef0c3e77abc9970a623327c6f48ca9aff
Finished!

Here is a blog post from the Truffle creator about truffle exec.

Hope this helps.

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.