Skip to content

Instantly share code, notes, and snippets.

View MichalZalecki's full-sized avatar

Michał Załęcki MichalZalecki

View GitHub Profile
@MichalZalecki
MichalZalecki / web3Events.js
Created April 8, 2018 15:48
MetaMask and web3 1.0
import Web3 from "web3";
const web3 = new Web3(Web3.givenProvider);
const web3Events = new Web3(new Web3.providers.WebsocketProvider("wss://mainnet.infura.io/ws"));
const Contract = new web3.eth.Contract(ABI, ADDRESS);
const ContractEvents = new web3Events.eth.Contract(ABI, ADDRESS);
@MichalZalecki
MichalZalecki / ScalableRewardDistribution.sol
Last active November 17, 2018 10:47
Scalable Reward Distribution implementation based on http://batog.info/papers/scalable-reward-distribution.pdf paper
pragma solidity 0.4.24;
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
/// @title Scalable Reward Distribution
/// @dev ref. http://batog.info/papers/scalable-reward-distribution.pdf
contract ScalableRewardDistribution {
using SafeMath for uint256;
import { performance } from "perf_hooks";
import { SHA256 as sha256 } from "crypto-js";
function calculateTarget(difficulty: number): number {
return Math.ceil(2 ** 256 / difficulty);
}
function padLeft(str: string, to: number, chr: string): string {
return str.length < to ? padLeft(chr + str, to, chr) : str;
}
const SimpleToken = artifacts.require("./SimpleToken.sol");
const PresaleCrowdsale = artifacts.require("./02-presale-multiple-rounds/PresaleCrowdsale.sol");
module.exports = (deployer, network, [owner]) => {
return deployer
.then(() => deployer.deploy(SimpleToken, "Tooploox", "TPX", 18, 21000000))
.then(() => deployer.deploy(PresaleCrowdsale, 10000, owner, SimpleToken.address, owner))
.then(() => SimpleToken.deployed())
.then(token => token.increaseApproval(PresaleCrowdsale.address, 1000000 * (10 ** 18)));
};
@MichalZalecki
MichalZalecki / utils.js
Last active November 29, 2017 22:06
Native implementation of common helper functions
function toPairs(obj) {
return Object.entries(obj); // ES2017
}
describe("toPairs", () => {
it("transforms object to key-value pairs", () => {
expect(toPairs({ foo: "bar", foz: "baz" })).toEqual([
["foo", "bar"],
["foz", "baz"]
]);
@MichalZalecki
MichalZalecki / validateType.js
Last active May 5, 2017 10:03
Filter uploaded files by file type
export default function validateType(accepts) {
const typeExps = accepts.split(/ *, */).map(type => new RegExp(type));
return file => Boolean(typeExps.find(exp => exp.test(file.type)));
}
// function onChange({ target }) {
// const acceptedFiles = target.files.filter(validateType("images/*, application/pdf"));
// }
@MichalZalecki
MichalZalecki / CycleRun.js
Created July 9, 2016 19:13
Mount Cycle.js app as react component
import React, { PropTypes, Component } from "react";
import ReactDOM from "react-dom";
import { makeDOMDriver } from "@cycle/dom";
import { run } from "@cycle/xstream-run";
class CycleRun extends Component {
static propTypes = {
main: PropTypes.func.isRequired,
drivers: PropTypes.object,
};
root=true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true
@MichalZalecki
MichalZalecki / mixins-decorators.js
Last active May 29, 2016 19:23
Multiple mixins by using extends/decorators/virtual methods/stamps
function hi() {
console.log(`Hi ${this.name}!`);
}
function by() {
console.log(`By ${this.name}!`);
}
function mixin(fn, name = fn.name) {
return target => {
@MichalZalecki
MichalZalecki / .eslintrc.json
Last active May 29, 2016 15:38
Reasonable .eslint confiig
{
"extends": "airbnb",
"parser": "babel-eslint",
"settings": {
"import/resolver": {
"webpack": { "config": "./webpack/webpack.prod.config.js" }
}
},
"rules": {
"quotes": ["error", "double"],