Skip to content

Commit 8c32262

Browse files
committed
Restructure examples folder and add nodejs example in typescript
1 parent ea3597c commit 8c32262

19 files changed

+192
-23
lines changed

.eslintrc.js

+6
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,11 @@ module.exports = {
3636
files: ["*.test.{js,ts,tsx}", "test.{js,ts,tsx}"],
3737
env: { jest: true },
3838
},
39+
{
40+
files: ["examples/**/*.{js,ts,tsx}"],
41+
rules: {
42+
"@typescript-eslint/explicit-module-boundary-types": "off",
43+
},
44+
},
3945
],
4046
};

examples/EntryInfo.tsx renamed to examples/development/EntryInfo.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
2-
import type { Entry } from "../src";
3-
import { DataStatus } from "../src/cache/DataStatus";
2+
import type { Entry } from "../../src";
3+
import { DataStatus } from "../../src/cache/DataStatus";
44

55
export function EntryInfo<T>({
66
entry,

examples/Helpers.tsx renamed to examples/development/Helpers.tsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React, { useEffect, useMemo } from "react";
2-
import { useAssetsFullInfo, useAssetsPrices } from "../src/react";
3-
import { useAssetsInfo } from "../src/react";
4-
import { useAddressLoans } from "../src/react";
2+
import { useAssetsFullInfo, useAssetsPrices } from "../../src/react";
3+
import { useAssetsInfo } from "../../src/react";
4+
import { useAddressLoans } from "../../src/react";
55
import { EntryInfo } from "./EntryInfo";
66
import { VStack } from "./VStack";
7-
import { TEST_ADDRESS } from "./config";
8-
import { client, useAddressAssets } from "../src";
7+
import { TEST_ADDRESS } from "../config";
8+
import { client, useAddressAssets } from "../../src";
99

1010
const USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
1111
const UNI = "0x1f9840a85d5af5bf1d1762f925bdaddc4201f984";
@@ -179,8 +179,7 @@ export function Helpers({
179179
.slice(0, 5)
180180
.map(addressAsset => (
181181
<div key={addressAsset.asset.asset_code}>
182-
{addressAsset.asset.name}{' '}
183-
{addressAsset.quantity}
182+
{addressAsset.asset.name} {addressAsset.quantity}
184183
</div>
185184
));
186185
}}
File renamed without changes.

examples/app.tsx renamed to examples/development/app.tsx

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import React, { useCallback, useMemo, useReducer, useState } from "react";
22
import ReactDOM from "react-dom";
3-
import { CachePolicy, Entry, useAssetsInfo } from "../src";
4-
import { client } from "../src";
5-
import { DataStatus } from "../src/cache/DataStatus";
6-
import { useAssetsFullInfo } from "../src/react";
7-
import { useSubscription } from "../src/react/useSubscription";
8-
import { ResponsePayload } from "../src/requests/ResponsePayload";
9-
import { endpoint, API_TOKEN } from "./config";
3+
import { CachePolicy, Entry } from "../../src";
4+
import { client } from "../../src";
5+
import { DataStatus } from "../../src/cache/DataStatus";
6+
import { useAssetsFullInfo } from "../../src/react";
7+
import { useSubscription } from "../../src/react/useSubscription";
8+
import { ResponsePayload } from "../../src/requests/ResponsePayload";
9+
import { endpoint, API_TOKEN } from "../config";
1010
import { EntryInfo } from "./EntryInfo";
1111
import { Helpers } from "./Helpers";
1212
import { VStack } from "./VStack";
1313

1414
client.configure({ url: endpoint, apiToken: API_TOKEN });
15+
Object.assign(window, { client });
1516

1617
const ETH = "eth";
1718
const USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
File renamed without changes.

examples/node/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const url = "wss://api-staging.zerion.io";
2+
export const apiToken = "Zerion.0JOY6zZTTw6yl5Cvz9sdmXc7d5AhzVMG";
3+
export const address = "0x42b9df65b219b3dd36ff330a4dd8f327a6ada990";

examples/node/index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* eslint-env node */
2+
/* eslint-disable @typescript-eslint/no-var-requires */
3+
import { client } from "../../";
4+
import { address, url, apiToken } from "./config";
5+
import { createViewModel } from "./viewModels/addressAsset";
6+
import { formatCurrency } from "./viewModels/shared/formatCurrency";
7+
import { formatNumber } from "./viewModels/shared/formatNumber";
8+
9+
client.configure({
10+
url,
11+
apiToken,
12+
});
13+
14+
const currency = "usd";
15+
16+
client.addressAssets({
17+
payload: {
18+
address,
19+
currency,
20+
},
21+
onData: data => {
22+
const assetsToDisplay = Object.values(data.assets)
23+
.map(addressAsset => createViewModel(addressAsset))
24+
.filter(asset => asset.value != null && asset.value > 1)
25+
.sort((a, b) => Number(b.value) - Number(a.value))
26+
.map(asset => ({
27+
...asset,
28+
price: formatCurrency(asset.price, currency),
29+
value: formatNumber(asset.value),
30+
}));
31+
32+
console.table(assetsToDisplay); // eslint-disable-line no-console
33+
},
34+
});

examples/node/package-lock.json

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/node/package.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@everdimension-testing/node",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "ts-node ./index.ts",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"keywords": [],
11+
"author": "everdimension",
12+
"license": "ISC",
13+
"dependencies": {
14+
"ts-node": "^9.1.1"
15+
}
16+
}

examples/node/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"declaration": true,
7+
"strict": true,
8+
"noUnusedLocals": true,
9+
"noUnusedParameters": true,
10+
"esModuleInterop": true,
11+
"pretty": true,
12+
"lib": ["esnext"]
13+
}
14+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import type { AddressAsset } from "../../../lib/entities/AddressAsset";
2+
import { convertNumber } from "./shared/convertNumber";
3+
4+
export function createViewModel(addressAsset: AddressAsset) {
5+
const { asset, quantity } = addressAsset;
6+
const price = asset.price ? asset.price.value : null;
7+
const commonQuantity = convertNumber(quantity, 0 - asset.decimals);
8+
const value = price ? price * commonQuantity : null;
9+
return {
10+
name: asset.name,
11+
symbol: asset.symbol,
12+
price: asset.price ? asset.price.value : null,
13+
quantity: commonQuantity,
14+
value: value,
15+
};
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function convertNumber(
2+
value: string | number,
3+
decimalPoints: number
4+
): number {
5+
return Number(value) * 10 ** decimalPoints;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function formatCurrency(value: number | null, currency: string): string {
2+
return value == null
3+
? "-"
4+
: new Intl.NumberFormat("en", { style: "currency", currency }).format(
5+
value
6+
);
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function formatNumber(value: number | null): string {
2+
return value == null ? "-" : new Intl.NumberFormat("en").format(value);
3+
}

examples/react-example/package-lock.json

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/react-example/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"author": "[email protected]",
1010
"license": "MIT",
1111
"dependencies": {
12-
"defi-sdk": "0.1.0-alpha.12",
12+
"defi-sdk": "0.1.0-alpha.14",
1313
"react": "^17.0.2",
1414
"react-dom": "^17.0.2"
1515
}

examples/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* esling-env node */
1+
/* eslint-env node */
22
/* eslint-disable @typescript-eslint/no-var-requires */
33
const { client } = require("../lib/defi-sdk.js");
44

index.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
</head>
88
<body>
99
<div id="root"></div>
10-
<div id="root2"></div>
11-
<script src="./examples/app.tsx"></script>
10+
<script src="./examples/development/app.tsx"></script>
1211
</body>
1312
</html>

0 commit comments

Comments
 (0)