@toon-protocol/client 0.24.0 → 0.25.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.
- package/README.md +28 -1
- package/dist/index.d.ts +161 -17
- package/dist/index.js +433 -78
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -1111,6 +1111,64 @@ var ConnectorEdgeClient = class {
|
|
|
1111
1111
|
}
|
|
1112
1112
|
}
|
|
1113
1113
|
};
|
|
1114
|
+
function settlementStr(s, key2) {
|
|
1115
|
+
const value = s[key2];
|
|
1116
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
1117
|
+
throw new ConnectorEdgeError(
|
|
1118
|
+
`x402 greeting settlement facts lack '${key2}'`,
|
|
1119
|
+
"TERMS_MALFORMED"
|
|
1120
|
+
);
|
|
1121
|
+
}
|
|
1122
|
+
return value;
|
|
1123
|
+
}
|
|
1124
|
+
function settlementDecimals(s) {
|
|
1125
|
+
const decimals = s["decimals"];
|
|
1126
|
+
if (typeof decimals !== "number") {
|
|
1127
|
+
throw new ConnectorEdgeError(
|
|
1128
|
+
"x402 greeting settlement facts lack 'decimals'",
|
|
1129
|
+
"TERMS_MALFORMED"
|
|
1130
|
+
);
|
|
1131
|
+
}
|
|
1132
|
+
return decimals;
|
|
1133
|
+
}
|
|
1134
|
+
function parseEvmSettlementTerms(s) {
|
|
1135
|
+
return {
|
|
1136
|
+
chain: settlementStr(s, "chain"),
|
|
1137
|
+
settlementAddress: settlementStr(s, "settlementAddress"),
|
|
1138
|
+
tokenNetworkRegistry: settlementStr(s, "tokenNetworkRegistry"),
|
|
1139
|
+
tokenNetwork: settlementStr(s, "tokenNetwork"),
|
|
1140
|
+
tokenAddress: settlementStr(s, "tokenAddress"),
|
|
1141
|
+
decimals: settlementDecimals(s)
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
function parseSolanaSettlementTerms(s) {
|
|
1145
|
+
return {
|
|
1146
|
+
chain: settlementStr(s, "chain"),
|
|
1147
|
+
settlementAddress: settlementStr(s, "settlementAddress"),
|
|
1148
|
+
programId: settlementStr(s, "programId"),
|
|
1149
|
+
tokenAddress: settlementStr(s, "tokenAddress"),
|
|
1150
|
+
decimals: settlementDecimals(s)
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
function parseChainSettlementEntry(raw) {
|
|
1154
|
+
if (typeof raw !== "object" || raw === null) {
|
|
1155
|
+
throw new ConnectorEdgeError(
|
|
1156
|
+
"x402 greeting settlements entry is not an object",
|
|
1157
|
+
"TERMS_MALFORMED"
|
|
1158
|
+
);
|
|
1159
|
+
}
|
|
1160
|
+
const s = raw;
|
|
1161
|
+
if (typeof s["programId"] === "string") {
|
|
1162
|
+
return { kind: "solana", ...parseSolanaSettlementTerms(s) };
|
|
1163
|
+
}
|
|
1164
|
+
if (typeof s["tokenNetworkRegistry"] === "string") {
|
|
1165
|
+
return { kind: "evm", ...parseEvmSettlementTerms(s) };
|
|
1166
|
+
}
|
|
1167
|
+
throw new ConnectorEdgeError(
|
|
1168
|
+
"x402 greeting settlements entry names neither an EVM ('tokenNetworkRegistry') nor a Solana ('programId') chain",
|
|
1169
|
+
"TERMS_MALFORMED"
|
|
1170
|
+
);
|
|
1171
|
+
}
|
|
1114
1172
|
function parseConnectorRouteTerms(body) {
|
|
1115
1173
|
if (typeof body !== "object" || body === null) {
|
|
1116
1174
|
throw new ConnectorEdgeError(
|
|
@@ -1131,9 +1189,24 @@ function parseConnectorRouteTerms(body) {
|
|
|
1131
1189
|
}
|
|
1132
1190
|
const destination = typeof greeting.resource?.url === "string" ? greeting.resource.url : "";
|
|
1133
1191
|
const extra = option["extra"];
|
|
1192
|
+
const rawSettlements = extra?.settlements;
|
|
1193
|
+
let settlements;
|
|
1194
|
+
if (rawSettlements !== void 0) {
|
|
1195
|
+
if (!Array.isArray(rawSettlements)) {
|
|
1196
|
+
throw new ConnectorEdgeError(
|
|
1197
|
+
"x402 greeting settlements is not an array",
|
|
1198
|
+
"TERMS_MALFORMED"
|
|
1199
|
+
);
|
|
1200
|
+
}
|
|
1201
|
+
settlements = rawSettlements.map(parseChainSettlementEntry);
|
|
1202
|
+
}
|
|
1134
1203
|
const rawSettlement = extra?.settlement;
|
|
1135
1204
|
if (rawSettlement === void 0) {
|
|
1136
|
-
return {
|
|
1205
|
+
return {
|
|
1206
|
+
destination,
|
|
1207
|
+
price: option["amount"],
|
|
1208
|
+
...settlements ? { settlements } : {}
|
|
1209
|
+
};
|
|
1137
1210
|
}
|
|
1138
1211
|
if (typeof rawSettlement !== "object" || rawSettlement === null) {
|
|
1139
1212
|
throw new ConnectorEdgeError(
|
|
@@ -1141,35 +1214,13 @@ function parseConnectorRouteTerms(body) {
|
|
|
1141
1214
|
"TERMS_MALFORMED"
|
|
1142
1215
|
);
|
|
1143
1216
|
}
|
|
1144
|
-
const s = rawSettlement;
|
|
1145
|
-
const str = (key2) => {
|
|
1146
|
-
const value = s[key2];
|
|
1147
|
-
if (typeof value !== "string" || value.length === 0) {
|
|
1148
|
-
throw new ConnectorEdgeError(
|
|
1149
|
-
`x402 greeting settlement facts lack '${key2}'`,
|
|
1150
|
-
"TERMS_MALFORMED"
|
|
1151
|
-
);
|
|
1152
|
-
}
|
|
1153
|
-
return value;
|
|
1154
|
-
};
|
|
1155
|
-
const decimals = s["decimals"];
|
|
1156
|
-
if (typeof decimals !== "number") {
|
|
1157
|
-
throw new ConnectorEdgeError(
|
|
1158
|
-
"x402 greeting settlement facts lack 'decimals'",
|
|
1159
|
-
"TERMS_MALFORMED"
|
|
1160
|
-
);
|
|
1161
|
-
}
|
|
1162
1217
|
return {
|
|
1163
1218
|
destination,
|
|
1164
1219
|
price: option["amount"],
|
|
1165
|
-
settlement:
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
tokenNetwork: str("tokenNetwork"),
|
|
1170
|
-
tokenAddress: str("tokenAddress"),
|
|
1171
|
-
decimals
|
|
1172
|
-
}
|
|
1220
|
+
settlement: parseEvmSettlementTerms(
|
|
1221
|
+
rawSettlement
|
|
1222
|
+
),
|
|
1223
|
+
...settlements ? { settlements } : {}
|
|
1173
1224
|
};
|
|
1174
1225
|
}
|
|
1175
1226
|
|
|
@@ -2812,7 +2863,10 @@ function deriveAssociatedTokenAccount(owner, tokenMint) {
|
|
|
2812
2863
|
padTo32(base58Decode(TOKEN_PROGRAM_ID2)),
|
|
2813
2864
|
padTo32(base58Decode(tokenMint))
|
|
2814
2865
|
];
|
|
2815
|
-
const { pda } = findProgramAddress(
|
|
2866
|
+
const { pda } = findProgramAddress(
|
|
2867
|
+
seeds,
|
|
2868
|
+
padTo32(base58Decode(ASSOCIATED_TOKEN_PROGRAM_ID))
|
|
2869
|
+
);
|
|
2816
2870
|
return base58Encode(pda);
|
|
2817
2871
|
}
|
|
2818
2872
|
function deriveVaultPDA(channelPDA, programId) {
|
|
@@ -2830,6 +2884,24 @@ function buildBalanceProofMessage(channelPDA, nonce, transferredAmount) {
|
|
|
2830
2884
|
return message;
|
|
2831
2885
|
}
|
|
2832
2886
|
var rpcIdCounter = 1;
|
|
2887
|
+
var SolanaRpcError = class extends Error {
|
|
2888
|
+
constructor(method, code, rpcMessage) {
|
|
2889
|
+
super(`Solana RPC error [${method}]: ${rpcMessage} (code ${code})`);
|
|
2890
|
+
this.method = method;
|
|
2891
|
+
this.code = code;
|
|
2892
|
+
this.rpcMessage = rpcMessage;
|
|
2893
|
+
this.name = "SolanaRpcError";
|
|
2894
|
+
}
|
|
2895
|
+
method;
|
|
2896
|
+
code;
|
|
2897
|
+
rpcMessage;
|
|
2898
|
+
};
|
|
2899
|
+
function isAccountNotFoundError(err) {
|
|
2900
|
+
if (!(err instanceof SolanaRpcError)) return false;
|
|
2901
|
+
return /could not find account|account not found|account does not exist/i.test(
|
|
2902
|
+
err.rpcMessage
|
|
2903
|
+
);
|
|
2904
|
+
}
|
|
2833
2905
|
async function solanaRpc(rpcUrl, method, params = []) {
|
|
2834
2906
|
const res = await fetch(rpcUrl, {
|
|
2835
2907
|
method: "POST",
|
|
@@ -2844,9 +2916,7 @@ async function solanaRpc(rpcUrl, method, params = []) {
|
|
|
2844
2916
|
});
|
|
2845
2917
|
const json = await res.json();
|
|
2846
2918
|
if (json.error) {
|
|
2847
|
-
throw new
|
|
2848
|
-
`Solana RPC error [${method}]: ${json.error.message} (code ${json.error.code})`
|
|
2849
|
-
);
|
|
2919
|
+
throw new SolanaRpcError(method, json.error.code, json.error.message);
|
|
2850
2920
|
}
|
|
2851
2921
|
return json.result;
|
|
2852
2922
|
}
|
|
@@ -2863,6 +2933,50 @@ async function getAccountInfo(rpcUrl, pubkey) {
|
|
|
2863
2933
|
]);
|
|
2864
2934
|
return result.value;
|
|
2865
2935
|
}
|
|
2936
|
+
async function getTokenAccountBalance(rpcUrl, tokenAccount) {
|
|
2937
|
+
try {
|
|
2938
|
+
const result = await solanaRpc(rpcUrl, "getTokenAccountBalance", [
|
|
2939
|
+
tokenAccount,
|
|
2940
|
+
{ commitment: "confirmed" }
|
|
2941
|
+
]);
|
|
2942
|
+
const amount = result?.value?.amount;
|
|
2943
|
+
return amount === void 0 ? null : BigInt(amount);
|
|
2944
|
+
} catch (err) {
|
|
2945
|
+
if (isAccountNotFoundError(err)) return null;
|
|
2946
|
+
throw err;
|
|
2947
|
+
}
|
|
2948
|
+
}
|
|
2949
|
+
async function getLamports(rpcUrl, pubkey) {
|
|
2950
|
+
const result = await solanaRpc(rpcUrl, "getBalance", [
|
|
2951
|
+
pubkey,
|
|
2952
|
+
{ commitment: "confirmed" }
|
|
2953
|
+
]);
|
|
2954
|
+
return BigInt(result?.value ?? 0);
|
|
2955
|
+
}
|
|
2956
|
+
var LAMPORTS_PER_SIGNATURE = 5000n;
|
|
2957
|
+
var MIN_LAMPORTS_FOR_CHANNEL_OPEN = 4179040n;
|
|
2958
|
+
var MIN_LAMPORTS_FOR_DEPOSIT = LAMPORTS_PER_SIGNATURE;
|
|
2959
|
+
async function assertOpenFunding(opts) {
|
|
2960
|
+
const { rpcUrl, payerPubkey, tokenMint, minLamports, deposit } = opts;
|
|
2961
|
+
const lamports = await getLamports(rpcUrl, payerPubkey);
|
|
2962
|
+
if (lamports < minLamports) {
|
|
2963
|
+
throw new ChannelFundingError(
|
|
2964
|
+
`Solana settlement wallet ${payerPubkey} holds ${lamports} lamports, below the ${minLamports} needed here (rent for the channel + vault accounts, plus signature fees). Native SOL is separate from the settlement token: holding the SPL token is not enough. Airdrop/fund SOL to that address and retry.`
|
|
2965
|
+
);
|
|
2966
|
+
}
|
|
2967
|
+
if (!deposit || deposit.amount <= 0n) return;
|
|
2968
|
+
const held = await getTokenAccountBalance(rpcUrl, deposit.payerTokenAccount);
|
|
2969
|
+
if (held === null) {
|
|
2970
|
+
throw new ChannelFundingError(
|
|
2971
|
+
`Solana token account ${deposit.payerTokenAccount} does not exist, so the ${deposit.amount} base-unit channel deposit of mint ${tokenMint} cannot be funded. Fund the settlement wallet with that token (which creates the associated token account) and retry.`
|
|
2972
|
+
);
|
|
2973
|
+
}
|
|
2974
|
+
if (held < deposit.amount) {
|
|
2975
|
+
throw new ChannelFundingError(
|
|
2976
|
+
`Solana token account ${deposit.payerTokenAccount} holds ${held} base units of mint ${tokenMint}, short of the ${deposit.amount} base-unit channel deposit. Fund the settlement wallet, or lower the deposit (\`solanaChannel.deposit.amount\` / \`initialDeposit\`), and retry.`
|
|
2977
|
+
);
|
|
2978
|
+
}
|
|
2979
|
+
}
|
|
2866
2980
|
async function waitForConfirmation(rpcUrl, signature, timeoutMs = 3e4) {
|
|
2867
2981
|
const start = Date.now();
|
|
2868
2982
|
while (Date.now() - start < timeoutMs) {
|
|
@@ -3015,6 +3129,13 @@ async function buildAndSendTransaction(rpcUrl, feePayer, instructions, additiona
|
|
|
3015
3129
|
return txSig;
|
|
3016
3130
|
}
|
|
3017
3131
|
var STATE_MAP = ["opened", "closed", "settled"];
|
|
3132
|
+
function readU64LE(data, offset) {
|
|
3133
|
+
let value = 0n;
|
|
3134
|
+
for (let i = 7; i >= 0; i--) {
|
|
3135
|
+
value = value << 8n | BigInt(data[offset + i] ?? 0);
|
|
3136
|
+
}
|
|
3137
|
+
return value;
|
|
3138
|
+
}
|
|
3018
3139
|
async function getChannelAccountState(rpcUrl, channelPDA) {
|
|
3019
3140
|
const info = await getAccountInfo(rpcUrl, channelPDA);
|
|
3020
3141
|
if (!info) return { exists: false };
|
|
@@ -3027,9 +3148,53 @@ async function getChannelAccountState(rpcUrl, channelPDA) {
|
|
|
3027
3148
|
exists: true,
|
|
3028
3149
|
state: STATE_MAP[data[160] ?? 0] ?? "opened",
|
|
3029
3150
|
participantA: base58Encode(data.slice(8, 40)),
|
|
3030
|
-
participantB: base58Encode(data.slice(40, 72))
|
|
3151
|
+
participantB: base58Encode(data.slice(40, 72)),
|
|
3152
|
+
depositA: readU64LE(data, 104),
|
|
3153
|
+
depositB: readU64LE(data, 112)
|
|
3031
3154
|
};
|
|
3032
3155
|
}
|
|
3156
|
+
function ownDeposit(account, payerPubkey) {
|
|
3157
|
+
if (account.participantA === payerPubkey) return account.depositA ?? 0n;
|
|
3158
|
+
if (account.participantB === payerPubkey) return account.depositB ?? 0n;
|
|
3159
|
+
throw new Error(
|
|
3160
|
+
`Solana channel participants (${account.participantA}, ${account.participantB}) do not include the payer ${payerPubkey} \u2014 refusing to reason about its collateral.`
|
|
3161
|
+
);
|
|
3162
|
+
}
|
|
3163
|
+
async function topUpExistingChannel(opts) {
|
|
3164
|
+
const {
|
|
3165
|
+
rpcUrl,
|
|
3166
|
+
programId,
|
|
3167
|
+
tokenMint,
|
|
3168
|
+
channelPDA,
|
|
3169
|
+
payerPubkey,
|
|
3170
|
+
existing,
|
|
3171
|
+
deposit
|
|
3172
|
+
} = opts;
|
|
3173
|
+
if (!deposit || deposit.amount <= 0n) return {};
|
|
3174
|
+
const own = ownDeposit(existing, payerPubkey);
|
|
3175
|
+
if (own >= deposit.amount) return { depositTotal: own };
|
|
3176
|
+
const shortfall = deposit.amount - own;
|
|
3177
|
+
await assertOpenFunding({
|
|
3178
|
+
rpcUrl,
|
|
3179
|
+
payerPubkey,
|
|
3180
|
+
tokenMint,
|
|
3181
|
+
minLamports: MIN_LAMPORTS_FOR_DEPOSIT,
|
|
3182
|
+
deposit: {
|
|
3183
|
+
amount: shortfall,
|
|
3184
|
+
payerTokenAccount: deposit.payerTokenAccount
|
|
3185
|
+
}
|
|
3186
|
+
});
|
|
3187
|
+
const { depositTxSignature } = await depositSolanaChannel({
|
|
3188
|
+
rpcUrl,
|
|
3189
|
+
programId,
|
|
3190
|
+
channelPDA,
|
|
3191
|
+
payerSeed: opts.payerSeed,
|
|
3192
|
+
payerPubkey,
|
|
3193
|
+
payerTokenAccount: deposit.payerTokenAccount,
|
|
3194
|
+
amount: shortfall
|
|
3195
|
+
});
|
|
3196
|
+
return { depositTxSignature, depositTotal: own + shortfall };
|
|
3197
|
+
}
|
|
3033
3198
|
async function openSolanaChannel(params) {
|
|
3034
3199
|
const {
|
|
3035
3200
|
rpcUrl,
|
|
@@ -3048,8 +3213,28 @@ async function openSolanaChannel(params) {
|
|
|
3048
3213
|
);
|
|
3049
3214
|
const existing = await getChannelAccountState(rpcUrl, channelPDA);
|
|
3050
3215
|
if (existing.exists) {
|
|
3051
|
-
|
|
3216
|
+
if (existing.state !== "opened") {
|
|
3217
|
+
return { channelPDA, opened: false };
|
|
3218
|
+
}
|
|
3219
|
+
const topUp = await topUpExistingChannel({
|
|
3220
|
+
rpcUrl,
|
|
3221
|
+
programId,
|
|
3222
|
+
tokenMint,
|
|
3223
|
+
channelPDA,
|
|
3224
|
+
payerSeed,
|
|
3225
|
+
payerPubkey,
|
|
3226
|
+
existing,
|
|
3227
|
+
deposit: params.deposit
|
|
3228
|
+
});
|
|
3229
|
+
return { channelPDA, opened: false, ...topUp };
|
|
3052
3230
|
}
|
|
3231
|
+
await assertOpenFunding({
|
|
3232
|
+
rpcUrl,
|
|
3233
|
+
payerPubkey,
|
|
3234
|
+
tokenMint,
|
|
3235
|
+
minLamports: MIN_LAMPORTS_FOR_CHANNEL_OPEN,
|
|
3236
|
+
deposit: params.deposit
|
|
3237
|
+
});
|
|
3053
3238
|
const payerPublicKey = padTo32(base58Decode(payerPubkey));
|
|
3054
3239
|
const payer = { publicKey: payerPublicKey, privateKey: payerSeed };
|
|
3055
3240
|
const { pda: vaultPDA } = deriveVaultPDA(channelPDA, programId);
|
|
@@ -3087,10 +3272,24 @@ async function openSolanaChannel(params) {
|
|
|
3087
3272
|
amount: params.deposit.amount
|
|
3088
3273
|
}));
|
|
3089
3274
|
}
|
|
3090
|
-
return {
|
|
3275
|
+
return {
|
|
3276
|
+
channelPDA,
|
|
3277
|
+
opened: true,
|
|
3278
|
+
initTxSignature,
|
|
3279
|
+
depositTxSignature,
|
|
3280
|
+
...depositTxSignature ? { depositTotal: params.deposit?.amount } : {}
|
|
3281
|
+
};
|
|
3091
3282
|
}
|
|
3092
3283
|
async function depositSolanaChannel(params) {
|
|
3093
|
-
const {
|
|
3284
|
+
const {
|
|
3285
|
+
rpcUrl,
|
|
3286
|
+
programId,
|
|
3287
|
+
channelPDA,
|
|
3288
|
+
payerSeed,
|
|
3289
|
+
payerPubkey,
|
|
3290
|
+
payerTokenAccount,
|
|
3291
|
+
amount
|
|
3292
|
+
} = params;
|
|
3094
3293
|
if (amount <= 0n) throw new Error("Solana deposit amount must be positive.");
|
|
3095
3294
|
const payer = {
|
|
3096
3295
|
publicKey: padTo32(base58Decode(payerPubkey)),
|
|
@@ -3337,7 +3536,10 @@ var OnChainChannelClient = class {
|
|
|
3337
3536
|
}
|
|
3338
3537
|
const chainPrefix = ctx.chain.split(":")[0];
|
|
3339
3538
|
if (chainPrefix === "solana") {
|
|
3340
|
-
return this.depositSolana(channelId, amount, opts.currentDeposit
|
|
3539
|
+
return this.depositSolana(channelId, amount, opts.currentDeposit, {
|
|
3540
|
+
programId: ctx.tokenNetworkAddress,
|
|
3541
|
+
...ctx.tokenAddress ? { tokenMint: ctx.tokenAddress } : {}
|
|
3542
|
+
});
|
|
3341
3543
|
}
|
|
3342
3544
|
if (chainPrefix === "mina") {
|
|
3343
3545
|
throw new Error(
|
|
@@ -3351,8 +3553,14 @@ var OnChainChannelClient = class {
|
|
|
3351
3553
|
* vault. Incremental on-chain (the program adds `amount`), so the new total is
|
|
3352
3554
|
* the caller-tracked current plus the delta. Requires the funded payer token
|
|
3353
3555
|
* account (the funded ATA) from the Solana channel config.
|
|
3556
|
+
*
|
|
3557
|
+
* `channel` carries the program and mint the channel was actually OPENED
|
|
3558
|
+
* with (the greeting's, when it named them — see
|
|
3559
|
+
* {@link OnChainChannelClient.openSolanaChannel}), NOT
|
|
3560
|
+
* `solanaConfig.programId`/`tokenMint`. Both PDAs this deposit addresses are
|
|
3561
|
+
* derived from those: the vault from the program, the payer ATA from the mint.
|
|
3354
3562
|
*/
|
|
3355
|
-
async depositSolana(channelId, amount, currentDeposit) {
|
|
3563
|
+
async depositSolana(channelId, amount, currentDeposit, channel) {
|
|
3356
3564
|
if (!this.solanaConfig) {
|
|
3357
3565
|
throw new Error("Solana channel config not set \u2014 cannot deposit.");
|
|
3358
3566
|
}
|
|
@@ -3361,21 +3569,19 @@ var OnChainChannelClient = class {
|
|
|
3361
3569
|
const payerPubkey = base58Encode2(
|
|
3362
3570
|
new Uint8Array(ed255192.getPublicKey(payerSeed))
|
|
3363
3571
|
);
|
|
3572
|
+
const tokenMint = channel.tokenMint ?? cfg.tokenMint;
|
|
3364
3573
|
let payerTokenAccount = cfg.deposit?.payerTokenAccount;
|
|
3365
3574
|
if (!payerTokenAccount) {
|
|
3366
|
-
if (!
|
|
3575
|
+
if (!tokenMint) {
|
|
3367
3576
|
throw new Error(
|
|
3368
|
-
"Solana deposit requires solanaConfig.deposit.payerTokenAccount or
|
|
3577
|
+
"Solana deposit requires solanaConfig.deposit.payerTokenAccount or a known channel mint to derive the payer ATA."
|
|
3369
3578
|
);
|
|
3370
3579
|
}
|
|
3371
|
-
payerTokenAccount = deriveAssociatedTokenAccount(
|
|
3372
|
-
payerPubkey,
|
|
3373
|
-
cfg.tokenMint
|
|
3374
|
-
);
|
|
3580
|
+
payerTokenAccount = deriveAssociatedTokenAccount(payerPubkey, tokenMint);
|
|
3375
3581
|
}
|
|
3376
3582
|
const { depositTxSignature } = await depositSolanaChannel({
|
|
3377
3583
|
rpcUrl: cfg.rpcUrl,
|
|
3378
|
-
programId:
|
|
3584
|
+
programId: channel.programId,
|
|
3379
3585
|
channelPDA: channelId,
|
|
3380
3586
|
payerSeed,
|
|
3381
3587
|
payerPubkey,
|
|
@@ -3559,12 +3765,19 @@ var OnChainChannelClient = class {
|
|
|
3559
3765
|
*
|
|
3560
3766
|
* Derives the connector-parity channel PDA
|
|
3561
3767
|
* (`[b"channel", min_pubkey, max_pubkey, token_mint]`), submits the
|
|
3562
|
-
* `initialize_channel` instruction
|
|
3563
|
-
*
|
|
3768
|
+
* `initialize_channel` instruction plus the `deposit` that collateralizes the
|
|
3769
|
+
* channel, and returns the base58 PDA as the channel id. That
|
|
3564
3770
|
* PDA is what the claim carries as `channelAccount`, and the on-chain channel
|
|
3565
3771
|
* is what the connector's `verifySolanaClaim` reads via
|
|
3566
3772
|
* `provider.getChannelState` before accepting the claim.
|
|
3567
3773
|
*
|
|
3774
|
+
* Both PDA seeds are NEGOTIATED, not configured: the mint is
|
|
3775
|
+
* `OpenChannelParams.token` and the program is `OpenChannelParams.tokenNetwork`
|
|
3776
|
+
* — the greeting's `tokenAddress`/`programId` as threaded through by
|
|
3777
|
+
* `ChannelManager.ensureChannel` — each falling back to the `solanaConfig`
|
|
3778
|
+
* default only when the negotiation named none. That is what keeps the channel
|
|
3779
|
+
* this opens and the channel the signed claim asserts the same object (#473).
|
|
3780
|
+
*
|
|
3568
3781
|
* Mirrors `openEvmChannel`'s open(+deposit) structure. Idempotent: if the
|
|
3569
3782
|
* channel account already exists on-chain, returns its PDA without
|
|
3570
3783
|
* re-initializing.
|
|
@@ -3586,6 +3799,7 @@ var OnChainChannelClient = class {
|
|
|
3586
3799
|
"Solana channel requires a token mint (OpenChannelParams.token or solanaConfig.tokenMint)"
|
|
3587
3800
|
);
|
|
3588
3801
|
}
|
|
3802
|
+
const programId = params.tokenNetwork ?? cfg.programId;
|
|
3589
3803
|
if (!params.peerAddress) {
|
|
3590
3804
|
throw new Error(
|
|
3591
3805
|
"Solana channel requires peerAddress (apex settlement pubkey, base58)"
|
|
@@ -3594,15 +3808,18 @@ var OnChainChannelClient = class {
|
|
|
3594
3808
|
const challengeDuration = BigInt(
|
|
3595
3809
|
cfg.challengeDuration ?? params.settlementTimeout ?? 86400
|
|
3596
3810
|
);
|
|
3597
|
-
const
|
|
3598
|
-
|
|
3811
|
+
const depositAmount = BigInt(
|
|
3812
|
+
cfg.deposit?.amount ?? params.initialDeposit ?? "0"
|
|
3813
|
+
);
|
|
3814
|
+
const deposit = depositAmount > 0n ? {
|
|
3815
|
+
amount: depositAmount,
|
|
3599
3816
|
// Derive the payer ATA (owner + channel mint) when not supplied — it is
|
|
3600
3817
|
// deterministic, so the caller need not thread payerTokenAccount through.
|
|
3601
|
-
payerTokenAccount: cfg.deposit
|
|
3818
|
+
payerTokenAccount: cfg.deposit?.payerTokenAccount || deriveAssociatedTokenAccount(payerPubkey, tokenMint)
|
|
3602
3819
|
} : void 0;
|
|
3603
|
-
const { channelPDA } = await openSolanaChannel({
|
|
3820
|
+
const { channelPDA, depositTotal } = await openSolanaChannel({
|
|
3604
3821
|
rpcUrl: cfg.rpcUrl,
|
|
3605
|
-
programId
|
|
3822
|
+
programId,
|
|
3606
3823
|
tokenMint,
|
|
3607
3824
|
payerSeed,
|
|
3608
3825
|
payerPubkey,
|
|
@@ -3612,9 +3829,18 @@ var OnChainChannelClient = class {
|
|
|
3612
3829
|
});
|
|
3613
3830
|
this.channelContext.set(channelPDA, {
|
|
3614
3831
|
chain: params.chain,
|
|
3615
|
-
tokenNetworkAddress:
|
|
3832
|
+
tokenNetworkAddress: programId,
|
|
3833
|
+
tokenAddress: tokenMint
|
|
3616
3834
|
});
|
|
3617
|
-
return {
|
|
3835
|
+
return {
|
|
3836
|
+
channelId: channelPDA,
|
|
3837
|
+
status: "opening",
|
|
3838
|
+
// Report the collateral now in the vault, when this call established it
|
|
3839
|
+
// (locked here on a fresh open, read from chain on a top-up). Reporting
|
|
3840
|
+
// only — no spend decision reads it; it exists so callers can show and
|
|
3841
|
+
// log real collateral instead of 0.
|
|
3842
|
+
...depositTotal !== void 0 ? { depositTotal } : {}
|
|
3843
|
+
};
|
|
3618
3844
|
}
|
|
3619
3845
|
/**
|
|
3620
3846
|
* Opens a REAL on-chain Mina payment channel on the deployed `PaymentChannel`
|
|
@@ -4785,7 +5011,8 @@ var ChannelManager = class {
|
|
|
4785
5011
|
const t = this.channels.get(channelId);
|
|
4786
5012
|
if (!t || t.closedAt === void 0) return "open";
|
|
4787
5013
|
if (t.settledAt !== void 0) return "settled";
|
|
4788
|
-
if (t.settleableAt !== void 0 && nowSec >= t.settleableAt)
|
|
5014
|
+
if (t.settleableAt !== void 0 && nowSec >= t.settleableAt)
|
|
5015
|
+
return "settleable";
|
|
4789
5016
|
return "closing";
|
|
4790
5017
|
}
|
|
4791
5018
|
/**
|
|
@@ -5919,6 +6146,14 @@ var ToonClient = class {
|
|
|
5919
6146
|
/** Concrete on-chain client, kept so deposit/withdraw can reach chain methods. */
|
|
5920
6147
|
onChainChannelClient;
|
|
5921
6148
|
peerNegotiations = /* @__PURE__ */ new Map();
|
|
6149
|
+
/**
|
|
6150
|
+
* Peers whose x402 greeting advertised a Solana settlement leg that this
|
|
6151
|
+
* client could not weigh, because `config.solanaChannel` is unset and
|
|
6152
|
+
* {@link getBalances} therefore reports no Solana balance at all (issue
|
|
6153
|
+
* #474). The EVM leg was taken by default for these; if opening it then
|
|
6154
|
+
* fails for lack of funds, {@link withSolanaLegHint} says so.
|
|
6155
|
+
*/
|
|
6156
|
+
unreadableSolanaLegs = /* @__PURE__ */ new Set();
|
|
5922
6157
|
/**
|
|
5923
6158
|
* Asks terminating connectors for their identity key, caching one per
|
|
5924
6159
|
* client edge. A packet cannot be formed without the key of the connector
|
|
@@ -6055,7 +6290,10 @@ var ToonClient = class {
|
|
|
6055
6290
|
try {
|
|
6056
6291
|
if (this.evmSigner) {
|
|
6057
6292
|
const store = this.config.channelStorePath ? new JsonFileChannelStore(this.config.channelStorePath) : void 0;
|
|
6058
|
-
this.channelManager = new ChannelManager(this.evmSigner, store
|
|
6293
|
+
this.channelManager = new ChannelManager(this.evmSigner, store, {
|
|
6294
|
+
...this.config.initialDeposit !== void 0 ? { initialDeposit: this.config.initialDeposit } : {},
|
|
6295
|
+
...this.config.settlementTimeout !== void 0 ? { settlementTimeout: this.config.settlementTimeout } : {}
|
|
6296
|
+
});
|
|
6059
6297
|
if (this.config.mnemonic) {
|
|
6060
6298
|
await this.registerMnemonicChainSigners(
|
|
6061
6299
|
this.config.mnemonic,
|
|
@@ -6287,7 +6525,7 @@ var ToonClient = class {
|
|
|
6287
6525
|
String(error),
|
|
6288
6526
|
error instanceof Error ? error.stack : ""
|
|
6289
6527
|
);
|
|
6290
|
-
if (error instanceof ToonClientError && (error.code === "NO_TERMINATED_ROUTE" || error.code === "NO_CONNECTOR_EDGE" || error.code === "PEER_NOT_NEGOTIATED")) {
|
|
6528
|
+
if (error instanceof ToonClientError && (error.code === "NO_TERMINATED_ROUTE" || error.code === "NO_CONNECTOR_EDGE" || error.code === "PEER_NOT_NEGOTIATED" || error.code === "CHANNEL_FUNDING")) {
|
|
6291
6529
|
throw error;
|
|
6292
6530
|
}
|
|
6293
6531
|
throw new ToonClientError(
|
|
@@ -6552,10 +6790,15 @@ var ToonClient = class {
|
|
|
6552
6790
|
"PEER_NOT_NEGOTIATED"
|
|
6553
6791
|
);
|
|
6554
6792
|
}
|
|
6555
|
-
|
|
6556
|
-
|
|
6557
|
-
|
|
6558
|
-
|
|
6793
|
+
let channelId;
|
|
6794
|
+
try {
|
|
6795
|
+
channelId = await this.channelManager.ensureChannel(
|
|
6796
|
+
peerId,
|
|
6797
|
+
negotiation
|
|
6798
|
+
);
|
|
6799
|
+
} catch (error) {
|
|
6800
|
+
throw this.withSolanaLegHint(peerId, error);
|
|
6801
|
+
}
|
|
6559
6802
|
const proof = await this.channelManager.signBalanceProof(
|
|
6560
6803
|
channelId,
|
|
6561
6804
|
amount
|
|
@@ -6629,7 +6872,11 @@ var ToonClient = class {
|
|
|
6629
6872
|
"PEER_NOT_NEGOTIATED"
|
|
6630
6873
|
);
|
|
6631
6874
|
}
|
|
6632
|
-
|
|
6875
|
+
try {
|
|
6876
|
+
return await this.channelManager.ensureChannel(peerId, negotiation);
|
|
6877
|
+
} catch (error) {
|
|
6878
|
+
throw this.withSolanaLegHint(peerId, error);
|
|
6879
|
+
}
|
|
6633
6880
|
}
|
|
6634
6881
|
/**
|
|
6635
6882
|
* Gets list of tracked payment channel IDs.
|
|
@@ -7001,10 +7248,23 @@ var ToonClient = class {
|
|
|
7001
7248
|
* connector's x402 greeting carries the channel-opening facts, and ADR
|
|
7002
7249
|
* 0022 makes the greeting the ONLY place the Rust fleet will ever state
|
|
7003
7250
|
* them. Synthesizes a {@link PeerNegotiation} from those facts, remembers
|
|
7004
|
-
* it, and returns it; `undefined` when there is no edge to ask
|
|
7005
|
-
* greeting carries no settlement facts
|
|
7006
|
-
*
|
|
7007
|
-
*
|
|
7251
|
+
* it, and returns it; `undefined` when there is no edge to ask or the
|
|
7252
|
+
* greeting carries no settlement facts this client can open a channel on.
|
|
7253
|
+
*
|
|
7254
|
+
* A two-chain greeting (connector #632's `extra.settlements`) prefers EVM
|
|
7255
|
+
* — the long-standing default — UNLESS the wallet holds Solana
|
|
7256
|
+
* settlement-token funds and holds none on EVM (issue #470), in which case
|
|
7257
|
+
* it opens the Solana leg instead: a wallet funded only with Solana devnet
|
|
7258
|
+
* assets can bootstrap exactly as an EVM one does. A Solana-only greeting
|
|
7259
|
+
* (no EVM leg at all) always opens Solana — there is nothing to compare
|
|
7260
|
+
* funds against.
|
|
7261
|
+
*
|
|
7262
|
+
* That comparison is only as good as what {@link getBalances} can SEE: with
|
|
7263
|
+
* no `solanaChannel` config it cannot read the Solana side at all, so a
|
|
7264
|
+
* Solana-funded client silently looks broke and takes the EVM leg (issue
|
|
7265
|
+
* #474). We cannot fix the read here — the config genuinely isn't there —
|
|
7266
|
+
* but we remember the peer so the resulting EVM funding failure names the
|
|
7267
|
+
* missing config instead of reading as a plain "fund your wallet".
|
|
7008
7268
|
*
|
|
7009
7269
|
* Never throws: this is a fallback on a path that already has a precise
|
|
7010
7270
|
* error (`PEER_NOT_NEGOTIATED`), and a transport failure while ASKING
|
|
@@ -7019,19 +7279,21 @@ var ToonClient = class {
|
|
|
7019
7279
|
}
|
|
7020
7280
|
try {
|
|
7021
7281
|
const terms = await this.connectorEdge.getRouteTerms(edge, destination);
|
|
7022
|
-
const
|
|
7023
|
-
|
|
7024
|
-
|
|
7025
|
-
|
|
7026
|
-
if (
|
|
7027
|
-
|
|
7028
|
-
|
|
7029
|
-
|
|
7030
|
-
|
|
7031
|
-
|
|
7032
|
-
|
|
7033
|
-
|
|
7034
|
-
|
|
7282
|
+
const evmSettlement = terms?.settlement;
|
|
7283
|
+
const solanaSettlement = terms?.settlements?.find(
|
|
7284
|
+
(entry) => entry.kind === "solana"
|
|
7285
|
+
);
|
|
7286
|
+
if (!evmSettlement && !solanaSettlement) return void 0;
|
|
7287
|
+
let negotiation;
|
|
7288
|
+
if (solanaSettlement && (!evmSettlement || await this.walletPrefersSolana())) {
|
|
7289
|
+
negotiation = this.solanaNegotiationFromSettlement(solanaSettlement);
|
|
7290
|
+
} else if (evmSettlement) {
|
|
7291
|
+
negotiation = this.evmNegotiationFromSettlement(evmSettlement);
|
|
7292
|
+
if (solanaSettlement && !this.canReadSolanaBalance()) {
|
|
7293
|
+
this.unreadableSolanaLegs.add(peerId);
|
|
7294
|
+
}
|
|
7295
|
+
}
|
|
7296
|
+
if (!negotiation) return void 0;
|
|
7035
7297
|
this.peerNegotiations.set(peerId, negotiation);
|
|
7036
7298
|
return negotiation;
|
|
7037
7299
|
} catch (error) {
|
|
@@ -7042,6 +7304,99 @@ var ToonClient = class {
|
|
|
7042
7304
|
return void 0;
|
|
7043
7305
|
}
|
|
7044
7306
|
}
|
|
7307
|
+
/**
|
|
7308
|
+
* Build the EVM {@link PeerNegotiation} from the greeting's legacy
|
|
7309
|
+
* `settlement` object (connector #617). `undefined` when `chain` is not
|
|
7310
|
+
* the documented `evm:<chainId>` shape — the greeting is malformed rather
|
|
7311
|
+
* than a chain this client cannot open, so bootstrap declines instead of
|
|
7312
|
+
* opening against unverified facts.
|
|
7313
|
+
*/
|
|
7314
|
+
evmNegotiationFromSettlement(settlement) {
|
|
7315
|
+
const [chainType, chainIdText] = settlement.chain.split(":");
|
|
7316
|
+
const chainId = Number(chainIdText);
|
|
7317
|
+
if (chainType !== "evm" || !Number.isFinite(chainId)) return void 0;
|
|
7318
|
+
return {
|
|
7319
|
+
chain: settlement.chain,
|
|
7320
|
+
chainType,
|
|
7321
|
+
chainId,
|
|
7322
|
+
settlementAddress: settlement.settlementAddress,
|
|
7323
|
+
tokenAddress: settlement.tokenAddress,
|
|
7324
|
+
tokenNetwork: settlement.tokenNetwork
|
|
7325
|
+
};
|
|
7326
|
+
}
|
|
7327
|
+
/**
|
|
7328
|
+
* Build the Solana {@link PeerNegotiation} from a `settlements` entry
|
|
7329
|
+
* (connector #632). Unlike the EVM leg there is no numeric chain id to
|
|
7330
|
+
* parse or validate — `chain` is always the literal `'solana'` — so,
|
|
7331
|
+
* unlike {@link evmNegotiationFromSettlement}, this cannot fail.
|
|
7332
|
+
*/
|
|
7333
|
+
solanaNegotiationFromSettlement(settlement) {
|
|
7334
|
+
return {
|
|
7335
|
+
chain: "solana",
|
|
7336
|
+
chainType: "solana",
|
|
7337
|
+
chainId: "solana",
|
|
7338
|
+
settlementAddress: settlement.settlementAddress,
|
|
7339
|
+
tokenAddress: settlement.tokenAddress,
|
|
7340
|
+
tokenNetwork: settlement.programId
|
|
7341
|
+
};
|
|
7342
|
+
}
|
|
7343
|
+
/**
|
|
7344
|
+
* Whether a two-chain greeting-bootstrap should open the Solana leg
|
|
7345
|
+
* instead of the default EVM one (issue #470): the wallet holds Solana
|
|
7346
|
+
* SETTLEMENT-TOKEN funds and holds none on EVM.
|
|
7347
|
+
*
|
|
7348
|
+
* Reads {@link getBalances}, which reports one balance per chain — the SPL
|
|
7349
|
+
* settlement token on Solana, NOT native SOL (issue #474). That is
|
|
7350
|
+
* deliberate: SOL alone cannot collateralize a channel, so a SOL-only
|
|
7351
|
+
* wallet must not steer the negotiation to Solana. The native SOL a fresh
|
|
7352
|
+
* open needs for rent + fees is checked separately, at open time, by the
|
|
7353
|
+
* Solana opener's funding preflight.
|
|
7354
|
+
*
|
|
7355
|
+
* `getBalances` is itself best-effort per chain (a missing config or failed
|
|
7356
|
+
* RPC read just omits that chain) — this never throws, degrading to "prefer
|
|
7357
|
+
* EVM" (the pre-existing default) on any read failure. When Solana is
|
|
7358
|
+
* omitted because `solanaChannel` is unset, see
|
|
7359
|
+
* {@link canReadSolanaBalance} / {@link unreadableSolanaLegs}.
|
|
7360
|
+
*/
|
|
7361
|
+
async walletPrefersSolana() {
|
|
7362
|
+
try {
|
|
7363
|
+
const balances = await this.getBalances();
|
|
7364
|
+
const hasFunds = (chain) => balances.some((b) => b.chain === chain && BigInt(b.amount) > 0n);
|
|
7365
|
+
return hasFunds("solana") && !hasFunds("evm");
|
|
7366
|
+
} catch {
|
|
7367
|
+
return false;
|
|
7368
|
+
}
|
|
7369
|
+
}
|
|
7370
|
+
/**
|
|
7371
|
+
* Whether {@link getBalances} can report a Solana balance at all — the exact
|
|
7372
|
+
* condition that reader gates on. False means a Solana-funded wallet reads
|
|
7373
|
+
* as empty, so `walletPrefersSolana` cannot see its funds.
|
|
7374
|
+
*/
|
|
7375
|
+
canReadSolanaBalance() {
|
|
7376
|
+
const sol = this.config.solanaChannel;
|
|
7377
|
+
return Boolean(this.getSolanaAddress() && sol?.rpcUrl && sol.tokenMint);
|
|
7378
|
+
}
|
|
7379
|
+
/**
|
|
7380
|
+
* Re-throw a channel-open failure with the missing-`solanaChannel` diagnosis
|
|
7381
|
+
* attached (issue #474).
|
|
7382
|
+
*
|
|
7383
|
+
* When the peer's greeting advertised a Solana leg this client could not
|
|
7384
|
+
* evaluate, "the EVM wallet has no funds/gas" is a symptom, not the cause:
|
|
7385
|
+
* the funds may be sitting on Solana, invisible because `solanaChannel` is
|
|
7386
|
+
* unconfigured. Only funding failures are rewritten — every other error is
|
|
7387
|
+
* returned untouched, since a transport/RPC/contract fault has nothing to do
|
|
7388
|
+
* with which chain was picked.
|
|
7389
|
+
*/
|
|
7390
|
+
withSolanaLegHint(peerId, error) {
|
|
7391
|
+
if (!this.unreadableSolanaLegs.has(peerId)) return error;
|
|
7392
|
+
const isFunding = error instanceof ChannelFundingError || isInsufficientGasError(error);
|
|
7393
|
+
if (!isFunding) return error;
|
|
7394
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
7395
|
+
return new ChannelFundingError(
|
|
7396
|
+
`${detail} NOTE: the route also offered a Solana settlement leg, but this client has no \`solanaChannel\` config (rpcUrl + tokenMint), so its Solana balance could not be read and the EVM leg was chosen by default. If the wallet is funded on Solana, configure \`solanaChannel\` and retry.`,
|
|
7397
|
+
error instanceof Error ? error : void 0
|
|
7398
|
+
);
|
|
7399
|
+
}
|
|
7045
7400
|
resolvePeerId(destination) {
|
|
7046
7401
|
const segments = destination.split(".");
|
|
7047
7402
|
const lastSegment = segments[segments.length - 1] ?? "";
|