Published
Edited
Jun 22, 2021
Importers
Insert cell
Insert cell
Insert cell
Insert cell
options = [
{ value: 0, label: "0 bp" },
{ value: 25, label: "25 bp" },
{ value: 50, label: "50 bp" },
{ value: 75, label: "75 bp" },
{ value: 100, label: "100 bp" },
{ value: 125, label: "125 bp" }
]
Insert cell
Insert cell
Insert cell
rankings
Insert cell
Insert cell
optionId = Bignumber.fromBuffer(rankingsByteArray).toString()
Insert cell
md`If we convert the byte array into an integer, we get an option id we can represent with a uint256 on-chain. In this case **${optionId}**`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Bignumber = {
const Bignumber = await require('bignumber.js');
// https://github.com/LiskHQ/lisk-sdk/blob/01bf90e32cd336417af260f4969fd12659bf5691/helpers/bignum.js
Bignumber.prototype.toBuffer = function(opts) {
var abs = this.abs();
var isNeg = this.lt(0);
var buf;
var len;
var ret;
var endian;
var hex = this.toString(16);
var size;
var hx;

if (!opts) {
opts = {};
}

endian = { 1: 'big', '-1': 'little' }[opts.endian] || opts.endian || 'big';

if (hex.charAt(0) === '-') {
throw new Error(
'Converting negative numbers to Buffers not supported yet'
);
}

size = opts.size === 'auto' ? Math.ceil(hex.length / 2) : opts.size || 1;

len = Math.ceil(hex.length / (2 * size)) * size;
buf = Buffer.alloc(len);

// Zero-pad the hex string so the chunks are all `size` long
while (hex.length < 2 * len) {
hex = `0${hex}`;
}

hx = hex.split(new RegExp(`(.{${2 * size}})`)).filter(s => s.length > 0);

hx.forEach((chunk, i) => {
for (var j = 0; j < size; j++) {
var ix = i * size + (endian === 'big' ? j : size - j - 1);
buf[ix] = parseInt(chunk.slice(j * 2, j * 2 + 2), 16);
}
});

return buf;
};

// https://github.com/LiskHQ/lisk-sdk/blob/01bf90e32cd336417af260f4969fd12659bf5691/helpers/bignum.js
Bignumber.fromBuffer = function(buf, opts) {
if (!opts) {
opts = {};
}

var endian =
{ 1: 'big', '-1': 'little' }[opts.endian] || opts.endian || 'big';

var size = opts.size === 'auto' ? Math.ceil(buf.length) : opts.size || 1;

if (buf.length % size !== 0) {
throw new RangeError(
`Buffer length (${buf.length}) must be a multiple of size (${size})`
);
}

var hex = [];
for (var i = 0; i < buf.length; i += size) {
var chunk = [];
for (var j = 0; j < size; j++) {
chunk.push(buf[i + (endian === 'big' ? j : size - j - 1)]);
}

hex.push(chunk.map(c => (c < 16 ? '0' : '') + c.toString(16)).join(''));
}

return new Bignumber(hex.join(''), 16);
};

return Bignumber;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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