Skip to content

Instantly share code, notes, and snippets.

@iamwilhelm
Created August 4, 2021 03:33
Show Gist options
  • Save iamwilhelm/d56bee80bf5a9629314964d580f19165 to your computer and use it in GitHub Desktop.
Save iamwilhelm/d56bee80bf5a9629314964d580f19165 to your computer and use it in GitHub Desktop.
import * as fs from "fs/promises";
import Knex from "knex";
const Web3 = require("web3");
import { Refract, Component, Fiber } from "./refract";
/******* custom hooks ******/
export const useFetchJson = filePath => {
let [json, setJson] = Refract.useState("useFetchJson", null);
Refract.useEffect(
"useFetchJson",
() => {
(async () => {
console.log(`loading ${filePath}...`);
const result = await fs.readFile(filePath);
// TODO use simdjson
const parsed = JSON.parse(String(result));
setJson(parsed);
console.log(`loaded ${filePath}`);
})();
},
[filePath]
);
return json;
};
export const useWeb3 = (providerUri: string) => {
let [web3, setWeb3] = Refract.useState("useWeb3", null);
Refract.useEffect(
"useWeb3",
() => {
console.log("connecting to web3 provider...");
setWeb3(new Web3(providerUri));
console.log(`connected to web3 ${providerUri}`);
},
[providerUri]
);
return web3;
};
export const useCompoundContract =
(web3, mainnetAbi, mainnet) => cTokenName => {
let [contract, setContract] = Refract.useState("useCompoundContract", null);
Refract.useEffect(
"useCompoundContract",
() => {
if (!web3 || !mainnetAbi || !mainnet) return;
const contract = new web3.eth.Contract(
mainnetAbi[cTokenName],
mainnet.Contracts[cTokenName]
);
setContract(contract);
console.log(`contract created for ${cTokenName}`);
},
[web3, mainnet, mainnetAbi, cTokenName]
);
return contract;
};
export const useBlockHeader = web3 => {
let [blockHeader, setBlockHeader] = Refract.useState("useBlockHeader", null);
Refract.useEffect(
"useBlockHeader",
() => {
if (!web3) return;
console.log("subscribing to block headers...");
web3.eth.subscribe("newBlockHeaders").on("data", blockHeader => {
setBlockHeader(blockHeader);
console.log("new block...", blockHeader);
});
},
[web3]
);
return blockHeader;
};
export const useBorrowAndSupplyRate = (contract, blockHeader) => {
let [borrowRate, setBorrowRate] = Refract.useState("useRates", null);
let [supplyRate, setSupplyRate] = Refract.useState("useRates", null);
Refract.useEffect(
"useRates",
() => {
if (!contract || !blockHeader) return;
(async function () {
console.log("getting APY rates...");
const [brate, srate] = await Promise.all([
contract.methods.borrowRatePerBlock().call(),
contract.methods.supplyRatePerBlock().call()
]);
setBorrowRate(brate);
setSupplyRate(srate);
console.log("APY rates fetched");
})();
},
[contract, blockHeader]
);
return [borrowRate, supplyRate];
};
export const useKnex = dbConfig => {
let [knex, setKnex] = Refract.useState("useKnex", null);
Refract.useEffect(
"useKnex",
() => {
const kx = Knex(dbConfig);
console.log(`Set knex configuration`);
setKnex(kx);
},
[]
);
return knex;
};
// Persist to the db.
// - keys: how to identify a unique record. If these are all the same, the persistance
// doesn't run.
//
// TODO Should switch to an upsert
// TODO need to implement error handling and retries
// TODO would this be a candidate effect for "rendering"?
export const usePersist = (knex, record, keys) => {
let values = keys.map(key => record && record[key]);
Refract.useEffect(
"usePersist",
() => {
if (!knex || !record) return;
(async () => {
console.log(`inserting record:`, record);
await knex("annual_percentage_yields").insert(record);
// inserted
})();
},
values
);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment