@cmdoss/agent-pass-plugin 0.0.0-stage → 0.1.0
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.
- package/README.md +131 -2
- package/dist/agentpass.js +199 -0
- package/dist/cli.js +37 -0
- package/dist/domain.js +103 -0
- package/dist/engine.js +260 -0
- package/dist/http.js +170 -0
- package/dist/mcp.js +259 -0
- package/dist/onboarding.js +258 -0
- package/dist/public/catalog.js +35 -0
- package/dist/public/http.js +89 -0
- package/dist/public-server.js +11 -0
- package/dist/runtime/config.js +16 -0
- package/dist/runtime/installation.js +107 -0
- package/dist/runtime/lock.js +38 -0
- package/dist/runtime/paths.js +56 -0
- package/dist/runtime/setup.js +155 -0
- package/dist/sdk-ui.js +48 -0
- package/dist/server.js +107 -0
- package/dist/shared/constants.js +9 -0
- package/dist/store.js +84 -0
- package/dist/testnet/adapter.js +275 -0
- package/dist/testnet/authorization.js +620 -0
- package/dist/testnet/chain/codec.js +68 -0
- package/dist/testnet/chain/inspection.js +54 -0
- package/dist/testnet/chain/policy.js +78 -0
- package/dist/testnet/chain/prepared.js +150 -0
- package/dist/testnet/chain/receipt.js +49 -0
- package/dist/testnet/chain/sdk.js +34 -0
- package/dist/testnet/chain/types.js +1 -0
- package/dist/testnet/engine.js +395 -0
- package/dist/testnet/http.js +139 -0
- package/dist/testnet/model.js +49 -0
- package/dist/testnet/policy.js +44 -0
- package/dist/testnet/read-model.js +217 -0
- package/dist/testnet/repository.js +62 -0
- package/dist/testnet/runner.js +306 -0
- package/dist/testnet/types.js +1 -0
- package/dist/workflows.js +379 -0
- package/docs/agent-pass-integration.md +75 -0
- package/docs/architecture.md +93 -0
- package/docs/npm-release.md +83 -0
- package/npm-shrinkwrap.json +5376 -0
- package/package.json +72 -4
- package/public/assets/agent-pass-plugin-client.js +16 -0
- package/public/assets/agent-pass-plugin-sdk.js +1 -0
- package/public/assets/sui-payment-rails.avif +0 -0
- package/public/assets/sui-payments-hero.json +1 -0
- package/public/fonts/TWKEverett-Medium.woff2 +0 -0
- package/public/fonts/TWKEverett-Regular.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Medium.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Regular.woff2 +0 -0
- package/public/index.html +81 -0
- package/public/styles/base.css +117 -0
- package/public/styles/catalog.css +279 -0
- package/public/styles/controls.css +118 -0
- package/public/styles/dialog.css +257 -0
- package/public/styles/home.css +82 -0
- package/public/styles/layout.css +45 -0
- package/public/styles/plans.css +271 -0
- package/public/styles/responsive.css +643 -0
- package/public/styles/review.css +150 -0
- package/public/styles/sdk-wallet.css +125 -0
- package/public/styles/sdk.css +441 -0
- package/public/styles/shell.css +348 -0
- package/public/styles/tokens.css +75 -0
- package/public/styles.css +11 -0
- package/scripts/setup.mjs +10 -0
- package/skills/agent-pass-plugin-trading/SKILL.md +60 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
export const SUI = "0x" + "0".repeat(63) + "2::sui::SUI";
|
|
2
|
+
export const record = (value) => value && typeof value === "object" && !Array.isArray(value)
|
|
3
|
+
? value
|
|
4
|
+
: {};
|
|
5
|
+
export const list = (value) => Array.isArray(value) ? value : [];
|
|
6
|
+
export const address = (value) => {
|
|
7
|
+
if (typeof value !== "string" || !/^0x[0-9a-fA-F]{1,64}$/.test(value))
|
|
8
|
+
throw new Error("Invalid Sui object/address in testnet binding.");
|
|
9
|
+
return "0x" + value.slice(2).toLowerCase().padStart(64, "0");
|
|
10
|
+
};
|
|
11
|
+
export const coinType = (value) => {
|
|
12
|
+
if (typeof value !== "string")
|
|
13
|
+
throw new Error("Invalid coin type.");
|
|
14
|
+
const parts = value.split("::");
|
|
15
|
+
if (parts.length !== 3 ||
|
|
16
|
+
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(parts[1]) ||
|
|
17
|
+
!/^[A-Za-z_][A-Za-z0-9_]*$/.test(parts[2]))
|
|
18
|
+
throw new Error("Unsupported coin type.");
|
|
19
|
+
return `${address(parts[0])}::${parts[1]}::${parts[2]}`;
|
|
20
|
+
};
|
|
21
|
+
export const integer = (value, positive = false) => {
|
|
22
|
+
if (typeof value !== "string" || !/^(0|[1-9][0-9]*)$/.test(value))
|
|
23
|
+
throw new Error("Chain amounts must be exact integer strings.");
|
|
24
|
+
const result = BigInt(value);
|
|
25
|
+
if (positive && result <= 0n)
|
|
26
|
+
throw new Error("Amount must be positive.");
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
export const signedInteger = (value) => {
|
|
30
|
+
if (typeof value !== "string" || !/^-?(0|[1-9][0-9]*)$/.test(value))
|
|
31
|
+
throw new Error("Invalid chain balance change.");
|
|
32
|
+
return BigInt(value);
|
|
33
|
+
};
|
|
34
|
+
export function stable(value) {
|
|
35
|
+
if (Array.isArray(value))
|
|
36
|
+
return `[${value.map(stable).join(",")}]`;
|
|
37
|
+
if (value && typeof value === "object")
|
|
38
|
+
return `{${Object.entries(value)
|
|
39
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
40
|
+
.map(([key, item]) => `${JSON.stringify(key)}:${stable(item)}`)
|
|
41
|
+
.join(",")}}`;
|
|
42
|
+
return JSON.stringify(value);
|
|
43
|
+
}
|
|
44
|
+
export function decimalUnits(value, decimals) {
|
|
45
|
+
const sign = value < 0n ? "-" : "";
|
|
46
|
+
const absolute = value < 0n ? -value : value;
|
|
47
|
+
const scale = 10n ** BigInt(decimals);
|
|
48
|
+
const fractional = (absolute % scale)
|
|
49
|
+
.toString()
|
|
50
|
+
.padStart(decimals, "0")
|
|
51
|
+
.replace(/0+$/, "");
|
|
52
|
+
return `${sign}${absolute / scale}${fractional ? `.${fractional}` : ""}`;
|
|
53
|
+
}
|
|
54
|
+
export function ratio(numerator, denominator) {
|
|
55
|
+
if (denominator <= 0n)
|
|
56
|
+
throw new Error("Cannot value a zero-size quote.");
|
|
57
|
+
return decimalUnits((numerator * 1000000000n) / denominator, 9);
|
|
58
|
+
}
|
|
59
|
+
export function decode(bytes) {
|
|
60
|
+
if (typeof bytes !== "string" ||
|
|
61
|
+
bytes.length > 180_000 ||
|
|
62
|
+
!/^[A-Za-z0-9+/]+={0,2}$/.test(bytes))
|
|
63
|
+
throw new Error("Expected canonical full transaction bytes.");
|
|
64
|
+
const decoded = Buffer.from(bytes, "base64");
|
|
65
|
+
if (decoded.toString("base64") !== bytes)
|
|
66
|
+
throw new Error("Transaction bytes are not canonical base64.");
|
|
67
|
+
return decoded;
|
|
68
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { coinType, integer, list, record } from "./codec.js";
|
|
2
|
+
export function readInspection(binding, context) {
|
|
3
|
+
const { sdk, live, info, gasAllowance } = context;
|
|
4
|
+
const control = record(live.control), allowances = record(live.allowances), allowance = record(allowances[coinType(binding.coinIn.type)]);
|
|
5
|
+
const balances = record(live.ownerBalances), gas = record(live.gas);
|
|
6
|
+
const swap = list(record(control.policies).swaps)
|
|
7
|
+
.map(record)
|
|
8
|
+
.find((policy) => coinType(policy.coinIn) === coinType(binding.coinIn.type) &&
|
|
9
|
+
coinType(policy.coinOut) === coinType(binding.coinOut.type));
|
|
10
|
+
const chainTimeMs = Number(control.chainTimeMs);
|
|
11
|
+
if (!Number.isSafeInteger(chainTimeMs) || chainTimeMs <= 0)
|
|
12
|
+
throw new Error("Chain time is unavailable.");
|
|
13
|
+
const revoked = live.revoked === true || control.revoked === true;
|
|
14
|
+
const expired = integer(String(control.agentExpiresMs ?? "0")) <= BigInt(chainTimeMs);
|
|
15
|
+
const result = {
|
|
16
|
+
active: false,
|
|
17
|
+
revoked,
|
|
18
|
+
expired,
|
|
19
|
+
chainTimeMs,
|
|
20
|
+
ownerInputBalance: String(record(balances[coinType(binding.coinIn.type)]).addressBalance ?? "0"),
|
|
21
|
+
ownerOutputBalance: String(record(balances[coinType(binding.coinOut.type)]).addressBalance ?? "0"),
|
|
22
|
+
agentGasBalance: String(gas.addressBalance ?? "0"),
|
|
23
|
+
allowanceRemaining: "0",
|
|
24
|
+
dailyRemaining: "0",
|
|
25
|
+
};
|
|
26
|
+
for (const value of [
|
|
27
|
+
result.ownerInputBalance,
|
|
28
|
+
result.ownerOutputBalance,
|
|
29
|
+
result.agentGasBalance,
|
|
30
|
+
])
|
|
31
|
+
integer(value);
|
|
32
|
+
try {
|
|
33
|
+
sdk.assertLiveAuthorization(info, binding, gasAllowance);
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
result.reason =
|
|
37
|
+
error instanceof Error
|
|
38
|
+
? error.message
|
|
39
|
+
: "The on-chain authorization does not match the binding.";
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
if (revoked || expired) {
|
|
43
|
+
result.reason = revoked ? "Agent Pass was revoked." : "Agent Pass expired.";
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
const remaining = integer(allowance.lifetimeCap) - integer(allowance.currentSpend);
|
|
47
|
+
const daily = swap
|
|
48
|
+
? integer(swap.daily) - integer(record(swap.usage).usedToday)
|
|
49
|
+
: 0n;
|
|
50
|
+
result.allowanceRemaining = (remaining > 0n ? remaining : 0n).toString();
|
|
51
|
+
result.dailyRemaining = (daily > 0n ? daily : 0n).toString();
|
|
52
|
+
result.active = true;
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { isAbsolute } from "node:path";
|
|
3
|
+
import { assertBindingPolicy, canonical } from "../authorization.js";
|
|
4
|
+
import { address, coinType, integer, list, record } from "./codec.js";
|
|
5
|
+
export function validateBinding(binding) {
|
|
6
|
+
if (binding.network !== "testnet")
|
|
7
|
+
throw new Error("This adapter executes only on testnet.");
|
|
8
|
+
for (const field of [
|
|
9
|
+
"owner",
|
|
10
|
+
"sessionAddress",
|
|
11
|
+
"passId",
|
|
12
|
+
"packageId",
|
|
13
|
+
"configId",
|
|
14
|
+
"poolId",
|
|
15
|
+
])
|
|
16
|
+
address(binding[field]);
|
|
17
|
+
if (address(binding.owner) === address(binding.sessionAddress))
|
|
18
|
+
throw new Error("The delegated agent must be distinct from the owner.");
|
|
19
|
+
if (!binding.chainIdentifier ||
|
|
20
|
+
!binding.authorizationDigest ||
|
|
21
|
+
!binding.policyHash ||
|
|
22
|
+
!binding.planHash ||
|
|
23
|
+
!/^[a-f0-9]{64}$/.test(binding.deploymentHash ?? "") ||
|
|
24
|
+
!isAbsolute(binding.directory))
|
|
25
|
+
throw new Error("The binding lacks verified authorization metadata.");
|
|
26
|
+
if (!Number.isFinite(Date.parse(binding.expiresAt)))
|
|
27
|
+
throw new Error("The binding expiration is invalid.");
|
|
28
|
+
const policy = record(record(binding.expected.setup).policy);
|
|
29
|
+
const swaps = list(policy.swaps).map(record);
|
|
30
|
+
if (swaps.length !== 1 ||
|
|
31
|
+
list(policy.transfers).length ||
|
|
32
|
+
list(policy.lending).length ||
|
|
33
|
+
policy.staking !== null)
|
|
34
|
+
throw new Error("Only one bounded swap policy is supported.");
|
|
35
|
+
const swap = swaps[0];
|
|
36
|
+
if (coinType(swap.coinIn) !== coinType(binding.coinIn.type) ||
|
|
37
|
+
coinType(swap.coinOut) !== coinType(binding.coinOut.type) ||
|
|
38
|
+
list(swap.pools).length !== 1 ||
|
|
39
|
+
address(list(swap.pools)[0]) !== address(binding.poolId))
|
|
40
|
+
throw new Error("Bound swap policy, pool or coin direction changed.");
|
|
41
|
+
integer(swap.perTrade, true);
|
|
42
|
+
integer(swap.daily, true);
|
|
43
|
+
integer(swap.minPrice, true);
|
|
44
|
+
}
|
|
45
|
+
export function validateInput(binding, input) {
|
|
46
|
+
validateBinding(binding);
|
|
47
|
+
assertBindingPolicy(binding, input);
|
|
48
|
+
for (const key of ["coinIn", "coinOut"]) {
|
|
49
|
+
if (binding[key].symbol !== input[key].symbol ||
|
|
50
|
+
binding[key].decimals !== input[key].decimals ||
|
|
51
|
+
input[key].decimals !== (input[key].symbol === "SUI" ? 9 : 6))
|
|
52
|
+
throw new Error("Coin metadata changed from the verified testnet binding.");
|
|
53
|
+
}
|
|
54
|
+
if (input.direction !==
|
|
55
|
+
(input.coinIn.symbol === "USDC" ? "usdc_to_sui" : "sui_to_usdc"))
|
|
56
|
+
throw new Error("The swap direction changed from the approved plan.");
|
|
57
|
+
const setup = record(binding.expected.setup), policy = record(list(record(setup.policy).swaps)[0]);
|
|
58
|
+
if (binding.planHash !== input.planHash ||
|
|
59
|
+
address(binding.owner) !== address(input.expectedOwner) ||
|
|
60
|
+
address(binding.poolId) !== address(input.poolId) ||
|
|
61
|
+
coinType(binding.coinIn.type) !== coinType(input.coinIn.type) ||
|
|
62
|
+
coinType(binding.coinOut.type) !== coinType(input.coinOut.type))
|
|
63
|
+
throw new Error("Plan, owner, coin or pool does not match the approved binding.");
|
|
64
|
+
if (integer(input.amountIn, true) !== integer(policy.perTrade, true) ||
|
|
65
|
+
integer(input.dailyLimitIn, true) !== integer(policy.daily, true) ||
|
|
66
|
+
integer(input.budgetIn, true) !==
|
|
67
|
+
integer(record(setup.allowances)[coinType(input.coinIn.type)], true))
|
|
68
|
+
throw new Error("Plan spending limits do not match the approved policy.");
|
|
69
|
+
if (input.coinIn.symbol === input.coinOut.symbol ||
|
|
70
|
+
!["SUI", "USDC"].includes(input.coinIn.symbol) ||
|
|
71
|
+
!["SUI", "USDC"].includes(input.coinOut.symbol))
|
|
72
|
+
throw new Error("Only SUI/demo-USDC swaps are supported.");
|
|
73
|
+
}
|
|
74
|
+
export function assertDeployment(binding, deployment) {
|
|
75
|
+
const hash = createHash("sha256").update(canonical(deployment)).digest("hex");
|
|
76
|
+
if (hash !== binding.deploymentHash)
|
|
77
|
+
throw new Error("Executable deployment metadata changed from the verified binding.");
|
|
78
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { address, coinType, decode, integer, list, record, stable, } from "./codec.js";
|
|
2
|
+
import { readInspection } from "./inspection.js";
|
|
3
|
+
import { assertDeployment } from "./policy.js";
|
|
4
|
+
export async function validatePrepared(binding, prepared, context, bounds) {
|
|
5
|
+
if (!prepared.digest ||
|
|
6
|
+
!Number.isSafeInteger(prepared.expiresAt) ||
|
|
7
|
+
prepared.expiresAt <= bounds.now() ||
|
|
8
|
+
prepared.expiresAt > prepared.quoteAt + bounds.preparedTtlMs ||
|
|
9
|
+
prepared.expiresAt > Date.parse(binding.expiresAt))
|
|
10
|
+
throw new Error("Prepared transaction expired or has invalid validity bounds.");
|
|
11
|
+
integer(prepared.amountIn, true);
|
|
12
|
+
integer(prepared.minOut, true);
|
|
13
|
+
integer(prepared.quotedOut, true);
|
|
14
|
+
if (!Number.isSafeInteger(prepared.quoteAt) ||
|
|
15
|
+
prepared.quoteAt > bounds.now() + 1000 ||
|
|
16
|
+
bounds.now() - prepared.quoteAt > bounds.quoteMaxAgeMs ||
|
|
17
|
+
integer(prepared.minOut) > integer(prepared.quotedOut))
|
|
18
|
+
throw new Error("Prepared quote is stale or invalid.");
|
|
19
|
+
const bytes = decode(prepared.bytes), tx = context.sdk.transactionFrom(bytes), data = tx.getData();
|
|
20
|
+
if ((await tx.getDigest()) !== prepared.digest)
|
|
21
|
+
throw new Error("Prepared transaction digest does not match its bytes.");
|
|
22
|
+
assertDeployment(binding, context.session.identity.deployment);
|
|
23
|
+
if (address(data.sender) !== address(binding.sessionAddress) ||
|
|
24
|
+
address(data.gasData.owner) !== address(binding.sessionAddress) ||
|
|
25
|
+
!Array.isArray(data.gasData.payment) ||
|
|
26
|
+
data.gasData.payment.length)
|
|
27
|
+
throw new Error("Prepared sender or gas ownership changed.");
|
|
28
|
+
const gasBudget = integer(data.gasData.budget, true);
|
|
29
|
+
if (gasBudget > 20000000n ||
|
|
30
|
+
gasBudget > integer(readInspection(binding, context).agentGasBalance))
|
|
31
|
+
throw new Error("Prepared gas budget exceeds the permitted local gas bound.");
|
|
32
|
+
const validity = data.expiration?.ValidDuring ?? data.expiration?.Validity;
|
|
33
|
+
if (!validity ||
|
|
34
|
+
validity.chain !== binding.chainIdentifier ||
|
|
35
|
+
validity.minTimestamp !== null ||
|
|
36
|
+
validity.maxTimestamp !== null ||
|
|
37
|
+
typeof validity.nonce !== "number" ||
|
|
38
|
+
!Number.isInteger(validity.nonce) ||
|
|
39
|
+
validity.nonce < 0 ||
|
|
40
|
+
validity.nonce > 0xffffffff)
|
|
41
|
+
throw new Error("Prepared transaction has invalid chain, epoch-only expiration or nonce bounds.");
|
|
42
|
+
const minEpoch = integer(validity.minEpoch), maxEpoch = integer(validity.maxEpoch), { systemState } = await context.session.client.getCurrentSystemState(), currentEpoch = integer(systemState.epoch);
|
|
43
|
+
if (maxEpoch > 18446744073709551615n ||
|
|
44
|
+
maxEpoch !== minEpoch + 1n ||
|
|
45
|
+
currentEpoch < minEpoch ||
|
|
46
|
+
currentEpoch > maxEpoch)
|
|
47
|
+
throw new Error("Prepared transaction epoch window is invalid or expired.");
|
|
48
|
+
assertDeployment(binding, context.session.identity.deployment);
|
|
49
|
+
const deployment = context.session.identity.deployment, active = context.session.identity.active;
|
|
50
|
+
const pool = list(deployment.pools)
|
|
51
|
+
.map(record)
|
|
52
|
+
.find((candidate) => address(candidate.poolId) === address(binding.poolId));
|
|
53
|
+
if (!pool)
|
|
54
|
+
throw new Error("The bound pool is missing from the official session.");
|
|
55
|
+
const isDeepbook = pool.venue === "deepbook", forward = coinType(pool.coinA) === coinType(binding.coinIn.type);
|
|
56
|
+
if (!["deepbook", "cetus"].includes(String(pool.venue)))
|
|
57
|
+
throw new Error("Unsupported venue.");
|
|
58
|
+
const functions = [
|
|
59
|
+
"begin_swap",
|
|
60
|
+
isDeepbook
|
|
61
|
+
? forward
|
|
62
|
+
? "deepbook_base_to_quote"
|
|
63
|
+
: "deepbook_quote_to_base"
|
|
64
|
+
: forward
|
|
65
|
+
? "cetus_a_to_b"
|
|
66
|
+
: "cetus_b_to_a",
|
|
67
|
+
"finish_swap",
|
|
68
|
+
];
|
|
69
|
+
if (data.commands.length !== 3)
|
|
70
|
+
throw new Error("Prepared bytes contain unexpected commands.");
|
|
71
|
+
const used = new Set();
|
|
72
|
+
const inputAt = (arg) => {
|
|
73
|
+
const index = record(arg).Input;
|
|
74
|
+
if (typeof index !== "number" ||
|
|
75
|
+
!Number.isInteger(index) ||
|
|
76
|
+
!data.inputs[index])
|
|
77
|
+
throw new Error("Unexpected transaction input.");
|
|
78
|
+
used.add(index);
|
|
79
|
+
return data.inputs[index];
|
|
80
|
+
};
|
|
81
|
+
const objectArg = (arg, expected) => {
|
|
82
|
+
const object = record(inputAt(arg).Object);
|
|
83
|
+
const ref = record(object.SharedObject ?? object.ImmOrOwnedObject ?? object.Receiving);
|
|
84
|
+
if (address(ref.objectId) !== address(expected))
|
|
85
|
+
throw new Error("Prepared object input differs from its binding.");
|
|
86
|
+
};
|
|
87
|
+
const resultArg = (arg, expected) => {
|
|
88
|
+
if (record(arg).Result !== expected)
|
|
89
|
+
throw new Error("Unexpected swap route result.");
|
|
90
|
+
};
|
|
91
|
+
const calls = data.commands.map((command, index) => {
|
|
92
|
+
const call = record(command.MoveCall);
|
|
93
|
+
if (address(call.package) !== address(deployment.publishedAt) ||
|
|
94
|
+
call.module !== "pass" ||
|
|
95
|
+
call.function !== functions[index])
|
|
96
|
+
throw new Error("Prepared bytes call an unapproved function.");
|
|
97
|
+
const expectedTypes = index === 1
|
|
98
|
+
? [
|
|
99
|
+
binding.coinIn.type,
|
|
100
|
+
binding.coinOut.type,
|
|
101
|
+
String(pool.coinA),
|
|
102
|
+
String(pool.coinB),
|
|
103
|
+
]
|
|
104
|
+
: [binding.coinIn.type, binding.coinOut.type];
|
|
105
|
+
if (stable(list(call.typeArguments).map(coinType)) !==
|
|
106
|
+
stable(expectedTypes.map(coinType)))
|
|
107
|
+
throw new Error("Prepared coin type arguments changed.");
|
|
108
|
+
return list(call.arguments);
|
|
109
|
+
});
|
|
110
|
+
const first = calls[0], middle = calls[1], last = calls[2];
|
|
111
|
+
if (first.length !== 5 ||
|
|
112
|
+
middle.length !== (isDeepbook ? 4 : 5) ||
|
|
113
|
+
last.length !== 3)
|
|
114
|
+
throw new Error("Unexpected swap call arguments.");
|
|
115
|
+
const allowance = record(record(active.allowances)[coinType(binding.coinIn.type)]).allowance;
|
|
116
|
+
objectArg(first[0], binding.passId);
|
|
117
|
+
objectArg(first[1], binding.configId);
|
|
118
|
+
objectArg(first[2], allowance);
|
|
119
|
+
objectArg(first[4], "0x6");
|
|
120
|
+
const withdrawal = record(inputAt(first[3]).FundsWithdrawal), source = record(record(withdrawal.withdrawFrom).SenderAllowance);
|
|
121
|
+
if (record(withdrawal.reservation).MaxAmountU64 !== prepared.amountIn ||
|
|
122
|
+
coinType(record(withdrawal.typeArg).Balance) !==
|
|
123
|
+
coinType(binding.coinIn.type) ||
|
|
124
|
+
address(source.funder) !== address(binding.owner) ||
|
|
125
|
+
address(source.allowance) !== address(allowance))
|
|
126
|
+
throw new Error("Prepared allowance withdrawal changed.");
|
|
127
|
+
objectArg(middle[0], binding.passId);
|
|
128
|
+
resultArg(middle[1], 0);
|
|
129
|
+
if (isDeepbook) {
|
|
130
|
+
objectArg(middle[2], binding.poolId);
|
|
131
|
+
objectArg(middle[3], "0x6");
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
objectArg(middle[2], record(record(deployment.protocols).cetus).configId);
|
|
135
|
+
objectArg(middle[3], binding.poolId);
|
|
136
|
+
objectArg(middle[4], "0x6");
|
|
137
|
+
}
|
|
138
|
+
objectArg(last[0], binding.passId);
|
|
139
|
+
resultArg(last[1], 1);
|
|
140
|
+
const pure = record(inputAt(last[2]).Pure).bytes;
|
|
141
|
+
if (typeof pure !== "string")
|
|
142
|
+
throw new Error("Missing minimum-output input.");
|
|
143
|
+
const minBytes = decode(pure);
|
|
144
|
+
if (minBytes.length !== 8 ||
|
|
145
|
+
Buffer.from(minBytes).readBigUInt64LE() !== integer(prepared.minOut, true))
|
|
146
|
+
throw new Error("Prepared minimum output changed.");
|
|
147
|
+
if (used.size !== data.inputs.length)
|
|
148
|
+
throw new Error("Prepared bytes contain unused or extra inputs.");
|
|
149
|
+
return bytes;
|
|
150
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { address, coinType, decimalUnits, integer, list, record, signedInteger, } from "./codec.js";
|
|
2
|
+
export function decodeReceipt(binding, prepared, result) {
|
|
3
|
+
const value = record(result.Transaction ?? result.FailedTransaction), status = record(value.status);
|
|
4
|
+
if (value.digest !== prepared.digest || typeof status.success !== "boolean")
|
|
5
|
+
throw new Error("The returned transaction does not match the persisted digest.");
|
|
6
|
+
const timestampMs = value.timestampMs;
|
|
7
|
+
if (typeof timestampMs !== "number" ||
|
|
8
|
+
!Number.isSafeInteger(timestampMs) ||
|
|
9
|
+
timestampMs <= 0 ||
|
|
10
|
+
!Number.isFinite(new Date(timestampMs).getTime()))
|
|
11
|
+
throw new Error("The confirmed chain timestamp is unavailable; reconcile this digest before recording its UTC day.");
|
|
12
|
+
const gas = record(record(value.effects).gasUsed);
|
|
13
|
+
const gasSui = decimalUnits(integer(gas.computationCost) +
|
|
14
|
+
integer(gas.storageCost) -
|
|
15
|
+
integer(gas.storageRebate), 9);
|
|
16
|
+
let input = 0n, output = 0n, accountingVerified = Array.isArray(value.balanceChanges);
|
|
17
|
+
for (const raw of list(value.balanceChanges)) {
|
|
18
|
+
const change = record(raw);
|
|
19
|
+
if (address(change.address) !== address(binding.owner))
|
|
20
|
+
continue;
|
|
21
|
+
const coin = coinType(change.coinType), delta = signedInteger(change.amount);
|
|
22
|
+
if (coin === coinType(binding.coinIn.type))
|
|
23
|
+
input += delta;
|
|
24
|
+
else if (coin === coinType(binding.coinOut.type))
|
|
25
|
+
output += delta;
|
|
26
|
+
else if (delta !== 0n)
|
|
27
|
+
accountingVerified = false;
|
|
28
|
+
}
|
|
29
|
+
if (status.success)
|
|
30
|
+
accountingVerified =
|
|
31
|
+
accountingVerified &&
|
|
32
|
+
input < 0n &&
|
|
33
|
+
-input <= integer(prepared.amountIn, true) &&
|
|
34
|
+
output >= integer(prepared.minOut, true);
|
|
35
|
+
else
|
|
36
|
+
accountingVerified = accountingVerified && input === 0n && output === 0n;
|
|
37
|
+
return {
|
|
38
|
+
digest: prepared.digest,
|
|
39
|
+
success: status.success,
|
|
40
|
+
at: timestampMs,
|
|
41
|
+
amountIn: accountingVerified && status.success ? (-input).toString() : "0",
|
|
42
|
+
amountOut: accountingVerified && status.success ? output.toString() : "0",
|
|
43
|
+
gasSui,
|
|
44
|
+
accountingVerified,
|
|
45
|
+
...(status.success
|
|
46
|
+
? {}
|
|
47
|
+
: { error: JSON.stringify(status.error ?? "Chain execution failed.") }),
|
|
48
|
+
};
|
|
49
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { pathToFileURL } from "node:url";
|
|
3
|
+
export async function officialSDK() {
|
|
4
|
+
const packageName = "@mysten-incubation/sui-agent-pass";
|
|
5
|
+
const require = createRequire(import.meta.url);
|
|
6
|
+
const sdkRequire = createRequire(require.resolve(`${packageName}/agent`));
|
|
7
|
+
// Resolve utilities in the official SDK's dependency context (2.33.1), not
|
|
8
|
+
// the frontend's potentially different root @mysten/sui version.
|
|
9
|
+
const agentModule = `${packageName}/agent`, chainModule = `${packageName}/chain`;
|
|
10
|
+
const helperModule = "../authorization.js";
|
|
11
|
+
const [agent, chain, transactions, verify, authorization] = await Promise.all([
|
|
12
|
+
import(agentModule),
|
|
13
|
+
import(chainModule),
|
|
14
|
+
import(pathToFileURL(sdkRequire.resolve("@mysten/sui/transactions")).href),
|
|
15
|
+
import(pathToFileURL(sdkRequire.resolve("@mysten/sui/verify")).href),
|
|
16
|
+
import(helperModule),
|
|
17
|
+
]);
|
|
18
|
+
return {
|
|
19
|
+
open: agent.Agent.open.bind(agent.Agent),
|
|
20
|
+
assertChain: chain.assertChain,
|
|
21
|
+
readStatus: chain.readStatus,
|
|
22
|
+
readAllowance: chain.readAllowance,
|
|
23
|
+
quote: agent.quote,
|
|
24
|
+
swap: agent.swap,
|
|
25
|
+
transactionFrom: transactions.Transaction.from.bind(transactions.Transaction),
|
|
26
|
+
verifySignature: async (bytes, signature, owner, client) => {
|
|
27
|
+
await verify.verifyTransactionSignature(bytes, signature, {
|
|
28
|
+
address: owner,
|
|
29
|
+
client,
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
assertLiveAuthorization: authorization.assertLiveAuthorization,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|