-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix: remove deno from lit-agent-tool-uniswap
- Loading branch information
1 parent
b8849e5
commit 5570fad
Showing
13 changed files
with
544 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PINATA_JWT= |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Oops, something went wrong.