Skip to content

Instantly share code, notes, and snippets.

View andreyluiz's full-sized avatar

Andrey Luiz andreyluiz

View GitHub Profile
@andreyluiz
andreyluiz / onboard.ts
Created June 10, 2024 12:14
Web3Auth initialization
import { CHAIN_NAMESPACES, WALLET_ADAPTERS, WEB3AUTH_NETWORK } from '@web3auth/base';
import { EthereumPrivateKeyProvider } from '@web3auth/ethereum-provider';
import { MetamaskAdapter } from '@web3auth/metamask-adapter';
import { Web3Auth } from '@web3auth/modal';
import { OpenloginAdapter } from '@web3auth/openlogin-adapter';
import { ethers } from 'ethers';
import wallet from './store/wallet';
const clientId = '...';
@andreyluiz
andreyluiz / repeated.js
Created August 26, 2022 11:47
Repeated words
function atLeastTwice(word) {
const wordArray = word.split('');
const repeatedWords = {};
wordArray.forEach((char) => {
if (repeatedWords[char]) {
repeatedWords[char] += 1;
} else {
repeatedWords[char] = 1;
}
});
@andreyluiz
andreyluiz / try.js
Created August 3, 2022 14:48
Try 10 times to fetch the event.
await tryAgain(10, async () => {
const result = await fetchEvent(parseInt(requestId, 10));
if (result) {
setValue(result);
return true;
}
return false;
});
@andreyluiz
andreyluiz / process.js
Last active August 3, 2022 14:47
Processing the result of the fetch call.
const { data, errors } = await response.json();
if (errors) {
throw errors[0];
}
if (data && Array.isArray(data.getEntries)) {
const request = data.getEntries.find((e) =>
e.event.args.some((a) => a[0] === "requestId" && parseInt(a[1], 16) === requestId)
);
if (request) {
const arg = request.event.args.find((a) => a[0] === "result");
@andreyluiz
andreyluiz / fetch.js
Created August 3, 2022 14:44
Calling the GraphQL API.
const endpoint = "https://api.kenshi.io/index/graphql";
const apiKey = process.env.REACT_APP_DEEP_INDEX_API_KEY;
const owner = process.env.REACT_APP_OWNER;
const contract = process.env.REACT_APP_ADDRESS;
const query = `{
getEntries(blockchain: "avalanche-fuji", apikey: "${apiKey}", owner: "${owner}", address: "${contract}", event: "Rolled") {
event {
args,
name
@andreyluiz
andreyluiz / roll.js
Created August 2, 2022 21:28
Rolling the dice.
export const rollDice = async (contract, account) => {
const sides = 20;
const callResult = await contract.methods.roll(sides).send({ from: account });
const requestEvent = callResult.events?.Request;
if (!requestEvent) {
throw new Error("No request event.");
}
const requestId = requestEvent.returnValues.requestId;
@andreyluiz
andreyluiz / config.js
Created August 2, 2022 21:25
Calling the VRF config function.
const hasConfigFlag = () => {
return localStorage.getItem(CONFIG_FLAG) === "true";
};
const setConfigFlag = () => {
localStorage.setItem(CONFIG_FLAG, "true");
};
export const maybeConfig = async (contract, account) => {
if (!hasConfigFlag()) {
@andreyluiz
andreyluiz / roll.jsx
Last active September 1, 2022 17:53
How the Roll function will be structured.
const roll = async () => {
// Step 1: roll the dice
const requestId = await rollDice(contract, accounts[0]);
// Step 2: try to listen for the final event for 10 times
await tryAgain(10, async () => {
const result = await fetchEvent(parseInt(requestId, 10));
if (result) {
setValue(result);
//SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.15;
import "@kenshi.io/vrf-consumer/contracts/VRFConsumer.sol";
contract DiceRoller is VRFConsumer {
struct Roll {
address player;
uint8 size;
}
@andreyluiz
andreyluiz / Text.js
Created July 11, 2018 16:17
The Text component.
// @flow
import React from 'react';
import { Text as RNText, StyleSheet } from 'react-native';
type Props = {
children: string,
color: string,
style: object
};