@caatinga/client 0.2.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/LICENSE +21 -0
- package/README.md +136 -0
- package/dist/freighter.cjs +42 -0
- package/dist/freighter.d.cts +6 -0
- package/dist/freighter.d.ts +6 -0
- package/dist/freighter.js +15 -0
- package/dist/index.cjs +339 -0
- package/dist/index.d.cts +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +308 -0
- package/dist/types-Bmw_J1fg.d.cts +66 -0
- package/dist/types-Bmw_J1fg.d.ts +66 -0
- package/dist/types-CND6fcI5.d.cts +66 -0
- package/dist/types-CND6fcI5.d.ts +66 -0
- package/dist/types-CiM5FkDn.d.cts +66 -0
- package/dist/types-CiM5FkDn.d.ts +66 -0
- package/dist/types-DEys6TaK.d.cts +66 -0
- package/dist/types-DEys6TaK.d.ts +66 -0
- package/package.json +69 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Kaleido contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @caatinga/client
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
pnpm add @caatinga/client
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
If you are using Freighter, add the optional adapter dependency:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
pnpm add @stellar/freighter-api
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createCaatingaClient } from "@caatinga/client";
|
|
17
|
+
import { freighterWalletAdapter } from "@caatinga/client/freighter";
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
The `@caatinga/client/freighter` subpath is optional and only needed when you want the bundled Freighter adapter.
|
|
21
|
+
|
|
22
|
+
## What It Solves
|
|
23
|
+
|
|
24
|
+
`@caatinga/client` is the supported browser and Node integration layer for invoking generated Soroban bindings with Caatinga artifacts, network configuration, and a wallet adapter.
|
|
25
|
+
|
|
26
|
+
It connects:
|
|
27
|
+
|
|
28
|
+
- generated contract bindings
|
|
29
|
+
- `caatinga.artifacts.json`
|
|
30
|
+
- RPC URL and network passphrase
|
|
31
|
+
- wallet-backed signing for invocation and XDR preparation flows
|
|
32
|
+
|
|
33
|
+
## Supported Surface
|
|
34
|
+
|
|
35
|
+
Supported runtime root exports:
|
|
36
|
+
|
|
37
|
+
- `createCaatingaClient`
|
|
38
|
+
- `resolveContractId`
|
|
39
|
+
- `createDefaultBindingAdapter`
|
|
40
|
+
- `CaatingaContractClient`
|
|
41
|
+
- `buildXdr`
|
|
42
|
+
|
|
43
|
+
Supported type-only root exports:
|
|
44
|
+
|
|
45
|
+
- `CaatingaBindingAdapter`
|
|
46
|
+
- `CaatingaClientConfig`
|
|
47
|
+
- `CaatingaContractRegistration`
|
|
48
|
+
- `CaatingaInvokeOptions`
|
|
49
|
+
- `CaatingaInvokeResult`
|
|
50
|
+
- `CaatingaInvokeStatus`
|
|
51
|
+
- `CaatingaNetwork`
|
|
52
|
+
- `CaatingaWalletAdapter`
|
|
53
|
+
- `CaatingaXdrBuildResult`
|
|
54
|
+
|
|
55
|
+
Supported subpath export:
|
|
56
|
+
|
|
57
|
+
- `@caatinga/client/freighter` -> `freighterWalletAdapter` (optional)
|
|
58
|
+
|
|
59
|
+
Primary flow:
|
|
60
|
+
|
|
61
|
+
- `createCaatingaClient(...)`
|
|
62
|
+
- `client.contract(name).invoke(method, args?)`
|
|
63
|
+
- `client.contract(name).buildXdr(method, args?)`
|
|
64
|
+
|
|
65
|
+
## Counter Example
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { createCaatingaClient } from "@caatinga/client";
|
|
69
|
+
import { freighterWalletAdapter } from "@caatinga/client/freighter";
|
|
70
|
+
import * as Counter from "./contracts/generated/counter";
|
|
71
|
+
import artifacts from "../caatinga.artifacts.json";
|
|
72
|
+
|
|
73
|
+
const client = createCaatingaClient({
|
|
74
|
+
network: {
|
|
75
|
+
name: "testnet",
|
|
76
|
+
rpcUrl: "https://soroban-testnet.stellar.org",
|
|
77
|
+
networkPassphrase: "Test SDF Network ; September 2015"
|
|
78
|
+
},
|
|
79
|
+
artifacts,
|
|
80
|
+
wallet: freighterWalletAdapter,
|
|
81
|
+
contracts: {
|
|
82
|
+
counter: {
|
|
83
|
+
binding: Counter
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const result = await client.contract("counter").invoke("increment");
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Wallet Adapter Contract
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
export interface CaatingaWalletAdapter {
|
|
95
|
+
getPublicKey(): Promise<string>;
|
|
96
|
+
|
|
97
|
+
signTransaction(input: {
|
|
98
|
+
xdr: string;
|
|
99
|
+
networkPassphrase: string;
|
|
100
|
+
}): Promise<string>;
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The default Freighter adapter is exported from `@caatinga/client/freighter`.
|
|
105
|
+
|
|
106
|
+
## Debug Output Rules
|
|
107
|
+
|
|
108
|
+
- XDR data is omitted by default
|
|
109
|
+
- `debugXdr: true` includes XDR snapshots such as unsigned, prepared, and signed values
|
|
110
|
+
- raw binding or submission output is omitted by default
|
|
111
|
+
- `debugRaw: true` includes raw binding or submission output
|
|
112
|
+
|
|
113
|
+
Consumers should treat debug fields as opt-in diagnostics, not part of the default happy-path payload.
|
|
114
|
+
|
|
115
|
+
## Errors
|
|
116
|
+
|
|
117
|
+
`@caatinga/client` emits documented `CAATINGA_*` codes for public failures. Consumers should key automation on the code, not the message text.
|
|
118
|
+
|
|
119
|
+
Common codes include:
|
|
120
|
+
|
|
121
|
+
- `CAATINGA_CONTRACT_ARTIFACT_NOT_FOUND`
|
|
122
|
+
- `CAATINGA_BINDING_CLIENT_NOT_FOUND`
|
|
123
|
+
- `CAATINGA_BINDING_METHOD_NOT_FOUND`
|
|
124
|
+
- `CAATINGA_WALLET_NOT_CONNECTED`
|
|
125
|
+
- `CAATINGA_XDR_BUILD_FAILED`
|
|
126
|
+
- `CAATINGA_XDR_PREPARE_FAILED`
|
|
127
|
+
- `CAATINGA_XDR_SIGN_FAILED`
|
|
128
|
+
- `CAATINGA_XDR_SUBMIT_FAILED`
|
|
129
|
+
- `CAATINGA_XDR_RESULT_FAILED`
|
|
130
|
+
|
|
131
|
+
## Limitations
|
|
132
|
+
|
|
133
|
+
- this package does not replace Stellar CLI, Stellar SDK, Soroban SDK, or generated bindings
|
|
134
|
+
- manual SCVal serialization and manual XDR parsing are out of scope
|
|
135
|
+
- React hooks, multisig orchestration, backend signing, and non-documented wallet integrations are not part of the supported contract
|
|
136
|
+
- private module paths and undocumented helpers are less stable than the exports listed above
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/freighter.ts
|
|
21
|
+
var freighter_exports = {};
|
|
22
|
+
__export(freighter_exports, {
|
|
23
|
+
freighterWalletAdapter: () => freighterWalletAdapter
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(freighter_exports);
|
|
26
|
+
|
|
27
|
+
// src/adapters/freighter.ts
|
|
28
|
+
var import_freighter_api = require("@stellar/freighter-api");
|
|
29
|
+
var freighterWalletAdapter = {
|
|
30
|
+
async getPublicKey() {
|
|
31
|
+
const response = await (0, import_freighter_api.getAddress)();
|
|
32
|
+
return response.address;
|
|
33
|
+
},
|
|
34
|
+
async signTransaction({ xdr, networkPassphrase }) {
|
|
35
|
+
const response = await (0, import_freighter_api.signTransaction)(xdr, { networkPassphrase });
|
|
36
|
+
return response.signedTxXdr;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
40
|
+
0 && (module.exports = {
|
|
41
|
+
freighterWalletAdapter
|
|
42
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/adapters/freighter.ts
|
|
2
|
+
import { getAddress, signTransaction } from "@stellar/freighter-api";
|
|
3
|
+
var freighterWalletAdapter = {
|
|
4
|
+
async getPublicKey() {
|
|
5
|
+
const response = await getAddress();
|
|
6
|
+
return response.address;
|
|
7
|
+
},
|
|
8
|
+
async signTransaction({ xdr, networkPassphrase }) {
|
|
9
|
+
const response = await signTransaction(xdr, { networkPassphrase });
|
|
10
|
+
return response.signedTxXdr;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
export {
|
|
14
|
+
freighterWalletAdapter
|
|
15
|
+
};
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
CaatingaContractClient: () => CaatingaContractClient,
|
|
24
|
+
buildXdr: () => buildXdr,
|
|
25
|
+
createCaatingaClient: () => createCaatingaClient,
|
|
26
|
+
createDefaultBindingAdapter: () => createDefaultBindingAdapter,
|
|
27
|
+
resolveContractId: () => resolveContractId
|
|
28
|
+
});
|
|
29
|
+
module.exports = __toCommonJS(index_exports);
|
|
30
|
+
|
|
31
|
+
// src/artifacts/resolve-contract-id.ts
|
|
32
|
+
var import_core = require("@caatinga/core");
|
|
33
|
+
function resolveContractId(input) {
|
|
34
|
+
if (input.explicitContractId) {
|
|
35
|
+
return input.explicitContractId;
|
|
36
|
+
}
|
|
37
|
+
const contractId = input.artifacts.networks[input.network]?.contracts[input.contract]?.contractId;
|
|
38
|
+
if (contractId) {
|
|
39
|
+
return contractId;
|
|
40
|
+
}
|
|
41
|
+
throw new import_core.CaatingaError(
|
|
42
|
+
`No contract artifact found for "${input.contract}" on "${input.network}".`,
|
|
43
|
+
import_core.CaatingaErrorCode.CONTRACT_ARTIFACT_NOT_FOUND,
|
|
44
|
+
"Deploy the contract first or pass contractId explicitly."
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/bindings/default-binding-adapter.ts
|
|
49
|
+
var import_core2 = require("@caatinga/core");
|
|
50
|
+
function createDefaultBindingAdapter(binding) {
|
|
51
|
+
return {
|
|
52
|
+
createClient({ contractId, publicKey, rpcUrl, networkPassphrase }) {
|
|
53
|
+
if (!binding.Client) {
|
|
54
|
+
throw new import_core2.CaatingaError(
|
|
55
|
+
"Generated binding does not export Client.",
|
|
56
|
+
import_core2.CaatingaErrorCode.BINDING_CLIENT_NOT_FOUND,
|
|
57
|
+
"Regenerate bindings with Stellar CLI."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
return new binding.Client({
|
|
61
|
+
contractId,
|
|
62
|
+
publicKey,
|
|
63
|
+
rpcUrl,
|
|
64
|
+
networkPassphrase
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
async callMethod({ client, method, args }) {
|
|
68
|
+
const candidate = client;
|
|
69
|
+
const fn = candidate[method];
|
|
70
|
+
if (typeof fn !== "function") {
|
|
71
|
+
throw new import_core2.CaatingaError(
|
|
72
|
+
`Binding method "${method}" was not found.`,
|
|
73
|
+
import_core2.CaatingaErrorCode.BINDING_METHOD_NOT_FOUND,
|
|
74
|
+
"Check the contract method name or regenerate bindings."
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return args ? fn.call(client, args) : fn.call(client);
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/client/create-caatinga-client.ts
|
|
83
|
+
var import_core5 = require("@caatinga/core");
|
|
84
|
+
|
|
85
|
+
// src/client/caatinga-contract-client.ts
|
|
86
|
+
var import_core4 = require("@caatinga/core");
|
|
87
|
+
|
|
88
|
+
// src/xdr/build-xdr.ts
|
|
89
|
+
var import_core3 = require("@caatinga/core");
|
|
90
|
+
async function buildXdr(input) {
|
|
91
|
+
try {
|
|
92
|
+
const transaction = input.transaction;
|
|
93
|
+
const unsignedXdr = readXdr(transaction);
|
|
94
|
+
let preparedTransaction;
|
|
95
|
+
if (typeof transaction.prepare === "function") {
|
|
96
|
+
try {
|
|
97
|
+
preparedTransaction = await transaction.prepare();
|
|
98
|
+
} catch (error) {
|
|
99
|
+
if (error instanceof import_core3.CaatingaError) {
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
throw new import_core3.CaatingaError(
|
|
103
|
+
`Failed to prepare XDR for "${input.contractName}.${input.method}".`,
|
|
104
|
+
import_core3.CaatingaErrorCode.XDR_PREPARE_FAILED,
|
|
105
|
+
"Check RPC connectivity, simulation errors, and binding compatibility.",
|
|
106
|
+
error
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
} else {
|
|
110
|
+
preparedTransaction = transaction;
|
|
111
|
+
}
|
|
112
|
+
const preparedXdr = readXdr(preparedTransaction);
|
|
113
|
+
return {
|
|
114
|
+
contract: input.contractName,
|
|
115
|
+
method: input.method,
|
|
116
|
+
contractId: input.contractId,
|
|
117
|
+
unsignedXdr,
|
|
118
|
+
preparedXdr,
|
|
119
|
+
...input.debug ? { raw: preparedTransaction } : {}
|
|
120
|
+
};
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (error instanceof import_core3.CaatingaError) {
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
throw new import_core3.CaatingaError(
|
|
126
|
+
`Failed to build XDR for "${input.contractName}.${input.method}".`,
|
|
127
|
+
import_core3.CaatingaErrorCode.XDR_BUILD_FAILED,
|
|
128
|
+
"Check the generated binding transaction object.",
|
|
129
|
+
error
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function readXdr(transaction) {
|
|
134
|
+
const candidate = transaction;
|
|
135
|
+
if (typeof candidate.toXDR !== "function") {
|
|
136
|
+
throw new import_core3.CaatingaError(
|
|
137
|
+
"Binding transaction object does not expose toXDR().",
|
|
138
|
+
import_core3.CaatingaErrorCode.XDR_BUILD_FAILED,
|
|
139
|
+
"Regenerate bindings or provide a compatible binding adapter."
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
return candidate.toXDR();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/client/caatinga-contract-client.ts
|
|
146
|
+
var CaatingaContractClient = class {
|
|
147
|
+
constructor(config, contractName, registration, bindingAdapter = createDefaultBindingAdapter(
|
|
148
|
+
registration.binding
|
|
149
|
+
)) {
|
|
150
|
+
this.config = config;
|
|
151
|
+
this.contractName = contractName;
|
|
152
|
+
this.registration = registration;
|
|
153
|
+
this.bindingAdapter = bindingAdapter;
|
|
154
|
+
}
|
|
155
|
+
config;
|
|
156
|
+
contractName;
|
|
157
|
+
registration;
|
|
158
|
+
bindingAdapter;
|
|
159
|
+
async buildXdr(method, argsOrOptions, maybeOptions) {
|
|
160
|
+
const { args, debugRaw } = splitArgsAndOptions(argsOrOptions, maybeOptions);
|
|
161
|
+
const { contractId, transaction } = await this.createTransaction(method, args);
|
|
162
|
+
return buildXdr({
|
|
163
|
+
contractName: this.contractName,
|
|
164
|
+
method,
|
|
165
|
+
contractId,
|
|
166
|
+
transaction,
|
|
167
|
+
debug: debugRaw
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async invoke(method, argsOrOptions, maybeOptions) {
|
|
171
|
+
const { args, debugXdr, debugRaw } = splitInvokeArgsAndOptions(argsOrOptions, maybeOptions);
|
|
172
|
+
const { contractId, transaction } = await this.createTransaction(method, args);
|
|
173
|
+
const xdr = await buildXdr({
|
|
174
|
+
contractName: this.contractName,
|
|
175
|
+
method,
|
|
176
|
+
contractId,
|
|
177
|
+
transaction,
|
|
178
|
+
debug: debugRaw
|
|
179
|
+
});
|
|
180
|
+
let signedXdr;
|
|
181
|
+
try {
|
|
182
|
+
signedXdr = await this.config.wallet.signTransaction({
|
|
183
|
+
xdr: xdr.preparedXdr,
|
|
184
|
+
networkPassphrase: this.config.network.networkPassphrase
|
|
185
|
+
});
|
|
186
|
+
} catch (error) {
|
|
187
|
+
throw new import_core4.CaatingaError(
|
|
188
|
+
`Failed to sign XDR for "${this.contractName}.${method}".`,
|
|
189
|
+
import_core4.CaatingaErrorCode.XDR_SIGN_FAILED,
|
|
190
|
+
"Connect a wallet and approve the transaction.",
|
|
191
|
+
error
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
const raw = await submitTransaction(transaction, signedXdr, this.contractName, method);
|
|
195
|
+
const normalized = normalizeSubmitResult(raw);
|
|
196
|
+
return {
|
|
197
|
+
status: "confirmed",
|
|
198
|
+
contract: this.contractName,
|
|
199
|
+
method,
|
|
200
|
+
contractId,
|
|
201
|
+
...normalized.transactionHash ? { transactionHash: normalized.transactionHash } : {},
|
|
202
|
+
...normalized.result !== void 0 ? { result: normalized.result } : {},
|
|
203
|
+
...debugXdr ? {
|
|
204
|
+
xdr: {
|
|
205
|
+
unsigned: xdr.unsignedXdr,
|
|
206
|
+
prepared: xdr.preparedXdr,
|
|
207
|
+
signed: signedXdr
|
|
208
|
+
}
|
|
209
|
+
} : {},
|
|
210
|
+
...debugRaw ? { raw } : {}
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
async createTransaction(method, args) {
|
|
214
|
+
const contractId = resolveContractId({
|
|
215
|
+
artifacts: this.config.artifacts,
|
|
216
|
+
network: this.config.network.name,
|
|
217
|
+
contract: this.contractName,
|
|
218
|
+
explicitContractId: this.registration.contractId
|
|
219
|
+
});
|
|
220
|
+
let publicKey;
|
|
221
|
+
try {
|
|
222
|
+
publicKey = await this.config.wallet.getPublicKey();
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error instanceof import_core4.CaatingaError) {
|
|
225
|
+
throw error;
|
|
226
|
+
}
|
|
227
|
+
throw new import_core4.CaatingaError(
|
|
228
|
+
`Wallet is not connected or the public key is unavailable for "${this.contractName}".`,
|
|
229
|
+
import_core4.CaatingaErrorCode.WALLET_NOT_CONNECTED,
|
|
230
|
+
"Connect the wallet and grant account access, then retry.",
|
|
231
|
+
error
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
const client = this.bindingAdapter.createClient({
|
|
235
|
+
contractId,
|
|
236
|
+
publicKey,
|
|
237
|
+
rpcUrl: this.config.network.rpcUrl,
|
|
238
|
+
networkPassphrase: this.config.network.networkPassphrase
|
|
239
|
+
});
|
|
240
|
+
const transaction = await this.bindingAdapter.callMethod({ client, method, args });
|
|
241
|
+
return { contractId, transaction };
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
function splitArgsAndOptions(argsOrOptions, maybeOptions) {
|
|
245
|
+
return {
|
|
246
|
+
args: argsOrOptions,
|
|
247
|
+
debugRaw: maybeOptions?.debugRaw ?? false
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
function splitInvokeArgsAndOptions(argsOrOptions, maybeOptions) {
|
|
251
|
+
const looksLikeOptions = argsOrOptions !== void 0 && ("debugXdr" in argsOrOptions || "debugRaw" in argsOrOptions) && maybeOptions === void 0;
|
|
252
|
+
if (looksLikeOptions) {
|
|
253
|
+
const options = argsOrOptions;
|
|
254
|
+
return {
|
|
255
|
+
args: void 0,
|
|
256
|
+
debugXdr: options.debugXdr ?? false,
|
|
257
|
+
debugRaw: options.debugRaw ?? false
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return {
|
|
261
|
+
args: argsOrOptions,
|
|
262
|
+
debugXdr: maybeOptions?.debugXdr ?? false,
|
|
263
|
+
debugRaw: maybeOptions?.debugRaw ?? false
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
async function submitTransaction(transaction, signedXdr, contractName, method) {
|
|
267
|
+
const candidate = transaction;
|
|
268
|
+
const submit = candidate.signAndSend ?? candidate.send;
|
|
269
|
+
if (typeof submit !== "function") {
|
|
270
|
+
throw new import_core4.CaatingaError(
|
|
271
|
+
`Binding transaction for "${contractName}.${method}" cannot be submitted.`,
|
|
272
|
+
import_core4.CaatingaErrorCode.XDR_SUBMIT_FAILED,
|
|
273
|
+
"Regenerate bindings or provide a compatible binding adapter."
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
try {
|
|
277
|
+
const raw = await submit.call(transaction, { signedXdr });
|
|
278
|
+
assertSubmitResultRecognized(raw, contractName, method);
|
|
279
|
+
return raw;
|
|
280
|
+
} catch (error) {
|
|
281
|
+
if (error instanceof import_core4.CaatingaError) {
|
|
282
|
+
throw error;
|
|
283
|
+
}
|
|
284
|
+
throw new import_core4.CaatingaError(
|
|
285
|
+
`Failed to submit XDR for "${contractName}.${method}".`,
|
|
286
|
+
import_core4.CaatingaErrorCode.XDR_SUBMIT_FAILED,
|
|
287
|
+
"Check wallet signature and RPC connectivity.",
|
|
288
|
+
error
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
function assertSubmitResultRecognized(raw, contractName, method) {
|
|
293
|
+
if (raw === null || typeof raw !== "object") {
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const record = raw;
|
|
297
|
+
const hasTransactionId = "txHash" in record || "transactionHash" in record || "hash" in record;
|
|
298
|
+
const hasResult = "result" in record;
|
|
299
|
+
if (hasTransactionId || hasResult) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
throw new import_core4.CaatingaError(
|
|
303
|
+
`Submit returned an unrecognized payload for "${contractName}.${method}".`,
|
|
304
|
+
import_core4.CaatingaErrorCode.XDR_RESULT_FAILED,
|
|
305
|
+
"Expected txHash, transactionHash, hash, or result on the submit response. Use debugRaw to inspect the binding output."
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
function normalizeSubmitResult(raw) {
|
|
309
|
+
const candidate = raw;
|
|
310
|
+
return {
|
|
311
|
+
transactionHash: candidate.txHash ?? candidate.transactionHash ?? candidate.hash,
|
|
312
|
+
result: candidate.result
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// src/client/create-caatinga-client.ts
|
|
317
|
+
function createCaatingaClient(config) {
|
|
318
|
+
return {
|
|
319
|
+
contract(contractName) {
|
|
320
|
+
const registration = config.contracts[contractName];
|
|
321
|
+
if (!registration) {
|
|
322
|
+
throw new import_core5.CaatingaError(
|
|
323
|
+
`Contract "${contractName}" is not registered.`,
|
|
324
|
+
import_core5.CaatingaErrorCode.CONTRACT_NOT_FOUND,
|
|
325
|
+
"Add the contract binding to createCaatingaClient()."
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
return new CaatingaContractClient(config, contractName, registration);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
333
|
+
0 && (module.exports = {
|
|
334
|
+
CaatingaContractClient,
|
|
335
|
+
buildXdr,
|
|
336
|
+
createCaatingaClient,
|
|
337
|
+
createDefaultBindingAdapter,
|
|
338
|
+
resolveContractId
|
|
339
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { a as CaatingaBindingAdapter, b as CaatingaClientConfig, c as CaatingaContractRegistration, d as CaatingaXdrBuildResult, e as CaatingaInvokeOptions, f as CaatingaInvokeResult } from './types-CiM5FkDn.cjs';
|
|
2
|
+
export { g as CaatingaInvokeStatus, h as CaatingaNetwork, C as CaatingaWalletAdapter } from './types-CiM5FkDn.cjs';
|
|
3
|
+
import { CaatingaArtifacts } from '@caatinga/core';
|
|
4
|
+
|
|
5
|
+
declare function resolveContractId(input: {
|
|
6
|
+
artifacts: CaatingaArtifacts;
|
|
7
|
+
network: string;
|
|
8
|
+
contract: string;
|
|
9
|
+
explicitContractId?: string;
|
|
10
|
+
}): string;
|
|
11
|
+
|
|
12
|
+
interface BindingWithClient {
|
|
13
|
+
Client?: new (input: {
|
|
14
|
+
contractId: string;
|
|
15
|
+
publicKey: string;
|
|
16
|
+
rpcUrl: string;
|
|
17
|
+
networkPassphrase: string;
|
|
18
|
+
}) => unknown;
|
|
19
|
+
}
|
|
20
|
+
declare function createDefaultBindingAdapter(binding: BindingWithClient): CaatingaBindingAdapter;
|
|
21
|
+
|
|
22
|
+
declare class CaatingaContractClient {
|
|
23
|
+
private readonly config;
|
|
24
|
+
private readonly contractName;
|
|
25
|
+
private readonly registration;
|
|
26
|
+
private readonly bindingAdapter;
|
|
27
|
+
constructor(config: CaatingaClientConfig, contractName: string, registration: CaatingaContractRegistration, bindingAdapter?: CaatingaBindingAdapter);
|
|
28
|
+
buildXdr(method: string, argsOrOptions?: Record<string, unknown>, maybeOptions?: {
|
|
29
|
+
debugRaw?: boolean;
|
|
30
|
+
}): Promise<CaatingaXdrBuildResult>;
|
|
31
|
+
invoke<T = unknown>(method: string, argsOrOptions?: Record<string, unknown> | CaatingaInvokeOptions, maybeOptions?: CaatingaInvokeOptions): Promise<CaatingaInvokeResult<T>>;
|
|
32
|
+
private createTransaction;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare function createCaatingaClient(config: CaatingaClientConfig): {
|
|
36
|
+
contract(contractName: string): CaatingaContractClient;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
declare function buildXdr(input: {
|
|
40
|
+
contractName: string;
|
|
41
|
+
method: string;
|
|
42
|
+
contractId: string;
|
|
43
|
+
transaction: unknown;
|
|
44
|
+
debug?: boolean;
|
|
45
|
+
}): Promise<CaatingaXdrBuildResult>;
|
|
46
|
+
|
|
47
|
+
export { CaatingaBindingAdapter, CaatingaClientConfig, CaatingaContractClient, CaatingaContractRegistration, CaatingaInvokeOptions, CaatingaInvokeResult, CaatingaXdrBuildResult, buildXdr, createCaatingaClient, createDefaultBindingAdapter, resolveContractId };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { a as CaatingaBindingAdapter, b as CaatingaClientConfig, c as CaatingaContractRegistration, d as CaatingaXdrBuildResult, e as CaatingaInvokeOptions, f as CaatingaInvokeResult } from './types-CiM5FkDn.js';
|
|
2
|
+
export { g as CaatingaInvokeStatus, h as CaatingaNetwork, C as CaatingaWalletAdapter } from './types-CiM5FkDn.js';
|
|
3
|
+
import { CaatingaArtifacts } from '@caatinga/core';
|
|
4
|
+
|
|
5
|
+
declare function resolveContractId(input: {
|
|
6
|
+
artifacts: CaatingaArtifacts;
|
|
7
|
+
network: string;
|
|
8
|
+
contract: string;
|
|
9
|
+
explicitContractId?: string;
|
|
10
|
+
}): string;
|
|
11
|
+
|
|
12
|
+
interface BindingWithClient {
|
|
13
|
+
Client?: new (input: {
|
|
14
|
+
contractId: string;
|
|
15
|
+
publicKey: string;
|
|
16
|
+
rpcUrl: string;
|
|
17
|
+
networkPassphrase: string;
|
|
18
|
+
}) => unknown;
|
|
19
|
+
}
|
|
20
|
+
declare function createDefaultBindingAdapter(binding: BindingWithClient): CaatingaBindingAdapter;
|
|
21
|
+
|
|
22
|
+
declare class CaatingaContractClient {
|
|
23
|
+
private readonly config;
|
|
24
|
+
private readonly contractName;
|
|
25
|
+
private readonly registration;
|
|
26
|
+
private readonly bindingAdapter;
|
|
27
|
+
constructor(config: CaatingaClientConfig, contractName: string, registration: CaatingaContractRegistration, bindingAdapter?: CaatingaBindingAdapter);
|
|
28
|
+
buildXdr(method: string, argsOrOptions?: Record<string, unknown>, maybeOptions?: {
|
|
29
|
+
debugRaw?: boolean;
|
|
30
|
+
}): Promise<CaatingaXdrBuildResult>;
|
|
31
|
+
invoke<T = unknown>(method: string, argsOrOptions?: Record<string, unknown> | CaatingaInvokeOptions, maybeOptions?: CaatingaInvokeOptions): Promise<CaatingaInvokeResult<T>>;
|
|
32
|
+
private createTransaction;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
declare function createCaatingaClient(config: CaatingaClientConfig): {
|
|
36
|
+
contract(contractName: string): CaatingaContractClient;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
declare function buildXdr(input: {
|
|
40
|
+
contractName: string;
|
|
41
|
+
method: string;
|
|
42
|
+
contractId: string;
|
|
43
|
+
transaction: unknown;
|
|
44
|
+
debug?: boolean;
|
|
45
|
+
}): Promise<CaatingaXdrBuildResult>;
|
|
46
|
+
|
|
47
|
+
export { CaatingaBindingAdapter, CaatingaClientConfig, CaatingaContractClient, CaatingaContractRegistration, CaatingaInvokeOptions, CaatingaInvokeResult, CaatingaXdrBuildResult, buildXdr, createCaatingaClient, createDefaultBindingAdapter, resolveContractId };
|