diff --git a/package.json b/package.json index db33995ba..1117737da 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" + }, + "./ContractClient": { + "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", @@ -164,4 +179,4 @@ ], "timeout": "2m" } -} +} \ No newline at end of file diff --git a/src/soroban/contract_client/assembled_transaction.ts b/src/contractclient/assembled_transaction.ts similarity index 97% rename from src/soroban/contract_client/assembled_transaction.ts rename to src/contractclient/assembled_transaction.ts index a8436fc3d..a7f2c965e 100644 --- a/src/soroban/contract_client/assembled_transaction.ts +++ b/src/contractclient/assembled_transaction.ts @@ -12,15 +12,15 @@ import { } from "@stellar/stellar-base"; import type { AssembledTransactionOptions, - ContractClientOptions, + Options, MethodOptions, Tx, XDR_BASE64, } from "./types"; -import { Server } from "../server"; -import { Api } from "../api"; -import { assembleTransaction } from "../transaction"; -import type { ContractClient } from "./client"; +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, @@ -33,7 +33,7 @@ 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. @@ -70,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({ @@ -94,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 @@ -118,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): * @@ -521,7 +521,7 @@ export class AssembledTransaction { /** * TSDoc: You must provide this here if you did not provide one before */ - signTransaction?: ContractClientOptions["signTransaction"]; + signTransaction?: Options["signTransaction"]; } = {}): Promise> => { if (!this.built) { throw new Error("Transaction has not yet been simulated"); @@ -669,7 +669,7 @@ export class AssembledTransaction { * the `signAuthEntry` function from the `ContractClient` options. Must * sign things as the given `publicKey`. */ - signAuthEntry?: ContractClientOptions["signAuthEntry"]; + signAuthEntry?: Options["signAuthEntry"]; } = {}): Promise => { if (!this.built) throw new Error("Transaction has not yet been assembled or simulated"); diff --git a/src/soroban/contract_client/basic_node_signer.ts b/src/contractclient/basic_node_signer.ts similarity index 89% rename from src/soroban/contract_client/basic_node_signer.ts rename to src/contractclient/basic_node_signer.ts index db1834815..9e50327cd 100644 --- a/src/soroban/contract_client/basic_node_signer.ts +++ b/src/contractclient/basic_node_signer.ts @@ -1,9 +1,9 @@ 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/soroban/contract_client/client.ts b/src/contractclient/client.ts similarity index 80% rename from src/soroban/contract_client/client.ts rename to src/contractclient/client.ts index a90edc5d1..ea4214139 100644 --- a/src/soroban/contract_client/client.ts +++ b/src/contractclient/client.ts @@ -1,9 +1,9 @@ import { xdr } from "@stellar/stellar-base"; -import { ContractSpec } from "../../contract_spec"; +import { Spec } from "./spec"; import { AssembledTransaction } from "./assembled_transaction"; -import type { ContractClientOptions, MethodOptions } from "./types"; +import type { Options, MethodOptions } from "./types"; -export class ContractClient { +export class Client { /** * Generate a class from the contract spec that where each contract method * gets included with an identical name. @@ -13,10 +13,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 Options} */ + public readonly options: Options, ) { this.spec.funcs().forEach((xdrFn) => { const method = xdrFn.name().toString(); @@ -34,7 +34,7 @@ export class ContractClient { ...acc, [curr.value()]: { message: curr.doc().toString() }, }), - {} as Pick, + {} as Pick, ), parseResultXdr: (result: xdr.ScVal) => spec.funcResToNative(method, result), diff --git a/src/soroban/contract_client/index.ts b/src/contractclient/index.ts similarity index 88% rename from src/soroban/contract_client/index.ts rename to src/contractclient/index.ts index b9d7498cd..8b9e1dc5e 100644 --- a/src/soroban/contract_client/index.ts +++ b/src/contractclient/index.ts @@ -3,5 +3,5 @@ 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/soroban/contract_client/rust_result.ts b/src/contractclient/rust_result.ts similarity index 100% rename from src/soroban/contract_client/rust_result.ts rename to src/contractclient/rust_result.ts diff --git a/src/soroban/contract_client/sent_transaction.ts b/src/contractclient/sent_transaction.ts similarity index 96% rename from src/soroban/contract_client/sent_transaction.ts rename to src/contractclient/sent_transaction.ts index f2c3974f3..a4a1c0988 100644 --- a/src/soroban/contract_client/sent_transaction.ts +++ b/src/contractclient/sent_transaction.ts @@ -1,9 +1,9 @@ /* disable max-classes rule, because extending error shouldn't count! */ /* eslint max-classes-per-file: 0 */ import { SorobanDataBuilder, TransactionBuilder } from "@stellar/stellar-base"; -import type { ContractClientOptions, MethodOptions, Tx } from "./types"; -import { Server } from "../server" -import { Api } from "../api" +import type { Options, 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"; @@ -53,7 +53,7 @@ export class SentTransaction { }; constructor( - public signTransaction: ContractClientOptions["signTransaction"], + public signTransaction: Options["signTransaction"], public assembled: AssembledTransaction, ) { if (!signTransaction) { @@ -73,7 +73,7 @@ export class SentTransaction { */ static init = async ( /** More info in {@link MethodOptions} */ - signTransaction: ContractClientOptions["signTransaction"], + signTransaction: Options["signTransaction"], /** {@link AssembledTransaction} from which this SentTransaction was initialized */ assembled: AssembledTransaction, ): Promise> => { diff --git a/src/contract_spec.ts b/src/contractclient/spec.ts similarity index 99% rename from src/contract_spec.ts rename to src/contractclient/spec.ts index dc80f9a1d..937875fbb 100644 --- a/src/contract_spec.ts +++ b/src/contractclient/spec.ts @@ -7,7 +7,7 @@ import { Contract, scValToBigInt, } from "@stellar/stellar-base" -import { Ok } from "./soroban/contract_client/rust_result" +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[] = []; /** diff --git a/src/soroban/contract_client/types.ts b/src/contractclient/types.ts similarity index 94% rename from src/soroban/contract_client/types.ts rename to src/contractclient/types.ts index 70c8b3b47..740ebd7d8 100644 --- a/src/soroban/contract_client/types.ts +++ b/src/contractclient/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 "@stellar/stellar-base"; -import type { ContractClient } from "./client"; +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 Options = { /** * 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 & { + Options & { method: string; args?: any[]; parseResultXdr: (xdr: xdr.ScVal) => T; diff --git a/src/soroban/contract_client/utils.ts b/src/contractclient/utils.ts similarity index 100% rename from src/soroban/contract_client/utils.ts rename to src/contractclient/utils.ts diff --git a/src/index.ts b/src/index.ts index ec315bd0c..8ddc28a9c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -17,8 +17,10 @@ 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'; + +// Soroban Contract-related classes to expose +export * as ContractClient from './contractclient' // 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/soroban/index.ts b/src/rpc/index.ts similarity index 88% rename from src/soroban/index.ts rename to src/rpc/index.ts index 45702b70d..99ee2ba2a 100644 --- a/src/soroban/index.ts +++ b/src/rpc/index.ts @@ -9,6 +9,5 @@ export { Server, Durability } from "./server"; export { default as AxiosClient } from "./axios"; export { parseRawSimulation, parseRawEvents } from "./parsers"; export * from "./transaction"; -export * as ContractClient from "./contract_client"; 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/test/e2e/src/test-custom-types.js b/test/e2e/src/test-custom-types.js index 88ccdc703..ab3f96f53 100644 --- a/test/e2e/src/test-custom-types.js +++ b/test/e2e/src/test-custom-types.js @@ -1,9 +1,7 @@ const test = require('ava') -const { Address, SorobanRpc } = require('../../..') +const { Address, ContractClient } = require('../../..') const { clientFor } = require('./util') -const { Ok, Err } = SorobanRpc.ContractClient - test.before(async t => { const { client, keypair, contractId } = await clientFor('customTypes') const publicKey = keypair.publicKey() @@ -31,11 +29,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 ContractClient.Ok(1) ) t.deepEqual( (await t.context.client.u32_fail_on_even({ u32_: 2 })).result, - new Err({ message: "Please provide an odd number" }) + new ContractClient.Err({ message: "Please provide an odd number" }) ) }) diff --git a/test/e2e/src/test-swap.js b/test/e2e/src/test-swap.js index d36f698f5..119953fa0 100644 --- a/test/e2e/src/test-swap.js +++ b/test/e2e/src/test-swap.js @@ -1,9 +1,7 @@ const test = require('ava') -const { SorobanRpc } = require('../../..') +const { ContractClient } = require('../../..') const { clientFor, generateFundedKeypair } = require('./util') -const { AssembledTransaction } = SorobanRpc.ContractClient - const amountAToSwap = 2n const amountBToSwap = 1n @@ -53,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 ContractClient.AssembledTransaction.Errors.NeedsMoreSignatures, `error is not of type 'NeedsMoreSignaturesError'; instead it is of type '${error?.constructor.name}'`) if (error) t.regex(error.message, /needsNonInvokerSigningBy/) }) diff --git a/test/e2e/src/util.js b/test/e2e/src/util.js index a7d1e731c..764d5f064 100644 --- a/test/e2e/src/util.js +++ b/test/e2e/src/util.js @@ -1,9 +1,5 @@ const { spawnSync } = require('node:child_process') -const { ContractSpec, Keypair, SorobanRpc } = require('../../..') -const { - ContractClient, - basicNodeSigner, -} = SorobanRpc.ContractClient +const { ContractClient, 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 Contrat.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. @@ -57,9 +53,9 @@ async function clientFor(contract, { keypair = generateFundedKeypair(), contract } keypair = await keypair // eslint-disable-line no-param-reassign - const wallet = basicNodeSigner(keypair, networkPassphrase) + const wallet = ContractClient.basicNodeSigner(keypair, networkPassphrase) - const spec = new ContractSpec(contracts[contract].xdr) + const spec = new ContractClient.Spec(contracts[contract].xdr) const wasmHash = contracts[contract].hash; if (!wasmHash) { @@ -76,7 +72,7 @@ async function clientFor(contract, { keypair = generateFundedKeypair(), contract wasmHash, ], { shell: true, encoding: "utf8" }).stdout.trim(); - const client = new ContractClient(spec, { + const client = new ContractClient.Client(spec, { networkPassphrase, contractId, rpcUrl, diff --git a/test/unit/spec/contract_spec.ts b/test/unit/spec/contract_spec.ts index 861a7c968..c81b6cf6e 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, ContractClient, 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: ContractClient.Spec; JSONSchemaFaker.format("address", () => { let keypair = Keypair.random(); @@ -14,12 +14,12 @@ JSONSchemaFaker.format("address", () => { }); before(() => { - SPEC = new ContractSpec(spec); + SPEC = new ContractClient.Spec(spec); }); it("throws if no entries", () => { - expect(() => new ContractSpec([])).to.throw( - /Contract spec must have at least one entry/i + expect(() => new ContractClient.Spec([])).to.throw( + /ContractClient 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: ContractClient.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 ContractClient.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 ContractClient.Spec([GIGA_MAP, func]); let fn = spec.findEntry("giga_map"); let gigaMap = spec.findEntry("GigaMap"); expect(gigaMap).deep.equal(GIGA_MAP);