From b4a12b94c1daf46e1cac1d1f11478eacba165b6b Mon Sep 17 00:00:00 2001 From: Chad Ostrowski <221614+chadoh@users.noreply.github.com> Date: Fri, 10 May 2024 15:27:01 -0400 Subject: [PATCH] feat!: new exports `contract` and `rpc` - `SorobanRpc` now also exported as `rpc` - New main export `contract` This allows us to import it the usual way, instead of needing to do things like import { ContractClient } from "stellar-sdk/lib/contract_client" which doesn't work in the browser (because `lib`) - `ContractSpec` now available at `contract.Spec` - Also includes other supporting classes, functions, and types: - `contract.AssembledTransaction` - `contract.ClientOptions` - etc - These are also available at matching entrypoints, if your node and TypeScript configuration support the `exports` declaration: import { Client, ClientOptions, AssembledTransaction, } from 'stellar-sdk/contract' Co-authored-by: George --- .github/workflows/e2e.yml | 4 +- CHANGELOG.md | 71 +++++++++++++- README.md | 2 +- package.json | 15 +++ .../assembled_transaction.ts | 90 +++++++++--------- .../basic_node_signer.ts | 6 +- src/{contract_client => contract}/client.ts | 55 +++++------ src/{contract_client => contract}/index.ts | 3 +- .../result.ts => contract/rust_result.ts} | 43 +++++++-- .../sent_transaction.ts | 24 ++--- src/{contract_spec.ts => contract/spec.ts} | 95 +++++++++---------- src/{contract_client => contract}/types.ts | 12 +-- src/{contract_client => contract}/utils.ts | 2 +- src/index.ts | 7 +- src/{soroban => rpc}/api.ts | 0 src/{soroban => rpc}/axios.ts | 0 src/{soroban => rpc}/browser.ts | 0 src/rpc/index.ts | 13 +++ src/{soroban => rpc}/jsonrpc.ts | 0 src/{soroban => rpc}/parsers.ts | 0 src/{soroban => rpc}/server.ts | 0 src/{soroban => rpc}/transaction.ts | 0 src/{soroban => rpc}/utils.ts | 0 src/rust_types/index.ts | 1 - src/soroban/index.ts | 13 --- .../src/test-contract-client-constructor.js | 33 +++---- test/e2e/src/test-custom-types.js | 7 +- test/e2e/src/test-hello-world.js | 1 - test/e2e/src/test-swap.js | 7 +- test/e2e/src/util.js | 22 ++--- test/unit/server/soroban/constructor_test.js | 2 +- test/unit/server/soroban/get_account_test.js | 2 +- .../server/soroban/get_contract_data_test.js | 2 +- test/unit/server/soroban/get_events_test.js | 6 +- test/unit/server/soroban/get_health_test.js | 2 +- .../server/soroban/get_latest_ledger_test.js | 2 +- .../server/soroban/get_ledger_entries_test.js | 2 +- test/unit/server/soroban/get_network_test.js | 2 +- .../server/soroban/get_transaction_test.js | 2 +- .../server/soroban/request_airdrop_test.js | 2 +- .../server/soroban/send_transaction_test.js | 2 +- .../soroban/simulate_transaction_test.js | 14 +-- test/unit/spec/contract_spec.ts | 14 +-- test/unit/transaction_test.js | 14 +-- 44 files changed, 345 insertions(+), 249 deletions(-) rename src/{contract_client => contract}/assembled_transaction.ts (91%) rename src/{contract_client => contract}/basic_node_signer.ts (83%) rename src/{contract_client => contract}/client.ts (65%) rename src/{contract_client => contract}/index.ts (75%) rename src/{rust_types/result.ts => contract/rust_result.ts} (85%) rename src/{contract_client => contract}/sent_transaction.ts (89%) rename src/{contract_spec.ts => contract/spec.ts} (96%) rename src/{contract_client => contract}/types.ts (94%) rename src/{contract_client => contract}/utils.ts (98%) rename src/{soroban => rpc}/api.ts (100%) rename src/{soroban => rpc}/axios.ts (100%) rename src/{soroban => rpc}/browser.ts (100%) create mode 100644 src/rpc/index.ts rename src/{soroban => rpc}/jsonrpc.ts (100%) rename src/{soroban => rpc}/parsers.ts (100%) rename src/{soroban => rpc}/server.ts (100%) rename src/{soroban => rpc}/transaction.ts (100%) rename src/{soroban => rpc}/utils.ts (100%) delete mode 100644 src/rust_types/index.ts delete mode 100644 src/soroban/index.ts diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 0e881abf7..355552671 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -1,4 +1,4 @@ -name: ContractClient +name: contract Client on: push: @@ -7,7 +7,7 @@ on: jobs: test: - name: test generated ContractClient + name: test generated contract.Client runs-on: ubuntu-22.04 services: rpc: diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f303959d..cf2c9e45f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,10 +6,75 @@ A breaking change will get clearly marked in this log. ## Unreleased +### Breaking Changes + +- `ContractClient` functionality previously added in [v11.3.0](https://github.com/stellar/js-stellar-sdk/releases/tag/v11.3.0) was exported in a non-standard way. You can now import it as any other stellar-sdk module. + + ```diff + - import { ContractClient } from '@stellar/stellar-sdk/lib/contract_client' + + import { contract } from '@stellar/stellar-sdk' + + const { Client } = contract + ``` + + Note that this top-level `contract` export is a container for ContractClient and related functionality. The ContractClient class is now available at `contract.Client`, as shown. Further note that there is a capitalized `Contract` export as well, which comes [from stellar-base](https://github.com/stellar/js-stellar-base/blob/b96281b9b3f94af23a913f93bdb62477f5434ccc/src/contract.js#L6-L19). You can remember which is which because capital-C `Contract` is a class, whereas lowercase-c `contract` is a container/module with a bunch of classes, functions, and types. + + Additionally, this is available from the `/contract` entrypoint, if your version of Node [and TypeScript](https://stackoverflow.com/a/70020984/249801) support [the `exports` declaration](https://nodejs.org/api/packages.html#exports). Finally, some of its exports have been renamed. + + ```diff + import { + - ContractClient, + + Client, + AssembledTransaction, + - ContractClientOptions, + + ClientOptions, + SentTransaction, + -} from '@stellar/stellar-sdk/lib/contract_client' + +} from '@stellar/stellar-sdk/contract' + ``` + + +- The `ContractSpec` class is now nested under the `contract` module, and has been renamed to `Spec`. + + ```diff + -import { ContractSpec } from '@stellar/stellar-sdk' + +import { contract } from '@stellar/stellar-sdk' + +const { Spec } = contract + ``` + + Alternatively, you can import this from the `contract` entrypoint, if your version of Node [and TypeScript](https://stackoverflow.com/a/70020984/249801) support [the `exports` declaration](https://nodejs.org/api/packages.html#exports). + + ```diff + -import { ContractSpec } from '@stellar/stellar-sdk' + +import { Spec } from '@stellar/stellar-sdk/contract' + ``` + +- Previously, `AssembledTransaction.signAndSend()` would return a `SentTransaction` even if the transaction never finalized. That is, if it successfully sent the transaction to the network, but the transaction was still `status: 'PENDING'`, then it would `console.error` an error message, but return the indeterminate transaction anyhow. + + It now throws a `SentTransaction.Errors.TransactionStillPending` error with that error message instead. + +### Deprecated + +- `SorobanRpc` module is now also exported as `rpc`. You can import it with either name for now, but `SorobanRpc` will be removed in a future release. + + ```diff + import { SorobanRpc } from '@stellar/stellar-sdk' + +// OR + +import { rpc } from '@stellar/stellar-sdk' + ``` + + You can also now import it at the `/rpc` entrypoint, if your version of Node [and TypeScript](https://stackoverflow.com/a/70020984/249801) support [the `exports` declaration](https://nodejs.org/api/packages.html#exports). + + ```diff + -import { SorobanRpc } from '@stellar/stellar-sdk' + -const { Api } = SorobanRpc + +import { Api } from '@stellar/stellar-sdk/rpc' + ``` + ### Added -* Added a from method in `ContractClient` which takes the `ContractClientOptions` and instantiates the `ContractClient` by utilizing the `contractId` to retrieve the contract wasm from the blockchain. The custom section is then extracted and used to create a `ContractSpec` which is then used to create the client. -* Similarly adds `fromWasm` and `fromWasmHash` methods in `ContractClient` which can be used to initialize a `ContractClient` if you already have the wasm bytes or the wasm hash along with the `ContractClientOptions`. -* Added `getContractWasmByContractId` and `getContractWasmByHash` methods in `Server` which can be used to retrieve the wasm bytecode of a contract via its `contractId` and wasm hash respectively. + +* Added a `from` method in `contract.Client` which takes the `ClientOptions` and instantiates the `Client` by utilizing the `contractId` to retrieve the contract wasm from the blockchain. The custom section is then extracted and used to create a `contract.Spec` which is then used to create the client. +* Similarly adds `fromWasm` and `fromWasmHash` methods in `Client` which can be used to initialize a `Client` if you already have the wasm bytes or the wasm hash along with the `ClientOptions`. +* Added `getContractWasmByContractId` and `getContractWasmByHash` methods in `rpc.Server` which can be used to retrieve the wasm bytecode of a contract via its `contractId` and wasm hash respectively. ## [v12.0.0-rc.2](https://github.com/stellar/js-stellar-sdk/compare/v11.3.0...v12.0.0-rc.2) diff --git a/README.md b/README.md index 6edbabf81..1317425fd 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ The usage documentation for this library lives in a handful of places: You can also refer to: * the [documentation](https://developers.stellar.org/network/horizon) for the Horizon REST API (if using the `Horizon` module) and - * the [documentation](https://soroban.stellar.org/docs/reference/rpc) for Soroban RPC's API (if using the `SorobanRpc` module) + * the [documentation](https://soroban.stellar.org/docs/reference/rpc) for Soroban RPC's API (if using the `Rpc` module) ### Usage with React-Native diff --git a/package.json b/package.json index ba47095c0..d31f2a319 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,21 @@ "/lib", "/dist" ], + "exports": { + ".": { + "browser": "./dist/stellar-sdk.min.js", + "types": "./lib/index.d.ts", + "default": "./lib/index.js" + }, + "./contract": { + "types": "./lib/contract/index.d.ts", + "default": "./lib/contract/index.js" + }, + "./rpc": { + "types": "./lib/rpc/index.d.ts", + "default": "./lib/rpc/index.js" + } + }, "scripts": { "build": "cross-env NODE_ENV=development yarn _build", "build:prod": "cross-env NODE_ENV=production yarn _build", diff --git a/src/contract_client/assembled_transaction.ts b/src/contract/assembled_transaction.ts similarity index 91% rename from src/contract_client/assembled_transaction.ts rename to src/contract/assembled_transaction.ts index fad736533..bbe844064 100644 --- a/src/contract_client/assembled_transaction.ts +++ b/src/contract/assembled_transaction.ts @@ -1,25 +1,27 @@ /* disable max-classes rule, because extending error shouldn't count! */ /* eslint max-classes-per-file: 0 */ -import type { - AssembledTransactionOptions, - ContractClientOptions, - MethodOptions, - Tx, - XDR_BASE64, -} from "./types"; -import type { ContractClient } from "./client"; import { Account, BASE_FEE, Contract, Operation, - SorobanRpc, StrKey, TransactionBuilder, authorizeEntry, xdr, -} from ".."; -import { Err } from "../rust_types"; +} from "@stellar/stellar-base"; +import type { + AssembledTransactionOptions, + ClientOptions, + MethodOptions, + Tx, + XDR_BASE64, +} from "./types"; +import { Server } from "../rpc/server"; +import { Api } from "../rpc/api"; +import { assembleTransaction } from "../rpc/transaction"; +import type { Client } from "./client"; +import { Err } from "./rust_result"; import { DEFAULT_TIMEOUT, contractErrorPattern, @@ -31,15 +33,15 @@ export const NULL_ACCOUNT = "GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF"; /** - * The main workhorse of {@link ContractClient}. This class is used to wrap a + * The main workhorse of {@link Client}. This class is used to wrap a * transaction-under-construction and provide high-level interfaces to the most * common workflows, while still providing access to low-level stellar-sdk * transaction manipulation. * * Most of the time, you will not construct an `AssembledTransaction` directly, - * but instead receive one as the return value of a `ContractClient` method. If + * but instead receive one as the return value of a `Client` method. If * you're familiar with the libraries generated by soroban-cli's `contract - * bindings typescript` command, these also wraps `ContractClient` and return + * bindings typescript` command, these also wraps `Client` and return * `AssembledTransaction` instances. * * Let's look at examples of how to use `AssembledTransaction` for a variety of @@ -68,7 +70,7 @@ export const NULL_ACCOUNT = * ``` * * While that looks pretty complicated, most of the time you will use this in - * conjunction with {@link ContractClient}, which simplifies it to: + * conjunction with {@link Client}, which simplifies it to: * * ```ts * const { result } = await client.myReadMethod({ @@ -92,7 +94,7 @@ export const NULL_ACCOUNT = * const sentTx = await assembledTx.signAndSend() * ``` * - * Here we're assuming that you're using a {@link ContractClient}, rather than + * Here we're assuming that you're using a {@link Client}, rather than * constructing `AssembledTransaction`'s directly. * * Note that `sentTx`, the return value of `signAndSend`, is a @@ -116,7 +118,7 @@ export const NULL_ACCOUNT = * * If you need more control over the transaction before simulating it, you can * set various {@link MethodOptions} when constructing your - * `AssembledTransaction`. With a {@link ContractClient}, this is passed as a + * `AssembledTransaction`. With a {@link Client}, this is passed as a * second object after the arguments (or the only object, if the method takes * no arguments): * @@ -223,7 +225,7 @@ export const NULL_ACCOUNT = * ``` * * Under the hood, this uses `signAuthEntry`, which you either need to inject - * during initial construction of the `ContractClient`/`AssembledTransaction`, + * during initial construction of the `Client`/`AssembledTransaction`, * or which you can pass directly to `signAuthEntries`. * * Now Bob can again serialize the transaction and send back to Alice, where @@ -264,7 +266,7 @@ export class AssembledTransaction { * cached, serializable access to the data needed by AssembledTransaction * logic. */ - public simulation?: SorobanRpc.Api.SimulateTransactionResponse; + public simulation?: Api.SimulateTransactionResponse; /** * Cached simulation result. This is set after the first call to @@ -277,7 +279,7 @@ export class AssembledTransaction { * If you need access to this data after a transaction has been serialized * and then deserialized, you can call `simulationData.result`. */ - private simulationResult?: SorobanRpc.Api.SimulateHostFunctionResult; + private simulationResult?: Api.SimulateHostFunctionResult; /** * Cached simulation transaction data. This is set after the first call to @@ -296,7 +298,7 @@ export class AssembledTransaction { * The Soroban server to use for all RPC calls. This is constructed from the * `rpcUrl` in the options. */ - private server: SorobanRpc.Server; + private server: Server; /** * A list of the most important errors that various AssembledTransaction @@ -304,13 +306,13 @@ export class AssembledTransaction { * logic. */ static Errors = { - ExpiredState: class ExpiredStateError extends Error {}, - NeedsMoreSignatures: class NeedsMoreSignaturesError extends Error {}, - NoSignatureNeeded: class NoSignatureNeededError extends Error {}, - NoUnsignedNonInvokerAuthEntries: class NoUnsignedNonInvokerAuthEntriesError extends Error {}, - NoSigner: class NoSignerError extends Error {}, - NotYetSimulated: class NotYetSimulatedError extends Error {}, - FakeAccount: class FakeAccountError extends Error {}, + ExpiredState: class ExpiredStateError extends Error { }, + NeedsMoreSignatures: class NeedsMoreSignaturesError extends Error { }, + NoSignatureNeeded: class NoSignatureNeededError extends Error { }, + NoUnsignedNonInvokerAuthEntries: class NoUnsignedNonInvokerAuthEntriesError extends Error { }, + NoSigner: class NoSignerError extends Error { }, + NotYetSimulated: class NotYetSimulatedError extends Error { }, + FakeAccount: class FakeAccountError extends Error { }, }; /** @@ -364,7 +366,7 @@ export class AssembledTransaction { private constructor(public options: AssembledTransactionOptions) { this.options.simulate = this.options.simulate ?? true; - this.server = new SorobanRpc.Server(this.options.rpcUrl, { + this.server = new Server(this.options.rpcUrl, { allowHttp: this.options.allowHttp ?? false, }); } @@ -413,15 +415,15 @@ export class AssembledTransaction { if (!this.raw) { throw new Error( "Transaction has not yet been assembled; " + - "call `AssembledTransaction.build` first.", + "call `AssembledTransaction.build` first.", ); } this.built = this.raw.build(); this.simulation = await this.server.simulateTransaction(this.built); - if (SorobanRpc.Api.isSimulationSuccess(this.simulation)) { - this.built = SorobanRpc.assembleTransaction( + if (Api.isSimulationSuccess(this.simulation)) { + this.built = assembleTransaction( this.built, this.simulation, ).build(); @@ -431,7 +433,7 @@ export class AssembledTransaction { }; get simulationData(): { - result: SorobanRpc.Api.SimulateHostFunctionResult; + result: Api.SimulateHostFunctionResult; transactionData: xdr.SorobanTransactionData; } { if (this.simulationResult && this.simulationTransactionData) { @@ -446,11 +448,11 @@ export class AssembledTransaction { "Transaction has not yet been simulated", ); } - if (SorobanRpc.Api.isSimulationError(simulation)) { + if (Api.isSimulationError(simulation)) { throw new Error(`Transaction simulation failed: "${simulation.error}"`); } - if (SorobanRpc.Api.isSimulationRestore(simulation)) { + if (Api.isSimulationRestore(simulation)) { throw new AssembledTransaction.Errors.ExpiredState( `You need to restore some contract state before you can invoke this method. ${JSON.stringify( simulation, @@ -519,7 +521,7 @@ export class AssembledTransaction { /** * TSDoc: You must provide this here if you did not provide one before */ - signTransaction?: ContractClientOptions["signTransaction"]; + signTransaction?: ClientOptions["signTransaction"]; } = {}): Promise> => { if (!this.built) { throw new Error("Transaction has not yet been simulated"); @@ -528,21 +530,21 @@ export class AssembledTransaction { if (!force && this.isReadCall) { throw new AssembledTransaction.Errors.NoSignatureNeeded( "This is a read call. It requires no signature or sending. " + - "Use `force: true` to sign and send anyway.", + "Use `force: true` to sign and send anyway.", ); } if (!signTransaction) { throw new AssembledTransaction.Errors.NoSigner( "You must provide a signTransaction function, either when calling " + - "`signAndSend` or when initializing your ContractClient", + "`signAndSend` or when initializing your Client", ); } if (this.needsNonInvokerSigningBy().length) { throw new AssembledTransaction.Errors.NeedsMoreSignatures( "Transaction requires more signatures. " + - "See `needsNonInvokerSigningBy` for details.", + "See `needsNonInvokerSigningBy` for details.", ); } @@ -615,10 +617,10 @@ export class AssembledTransaction { .filter( (entry) => entry.credentials().switch() === - xdr.SorobanCredentialsType.sorobanCredentialsAddress() && + xdr.SorobanCredentialsType.sorobanCredentialsAddress() && (includeAlreadySigned || entry.credentials().address().signature().switch().name === - "scvVoid"), + "scvVoid"), ) .map((entry) => StrKey.encodeEd25519PublicKey( @@ -664,10 +666,10 @@ export class AssembledTransaction { publicKey?: string; /** * You must provide this here if you did not provide one before. Default: - * the `signAuthEntry` function from the `ContractClient` options. Must + * the `signAuthEntry` function from the `Client` options. Must * sign things as the given `publicKey`. */ - signAuthEntry?: ContractClientOptions["signAuthEntry"]; + signAuthEntry?: ClientOptions["signAuthEntry"]; } = {}): Promise => { if (!this.built) throw new Error("Transaction has not yet been assembled or simulated"); @@ -686,7 +688,7 @@ export class AssembledTransaction { if (!signAuthEntry) { throw new AssembledTransaction.Errors.NoSigner( "You must provide `signAuthEntry` when calling `signAuthEntries`, " + - "or when constructing the `ContractClient` or `AssembledTransaction`", + "or when constructing the `Client` or `AssembledTransaction`", ); } diff --git a/src/contract_client/basic_node_signer.ts b/src/contract/basic_node_signer.ts similarity index 83% rename from src/contract_client/basic_node_signer.ts rename to src/contract/basic_node_signer.ts index 9bfe72192..9e50327cd 100644 --- a/src/contract_client/basic_node_signer.ts +++ b/src/contract/basic_node_signer.ts @@ -1,9 +1,9 @@ -import { Keypair, TransactionBuilder, hash } from ".."; +import { Keypair, TransactionBuilder, hash } from "@stellar/stellar-base"; import type { AssembledTransaction } from "./assembled_transaction"; -import type { ContractClient } from "./client"; +import type { Client } from "./client"; /** - * For use with {@link ContractClient} and {@link AssembledTransaction}. + * For use with {@link Client} and {@link AssembledTransaction}. * Implements `signTransaction` and `signAuthEntry` with signatures expected by * those classes. This is useful for testing and maybe some simple Node * applications. Feel free to use this as a starting point for your own diff --git a/src/contract_client/client.ts b/src/contract/client.ts similarity index 65% rename from src/contract_client/client.ts rename to src/contract/client.ts index c9133c785..e0c298b4a 100644 --- a/src/contract_client/client.ts +++ b/src/contract/client.ts @@ -1,10 +1,11 @@ -import { ContractSpec, xdr } from ".."; -import { Server } from '../soroban'; +import { xdr } from "@stellar/stellar-base"; +import { Spec } from "./spec"; +import { Server } from '../rpc'; import { AssembledTransaction } from "./assembled_transaction"; -import type { ContractClientOptions, MethodOptions } from "./types"; +import type { ClientOptions, MethodOptions } from "./types"; import { processSpecEntryStream } from './utils'; -export class ContractClient { +export class Client { /** * Generate a class from the contract spec that where each contract method * gets included with an identical name. @@ -14,10 +15,10 @@ export class ContractClient { * transaction. */ constructor( - /** {@link ContractSpec} to construct a Client for */ - public readonly spec: ContractSpec, - /** see {@link ContractClientOptions} */ - public readonly options: ContractClientOptions, + /** {@link Spec} to construct a Client for */ + public readonly spec: Spec, + /** see {@link ClientOptions} */ + public readonly options: ClientOptions, ) { this.spec.funcs().forEach((xdrFn) => { const method = xdrFn.name().toString(); @@ -35,7 +36,7 @@ export class ContractClient { ...acc, [curr.value()]: { message: curr.doc().toString() }, }), - {} as Pick, + {} as Pick, ), parseResultXdr: (result: xdr.ScVal) => spec.funcResToNative(method, result), @@ -50,19 +51,19 @@ export class ContractClient { } /** - * Generates a ContractClient instance from the provided ContractClientOptions and the contract's wasm hash. + * Generates a Client instance from the provided ClientOptions and the contract's wasm hash. * The wasmHash can be provided in either hex or base64 format. * * @param wasmHash The hash of the contract's wasm binary, in either hex or base64 format. - * @param options The ContractClientOptions object containing the necessary configuration, including the rpcUrl. + * @param options The ClientOptions object containing the necessary configuration, including the rpcUrl. * @param format The format of the provided wasmHash, either "hex" or "base64". Defaults to "hex". - * @returns A Promise that resolves to a ContractClient instance. + * @returns A Promise that resolves to a Client instance. * @throws {TypeError} If the provided options object does not contain an rpcUrl. */ - static async fromWasmHash(wasmHash: Buffer | string, - options: ContractClientOptions, + static async fromWasmHash(wasmHash: Buffer | string, + options: ClientOptions, format: "hex" | "base64" = "hex" - ): Promise { + ): Promise { if (!options || !options.rpcUrl) { throw new TypeError('options must contain rpcUrl'); } @@ -70,18 +71,18 @@ export class ContractClient { const serverOpts: Server.Options = { allowHttp }; const server = new Server(rpcUrl, serverOpts); const wasm = await server.getContractWasmByHash(wasmHash, format); - return ContractClient.fromWasm(wasm, options); + return Client.fromWasm(wasm, options); } /** - * Generates a ContractClient instance from the provided ContractClientOptions and the contract's wasm binary. + * Generates a Client instance from the provided ClientOptions and the contract's wasm binary. * * @param wasm The contract's wasm binary as a Buffer. - * @param options The ContractClientOptions object containing the necessary configuration. - * @returns A Promise that resolves to a ContractClient instance. + * @param options The ClientOptions object containing the necessary configuration. + * @returns A Promise that resolves to a Client instance. * @throws {Error} If the contract spec cannot be obtained from the provided wasm binary. */ - static async fromWasm(wasm: Buffer, options: ContractClientOptions): Promise { + static async fromWasm(wasm: Buffer, options: ClientOptions): Promise { const wasmModule = await WebAssembly.compile(wasm); const xdrSections = WebAssembly.Module.customSections(wasmModule, "contractspecv0"); if (xdrSections.length === 0) { @@ -89,18 +90,18 @@ export class ContractClient { } const bufferSection = Buffer.from(xdrSections[0]); const specEntryArray = processSpecEntryStream(bufferSection); - const spec = new ContractSpec(specEntryArray); - return new ContractClient(spec, options); + const spec = new Spec(specEntryArray); + return new Client(spec, options); } /** - * Generates a ContractClient instance from the provided ContractClientOptions, which must include the contractId and rpcUrl. + * Generates a Client instance from the provided ClientOptions, which must include the contractId and rpcUrl. * - * @param options The ContractClientOptions object containing the necessary configuration, including the contractId and rpcUrl. - * @returns A Promise that resolves to a ContractClient instance. + * @param options The ClientOptions object containing the necessary configuration, including the contractId and rpcUrl. + * @returns A Promise that resolves to a Client instance. * @throws {TypeError} If the provided options object does not contain both rpcUrl and contractId. */ - static async from(options: ContractClientOptions): Promise { + static async from(options: ClientOptions): Promise { if (!options || !options.rpcUrl || !options.contractId) { throw new TypeError('options must contain rpcUrl and contractId'); } @@ -108,7 +109,7 @@ export class ContractClient { const serverOpts: Server.Options = { allowHttp }; const server = new Server(rpcUrl, serverOpts); const wasm = await server.getContractWasmByContractId(contractId); - return ContractClient.fromWasm(wasm, options); + return Client.fromWasm(wasm, options); } txFromJSON = (json: string): AssembledTransaction => { diff --git a/src/contract_client/index.ts b/src/contract/index.ts similarity index 75% rename from src/contract_client/index.ts rename to src/contract/index.ts index bd0c73fed..8b9e1dc5e 100644 --- a/src/contract_client/index.ts +++ b/src/contract/index.ts @@ -1,6 +1,7 @@ export * from "./assembled_transaction"; export * from "./basic_node_signer"; export * from "./client"; +export * from "./rust_result"; export * from "./sent_transaction"; +export * from "./spec"; export * from "./types"; -export * from "./utils"; diff --git a/src/rust_types/result.ts b/src/contract/rust_result.ts similarity index 85% rename from src/rust_types/result.ts rename to src/contract/rust_result.ts index 8a47de4f4..b3c160851 100644 --- a/src/rust_types/result.ts +++ b/src/contract/rust_result.ts @@ -1,3 +1,6 @@ +/* disable max-classes rule, because extending error shouldn't count! */ +/* eslint max-classes-per-file: 0 */ + /** * A minimal implementation of Rust's `Result` type. Used for contract * methods that return Results, to maintain their distinction from methods @@ -52,10 +55,22 @@ export interface ErrorMessage { */ export class Ok implements Result { constructor(readonly value: T) {} - unwrapErr(): never { throw new Error("No error") } - unwrap() { return this.value } - isOk() { return true } - isErr() { return false } + + unwrapErr(): never { + throw new Error("No error"); + } + + unwrap() { + return this.value; + } + + isOk() { + return true; + } + + isErr() { + return false; + } } /** @@ -65,8 +80,20 @@ export class Ok implements Result { */ export class Err implements Result { constructor(readonly error: E) {} - unwrapErr() { return this.error } - unwrap(): never { throw new Error(this.error.message) } - isOk() { return false } - isErr() { return true } + + unwrapErr() { + return this.error; + } + + unwrap(): never { + throw new Error(this.error.message); + } + + isOk() { + return false; + } + + isErr() { + return true; + } } diff --git a/src/contract_client/sent_transaction.ts b/src/contract/sent_transaction.ts similarity index 89% rename from src/contract_client/sent_transaction.ts rename to src/contract/sent_transaction.ts index baa5e1f6f..d6f275d78 100644 --- a/src/contract_client/sent_transaction.ts +++ b/src/contract/sent_transaction.ts @@ -1,7 +1,9 @@ /* disable max-classes rule, because extending error shouldn't count! */ /* eslint max-classes-per-file: 0 */ -import type { ContractClientOptions, MethodOptions, Tx } from "./types"; -import { SorobanDataBuilder, SorobanRpc, TransactionBuilder } from ".."; +import { SorobanDataBuilder, TransactionBuilder } from "@stellar/stellar-base"; +import type { ClientOptions, MethodOptions, Tx } from "./types"; +import { Server } from "../rpc/server" +import { Api } from "../rpc/api" import { DEFAULT_TIMEOUT, withExponentialBackoff } from "./utils"; import type { AssembledTransaction } from "./assembled_transaction"; @@ -20,7 +22,7 @@ import type { AssembledTransaction } from "./assembled_transaction"; * `getTransactionResponse`. */ export class SentTransaction { - public server: SorobanRpc.Server; + public server: Server; public signed?: Tx; @@ -28,7 +30,7 @@ export class SentTransaction { * The result of calling `sendTransaction` to broadcast the transaction to the * network. */ - public sendTransactionResponse?: SorobanRpc.Api.SendTransactionResponse; + public sendTransactionResponse?: Api.SendTransactionResponse; /** * If `sendTransaction` completes successfully (which means it has `status: 'PENDING'`), @@ -36,13 +38,13 @@ export class SentTransaction { * {@link MethodOptions.timeoutInSeconds} seconds. This array contains all * the results of those calls. */ - public getTransactionResponseAll?: SorobanRpc.Api.GetTransactionResponse[]; + public getTransactionResponseAll?: Api.GetTransactionResponse[]; /** * The most recent result of calling `getTransaction`, from the * `getTransactionResponseAll` array. */ - public getTransactionResponse?: SorobanRpc.Api.GetTransactionResponse; + public getTransactionResponse?: Api.GetTransactionResponse; static Errors = { SendFailed: class SendFailedError extends Error { }, @@ -51,7 +53,7 @@ export class SentTransaction { }; constructor( - public signTransaction: ContractClientOptions["signTransaction"], + public signTransaction: ClientOptions["signTransaction"], public assembled: AssembledTransaction, ) { if (!signTransaction) { @@ -59,7 +61,7 @@ export class SentTransaction { "You must provide a `signTransaction` function to send a transaction", ); } - this.server = new SorobanRpc.Server(this.assembled.options.rpcUrl, { + this.server = new Server(this.assembled.options.rpcUrl, { allowHttp: this.assembled.options.allowHttp ?? false, }); } @@ -71,7 +73,7 @@ export class SentTransaction { */ static init = async ( /** More info in {@link MethodOptions} */ - signTransaction: ContractClientOptions["signTransaction"], + signTransaction: ClientOptions["signTransaction"], /** {@link AssembledTransaction} from which this SentTransaction was initialized */ assembled: AssembledTransaction, ): Promise> => { @@ -124,7 +126,7 @@ export class SentTransaction { this.getTransactionResponseAll = await withExponentialBackoff( () => this.server.getTransaction(hash), - (resp) => resp.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND, + (resp) => resp.status === Api.GetTransactionStatus.NOT_FOUND, timeoutInSeconds, ); @@ -132,7 +134,7 @@ export class SentTransaction { this.getTransactionResponseAll[this.getTransactionResponseAll.length - 1]; if ( this.getTransactionResponse.status === - SorobanRpc.Api.GetTransactionStatus.NOT_FOUND + Api.GetTransactionStatus.NOT_FOUND ) { throw new SentTransaction.Errors.TransactionStillPending( `Waited ${timeoutInSeconds} seconds for transaction to complete, but it did not. ` + diff --git a/src/contract_spec.ts b/src/contract/spec.ts similarity index 96% rename from src/contract_spec.ts rename to src/contract/spec.ts index 91add10fb..937875fbb 100644 --- a/src/contract_spec.ts +++ b/src/contract/spec.ts @@ -1,4 +1,4 @@ -import { JSONSchema7, JSONSchema7Definition } from "json-schema"; +import type { JSONSchema7, JSONSchema7Definition } from "json-schema"; import { ScIntType, XdrLargeInt, @@ -6,8 +6,8 @@ import { Address, Contract, scValToBigInt, -} from "."; -import { Ok } from "./rust_types"; +} from "@stellar/stellar-base" +import { Ok } from "./rust_result" export interface Union { tag: string; @@ -48,7 +48,7 @@ function readObj(args: object, input: xdr.ScSpecFunctionInputV0): any { * console.log(result); // {success: true} * ``` */ -export class ContractSpec { +export class Spec { public entries: xdr.ScSpecEntry[] = []; /** @@ -65,7 +65,7 @@ export class ContractSpec { let entry = entries[0]; if (typeof entry === "string") { this.entries = (entries as string[]).map((s) => - xdr.ScSpecEntry.fromXDR(s, "base64") + xdr.ScSpecEntry.fromXDR(s, "base64"), ); } else { this.entries = entries as xdr.ScSpecEntry[]; @@ -83,7 +83,7 @@ export class ContractSpec { .filter( (entry) => entry.switch().value === - xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value + xdr.ScSpecEntryKind.scSpecEntryFunctionV0().value, ) .map((entry) => entry.functionV0()); } @@ -164,9 +164,7 @@ export class ContractSpec { } let output = outputs[0]; if (output.switch().value === xdr.ScSpecType.scSpecTypeResult().value) { - return new Ok( - this.scValToNative(val, output.result().okType()) - ); + return new Ok(this.scValToNative(val, output.result().okType())); } return this.scValToNative(val, output); } @@ -181,7 +179,7 @@ export class ContractSpec { */ findEntry(name: string): xdr.ScSpecEntry { let entry = this.entries.find( - (entry) => entry.value().name().toString() === name + (entry) => entry.value().name().toString() === name, ); if (!entry) { throw new Error(`no such entry: ${name}`); @@ -220,7 +218,7 @@ export class ContractSpec { return xdr.ScVal.scvVoid(); default: throw new TypeError( - `Type ${ty} was not void, but value was null` + `Type ${ty} was not void, but value was null`, ); } } @@ -232,7 +230,7 @@ export class ContractSpec { if (val instanceof Address) { if (ty.switch().value !== xdr.ScSpecType.scSpecTypeAddress().value) { throw new TypeError( - `Type ${ty} was not address, but value was Address` + `Type ${ty} was not address, but value was Address`, ); } return val.toScVal(); @@ -241,7 +239,7 @@ export class ContractSpec { if (val instanceof Contract) { if (ty.switch().value !== xdr.ScSpecType.scSpecTypeAddress().value) { throw new TypeError( - `Type ${ty} was not address, but value was Address` + `Type ${ty} was not address, but value was Address`, ); } return val.address().toScVal(); @@ -254,7 +252,7 @@ export class ContractSpec { let bytes_n = ty.bytesN(); if (copy.length !== bytes_n.n()) { throw new TypeError( - `expected ${bytes_n.n()} bytes, but got ${copy.length}` + `expected ${bytes_n.n()} bytes, but got ${copy.length}`, ); } //@ts-ignore @@ -265,7 +263,7 @@ export class ContractSpec { return xdr.ScVal.scvBytes(copy); default: throw new TypeError( - `invalid type (${ty}) specified for Bytes and BytesN` + `invalid type (${ty}) specified for Bytes and BytesN`, ); } } @@ -275,7 +273,7 @@ export class ContractSpec { let vec = ty.vec(); let elementType = vec.elementType(); return xdr.ScVal.scvVec( - val.map((v) => this.nativeToScVal(v, elementType)) + val.map((v) => this.nativeToScVal(v, elementType)), ); } case xdr.ScSpecType.scSpecTypeTuple().value: { @@ -283,11 +281,11 @@ export class ContractSpec { let valTypes = tup.valueTypes(); if (val.length !== valTypes.length) { throw new TypeError( - `Tuple expects ${valTypes.length} values, but ${val.length} were provided` + `Tuple expects ${valTypes.length} values, but ${val.length} were provided`, ); } return xdr.ScVal.scvVec( - val.map((v, i) => this.nativeToScVal(v, valTypes[i])) + val.map((v, i) => this.nativeToScVal(v, valTypes[i])), ); } case xdr.ScSpecType.scSpecTypeMap().value: { @@ -299,13 +297,13 @@ export class ContractSpec { let key = this.nativeToScVal(entry[0], keyType); let val = this.nativeToScVal(entry[1], valueType); return new xdr.ScMapEntry({ key, val }); - }) + }), ); } default: throw new TypeError( - `Type ${ty} was not vec, but value was Array` + `Type ${ty} was not vec, but value was Array`, ); } } @@ -330,14 +328,13 @@ export class ContractSpec { if ((val.constructor?.name ?? "") !== "Object") { throw new TypeError( - `cannot interpret ${ - val.constructor?.name - } value as ScVal (${JSON.stringify(val)})` + `cannot interpret ${val.constructor?.name + } value as ScVal (${JSON.stringify(val)})`, ); } throw new TypeError( - `Received object ${val} did not match the provided type ${ty}` + `Received object ${val} did not match the provided type ${ty}`, ); } @@ -380,7 +377,7 @@ export class ContractSpec { return xdr.ScVal.scvVoid(); default: throw new TypeError( - `Type ${ty} was not void, but value was undefined` + `Type ${ty} was not void, but value was undefined`, ); } } @@ -399,7 +396,7 @@ export class ContractSpec { case xdr.ScSpecEntryKind.scSpecEntryUdtEnumV0(): if (typeof val !== "number") { throw new TypeError( - `expected number for enum ${name}, but got ${typeof val}` + `expected number for enum ${name}, but got ${typeof val}`, ); } return this.nativeToEnum(val as number, entry.udtEnumV0()); @@ -414,7 +411,7 @@ export class ContractSpec { private nativeToUnion( val: Union, - union_: xdr.ScSpecUdtUnionV0 + union_: xdr.ScSpecUdtUnionV0, ): xdr.ScVal { let entry_name = val.tag; let case_ = union_.cases().find((entry) => { @@ -434,11 +431,11 @@ export class ContractSpec { if (Array.isArray(val.values)) { if (val.values.length != types.length) { throw new TypeError( - `union ${union_} expects ${types.length} values, but got ${val.values.length}` + `union ${union_} expects ${types.length} values, but got ${val.values.length}`, ); } let scvals = val.values.map((v, i) => - this.nativeToScVal(v, types[i]) + this.nativeToScVal(v, types[i]), ); scvals.unshift(key); return xdr.ScVal.scvVec(scvals); @@ -455,11 +452,11 @@ export class ContractSpec { if (fields.some(isNumeric)) { if (!fields.every(isNumeric)) { throw new Error( - "mixed numeric and non-numeric field names are not allowed" + "mixed numeric and non-numeric field names are not allowed", ); } return xdr.ScVal.scvVec( - fields.map((_, i) => this.nativeToScVal(val[i], fields[i].type())) + fields.map((_, i) => this.nativeToScVal(val[i], fields[i].type())), ); } return xdr.ScVal.scvMap( @@ -469,7 +466,7 @@ export class ContractSpec { key: this.nativeToScVal(name, xdr.ScSpecTypeDef.scSpecTypeSymbol()), val: this.nativeToScVal(val[name], field.type()), }); - }) + }), ); } @@ -531,13 +528,13 @@ export class ContractSpec { if (value == xdr.ScSpecType.scSpecTypeVec().value) { let vec = typeDef.vec(); return (scv.vec() ?? []).map((elm) => - this.scValToNative(elm, vec.elementType()) + this.scValToNative(elm, vec.elementType()), ) as T; } else if (value == xdr.ScSpecType.scSpecTypeTuple().value) { let tuple = typeDef.tuple(); let valTypes = tuple.valueTypes(); return (scv.vec() ?? []).map((elm, i) => - this.scValToNative(elm, valTypes[i]) + this.scValToNative(elm, valTypes[i]), ) as T; } throw new TypeError(`Type ${typeDef} was not vec, but ${scv} is`); @@ -562,8 +559,8 @@ export class ContractSpec { `ScSpecType ${t.name} was not map, but ${JSON.stringify( scv, null, - 2 - )} is` + 2, + )} is`, ); } @@ -581,9 +578,8 @@ export class ContractSpec { value !== xdr.ScSpecType.scSpecTypeSymbol().value ) { throw new Error( - `ScSpecType ${ - t.name - } was not string or symbol, but ${JSON.stringify(scv, null, 2)} is` + `ScSpecType ${t.name + } was not string or symbol, but ${JSON.stringify(scv, null, 2)} is`, ); } return scv.value()?.toString() as T; @@ -600,8 +596,8 @@ export class ContractSpec { `failed to convert ${JSON.stringify( scv, null, - 2 - )} to native type from type ${t.name}` + 2, + )} to native type from type ${t.name}`, ); } } @@ -617,7 +613,7 @@ export class ContractSpec { return this.unionToNative(scv, entry.udtUnionV0()); default: throw new Error( - `failed to parse udt ${udt.name().toString()}: ${entry}` + `failed to parse udt ${udt.name().toString()}: ${entry}`, ); } } @@ -629,7 +625,7 @@ export class ContractSpec { } if (vec.length === 0 && udt.cases.length !== 0) { throw new Error( - `${val} has length 0, but the there are at least one case in the union` + `${val} has length 0, but the there are at least one case in the union`, ); } let name = vec[0].sym().toString(); @@ -639,7 +635,7 @@ export class ContractSpec { let entry = udt.cases().find(findCase(name)); if (!entry) { throw new Error( - `failed to find entry ${name} in union {udt.name().toString()}` + `failed to find entry ${name} in union {udt.name().toString()}`, ); } let res: Union = { tag: name }; @@ -667,7 +663,7 @@ export class ContractSpec { let field = fields[i]; res[field.name().toString()] = this.scValToNative( entry.val(), - field.type() + field.type(), ); }); return res; @@ -692,7 +688,7 @@ export class ContractSpec { .filter( (entry) => entry.switch().value === - xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value + xdr.ScSpecEntryKind.scSpecEntryUdtErrorEnumV0().value, ) .flatMap((entry) => (entry.value() as xdr.ScSpecUdtErrorEnumV0).cases()); } @@ -1012,7 +1008,7 @@ function structToJsonSchema(udt: xdr.ScSpecUdtStructV0): object { if (fields.some(isNumeric)) { if (!fields.every(isNumeric)) { throw new Error( - "mixed numeric and non-numeric field names are not allowed" + "mixed numeric and non-numeric field names are not allowed", ); } let items = fields.map((_, i) => typeRef(fields[i].type())); @@ -1035,7 +1031,7 @@ function structToJsonSchema(udt: xdr.ScSpecUdtStructV0): object { } function args_and_required( - input: { type: () => xdr.ScSpecTypeDef; name: () => string | Buffer }[] + input: { type: () => xdr.ScSpecTypeDef; name: () => string | Buffer }[], ): { properties: object; required?: string[] } { let properties: any = {}; let required: string[] = []; @@ -1116,7 +1112,7 @@ function unionToJsonSchema(udt: xdr.ScSpecUdtUnionV0): any { tag: title, values: { type: "array", - items: c.type().map(typeRef) + items: c.type().map(typeRef), }, }, required: ["tag", "values"], @@ -1156,4 +1152,3 @@ function enumToJsonSchema(udt: xdr.ScSpecUdtEnumV0): any { } return res; } - diff --git a/src/contract_client/types.ts b/src/contract/types.ts similarity index 94% rename from src/contract_client/types.ts rename to src/contract/types.ts index 5005d28c6..8b92276c2 100644 --- a/src/contract_client/types.ts +++ b/src/contract/types.ts @@ -1,7 +1,7 @@ /* disable PascalCase naming convention, to avoid breaking change */ /* eslint-disable @typescript-eslint/naming-convention */ -import { BASE_FEE, Memo, MemoType, Operation, Transaction, xdr } from ".."; -import type { ContractClient } from "./client"; +import { BASE_FEE, Memo, MemoType, Operation, Transaction, xdr } from "@stellar/stellar-base"; +import type { Client } from "./client"; import type { AssembledTransaction } from "./assembled_transaction"; import { DEFAULT_TIMEOUT } from "./utils"; @@ -23,7 +23,7 @@ export type Duration = bigint; */ export type Tx = Transaction, Operation[]>; -export type ContractClientOptions = { +export type ClientOptions = { /** * The public key of the account that will send this transaction. You can * override this for specific methods later, like @@ -74,14 +74,14 @@ export type ContractClientOptions = { allowHttp?: boolean; /** * This gets filled in automatically from the ContractSpec when you - * instantiate a {@link ContractClient}. + * instantiate a {@link Client}. * * Background: If the contract you're calling uses the `#[contracterror]` * macro to create an `Error` enum, then those errors get included in the * on-chain XDR that also describes your contract's methods. Each error will * have a specific number. * - * A ContractClient makes method calls with an {@link AssembledTransaction}. + * A Client makes method calls with an {@link AssembledTransaction}. * When one of these method calls encounters an error, `AssembledTransaction` * will first attempt to parse the error as an "official" `contracterror` * error, by using this passed-in `errorTypes` object. See @@ -112,7 +112,7 @@ export type MethodOptions = { }; export type AssembledTransactionOptions = MethodOptions & - ContractClientOptions & { + ClientOptions & { method: string; args?: any[]; parseResultXdr: (xdr: xdr.ScVal) => T; diff --git a/src/contract_client/utils.ts b/src/contract/utils.ts similarity index 98% rename from src/contract_client/utils.ts rename to src/contract/utils.ts index de4293a91..44e7b8b02 100644 --- a/src/contract_client/utils.ts +++ b/src/contract/utils.ts @@ -1,4 +1,4 @@ -import { xdr, cereal } from ".."; +import { xdr, cereal } from "@stellar/stellar-base"; import type { AssembledTransaction } from "./assembled_transaction"; /** diff --git a/src/index.ts b/src/index.ts index ec315bd0c..45ca253b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,8 +17,11 @@ export * as Friendbot from './friendbot'; export * as Horizon from './horizon'; // Soroban RPC-related classes to expose -export * as SorobanRpc from './soroban'; -export * from './contract_spec'; +export * as rpc from './rpc'; +export * as SorobanRpc from './rpc'; + +// Soroban Contract-related classes to expose +export * as contract from './contract' // expose classes and functions from stellar-base export * from '@stellar/stellar-base'; diff --git a/src/soroban/api.ts b/src/rpc/api.ts similarity index 100% rename from src/soroban/api.ts rename to src/rpc/api.ts diff --git a/src/soroban/axios.ts b/src/rpc/axios.ts similarity index 100% rename from src/soroban/axios.ts rename to src/rpc/axios.ts diff --git a/src/soroban/browser.ts b/src/rpc/browser.ts similarity index 100% rename from src/soroban/browser.ts rename to src/rpc/browser.ts diff --git a/src/rpc/index.ts b/src/rpc/index.ts new file mode 100644 index 000000000..99ee2ba2a --- /dev/null +++ b/src/rpc/index.ts @@ -0,0 +1,13 @@ +// tslint:disable-next-line: no-reference +/// + +// Expose all types +export * from "./api"; + +// soroban-client classes to expose +export { Server, Durability } from "./server"; +export { default as AxiosClient } from "./axios"; +export { parseRawSimulation, parseRawEvents } from "./parsers"; +export * from "./transaction"; + +export default module.exports; diff --git a/src/soroban/jsonrpc.ts b/src/rpc/jsonrpc.ts similarity index 100% rename from src/soroban/jsonrpc.ts rename to src/rpc/jsonrpc.ts diff --git a/src/soroban/parsers.ts b/src/rpc/parsers.ts similarity index 100% rename from src/soroban/parsers.ts rename to src/rpc/parsers.ts diff --git a/src/soroban/server.ts b/src/rpc/server.ts similarity index 100% rename from src/soroban/server.ts rename to src/rpc/server.ts diff --git a/src/soroban/transaction.ts b/src/rpc/transaction.ts similarity index 100% rename from src/soroban/transaction.ts rename to src/rpc/transaction.ts diff --git a/src/soroban/utils.ts b/src/rpc/utils.ts similarity index 100% rename from src/soroban/utils.ts rename to src/rpc/utils.ts diff --git a/src/rust_types/index.ts b/src/rust_types/index.ts deleted file mode 100644 index bb162e4c8..000000000 --- a/src/rust_types/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./result"; diff --git a/src/soroban/index.ts b/src/soroban/index.ts deleted file mode 100644 index 36e296ee1..000000000 --- a/src/soroban/index.ts +++ /dev/null @@ -1,13 +0,0 @@ -// tslint:disable-next-line: no-reference -/// - -// Expose all types -export * from './api'; - -// soroban-client classes to expose -export { Server, Durability } from './server'; -export { default as AxiosClient } from './axios'; -export { parseRawSimulation, parseRawEvents } from './parsers'; -export * from './transaction'; - -export default module.exports; diff --git a/test/e2e/src/test-contract-client-constructor.js b/test/e2e/src/test-contract-client-constructor.js index 018b370db..83211941c 100644 --- a/test/e2e/src/test-contract-client-constructor.js +++ b/test/e2e/src/test-contract-client-constructor.js @@ -1,14 +1,7 @@ const test = require('ava') const { spawnSync } = require('node:child_process') -const { Address } = require('../../..') const { contracts, networkPassphrase, rpcUrl, friendbotUrl } = require('./util') -const { ContractSpec } = require('../../..') -const { Keypair } = require('../../..') - -const { - ContractClient, - basicNodeSigner, -} = require('../../../lib/contract_client') +const { Address, contract, Keypair } = require('../../..') async function generateFundedKeypair() { const keypair = Keypair.random() @@ -17,7 +10,7 @@ async function generateFundedKeypair() { }; /** - * Generates a ContractClient for the contract with the given name. + * Generates a Client for the contract with the given name. * Also generates a new account to use as as the keypair of this contract. This * account is funded by friendbot. You can pass in an account to re-use the * same account with multiple contract clients. @@ -25,21 +18,21 @@ async function generateFundedKeypair() { * By default, will re-deploy the contract every time. Pass in the same * `contractId` again if you want to re-use the a contract instance. */ -async function clientFromConstructor(contract, { keypair = generateFundedKeypair(), contractId } = {}) { - if (!contracts[contract]) { +async function clientFromConstructor(name, { keypair = generateFundedKeypair(), contractId } = {}) { + if (!contracts[name]) { throw new Error( - `Contract ${contract} not found. ` + + `Contract ${name} not found. ` + `Pick one of: ${Object.keys(contracts).join(", ")}` ) } keypair = await keypair // eslint-disable-line no-param-reassign - const wallet = basicNodeSigner(keypair, networkPassphrase) + const wallet = contract.basicNodeSigner(keypair, networkPassphrase) - const {path} = contracts[contract]; + const {path} = contracts[name]; const xdr = JSON.parse(spawnSync("./target/bin/soroban", ["contract", "inspect", "--wasm", path, "--output", "xdr-base64-array"], { shell: true, encoding: "utf8" }).stdout.trim()) - const spec = new ContractSpec(xdr); - let wasmHash = contracts[contract].hash; + const spec = new contract.Spec(xdr); + let wasmHash = contracts[name].hash; if (!wasmHash) { wasmHash = spawnSync("./target/bin/soroban", ["contract", "install", "--wasm", path], { shell: true, encoding: "utf8" }).stdout.trim() } @@ -54,7 +47,7 @@ async function clientFromConstructor(contract, { keypair = generateFundedKeypair wasmHash, ], { shell: true, encoding: "utf8" }).stdout.trim(); - const client = new ContractClient(spec, { + const client = new contract.Client(spec, { networkPassphrase, contractId, rpcUrl, @@ -70,11 +63,11 @@ async function clientFromConstructor(contract, { keypair = generateFundedKeypair } /** - * Generates a ContractClient given the contractId using the from method. + * Generates a Client given the contractId using the from method. */ async function clientForFromTest(contractId, publicKey, keypair) { keypair = await keypair; // eslint-disable-line no-param-reassign - const wallet = basicNodeSigner(keypair, networkPassphrase); + const wallet = contract.basicNodeSigner(keypair, networkPassphrase); const options = { networkPassphrase, contractId, @@ -83,7 +76,7 @@ async function clientForFromTest(contractId, publicKey, keypair) { publicKey, ...wallet, }; - return ContractClient.from(options); + return contract.Client.from(options); } test.before(async t => { diff --git a/test/e2e/src/test-custom-types.js b/test/e2e/src/test-custom-types.js index fd511e3a3..63094187b 100644 --- a/test/e2e/src/test-custom-types.js +++ b/test/e2e/src/test-custom-types.js @@ -1,6 +1,5 @@ const test = require('ava') -const { Address } = require('../../..') -const { Ok, Err } = require('../../../lib/rust_types') +const { Address, contract } = require('../../..') const { clientFor } = require('./util') test.before(async t => { @@ -29,11 +28,11 @@ test('woid', async t => { test('u32_fail_on_even', async t => { t.deepEqual( (await t.context.client.u32_fail_on_even({ u32_: 1 })).result, - new Ok(1) + new contract.Ok(1) ) t.deepEqual( (await t.context.client.u32_fail_on_even({ u32_: 2 })).result, - new Err({ message: "Please provide an odd number" }) + new contract.Err({ message: "Please provide an odd number" }) ) }) diff --git a/test/e2e/src/test-hello-world.js b/test/e2e/src/test-hello-world.js index a489f1692..fbd927be3 100644 --- a/test/e2e/src/test-hello-world.js +++ b/test/e2e/src/test-hello-world.js @@ -1,6 +1,5 @@ const test = require('ava') const { clientFor } = require('./util') -const { Keypair } = require('../../..') test("hello", async (t) => { const { client } = await clientFor('helloWorld') diff --git a/test/e2e/src/test-swap.js b/test/e2e/src/test-swap.js index 802ed0d73..885debda9 100644 --- a/test/e2e/src/test-swap.js +++ b/test/e2e/src/test-swap.js @@ -1,6 +1,5 @@ const test = require('ava') -const { SorobanRpc } = require('../../..') -const { AssembledTransaction } = require('../../../lib/contract_client') +const { contract, rpc } = require('../../..') const { clientFor, generateFundedKeypair } = require('./util') const amountAToSwap = 2n @@ -52,7 +51,7 @@ test('calling `signAndSend()` too soon throws descriptive error', async t => { min_b_for_a: amountBToSwap, }) const error = await t.throwsAsync(tx.signAndSend()) - t.true(error instanceof AssembledTransaction.Errors.NeedsMoreSignatures, `error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`) + t.true(error instanceof contract.AssembledTransaction.Errors.NeedsMoreSignatures, `error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`) if (error) t.regex(error.message, /needsNonInvokerSigningBy/) }) @@ -111,7 +110,7 @@ test('alice swaps bob 10 A for 1 B', async t => { ) t.is( result.getTransactionResponse.status, - SorobanRpc.Api.GetTransactionStatus.SUCCESS, + rpc.Api.GetTransactionStatus.SUCCESS, `tx failed: ${JSON.stringify(result.getTransactionResponse, null, 2)}` ) diff --git a/test/e2e/src/util.js b/test/e2e/src/util.js index 7ecb82f22..c70995de2 100644 --- a/test/e2e/src/util.js +++ b/test/e2e/src/util.js @@ -1,9 +1,5 @@ const { spawnSync } = require('node:child_process') -const { Keypair } = require('../../..') -const { - ContractClient, - basicNodeSigner, -} = require('../../../lib/contract_client') +const { contract, Keypair } = require('../../..') const contracts = { customTypes: { @@ -40,7 +36,7 @@ async function generateFundedKeypair() { module.exports.generateFundedKeypair = generateFundedKeypair /** - * Generates a ContractClient for the contract with the given name. + * Generates a Client for the contract with the given name. * Also generates a new account to use as as the keypair of this contract. This * account is funded by friendbot. You can pass in an account to re-use the * same account with multiple contract clients. @@ -48,20 +44,20 @@ module.exports.generateFundedKeypair = generateFundedKeypair * By default, will re-deploy the contract every time. Pass in the same * `contractId` again if you want to re-use the a contract instance. */ -async function clientFor(contract, { keypair = generateFundedKeypair(), contractId } = {}) { - if (!contracts[contract]) { +async function clientFor(name, { keypair = generateFundedKeypair(), contractId } = {}) { + if (!contracts[name]) { throw new Error( - `Contract ${contract} not found. ` + + `Contract ${name} not found. ` + `Pick one of: ${Object.keys(contracts).join(", ")}` ) } keypair = await keypair // eslint-disable-line no-param-reassign - const wallet = basicNodeSigner(keypair, networkPassphrase) + const wallet = contract.basicNodeSigner(keypair, networkPassphrase) - const wasmHash = contracts[contract].hash; + let wasmHash = contracts[name].hash; if (!wasmHash) { - throw new Error(`No wasm hash found for \`contracts[${contract}]\`! ${JSON.stringify(contracts[contract], null, 2)}`) + wasmHash = spawnSync("./target/bin/soroban", ["contract", "install", "--wasm", contracts[name].path], { shell: true, encoding: "utf8" }).stdout.trim() } // TODO: do this with js-stellar-sdk, instead of shelling out to the CLI @@ -74,7 +70,7 @@ async function clientFor(contract, { keypair = generateFundedKeypair(), contract wasmHash, ], { shell: true, encoding: "utf8" }).stdout.trim(); - const client = await ContractClient.fromWasmHash(wasmHash, { + const client = await contract.Client.fromWasmHash(wasmHash, { networkPassphrase, contractId, rpcUrl, diff --git a/test/unit/server/soroban/constructor_test.js b/test/unit/server/soroban/constructor_test.js index 82c4465e1..7999928d4 100644 --- a/test/unit/server/soroban/constructor_test.js +++ b/test/unit/server/soroban/constructor_test.js @@ -1,4 +1,4 @@ -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server.constructor", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_account_test.js b/test/unit/server/soroban/get_account_test.js index b6781f76c..4372a39c9 100644 --- a/test/unit/server/soroban/get_account_test.js +++ b/test/unit/server/soroban/get_account_test.js @@ -1,5 +1,5 @@ const { Account, Keypair, StrKey, hash, xdr } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getAccount", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_contract_data_test.js b/test/unit/server/soroban/get_contract_data_test.js index 1fb004a61..8b2996664 100644 --- a/test/unit/server/soroban/get_contract_data_test.js +++ b/test/unit/server/soroban/get_contract_data_test.js @@ -1,5 +1,5 @@ const { Address, xdr, nativeToScVal, hash } = StellarSdk; -const { Server, AxiosClient, Durability } = StellarSdk.SorobanRpc; +const { Server, AxiosClient, Durability } = StellarSdk.rpc; describe("Server#getContractData", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_events_test.js b/test/unit/server/soroban/get_events_test.js index 8b3f4f8e7..6861f887a 100644 --- a/test/unit/server/soroban/get_events_test.js +++ b/test/unit/server/soroban/get_events_test.js @@ -1,5 +1,5 @@ -const { nativeToScVal, SorobanRpc } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { nativeToScVal, rpc } = StellarSdk; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getEvents", function () { beforeEach(function () { @@ -225,7 +225,7 @@ function setupMock(axiosMock, params, result) { } function parseEvents(result) { - return SorobanRpc.parseRawEvents(result); + return rpc.parseRawEvents(result); } const contractId = "CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"; diff --git a/test/unit/server/soroban/get_health_test.js b/test/unit/server/soroban/get_health_test.js index 917a945f6..4682cbf67 100644 --- a/test/unit/server/soroban/get_health_test.js +++ b/test/unit/server/soroban/get_health_test.js @@ -1,4 +1,4 @@ -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getHealth", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_latest_ledger_test.js b/test/unit/server/soroban/get_latest_ledger_test.js index 6aa048033..fecf1a0ec 100644 --- a/test/unit/server/soroban/get_latest_ledger_test.js +++ b/test/unit/server/soroban/get_latest_ledger_test.js @@ -1,4 +1,4 @@ -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getLatestLedger", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_ledger_entries_test.js b/test/unit/server/soroban/get_ledger_entries_test.js index cf11dc5f3..a550d62f9 100644 --- a/test/unit/server/soroban/get_ledger_entries_test.js +++ b/test/unit/server/soroban/get_ledger_entries_test.js @@ -1,5 +1,5 @@ const { xdr, nativeToScVal, Durability, hash } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getLedgerEntries", function () { const address = "CCJZ5DGASBWQXR5MPFCJXMBI333XE5U3FSJTNQU7RIKE3P5GN2K2WYD5"; diff --git a/test/unit/server/soroban/get_network_test.js b/test/unit/server/soroban/get_network_test.js index a5b4e05a7..fd36476f2 100644 --- a/test/unit/server/soroban/get_network_test.js +++ b/test/unit/server/soroban/get_network_test.js @@ -1,4 +1,4 @@ -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getNetwork", function () { beforeEach(function () { diff --git a/test/unit/server/soroban/get_transaction_test.js b/test/unit/server/soroban/get_transaction_test.js index a63b7aa21..52b6c807a 100644 --- a/test/unit/server/soroban/get_transaction_test.js +++ b/test/unit/server/soroban/get_transaction_test.js @@ -6,7 +6,7 @@ const { nativeToScVal, XdrLargeInt, } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#getTransaction", function () { let keypair = Keypair.random(); diff --git a/test/unit/server/soroban/request_airdrop_test.js b/test/unit/server/soroban/request_airdrop_test.js index 74e0c6486..2465a981b 100644 --- a/test/unit/server/soroban/request_airdrop_test.js +++ b/test/unit/server/soroban/request_airdrop_test.js @@ -1,5 +1,5 @@ const { Account, Keypair, StrKey, Networks, xdr, hash } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#requestAirdrop", function () { function accountLedgerEntryData(accountId, sequence) { diff --git a/test/unit/server/soroban/send_transaction_test.js b/test/unit/server/soroban/send_transaction_test.js index be235e8bb..08f35a402 100644 --- a/test/unit/server/soroban/send_transaction_test.js +++ b/test/unit/server/soroban/send_transaction_test.js @@ -1,5 +1,5 @@ const { xdr } = StellarSdk; -const { Server, AxiosClient } = StellarSdk.SorobanRpc; +const { Server, AxiosClient } = StellarSdk.rpc; describe("Server#sendTransaction", function () { let keypair = StellarSdk.Keypair.random(); diff --git a/test/unit/server/soroban/simulate_transaction_test.js b/test/unit/server/soroban/simulate_transaction_test.js index f1c2bacbc..e35322e2d 100644 --- a/test/unit/server/soroban/simulate_transaction_test.js +++ b/test/unit/server/soroban/simulate_transaction_test.js @@ -2,13 +2,13 @@ const { Account, Keypair, Networks, - SorobanRpc, + rpc, SorobanDataBuilder, authorizeInvocation, authorizeEntry, xdr, } = StellarSdk; -const { Server, AxiosClient, parseRawSimulation } = StellarSdk.SorobanRpc; +const { Server, AxiosClient, parseRawSimulation } = StellarSdk.rpc; const randomSecret = Keypair.random().secret(); @@ -134,7 +134,7 @@ describe("Server#simulateTransaction", async function (done) { const parsed = parseRawSimulation(simResponse); expect(parsed).to.deep.equal(parsedCopy); - expect(SorobanRpc.Api.isSimulationSuccess(parsed)).to.be.true; + expect(rpc.Api.isSimulationSuccess(parsed)).to.be.true; }); it("works with no auth", async function () { @@ -146,7 +146,7 @@ describe("Server#simulateTransaction", async function (done) { const parsed = parseRawSimulation(simResponse); expect(parsed).to.be.deep.equal(parsedCopy); - expect(SorobanRpc.Api.isSimulationSuccess(parsed)).to.be.true; + expect(rpc.Api.isSimulationSuccess(parsed)).to.be.true; }); }); @@ -160,7 +160,7 @@ describe("Server#simulateTransaction", async function (done) { }; const parsed = parseRawSimulation(simResponse); - expect(SorobanRpc.Api.isSimulationRestore(parsed)).to.be.true; + expect(rpc.Api.isSimulationRestore(parsed)).to.be.true; expect(parsed).to.be.deep.equal(expected); }, ); @@ -180,7 +180,7 @@ describe("Server#simulateTransaction", async function (done) { const parsed = parseRawSimulation(simResponse); expect(parsed).to.be.deep.equal(expected); - expect(SorobanRpc.Api.isSimulationError(parsed)).to.be.true; + expect(rpc.Api.isSimulationError(parsed)).to.be.true; }); xit("simulates fee bump transactions"); @@ -302,7 +302,7 @@ describe("works with real responses", function () { }; it("parses the schema", function () { - expect(SorobanRpc.Api.isSimulationRaw(schema)).to.be.true; + expect(rpc.Api.isSimulationRaw(schema)).to.be.true; const parsed = parseRawSimulation(schema); diff --git a/test/unit/spec/contract_spec.ts b/test/unit/spec/contract_spec.ts index 861a7c968..e2ffbe3be 100644 --- a/test/unit/spec/contract_spec.ts +++ b/test/unit/spec/contract_spec.ts @@ -1,4 +1,4 @@ -import { xdr, Address, ContractSpec, Keypair } from "../../../lib"; +import { xdr, Address, contract, Keypair } from "../../.."; import { JSONSchemaFaker } from "json-schema-faker"; import spec from "../spec.json"; @@ -6,7 +6,7 @@ import { expect } from "chai"; const publicKey = "GCBVOLOM32I7OD5TWZQCIXCXML3TK56MDY7ZMTAILIBQHHKPCVU42XYW"; const addr = Address.fromString(publicKey); -let SPEC: ContractSpec; +let SPEC: contract.Spec; JSONSchemaFaker.format("address", () => { let keypair = Keypair.random(); @@ -14,11 +14,11 @@ JSONSchemaFaker.format("address", () => { }); before(() => { - SPEC = new ContractSpec(spec); + SPEC = new contract.Spec(spec); }); it("throws if no entries", () => { - expect(() => new ContractSpec([])).to.throw( + expect(() => new contract.Spec([])).to.throw( /Contract spec must have at least one entry/i ); }); @@ -36,7 +36,7 @@ describe("Can round trip custom types", function () { } async function jsonSchema_roundtrip( - spec: ContractSpec, + spec: contract.Spec, funcName: string, num: number = 100 ) { @@ -70,7 +70,7 @@ describe("Can round trip custom types", function () { } describe("Json Schema", () => { - SPEC = new ContractSpec(spec); + SPEC = new contract.Spec(spec); let names = SPEC.funcs().map((f) => f.name().toString()); const banned = ["strukt_hel", "not", "woid", "val", "multi_args"]; names @@ -259,7 +259,7 @@ describe.skip("Print contract spec", function () { describe("parsing and building ScVals", function () { it("Can parse entries", function () { - let spec = new ContractSpec([GIGA_MAP, func]); + let spec = new contract.Spec([GIGA_MAP, func]); let fn = spec.findEntry("giga_map"); let gigaMap = spec.findEntry("GigaMap"); expect(gigaMap).deep.equal(GIGA_MAP); diff --git a/test/unit/transaction_test.js b/test/unit/transaction_test.js index d5a1b3633..4ee16f7f2 100644 --- a/test/unit/transaction_test.js +++ b/test/unit/transaction_test.js @@ -1,4 +1,4 @@ -const { xdr, SorobanRpc } = StellarSdk; +const { xdr, rpc } = StellarSdk; describe("assembleTransaction", () => { xit("works with keybump transactions"); @@ -80,7 +80,7 @@ describe("assembleTransaction", () => { it("simulate updates the tx data from simulation response", () => { const txn = singleContractFnTransaction(); - const result = SorobanRpc.assembleTransaction( + const result = rpc.assembleTransaction( txn, simulationResponse, ).build(); @@ -97,7 +97,7 @@ describe("assembleTransaction", () => { it("simulate adds the auth to the host function in tx operation", () => { const txn = singleContractFnTransaction(); - const result = SorobanRpc.assembleTransaction( + const result = rpc.assembleTransaction( txn, simulationResponse, ).build(); @@ -141,7 +141,7 @@ describe("assembleTransaction", () => { const txn = singleContractFnTransaction(); let simulateResp = JSON.parse(JSON.stringify(simulationResponse)); simulateResp.results[0].auth = null; - const result = SorobanRpc.assembleTransaction(txn, simulateResp).build(); + const result = rpc.assembleTransaction(txn, simulateResp).build(); expect( result @@ -170,7 +170,7 @@ describe("assembleTransaction", () => { .build(); expect(() => { - SorobanRpc.assembleTransaction(txn, { + rpc.assembleTransaction(txn, { transactionData: {}, events: [], minResourceFee: "0", @@ -198,7 +198,7 @@ describe("assembleTransaction", () => { .addOperation(op) .build(); - const tx = SorobanRpc.assembleTransaction( + const tx = rpc.assembleTransaction( txn, simulationResponse, ).build(); @@ -208,7 +208,7 @@ describe("assembleTransaction", () => { it("doesn't overwrite auth if it's present", function () { const txn = singleContractFnTransaction([fnAuth, fnAuth, fnAuth]); - const tx = SorobanRpc.assembleTransaction( + const tx = rpc.assembleTransaction( txn, simulationResponse, ).build();