context-markets 0.5.1 → 0.6.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/dist/index.cjs +130 -35
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -17
- package/dist/index.d.ts +38 -17
- package/dist/index.js +130 -35
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -168,6 +168,13 @@ var ERC20_ABI = [
|
|
|
168
168
|
{ name: "spender", type: "address" }
|
|
169
169
|
],
|
|
170
170
|
outputs: [{ name: "", type: "uint256" }]
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
name: "balanceOf",
|
|
174
|
+
type: "function",
|
|
175
|
+
stateMutability: "view",
|
|
176
|
+
inputs: [{ name: "account", type: "address" }],
|
|
177
|
+
outputs: [{ name: "", type: "uint256" }]
|
|
171
178
|
}
|
|
172
179
|
];
|
|
173
180
|
var HOLDINGS_ABI = [
|
|
@@ -1015,11 +1022,12 @@ var PortfolioModule = class {
|
|
|
1015
1022
|
// src/modules/account.ts
|
|
1016
1023
|
var import_viem2 = require("viem");
|
|
1017
1024
|
var AccountModule = class {
|
|
1018
|
-
constructor(http2, walletClient, account, chainConfig, rpcUrl) {
|
|
1025
|
+
constructor(http2, walletClient, account, chainConfig, chain, rpcUrl) {
|
|
1019
1026
|
this.http = http2;
|
|
1020
1027
|
this.walletClient = walletClient;
|
|
1021
1028
|
this.account = account;
|
|
1022
1029
|
this.chainConfig = chainConfig;
|
|
1030
|
+
this.chain = chain;
|
|
1023
1031
|
this.publicClient = (0, import_viem2.createPublicClient)({
|
|
1024
1032
|
chain: chainConfig.viemChain,
|
|
1025
1033
|
transport: (0, import_viem2.http)(rpcUrl)
|
|
@@ -1053,8 +1061,14 @@ var AccountModule = class {
|
|
|
1053
1061
|
async status() {
|
|
1054
1062
|
const addr = this.address;
|
|
1055
1063
|
const { settlement, holdings, usdc } = this.chainConfig;
|
|
1056
|
-
const [ethBalance, usdcAllowance, isOperatorApproved] = await Promise.all([
|
|
1064
|
+
const [ethBalance, usdcBalance, usdcAllowance, isOperatorApproved] = await Promise.all([
|
|
1057
1065
|
this.publicClient.getBalance({ address: addr }),
|
|
1066
|
+
this.publicClient.readContract({
|
|
1067
|
+
address: usdc,
|
|
1068
|
+
abi: ERC20_ABI,
|
|
1069
|
+
functionName: "balanceOf",
|
|
1070
|
+
args: [addr]
|
|
1071
|
+
}),
|
|
1058
1072
|
this.publicClient.readContract({
|
|
1059
1073
|
address: usdc,
|
|
1060
1074
|
abi: ERC20_ABI,
|
|
@@ -1068,51 +1082,92 @@ var AccountModule = class {
|
|
|
1068
1082
|
args: [addr, settlement]
|
|
1069
1083
|
})
|
|
1070
1084
|
]);
|
|
1085
|
+
const needsUsdcApproval = usdcAllowance === 0n;
|
|
1086
|
+
const needsOperatorApproval = !isOperatorApproved;
|
|
1071
1087
|
return {
|
|
1072
1088
|
address: addr,
|
|
1073
1089
|
ethBalance,
|
|
1090
|
+
usdcBalance,
|
|
1074
1091
|
usdcAllowance,
|
|
1075
1092
|
isOperatorApproved,
|
|
1076
|
-
|
|
1077
|
-
|
|
1093
|
+
needsUsdcApproval,
|
|
1094
|
+
needsOperatorApproval,
|
|
1095
|
+
isReady: !needsUsdcApproval && !needsOperatorApproval
|
|
1078
1096
|
};
|
|
1079
1097
|
}
|
|
1080
|
-
|
|
1098
|
+
// ─── Granular Approval Methods ───
|
|
1099
|
+
async approveUsdc() {
|
|
1081
1100
|
const wallet = this.requireWallet();
|
|
1082
1101
|
const account = this.requireAccount();
|
|
1083
|
-
const { viemChain,
|
|
1084
|
-
const
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
}
|
|
1097
|
-
if (!walletStatus.isOperatorApproved) {
|
|
1098
|
-
operatorApprovalTx = await wallet.writeContract({
|
|
1099
|
-
account,
|
|
1100
|
-
chain: viemChain,
|
|
1101
|
-
address: holdings,
|
|
1102
|
-
abi: HOLDINGS_ABI,
|
|
1103
|
-
functionName: "setOperator",
|
|
1104
|
-
args: [settlement, true]
|
|
1105
|
-
});
|
|
1106
|
-
}
|
|
1107
|
-
return { usdcApprovalTx, operatorApprovalTx };
|
|
1102
|
+
const { viemChain, holdings, usdc } = this.chainConfig;
|
|
1103
|
+
const status = await this.status();
|
|
1104
|
+
if (!status.needsUsdcApproval) return null;
|
|
1105
|
+
const hash = await wallet.writeContract({
|
|
1106
|
+
account,
|
|
1107
|
+
chain: viemChain,
|
|
1108
|
+
address: usdc,
|
|
1109
|
+
abi: ERC20_ABI,
|
|
1110
|
+
functionName: "approve",
|
|
1111
|
+
args: [holdings, import_viem2.maxUint256]
|
|
1112
|
+
});
|
|
1113
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
1114
|
+
return hash;
|
|
1108
1115
|
}
|
|
1109
|
-
async
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1116
|
+
async approveOperator() {
|
|
1117
|
+
const wallet = this.requireWallet();
|
|
1118
|
+
const account = this.requireAccount();
|
|
1119
|
+
const { viemChain, settlement, holdings } = this.chainConfig;
|
|
1120
|
+
const status = await this.status();
|
|
1121
|
+
if (!status.needsOperatorApproval) return null;
|
|
1122
|
+
const hash = await wallet.writeContract({
|
|
1123
|
+
account,
|
|
1124
|
+
chain: viemChain,
|
|
1125
|
+
address: holdings,
|
|
1126
|
+
abi: HOLDINGS_ABI,
|
|
1127
|
+
functionName: "setOperator",
|
|
1128
|
+
args: [settlement, true]
|
|
1113
1129
|
});
|
|
1130
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
1131
|
+
return hash;
|
|
1132
|
+
}
|
|
1133
|
+
// ─── Chain-Aware Dispatchers ───
|
|
1134
|
+
async setup() {
|
|
1135
|
+
if (this.chain === "testnet") {
|
|
1136
|
+
const result = await this.gaslessSetup();
|
|
1137
|
+
return {
|
|
1138
|
+
usdcApproval: { needed: false, txHash: null },
|
|
1139
|
+
operatorApproval: { needed: true, txHash: result.txHash }
|
|
1140
|
+
};
|
|
1141
|
+
}
|
|
1142
|
+
return this.onchainSetup();
|
|
1114
1143
|
}
|
|
1115
1144
|
async deposit(amount) {
|
|
1145
|
+
if (this.chain === "testnet") {
|
|
1146
|
+
const result = await this.gaslessDeposit(amount);
|
|
1147
|
+
return {
|
|
1148
|
+
txHash: result.txHash,
|
|
1149
|
+
amount: result.amount,
|
|
1150
|
+
gasless: true
|
|
1151
|
+
};
|
|
1152
|
+
}
|
|
1153
|
+
const amountRaw = (0, import_viem2.parseUnits)(amount.toString(), 6);
|
|
1154
|
+
const hash = await this.onchainDeposit(amount);
|
|
1155
|
+
return { txHash: hash, amount: amountRaw.toString(), gasless: false };
|
|
1156
|
+
}
|
|
1157
|
+
// ─── On-Chain Methods ───
|
|
1158
|
+
async onchainSetup() {
|
|
1159
|
+
const status = await this.status();
|
|
1160
|
+
const usdcTx = status.needsUsdcApproval ? await this.approveUsdcUnchecked() : null;
|
|
1161
|
+
const operatorTx = status.needsOperatorApproval ? await this.approveOperatorUnchecked() : null;
|
|
1162
|
+
return {
|
|
1163
|
+
usdcApproval: { needed: status.needsUsdcApproval, txHash: usdcTx },
|
|
1164
|
+
operatorApproval: {
|
|
1165
|
+
needed: status.needsOperatorApproval,
|
|
1166
|
+
txHash: operatorTx
|
|
1167
|
+
}
|
|
1168
|
+
};
|
|
1169
|
+
}
|
|
1170
|
+
async onchainDeposit(amount) {
|
|
1116
1171
|
const wallet = this.requireWallet();
|
|
1117
1172
|
const account = this.requireAccount();
|
|
1118
1173
|
const { viemChain, holdings, usdc } = this.chainConfig;
|
|
@@ -1144,6 +1199,12 @@ var AccountModule = class {
|
|
|
1144
1199
|
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
1145
1200
|
return hash;
|
|
1146
1201
|
}
|
|
1202
|
+
async mintTestUsdc(amount = 1e3) {
|
|
1203
|
+
return this.http.post(ENDPOINTS.balance.mintTestUsdc, {
|
|
1204
|
+
address: this.address,
|
|
1205
|
+
amount: amount.toString()
|
|
1206
|
+
});
|
|
1207
|
+
}
|
|
1147
1208
|
async mintCompleteSets(marketId, amount) {
|
|
1148
1209
|
const wallet = this.requireWallet();
|
|
1149
1210
|
const account = this.requireAccount();
|
|
@@ -1252,6 +1313,37 @@ var AccountModule = class {
|
|
|
1252
1313
|
req
|
|
1253
1314
|
);
|
|
1254
1315
|
}
|
|
1316
|
+
// ─── Private helpers (skip status check, used by onchainSetup) ───
|
|
1317
|
+
async approveUsdcUnchecked() {
|
|
1318
|
+
const wallet = this.requireWallet();
|
|
1319
|
+
const account = this.requireAccount();
|
|
1320
|
+
const { viemChain, holdings, usdc } = this.chainConfig;
|
|
1321
|
+
const hash = await wallet.writeContract({
|
|
1322
|
+
account,
|
|
1323
|
+
chain: viemChain,
|
|
1324
|
+
address: usdc,
|
|
1325
|
+
abi: ERC20_ABI,
|
|
1326
|
+
functionName: "approve",
|
|
1327
|
+
args: [holdings, import_viem2.maxUint256]
|
|
1328
|
+
});
|
|
1329
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
1330
|
+
return hash;
|
|
1331
|
+
}
|
|
1332
|
+
async approveOperatorUnchecked() {
|
|
1333
|
+
const wallet = this.requireWallet();
|
|
1334
|
+
const account = this.requireAccount();
|
|
1335
|
+
const { viemChain, settlement, holdings } = this.chainConfig;
|
|
1336
|
+
const hash = await wallet.writeContract({
|
|
1337
|
+
account,
|
|
1338
|
+
chain: viemChain,
|
|
1339
|
+
address: holdings,
|
|
1340
|
+
abi: HOLDINGS_ABI,
|
|
1341
|
+
functionName: "setOperator",
|
|
1342
|
+
args: [settlement, true]
|
|
1343
|
+
});
|
|
1344
|
+
await this.publicClient.waitForTransactionReceipt({ hash });
|
|
1345
|
+
return hash;
|
|
1346
|
+
}
|
|
1255
1347
|
};
|
|
1256
1348
|
|
|
1257
1349
|
// src/client.ts
|
|
@@ -1263,11 +1355,14 @@ var ContextClient = class {
|
|
|
1263
1355
|
account;
|
|
1264
1356
|
/** The resolved chain configuration. */
|
|
1265
1357
|
chainConfig;
|
|
1358
|
+
/** Which chain this client is connected to. */
|
|
1359
|
+
chain;
|
|
1266
1360
|
/** The trader's on-chain address, or null if no signer was provided. */
|
|
1267
1361
|
address;
|
|
1268
1362
|
constructor(options = {}) {
|
|
1269
1363
|
const chainConfig = resolveChainConfig(options.chain);
|
|
1270
1364
|
this.chainConfig = chainConfig;
|
|
1365
|
+
this.chain = options.chain ?? "mainnet";
|
|
1271
1366
|
const http2 = createHttpClient({
|
|
1272
1367
|
apiKey: options.apiKey,
|
|
1273
1368
|
baseUrl: options.baseUrl ?? chainConfig.apiBase
|
|
@@ -1288,7 +1383,7 @@ var ContextClient = class {
|
|
|
1288
1383
|
this.questions = new Questions(http2);
|
|
1289
1384
|
this.orders = new Orders(http2, builder, address);
|
|
1290
1385
|
this.portfolio = new PortfolioModule(http2, address);
|
|
1291
|
-
this.account = new AccountModule(http2, walletClient, account, chainConfig, options.rpcUrl);
|
|
1386
|
+
this.account = new AccountModule(http2, walletClient, account, chainConfig, this.chain, options.rpcUrl);
|
|
1292
1387
|
}
|
|
1293
1388
|
};
|
|
1294
1389
|
// Annotate the CommonJS export names for ESM import in node:
|