Skip to content

Commit

Permalink
Bug fix: remove deno from lit-agent-tool-uniswap
Browse files Browse the repository at this point in the history
  • Loading branch information
spacesailor24 committed Dec 19, 2024
1 parent b8849e5 commit 5570fad
Show file tree
Hide file tree
Showing 13 changed files with 544 additions and 197 deletions.
1 change: 1 addition & 0 deletions packages/lit-agent-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dev": "tsx watch src/cli.ts"
},
"dependencies": {
"@dotenvx/dotenvx": "^1.31.0",
"@lit-protocol/constants": "^7.0.2",
"commander": "^12.1.0",
"ethers": "v5",
Expand Down
21 changes: 11 additions & 10 deletions packages/lit-agent-cli/src/utils/tools.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// import {
// uniswapMetadata,
// uniswapLitActionDescription,
// } from "lit-agent-tool-uniswap";
import {
uniswapMetadata,
uniswapLitActionDescription,
} from "lit-agent-tool-uniswap";

// const uniswapMetadata = {
// uniswapLitAction: {
// IpfsHash: "QmQ9611111111111111111111111111111111111111111111111",
// },
// };
// const uniswapLitActionDescription = "Uniswap Lit Action";

const uniswapMetadata = {
uniswapLitAction: {
IpfsHash: "QmQ9611111111111111111111111111111111111111111111111",
},
};
const uniswapLitActionDescription = "Uniswap Lit Action";
export interface LitAgentTool {
name: string;
description: string;
Expand Down
1 change: 1 addition & 0 deletions packages/lit-agent-tool-uniswap/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PINATA_JWT=
6 changes: 0 additions & 6 deletions packages/lit-agent-tool-uniswap/.npmignore

This file was deleted.

9 changes: 0 additions & 9 deletions packages/lit-agent-tool-uniswap/deno.jsonc

This file was deleted.

51 changes: 0 additions & 51 deletions packages/lit-agent-tool-uniswap/deno.lock

This file was deleted.

12 changes: 10 additions & 2 deletions packages/lit-agent-tool-uniswap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@
}
},
"scripts": {
"deploy": "deno task deploy"
"build": "node scripts/build.js",
"deploy": "yarn build && node scripts/deploy.js"
},
"sideEffects": false
"dependencies": {
"@dotenvx/dotenvx": "^1.31.0",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"esbuild": "^0.20.1",
"typescript": "^5.0.0"
}
}
96 changes: 96 additions & 0 deletions packages/lit-agent-tool-uniswap/scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import * as esbuild from "esbuild";
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import fs from "fs/promises";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, "..");

// Function to bundle the lit action
async function bundleLitAction() {
const litActionResult = await esbuild.build({
entryPoints: [join(rootDir, "src", "litAction.ts")],
bundle: true,
platform: "neutral",
format: "esm",
target: "esnext",
minify: false,
inject: [join(rootDir, "src", "esbuild-shims.js")],
write: false,
define: {
"process.env.NODE_ENV": '"production"',
"process.env.NODE_DEBUG": "false",
},
});

return litActionResult.outputFiles[0].text;
}

// Function to generate the index files
async function generateIndexFiles(litActionCode, ipfsMetadata = {}) {
const indexContent = `
export const uniswapLitActionDescription = "Swap tokens using Uniswap V3";
export const uniswapLitAction = ${JSON.stringify(litActionCode)};
export const uniswapMetadata = ${JSON.stringify(ipfsMetadata)};
`;

await fs.writeFile(join(rootDir, "dist", "index.js"), indexContent);
await fs.writeFile(
join(rootDir, "dist", "index.d.ts"),
`
export declare const uniswapLitActionDescription: string;
export declare const uniswapLitAction: string;
export declare const uniswapMetadata: {
uniswapLitAction: {
IpfsHash: string;
PinSize: number;
Timestamp: string;
isDuplicate: boolean;
Duration: number;
};
};
`
);
}

