Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment/submodule data and controls #12

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{
"root": true,
"env": {
"browser": true,
"es2021": true,
"jest": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"prettier"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["react", "react-hooks", "@typescript-eslint", "prettier"],
"rules": {
"react/react-in-jsx-scope": "off",
"camelcase": "error",
"spaced-comment": "error",
"quotes": ["error", "single"],
"no-duplicate-imports": "error",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-unused-vars": [
"warn", // or "error"
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_",
"caughtErrorsIgnorePattern": "^_"
}
]
},
"settings": {
"import/resolver": {
"typescript": {}
},
"react": {
"createClass": "createReactClass", // Regex for Component Factory to use,
// default to "createReactClass"
"pragma": "React", // Pragma to use, default to "React"
"fragment": "Fragment", // Fragment to use (may be a property of <pragma>), default to "Fragment"
"version": "detect", // React version. "detect" automatically picks the version you have installed.
// You can also use `16.0`, `16.3`, etc, if you want to override the detected value.
// It will default to "latest" and warn if missing, and to "detect" in the future
"flowVersion": "0.53" // Flow version
},
"propWrapperFunctions": [
// The names of any function used to wrap propTypes, e.g. `forbidExtraProps`. If this isn't set, any propTypes wrapped in a function will be skipped.
"forbidExtraProps",
{ "property": "freeze", "object": "Object" },
{ "property": "myFavoriteWrapper" },
// for rules that check exact prop wrappers
{ "property": "forbidExtraProps", "exact": true }
],
"componentWrapperFunctions": [
// The name of any function used to wrap components, e.g. Mobx `observer` function. If this isn't set, components wrapped by these functions will be skipped.
"observer", // `property`
{ "property": "styled" }, // `object` is optional
{ "property": "observer", "object": "Mobx" },
{ "property": "observer", "object": "<pragma>" } // sets `object` to whatever value `settings.react.pragma` is set to
],
"formComponents": [
// Components used as alternatives to <form> for forms, eg. <Form endpoint={ url } />
"CustomForm",
{ "name": "Form", "formAttribute": "endpoint" }
],
"linkComponents": [
// Components used as alternatives to <a> for linking, eg. <Link to={ url } />
"Hyperlink",
{ "name": "Link", "linkAttribute": "to" }
]
},
"overrides": [{
"files": ["*.json"],
"rules": {
"quotes": "off",
"no-unused-expressions": "off"
}
}]
}
33 changes: 30 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
dist/
node_modules/
.env
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build
/dist

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log
yarn-debug.log
yarn-error.log
/test-results/
/playwright-report/
/dist-pw/
/.prettierignore
# Local Netlify folder
.netlify
.vscode/settings.json
3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-env"]
}
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "ambient-data-and-controls",
"name": "@crocswap-libs/ambient-utils",
"version": "0.1.0",
"license": "MIT",
"author": "surudhb",
Expand Down Expand Up @@ -38,9 +38,15 @@
"trailingComma": "es5"
},
"jest": {
"testEnvironment": "node"
"testEnvironment": "node",
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
"transformIgnorePatterns": [
"node_modules/(?!(wagmi|@wagmi/core)/)"
]
},
"peerDependencies": {},
"engines": {
"node": ">=12"
},
Expand All @@ -55,17 +61,24 @@
}
],
"devDependencies": {
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.23.3",
"@size-limit/preset-small-lib": "^10.0.1",
"@tsconfig/recommended": "^1.0.3",
"@types/d3": "^7.1.0",
"@typescript-eslint/eslint-plugin": "^5.22.0",
"@typescript-eslint/parser": "^5.22.0",
"babel-jest": "^29.7.0",
"dts-cli": "^2.0.3",
"eslint": "^8.53.0",
"husky": "^8.0.3",
"lint-staged": "^15.0.2",
"prettier": "^3.0.3",
"size-limit": "^10.0.1",
"ts-jest": "^29.1.1",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
"typescript": "^4.4.2"
},
"dependencies": {
"ethers": "^6.8.1"
}
}
69 changes: 0 additions & 69 deletions src/api/addressManagement/fetchENSAddresses.ts

This file was deleted.

28 changes: 28 additions & 0 deletions src/api/fetchAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable camelcase */
import { ANALYTICS_URL } from '../constants';
import { memoizePromiseFn } from '../dataLayer/functions/memoizePromiseFn';

export const fetchEnsAddress = async (address: string) => {
try {
const response = await fetch(
ANALYTICS_URL +
new URLSearchParams({
service: 'run',
config_path: 'ens_address',
include_data: '0',
address,
})
);
const result = await response.json();
return result?.ens_address;
} catch (e) {
console.warn(e);
return null;
}
};

export type FetchAddrFn = (address: string) => Promise<string | undefined>;

export function memoizeFetchEnsAddress(): FetchAddrFn {
return memoizePromiseFn(fetchEnsAddress) as FetchAddrFn;
}
43 changes: 43 additions & 0 deletions src/api/fetchAprEst.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { capitalConcFactor, CrocEnv } from '@crocswap-libs/sdk';
import { Provider } from '@ethersproject/providers';

// Approximately 24 hours in Ethereum. TODO make this generalizable across
// chains.
const FIXED_APY_N_BLOCK_LOOKBACK = 7000;

export async function estimateFrom24HrAmbientApr(
base: string,
quote: string,
crocEnv: CrocEnv,
provider: Provider,
lastBlockNumber: number
): Promise<number> {
if (!lastBlockNumber) {
lastBlockNumber = await provider.getBlockNumber();
}

const lookbackBlockNum = lastBlockNumber - FIXED_APY_N_BLOCK_LOOKBACK;
const lookbackBlock = provider.getBlock(lookbackBlockNum);

const nowGrowth = crocEnv.pool(base, quote).cumAmbientGrowth(lastBlockNumber);
const prevGrowth = crocEnv.pool(base, quote).cumAmbientGrowth(lookbackBlockNum);

const periodGrowth = (await nowGrowth) - (await prevGrowth);
const timeSecs = Date.now() / 1000 - (await lookbackBlock).timestamp;

const timeYears = timeSecs / (365 * 24 * 3600);
return periodGrowth / timeYears;
}

export async function estimateFrom24HrRangeApr(
rangePercent: number,
base: string,
quote: string,
crocEnv: CrocEnv,
provider: Provider,
lastBlockNumber: number
): Promise<number> {
const ambientApy = estimateFrom24HrAmbientApr(base, quote, crocEnv, provider, lastBlockNumber);
const concFactor = capitalConcFactor(1.0, 1.0 - rangePercent, 1.0 + rangePercent);
return (await ambientApy) * concFactor;
}
23 changes: 23 additions & 0 deletions src/api/fetchBlockTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ethers } from 'ethers';
import { memoizeProviderFn } from '../dataLayer/functions/memoizePromiseFn';

export async function fetchBlockTime(
provider: ethers.providers.Provider,
blockNumber: number
): Promise<number | undefined> {
try {
const block = provider.getBlock(blockNumber);
return (await block).timestamp;
} catch (error) {
return undefined;
}
}

export type FetchBlockTimeFn = (
provider: ethers.providers.Provider,
blockNumber: number
) => Promise<number | undefined>;

export function memoizeFetchBlockTime(): FetchBlockTimeFn {
return memoizeProviderFn(fetchBlockTime) as FetchBlockTimeFn;
}
Loading