Add seeds script

Allows to seed the smart contract with development data specified in
config/seeds.js

Usage:

truffle exec scripts/seeds.js

or

npm run bootstrap which runs them all:
* truffle migrate --reset
* truffle exec scripts/seeds.js
* npm run build-json
This commit is contained in:
2018-04-02 13:25:03 +02:00
parent 8f8582be69
commit 78b6b2e14f
4 changed files with 134 additions and 2 deletions

35
scripts/multihash.js Normal file
View File

@@ -0,0 +1,35 @@
var bs58 = require('bs58');
function getBytes32FromMultiash(multihash) {
const decoded = bs58.decode(multihash);
return {
digest: `0x${decoded.slice(2).toString('hex')}`,
hashFunction: decoded[0],
size: decoded[1],
};
}
function getMultihashFromBytes32(multihash) {
const { digest, hashFunction, size } = multihash;
if (size === 0) return null;
// cut off leading "0x"
const hashBytes = Buffer.from(digest.slice(2), 'hex');
// prepend hashFunction and digest size
//const multihashBytes = new (hashBytes.constructor)(2 + hashBytes.length);
const multihashBytes = new Buffer(2 + hashBytes.length);
console.log(hashBytes.constructor);
multihashBytes[0] = hashFunction;
multihashBytes[1] = size;
multihashBytes.set(hashBytes, 2);
return bs58.encode(multihashBytes);
}
var m = getBytes32FromMultiash(process.argv[2]);
console.log(m)

32
scripts/seeds.js Normal file
View File

@@ -0,0 +1,32 @@
const path = require('path');
const seeds = require(path.join(__dirname, '..', '/config/seeds.js'));
const IPFS = require('ipfs-api');
var ipfs = IPFS({host: 'localhost', port: '5001', protocol: 'http'})
module.exports = function(callback) {
const Registry = artifacts.require('./Registry.sol');
const contracts = {};
Registry.deployed().then((registry) => {
Object.keys(seeds.contractCalls).forEach(async (contract) => {
var address = await registry.getProxyFor(contract);
console.log(`Using ${contract} at ${address}`);
contracts[contract] = await artifacts.require(contract).at(address);
Object.keys(seeds.contractCalls[contract]).forEach((method) => {
seeds.contractCalls[contract][method].forEach((args) => {
console.log(`[Sending] ${contract}.#${method}(${JSON.stringify(args)})`);
contracts[contract][method](...args).then((result) => {
console.log(`[Result] ${contract}.${method}(${JSON.stringify(args)}) => ${result.tx}`);
});
});
});
});
});
seeds.ipfsContent.forEach((content) => {
ipfs.add(new ipfs.Buffer(JSON.stringify(content))).then((result) => { console.log(`[IPFS] added ${result[0].hash}`) });
});
}