Skip to content

Instantly share code, notes, and snippets.

View bertolo1988's full-sized avatar
🎯
Focusing

Tiago BΓ©rtolo bertolo1988

🎯
Focusing
View GitHub Profile
@bertolo1988
bertolo1988 / ProvableBetFunction.solidity
Created September 6, 2019 00:40
The bet function using ProvableAPI 0.5.
function bet(uint stake, uint spinUnder)external notOwner returns (uint spinUnderInput, uint stakeInput, uint prize, uint payout, uint previousBalance){
require(stake >= minBet && maxBet >= stake , 'stake is outside allowed limits');
require(spinUnder >= MIN_ROLL_UNDER && spinUnder <= MAX_ROLL_UNDER, 'spinUnder must be between or equal to 11 and 91');
require(balances[msg.sender] >= stake, 'insufficient balance to cover stake');
(payout, prize) = getPrizeAndPayout(stake, spinUnder);
require(balances[owner] >= (prize.sub(stake)), 'insufficient contract balance to cover the prize');
previousBalance = balances[msg.sender];
subtractAmountFromUser(stake);
uint QUERY_EXECUTION_DELAY = 0;
uint GAS_FOR_CALLBACK = 200000;
@bertolo1988
bertolo1988 / ProvableBetFunctionCallback.solidity
Created September 6, 2019 00:47
Callback called by Provable.
function __callback( bytes32 _queryId, string memory _result, bytes memory _proof ) public {
require(msg.sender == provable_cbAddress());
require(ongoingBets[_queryId].sender != address(0x0) , 'query does not exist');
if(provable_randomDS_proofVerify__returnCode( _queryId, _result, _proof) != 0){
revert();
} else {
uint ceiling = (MAX_INT_FROM_BYTE ** NUM_RANDOM_BYTES_REQUESTED) - 1;
uint randomNumber = uint(keccak256(abi.encodePacked(_result))) % ceiling;
uint spin = (randomNumber % 100) + 1;
Bet memory bet = ongoingBets[_queryId];
function memoize(func) {
var memo = {};
var slice = Array.prototype.slice;
return function () {
var args = slice.call(arguments);
if (args in memo) return memo[args];
else return (memo[args] = func.apply(this, args));
};
}
function deleteFbComments() {
let postDeleted = 0;
const CLICK_DELAY = 50;
function randomIntFromInterval(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function sleep(ms = 200) {
return new Promise((resolve) => setTimeout(resolve, ms));
function isPrime(num) {
for (let i = 2, s = Math.sqrt(num); i <= s; i++)
if (num % i === 0) return false;
return num > 1;
}
function getMapOfPrimeNumbersUpTo(n) {
const result = new Map();
for (let i = 0; i < n; i++) {
if (isPrime(i)) result.set(i, true);
@bertolo1988
bertolo1988 / how-to-clone-prototype-by-copying.js
Last active October 12, 2022 09:20
How to clone a javascript object without just referencing the prototype.
// How to clone a javascript object without just referencing the prototype
const assert = require('node:assert')
const input = { a: 1 }
// every prototype is an object with normal property descriptors
const prototypeDescriptors = Object.getOwnPropertyDescriptors(
Object.getPrototypeOf(input)
)
Method name string number boolean undefined null Symbol BigInt
cloneJSON βœ… βœ… βœ… ❌ βœ… ❌ ❌
structuredClone βœ… βœ… βœ… βœ… βœ… ❌ βœ…
underscoreContribClone βœ… βœ… βœ… βœ… βœ… βœ… βœ…
cloneNemisj βœ… βœ… βœ… βœ… βœ… βœ… βœ…
cloneTrincot βœ… βœ… βœ… βœ… βœ… βœ… βœ…
cloneKoolDandy βœ… βœ… βœ… βœ… βœ… βœ… βœ…
cloneRfdc βœ… βœ… βœ… βœ… βœ… βœ… βœ…
justClone βœ… βœ… βœ… βœ… βœ… βœ… βœ…
cloneDeep βœ… βœ… βœ… βœ… βœ… ❌ βœ…
Method name shallow object non shallow object circular references typeof constructor
cloneJSON βœ… βœ… ❌ βœ… βœ…
structuredClone βœ… βœ… βœ… βœ… βœ…
underscoreContribClone βœ… βœ… ❌ βœ… βœ…
cloneNemisj βœ… βœ… ❌ βœ… βœ…
cloneTrincot βœ… βœ… βœ… βœ… βœ…
cloneKoolDandy βœ… βœ… ❌ βœ… βœ…
cloneRfdc βœ… βœ… ❌ βœ… βœ…
justClone βœ… βœ… ❌ βœ… βœ…
cloneDeep βœ… βœ… ❌ βœ… βœ…
Method name getter setter enumerable non enumerable enumerable and writable non enumerable non writable and configurable non enumerable writable and non configurable non enumerable non writable and non configurable own symbol
cloneJSON ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
structuredClone ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
underscoreContribClone ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
cloneNemisj ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
cloneTrincot ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
cloneKoolDandy ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
cloneRfdc ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
justClone ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
cloneDeep ❌ ❌ βœ… ❌ ❌ ❌ ❌ ❌ ❌
Method name frozen sealed non extensible
cloneJSON ❌ ❌ ❌
structuredClone ❌ ❌ ❌
underscoreContribClone ❌ ❌ ❌
cloneNemisj ❌ ❌ ❌
cloneTrincot ❌ ❌ ❌
cloneKoolDandy ❌ ❌ ❌
cloneRfdc ❌ ❌ ❌
justClone ❌ ❌ ❌
cloneDeep ❌ ❌ ❌