Skip to content

Commit

Permalink
Fixed 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 87f36e3 commit 89f89cc
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 46 deletions.
42 changes: 33 additions & 9 deletions packages/lit-agent-tool-uniswap/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,35 @@ const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const rootDir = join(__dirname, "..");

// Function to get the description from index.ts
async function getDescription() {
const result = await esbuild.build({
entryPoints: [join(rootDir, "src", "index.ts")],
bundle: false,
write: false,
format: "esm",
target: "esnext",
platform: "neutral",
});

const code = result.outputFiles[0].text;
const match = code.match(
/const uniswapLitActionDescription\s*=\s*["']([^"']+)["']/
);

if (!match) {
console.error("Code content:", code);
throw new Error("Could not find description in index.ts");
}

return match[1];
}

// Function to generate the action string
async function generateActionString() {
// Build the action file to get its contents
const result = await esbuild.build({
entryPoints: [join(rootDir, "src", "uniswapAction.ts")],
entryPoints: [join(rootDir, "src", "litAction.ts")],
bundle: true, // Enable bundling to resolve imports
write: false,
format: "esm",
Expand All @@ -26,7 +50,7 @@ async function generateActionString() {
const actionCode = result.outputFiles[0].text;

// Extract everything between the var assignment and the export statement
const startMatch = actionCode.indexOf("var uniswapAction_default = ");
const startMatch = actionCode.indexOf("var litAction_default = ");
const endMatch = actionCode.indexOf("export {");

if (startMatch === -1 || endMatch === -1) {
Expand All @@ -36,7 +60,7 @@ async function generateActionString() {

// Extract the function definition (excluding the variable assignment)
const functionBody = actionCode
.slice(startMatch + "var uniswapAction_default = ".length, endMatch)
.slice(startMatch + "var litAction_default = ".length, endMatch)
.trim()
.replace(/;$/, ""); // Remove trailing semicolon if present

Expand All @@ -46,10 +70,13 @@ async function generateActionString() {

// Function to generate the index files
async function generateIndexFiles(ipfsMetadata = {}) {
const actionString = await generateActionString();
const [actionString, description] = await Promise.all([
generateActionString(),
getDescription(),
]);

const indexContent = `
export const uniswapLitActionDescription = "Swap tokens using Uniswap V3";
export const uniswapLitActionDescription = ${JSON.stringify(description)};
export const uniswapLitAction = ${JSON.stringify(actionString)};
export const uniswapMetadata = ${JSON.stringify(ipfsMetadata)};
`;
Expand All @@ -73,10 +100,7 @@ export declare const uniswapMetadata: {
);

// Write the action string to a separate file for IPFS upload
await fs.writeFile(
join(rootDir, "dist", "lit-action-string.js"),
actionString
);
await fs.writeFile(join(rootDir, "dist", "lit-action.js"), actionString);
}

// Main build function
Expand Down
2 changes: 1 addition & 1 deletion packages/lit-agent-tool-uniswap/scripts/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async function main() {

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

Expand Down
3 changes: 0 additions & 3 deletions packages/lit-agent-tool-uniswap/src/esbuild-shims.js

This file was deleted.

33 changes: 0 additions & 33 deletions packages/lit-agent-tool-uniswap/src/uploadToPinata.ts

This file was deleted.

0 comments on commit 89f89cc

Please sign in to comment.