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:
bumi 2018-04-02 13:25:03 +02:00
parent 8f8582be69
commit 78b6b2e14f
4 changed files with 134 additions and 2 deletions

63
config/seeds.js Normal file
View File

@ -0,0 +1,63 @@
let contractCalls = {
Operator: {
addContributor: [
// make sure to use an IPFS hash of one of the objects in ipfsContent
['0x24dd2aedd8a9fe52ac071b3a23b2fc8f225c185e', '0x272bbfc66166f26cae9c9b96b7f9590e095f02edf342ac2dd71e1667a12116ca', 18, 32, true], // QmQyZJT9uikzDYTZLhhyVZ5ReZVCoMucYzyvDokDJsijhj
['0xa502eb4021f3b9ab62f75b57a94e1cfbf81fd827', '0x9569ed44826286597982e40bbdff919c6b7752e29d13250efca452644e6b4b25', 18, 32, true] // QmYPu8zvtfDy18ZqHCviVxnKtxycw5UTJLJyk9oAEjWfnL
],
addProposal: [
[1, 23, 'QmQNA1hhVyL1Vm6HiRxXe9xmc6LUMBDyiNMVgsjThtyevs'],
[2, 42, 'QmQNA1hhVyL1Vm6HiRxXe9xmc6LUMBDyiNMVgsjThtyevs']
[2, 100, 'QmQNA1hhVyL1Vm6HiRxXe9xmc6LUMBDyiNMVgsjThtyevs']
],
vote: [
[1]
]
}
};
let ipfsContent = [
{
"@context": "https://schema.kosmos.org",
"@type": "Contributor",
"kind": "person",
"name": "Râu Cao",
"url": "https://sebastian.kip.pe",
"accounts": [
{
"site": "github.com",
"username": "skddc",
"uid": 842,
"url": "https://github.com/skddc/"
},
]
},
{
"@context": "https://schema.kosmos.org",
"@type": "Contributor",
"kind": "person",
"name": "Bumi",
"url": "https://michaelbumann.com",
"accounts": [
{
"site": "github.com",
"username": "bumi",
"uid": 318,
"url": "https://github.com/bumi/"
},
]
},
{
"@context": "https://schema.kosmos.org",
"@type": "Contribution",
"contributor": {
"ipfs": "QmQ2ZZS2bXgneQfKtVTVxe6dV7pcJuXnTeZJQtoVUFsAtJ"
},
"kind": "dev",
"description": "hacking hacking on kredits",
"url": "https://github.com/67P/kredits-web/pull/11",
"details": {}
}
]
module.exports = { contractCalls, ipfsContent };

View File

@ -8,6 +8,7 @@
}, },
"scripts": { "scripts": {
"build-json": "truffle compile && node ./scripts/build-json.js", "build-json": "truffle compile && node ./scripts/build-json.js",
"bootstrap": "truffle migrate --reset && truffle exec scripts/seeds.js && npm run build-json",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"repository": { "repository": {
@ -21,8 +22,9 @@
}, },
"homepage": "https://github.com/67P/truffle-kredits#readme", "homepage": "https://github.com/67P/truffle-kredits#readme",
"devDependencies": { "devDependencies": {
"zeppelin-solidity": "^1.7.0", "ganache-cli": "^6.0.3",
"ipfs-api": "^19.0.0",
"truffle": "^4.1.3", "truffle": "^4.1.3",
"ganache-cli": "^6.0.3" "zeppelin-solidity": "^1.7.0"
} }
} }

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}`) });
});
}