// Main build function
async function build() {
try {
// Ensure dist directory exists
await fs.mkdir(join(rootDir, "dist"), { recursive: true });

// Bundle the lit action
const litActionCode = await bundleLitAction();
await fs.writeFile(join(rootDir, "dist", "lit-action.js"), litActionCode);

// Read the IPFS metadata if it exists
let ipfsMetadata = {};
try {
const content = await fs.readFile(
join(rootDir, "dist", "ipfs.json"),
"utf-8"
);
ipfsMetadata = JSON.parse(content);
} catch (error) {
console.warn(
"Warning: ipfs.json not found or invalid, using empty metadata"
);
}

// Generate index files
await generateIndexFiles(litActionCode, ipfsMetadata);

console.log("Build completed successfully");
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}

export { bundleLitAction, generateIndexFiles };

// Only run build if this is the main module
if (import.meta.url === `file://${process.argv[1]}`) {
build();
}
128 changes: 128 additions & 0 deletions packages/lit-agent-tool-uniswap/scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { fileURLToPath } from "url";
import { dirname, join } from "path";
import fs from "fs/promises";
import fetch from "node-fetch";
import dotenvx from "@dotenvx/dotenvx";
import { generateIndexFiles } from "./build.js";

// Load environment variables
dotenvx.config();

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, "..");

async function uploadToPinata(pinataJwt, data) {
// Create boundary for multipart form data
const boundary =
"----WebKitFormBoundary" + Math.random().toString(36).substring(2);

// Create form data manually
const formData = [
`--${boundary}`,
'Content-Disposition: form-data; name="file"; filename="litAction.js"',
"Content-Type: text/plain",
"",
data,
`--${boundary}`,
'Content-Disposition: form-data; name="pinataMetadata"',
"",
JSON.stringify({ name: "Uniswap Swap Lit Action" }),
`--${boundary}`,
'Content-Disposition: form-data; name="pinataOptions"',
"",
JSON.stringify({ cidVersion: 0 }),
`--${boundary}--`,
].join("\r\n");

const response = await fetch(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
{
method: "POST",
headers: {
"Content-Type": `multipart/form-data; boundary=${boundary}`,
Authorization: `Bearer ${pinataJwt}`,
},
body: formData,
}
);

if (!response.ok) {
const text = await response.text();
throw new Error(`Pinata upload failed: ${response.status} - ${text}`);
}

return response.json();
}

async function updateIpfsMetadata(newMetadata) {
try {
// Ensure dist directory exists
await fs.mkdir(join(rootDir, "dist"), { recursive: true });

let metadata = {};
const ipfsPath = join(rootDir, "dist", "ipfs.json");

try {
const content = await fs.readFile(ipfsPath, "utf-8");
metadata = JSON.parse(content);
} catch (error) {
// File doesn't exist or is invalid, start with empty object
}

metadata["uniswapLitAction"] = newMetadata;
await fs.writeFile(ipfsPath, JSON.stringify(metadata, null, 2));

// Update index files with new metadata
const litActionCode = await fs.readFile(
join(rootDir, "dist", "lit-action.js"),
"utf-8"
);
await generateIndexFiles(litActionCode, metadata);
} catch (error) {
console.error("Failed to update ipfs.json:", error);
throw error;
}
}

async function main() {
const PINATA_JWT = process.env.PINATA_JWT;

if (!PINATA_JWT) {
console.error("Missing PINATA_JWT environment variable");
process.exit(1);
}

try {
// Ensure dist directory exists
await fs.mkdir(join(rootDir, "dist"), { recursive: true });

// Read the bundled lit action
const bundled = await fs.readFile(
join(rootDir, "dist", "lit-action.js"),
"utf-8"
);

const startTime = Date.now();
const pinataResponse = await uploadToPinata(PINATA_JWT, bundled);
const duration = (Date.now() - startTime) / 1000;

// Create metadata
const metadata = {
IpfsHash: pinataResponse.IpfsHash,
PinSize: pinataResponse.PinSize,
Timestamp: new Date().toISOString(),
isDuplicate: pinataResponse.isDuplicate || false,
Duration: duration,
};

await updateIpfsMetadata(metadata);
console.log("Deployment successful:", metadata);
process.exit(0);
} catch (error) {
console.error("Deployment failed:", error);
process.exit(1);
}
}

main();
Loading

0 comments on commit 5570fad

Please sign in to comment.