Skip to content

Commit

Permalink
Checkpoint: WIP LA policy checks
Browse files Browse the repository at this point in the history
  • Loading branch information
spacesailor24 committed Dec 19, 2024
1 parent b8e6f84 commit 36cb3b4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 22 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ source .env
forge script script/DeployLitAgentRegistry.s.sol:DeployLitAgentRegistry --rpc-url http://localhost:8545 --broadcast
```

# Adding Uniswap Tool

```bash
1000000000000000000

0x4200000000000000000000000000000000000006,0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf
```

# Prompts

```
Expand Down
6 changes: 5 additions & 1 deletion packages/lit-agent-cli/src/core/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ export async function processAgentRequest(
},
publicKey: config.pkp!.publicKey!,
pkpEthAddress: config.pkp!.ethAddress!,
params: analysis,
params: {
...analysis,
user: ethersSigner.address,
ipfsCid: matchedAction.ipfsCid,
},
}
);

Expand Down
84 changes: 63 additions & 21 deletions packages/lit-agent-tool-uniswap/src/litAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ declare global {
const params: any;
const pkpEthAddress: any;
const publicKey: any;
const LitAuth: any;
}

export default async () => {
Expand All @@ -16,6 +17,38 @@ export default async () => {
"0x728e8162603F35446D09961c4A285e2643f4FB91";

try {
// Validate auth parameters
if (!LitAuth.authSigAddress) {
throw new Error("Missing required parameter: LitAuth.authSigAddress");
}
if (!LitAuth.actionIpfsIds[0]) {
throw new Error("Missing required parameter: LitAuth.actionIpfsIds[0]");
}
if (!pkpEthAddress) {
throw new Error("Missing required parameter: pkpEthAddress");
}

// Validate swap parameters
if (!params.tokenIn) {
throw new Error("Missing required parameter: tokenIn");
}
if (!params.tokenOut) {
throw new Error("Missing required parameter: tokenOut");
}
if (!params.amountIn) {
throw new Error("Missing required parameter: amountIn");
}

// Validate and normalize token addresses
let tokenIn: string;
let tokenOut: string;
try {
tokenIn = ethers.utils.getAddress(params.tokenIn);
tokenOut = ethers.utils.getAddress(params.tokenOut);
} catch (error) {
throw new Error(`Invalid token address: ${error.message}`);
}

const UNISWAP_V3_ROUTER = "0x2626664c2603336E57B271c5C0b26F421741e481";
const ethersProvider = new ethers.providers.JsonRpcProvider(
chainInfo.rpcUrl
Expand All @@ -33,9 +66,9 @@ export default async () => {
);

const [isPermitted, , policy] = await registryContract.getActionPolicy(
params.user, // The user who owns the PKP
LitAuth.authSigAddress, // The user who owns the PKP
pkpEthAddress, // The PKP address
params.recommendedCID // The IPFS CID of this Lit Action
LitAuth.actionIpfsIds[0] // The IPFS CID of this Lit Action
);

if (!isPermitted) {
Expand Down Expand Up @@ -73,17 +106,21 @@ export default async () => {
);
}

// Validate that allowedTokens contains valid addresses
// Validate and normalize allowed token addresses
if (!Array.isArray(decodedPolicy.allowedTokens)) {
throw new Error("Invalid policy format: allowedTokens is not an array");
}
for (const token of decodedPolicy.allowedTokens) {
if (!ethers.utils.isAddress(token)) {
throw new Error(
`Invalid policy format: ${token} is not a valid address`
);
decodedPolicy.allowedTokens = decodedPolicy.allowedTokens.map(
(token: string) => {
if (!ethers.utils.isAddress(token)) {
throw new Error(
`Invalid policy format: ${token} is not a valid address`
);
}
// Normalize to checksum address
return ethers.utils.getAddress(token);
}
}
);

console.log("Policy:", {
maxAmount: decodedPolicy.maxAmount.toString(),
Expand All @@ -99,40 +136,45 @@ export default async () => {
throw error;
}

// Use the policy's allowed tokens
// Use the policy's allowed tokens (already normalized to checksum)
const allowedTokens = decodedPolicy.allowedTokens;

if (!allowedTokens.includes(params.tokenIn)) {
// Normalize input token addresses to checksum format for comparison
const normalizedTokenIn = ethers.utils.getAddress(tokenIn);
const normalizedTokenOut = ethers.utils.getAddress(tokenOut);

if (!allowedTokens.includes(normalizedTokenIn)) {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Token not allowed: ${params.tokenIn}`,
error: `Token not allowed: ${normalizedTokenIn}`,
}),
});
throw new Error(`Token not allowed: ${params.tokenIn}`);
throw new Error(`Token not allowed: ${normalizedTokenIn}`);
}
if (!allowedTokens.includes(params.tokenOut)) {
if (!allowedTokens.includes(normalizedTokenOut)) {
LitActions.setResponse({
response: JSON.stringify({
status: "error",
error: `Token not allowed: ${params.tokenOut}`,
error: `Token not allowed: ${normalizedTokenOut}`,
}),
});
throw new Error(`Token not allowed: ${params.tokenOut}`);
throw new Error(`Token not allowed: ${normalizedTokenOut}`);
}

const tokenInterface = new ethers.utils.Interface([
"function decimals() view returns (uint8)",
"function approve(address spender, uint256 amount) external returns (bool)",
]);

// Use normalized addresses for contract interactions
const tokenInContract = new ethers.Contract(
params.tokenIn,
normalizedTokenIn,
tokenInterface,
ethersProvider
);
const tokenOutContract = new ethers.Contract(
params.tokenOut,
normalizedTokenOut,
tokenInterface,
ethersProvider
);
Expand All @@ -156,7 +198,7 @@ export default async () => {
const amountOutMin = ethers.BigNumber.from(0);

const approvalTx = {
to: params.tokenIn,
to: normalizedTokenIn, // Use normalized address
data: tokenInterface.encodeFunctionData("approve", [
UNISWAP_V3_ROUTER,
amountIn,
Expand Down Expand Up @@ -216,8 +258,8 @@ export default async () => {
]);

const swapParamsArray = [
params.tokenIn,
params.tokenOut,
normalizedTokenIn, // Use normalized address
normalizedTokenOut, // Use normalized address
3000,
pkpEthAddress,
amountIn,
Expand Down

0 comments on commit 36cb3b4

Please sign in to comment.