@lit-protocol/vincent-policy-spending-limit 1.0.0-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CONTRIBUTING.md +89 -0
  2. package/README.md +64 -0
  3. package/dist/CONTRIBUTING.md +89 -0
  4. package/dist/README.md +64 -0
  5. package/dist/package.json +27 -0
  6. package/dist/src/generated/lit-action.js +9 -0
  7. package/dist/src/generated/vincent-bundled-policy.d.ts +212 -0
  8. package/dist/src/generated/vincent-bundled-policy.d.ts.map +1 -0
  9. package/dist/src/generated/vincent-bundled-policy.js +14 -0
  10. package/dist/src/generated/vincent-bundled-policy.ts +13 -0
  11. package/dist/src/generated/vincent-policy-metadata.json +3 -0
  12. package/dist/src/index.d.ts +4 -0
  13. package/dist/src/index.d.ts.map +1 -0
  14. package/dist/src/index.js +8 -0
  15. package/dist/src/lib/lit-action.d.ts +2 -0
  16. package/dist/src/lib/lit-action.d.ts.map +1 -0
  17. package/dist/src/lib/lit-action.js +11 -0
  18. package/dist/src/lib/policy-helpers/calculate-usd-value.d.ts +7 -0
  19. package/dist/src/lib/policy-helpers/calculate-usd-value.d.ts.map +1 -0
  20. package/dist/src/lib/policy-helpers/calculate-usd-value.js +21 -0
  21. package/dist/src/lib/policy-helpers/check-spending-limit.d.ts +16 -0
  22. package/dist/src/lib/policy-helpers/check-spending-limit.d.ts.map +1 -0
  23. package/dist/src/lib/policy-helpers/check-spending-limit.js +28 -0
  24. package/dist/src/lib/policy-helpers/get-eth-usd-price.d.ts +6 -0
  25. package/dist/src/lib/policy-helpers/get-eth-usd-price.d.ts.map +1 -0
  26. package/dist/src/lib/policy-helpers/get-eth-usd-price.js +21 -0
  27. package/dist/src/lib/policy-helpers/get-token-amount-in-usd.d.ts +10 -0
  28. package/dist/src/lib/policy-helpers/get-token-amount-in-usd.d.ts.map +1 -0
  29. package/dist/src/lib/policy-helpers/get-token-amount-in-usd.js +67 -0
  30. package/dist/src/lib/policy-helpers/get-uniswap-quote.d.ts +15 -0
  31. package/dist/src/lib/policy-helpers/get-uniswap-quote.d.ts.map +1 -0
  32. package/dist/src/lib/policy-helpers/get-uniswap-quote.js +113 -0
  33. package/dist/src/lib/policy-helpers/index.d.ts +7 -0
  34. package/dist/src/lib/policy-helpers/index.d.ts.map +1 -0
  35. package/dist/src/lib/policy-helpers/index.js +9 -0
  36. package/dist/src/lib/policy-helpers/send-spend-tx.d.ts +9 -0
  37. package/dist/src/lib/policy-helpers/send-spend-tx.d.ts.map +1 -0
  38. package/dist/src/lib/policy-helpers/send-spend-tx.js +143 -0
  39. package/dist/src/lib/policy-helpers/sign-tx.d.ts +3 -0
  40. package/dist/src/lib/policy-helpers/sign-tx.d.ts.map +1 -0
  41. package/dist/src/lib/policy-helpers/sign-tx.js +27 -0
  42. package/dist/src/lib/policy-helpers/spending-limit-contract.d.ts +4 -0
  43. package/dist/src/lib/policy-helpers/spending-limit-contract.d.ts.map +1 -0
  44. package/dist/src/lib/policy-helpers/spending-limit-contract.js +21 -0
  45. package/dist/src/lib/schemas.d.ts +101 -0
  46. package/dist/src/lib/schemas.d.ts.map +1 -0
  47. package/dist/src/lib/schemas.js +43 -0
  48. package/dist/src/lib/vincent-policy.d.ts +209 -0
  49. package/dist/src/lib/vincent-policy.d.ts.map +1 -0
  50. package/dist/src/lib/vincent-policy.js +116 -0
  51. package/package.json +26 -0
