Published
Edited
Jan 16, 2020
Fork of dType 2
Insert cell
md`# dType Create`
Insert cell
// mutable dTOptions = ({});
Insert cell
mutable tObj = ({value: [ ["0x00", "natural",new BN(55),new BN(55)], [ "0x01aa", "0x01bb"], "balalaika", new BN(1), ()=>{console.log("done!")}], type: { group1: { some:"address", type:"enum:types", signed: "int56", unsigned:"int48"}, somearr:"bytes2[]", name: "string", "sure?":"bool", makeLog:"jsfunction"}})

Insert cell
mutable tObj2 = ({value: [ ["0x00", "natural",new BN(55),new BN(55)], [[ "0x01aa", "0x01bb"], [ "0x01cc", "0x01dd"]], "balalaika", new BN(1), ()=>{console.log("done!")}], type: { group1: { some:"address", type:"enum:types", signed: "int56", unsigned:"int48"}, somearr:"bytes2[][]", name: "string", "sure?":"bool", makeLog:"jsfunction"}})

Insert cell
// mutable tObj3 = ({value: [[[ new BN(55),new BN(1)], [ new BN(3), new BN(10)]], [[true, false]], "balalaika", new BN(1), ()=>{console.log("done!")}], type: { somearr:"uint256[][]", bools: "bool[][]", name: "string", "sure?":"bool", makeLog:"jsfunction"}})

mutable tObj3 = ({value: [[[ new BN(55),new BN(1)], [ new BN(3), new BN(10)]], "balalaika", new BN(1), ()=>{console.log("done!")}], type: { somearr:"uint256[][]", name: "string", "sure?":"bool", makeLog:"jsfunction"}})


Insert cell
addType = ({
value: [ "label1", "uint16", new BN(0), new BN(0)],
type: { label:"string", "Component":"enum:types", "x1 from T[x1][x2]":"uint8", "x2 from T[x1][x2]":"uint8"}
})
Insert cell
dT.composites["type_component"]
Insert cell
addType2 = ({
value: ["Lorrr", [ [ "label1", "uint16", [new BN(0), new BN(0)]], [ "label2", "uint16", [new BN(0), new BN(0)]]]],
type: {name: "string", components: "type_component[]"}
})
Insert cell
{
yield html `<div id="control3" style="height:500px;"><div id="control2" style="width:50%;"></div><div id="control1" style="width:50%;"></div></div>`
showControl(addType2, "control1", {randomize: true, gui: {align: "left"}, onChange: showTypeInstance}) // tObj
// showControl(tObj3, "control2", {gui: {align: "right"}, onChange: console.log})
showTypeInstance(addType2);
}
Insert cell
// dT.t.apply({value: ["aaaa", 10], types: {label: 'string', label2: 'uint256'}, name: "smth"}, 'random');
Insert cell
mutable instanceSolidity = '';
Insert cell
function showTypeInstance(odef) {
const oinst = typeInstance(odef);
showControl(oinst, "control2", {
gui: {align: "right"},
buttons: [
{type: 'button', label: 'save', action: () => {
dT.t.setComposite(oinst.name, oinst.type);
}}
],
onChange: console.log
});
mutable instanceSolidity = solSource(oinst);
}
Insert cell
function solSource(oinst) {
console.log('===== oinst', oinst);
function resolveTuple(tuple, comps = {}) {
console.log('resolveTuple', tuple, comps, comps[tuple.name]);
if (comps[tuple.name]) return comps;
let subtypes = [];
Object.keys(tuple.type).forEach(label => {
const type = tuple.type[label];
subtypes.push(`${type} ${label};`);
let bareType = type;
while (dT.t.getArrayType({type: bareType})) {
bareType = dT.t.getArrayType({type: bareType});
}
const composite = dT.t.getComposite(bareType);
if (composite) {
comps = resolveTuple({type: composite, name: bareType}, comps);
}
})
const source = `struct ${tuple.name} {
${subtypes.join('\n ')}
}`;
comps[tuple.name] = source;
return comps;
}
const composites = resolveTuple(oinst);
console.log('composites', composites);
return Object.values(composites).join('\n\n');
}
Insert cell
function typeInstance(odef) {
console.log('typeInstance', odef);
function keyval(typed) {
const values = {};
Object.keys(typed.type).forEach((key, i) => {
values[key] = typed.value[i];
});
return values;
}
const values = keyval(odef);
let oinst = {value: [], type: {}, name: values.name};
console.log('------ values', values)
values.components.forEach(component => {
const cvalues = keyval({type: dT.composites["type_component"], value: component});
console.log('cvalues', cvalues);
let type = cvalues.Component;
if (cvalues.dimensions.length > 0) {
const dims = cvalues.dimensions.map(dim => {
if (dim.eq(new dT.BN(0))) return '';
return dim.toString();
}).join('][');
type += `[${dims}]`;
}
console.log('--- type', type);
oinst.type[cvalues.label] = type;
const val = dT.t.apply({type, value: null}, 'random');
console.log('------- random val', val);
oinst.value.push(val.value);
});
console.log('--- oinst', oinst);
return oinst;
}
Insert cell
md `## Set Types`
Insert cell
md `## Imports`
Insert cell
guify = require('guify@0.12.0/lib/guify.min.js');
Insert cell
BN = require('https://bundle.run/bn.js@4.11.8');
Insert cell
S = require ('sanctuary');
Insert cell
_ = require ('sanctuary-def');
Insert cell
// dT = (await require('http://127.0.0.1:5000/dtype2/dist/dtype2.min.js')).default;
Insert cell
dtypeControls = (await require('http://127.0.0.1:5000/dtype-controls/dist/dtype-controls.min.js'));
Insert cell
showControl = dtypeControls.showControl;
Insert cell
dT = dtypeControls.dT;
Insert cell
html`<link href="https://fonts.googleapis.com/css?family=Roboto+Condensed&display=swap" rel="stylesheet">`
Insert cell
FontFaceObserver = require('https://bundle.run/fontfaceobserver@2.1.0')
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more