@@ -0,0 +1,116 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.vincentPolicy = void 0;
4
+ const vincent_tool_sdk_1 = require("@lit-protocol/vincent-tool-sdk");
5
+ const send_spend_tx_1 = require("./policy-helpers/send-spend-tx");
6
+ const check_spending_limit_1 = require("./policy-helpers/check-spending-limit");
7
+ const schemas_1 = require("./schemas");
8
+ exports.vincentPolicy = (0, vincent_tool_sdk_1.createVincentPolicy)({
9
+ packageName: '@lit-protocol/vincent-policy-spending-limit',
10
+ toolParamsSchema: schemas_1.toolParamsSchema,
11
+ userParamsSchema: schemas_1.userParamsSchema,
12
+ commitParamsSchema: schemas_1.commitParamsSchema,
13
+ precheckAllowResultSchema: schemas_1.precheckAllowResultSchema,
14
+ precheckDenyResultSchema: schemas_1.precheckDenyResultSchema,
15
+ evalAllowResultSchema: schemas_1.evalAllowResultSchema,
16
+ evalDenyResultSchema: schemas_1.evalDenyResultSchema,
17
+ commitAllowResultSchema: schemas_1.commitAllowResultSchema,
18
+ commitDenyResultSchema: schemas_1.commitDenyResultSchema,
19
+ precheck: async ({ toolParams, userParams }, { allow, deny, appId, delegation: { delegatorPkpInfo } }) => {
20
+ console.log('Prechecking spending limit policy', { toolParams, userParams });
21
+ const { ethAddress } = delegatorPkpInfo;
22
+ const { buyAmount, ethRpcUrl, rpcUrlForUniswap, chainIdForUniswap, tokenAddress, tokenDecimals, } = toolParams;
23
+ const { maxDailySpendingLimitInUsdCents } = userParams;
24
+ const { buyAmountAllowed, buyAmountInUsd, adjustedMaxDailySpendingLimit } = await (0, check_spending_limit_1.checkIfBuyAmountAllowed)({
25
+ ethRpcUrl,
26
+ rpcUrlForUniswap,
27
+ chainIdForUniswap,
28
+ tokenAddress: tokenAddress,
29
+ tokenDecimals,
30
+ buyAmount,
31
+ maxDailySpendingLimitInUsdCents,
32
+ pkpEthAddress: ethAddress,
33
+ appId,
34
+ });
35
+ return buyAmountAllowed
36
+ ? allow({
37
+ maxSpendingLimitInUsd: Number(adjustedMaxDailySpendingLimit),
38
+ buyAmountInUsd: Number(buyAmountInUsd),
39
+ })
40
+ : deny({
41
+ reason: 'Attempted buy amount exceeds daily limit',
42
+ maxSpendingLimitInUsd: Number(adjustedMaxDailySpendingLimit),
43
+ buyAmountInUsd: Number(buyAmountInUsd),
44
+ });
45
+ },
46
+ evaluate: async ({ toolParams, userParams }, { allow, deny, appId, delegation: { delegatorPkpInfo } }) => {
47
+ const { ethAddress } = delegatorPkpInfo;
48
+ console.log('Evaluating spending limit policy', JSON.stringify(toolParams));
49
+ const { buyAmount, ethRpcUrl, rpcUrlForUniswap, chainIdForUniswap, tokenAddress, tokenDecimals, } = toolParams;
50
+ const { maxDailySpendingLimitInUsdCents } = userParams;
51
+ const checkBuyAmountResponse = await Lit.Actions.runOnce({ waitForResponse: true, name: 'checkBuyAmount' }, async () => {
52
+ try {
53
+ const { buyAmountAllowed, buyAmountInUsd, adjustedMaxDailySpendingLimit } = await (0, check_spending_limit_1.checkIfBuyAmountAllowed)({
54
+ ethRpcUrl,
55
+ rpcUrlForUniswap,
56
+ chainIdForUniswap,
57
+ tokenAddress: tokenAddress,
58
+ tokenDecimals,
59
+ buyAmount,
60
+ maxDailySpendingLimitInUsdCents,
61
+ pkpEthAddress: ethAddress,
62
+ appId,
63
+ });
64
+ return JSON.stringify({
65
+ status: 'success',
66
+ buyAmountAllowed,
67
+ buyAmountInUsd: buyAmountInUsd.toString(),
68
+ adjustedMaxDailySpendingLimit: adjustedMaxDailySpendingLimit.toString(),
69
+ });
70
+ }
71
+ catch (error) {
72
+ return JSON.stringify({
73
+ status: 'error',
74
+ error: error instanceof Error ? error.message : String(error),
75
+ });
76
+ }
77
+ });
78
+ const parsedCheckBuyAmountResponse = JSON.parse(checkBuyAmountResponse);
79
+ if (parsedCheckBuyAmountResponse.status === 'error') {
80
+ return deny({
81
+ reason: `Error checking buy amount: ${parsedCheckBuyAmountResponse.error} (evaluate)`,
82
+ });
83
+ }
84
+ const { buyAmountAllowed, buyAmountInUsd, adjustedMaxDailySpendingLimit } = parsedCheckBuyAmountResponse;
85
+ console.log('Evaluated spending limit policy', {
86
+ buyAmountAllowed,
87
+ buyAmountInUsd,
88
+ adjustedMaxDailySpendingLimit,
89
+ });
90
+ return buyAmountAllowed
91
+ ? allow({
92
+ maxSpendingLimitInUsd: Number(adjustedMaxDailySpendingLimit),
93
+ buyAmountInUsd: Number(buyAmountInUsd),
94
+ })
95
+ : deny({
96
+ reason: 'Attempted buy amount exceeds daily limit',
97
+ maxSpendingLimitInUsd: Number(adjustedMaxDailySpendingLimit),
98
+ buyAmountInUsd: Number(buyAmountInUsd),
99
+ });
100
+ },
101
+ commit: async (params, { allow, appId, delegation: { delegatorPkpInfo } }) => {
102
+ const { ethAddress, publicKey } = delegatorPkpInfo;
103
+ const { amountSpentUsd, maxSpendingLimitInUsd } = params;
104
+ const spendTxHash = await (0, send_spend_tx_1.sendSpendTx)({
105
+ appId,
106
+ amountSpentUsd,
107
+ maxSpendingLimitInUsd,
108
+ spendingLimitDuration: 86400, // number of seconds in a day
109
+ pkpEthAddress: ethAddress,
110
+ pkpPubKey: publicKey,
111
+ });
112
+ return allow({
113
+ spendTxHash,
114
+ });
115
+ },
116
+ });
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "@lit-protocol/vincent-policy-spending-limit",
3
+ "version": "1.0.0-1",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "dependencies": {
8
+ "@uniswap/sdk-core": "^7.7.2",
9
+ "ethers": "^5.8.0",
10
+ "tslib": "2.8.1",
11
+ "zod": "^3.25.64",
12
+ "@lit-protocol/vincent-tool-sdk": "1.0.0-1"
13
+ },
14
+ "devDependencies": {
15
+ "esbuild": "^0.19.12",
16
+ "esbuild-plugin-polyfill-node": "^0.3.0",
17
+ "ipfs-only-hash": "^4.0.0"
18
+ },
19
+ "main": "./dist/src/index.js",
20
+ "module": "./dist/src/index.js",
21
+ "types": "./dist/src/index.d.ts",
22
+ "files": [
23
+ "dist/**",
24
+ "*.md"
25
+ ]
26
+ }