From 406f90687fad7f04dd3c92a95f689a3d61eaddaf Mon Sep 17 00:00:00 2001 From: Chad Ostrowski <221614+chadoh@users.noreply.github.com> Date: Wed, 13 Mar 2024 16:38:24 -0400 Subject: [PATCH] build: remove redundant TS tests All tests that are being removed are now duplicates of tests that are in stellar-sdk. So are the test-custom-types tests, but those ones actually caught some edge cases, so maybe they're worth keeping. --- .../test_custom_types/package-lock.json | 2 +- .../ts-tests/initialize.sh | 36 +---- .../src/test-deserialized-transaction.ts | 12 +- .../ts-tests/src/test-hello-world.ts | 32 ---- .../ts-tests/src/test-methods-as-args.ts | 18 --- .../ts-tests/src/test-swap.ts | 140 ------------------ .../ts-tests/src/util.ts | 12 -- 7 files changed, 8 insertions(+), 244 deletions(-) delete mode 100644 cmd/crates/soroban-spec-typescript/ts-tests/src/test-hello-world.ts delete mode 100644 cmd/crates/soroban-spec-typescript/ts-tests/src/test-methods-as-args.ts delete mode 100644 cmd/crates/soroban-spec-typescript/ts-tests/src/test-swap.ts diff --git a/cmd/crates/soroban-spec-typescript/fixtures/test_custom_types/package-lock.json b/cmd/crates/soroban-spec-typescript/fixtures/test_custom_types/package-lock.json index adf57362af..0078d1cb56 100644 --- a/cmd/crates/soroban-spec-typescript/fixtures/test_custom_types/package-lock.json +++ b/cmd/crates/soroban-spec-typescript/fixtures/test_custom_types/package-lock.json @@ -45,7 +45,7 @@ }, "node_modules/@stellar/stellar-sdk": { "version": "11.2.2", - "resolved": "git+ssh://git@github.com/AhaLabs/js-stellar-sdk.git#3450acb5c658f90bf05d27820fcf23bddd14b0dc", + "resolved": "git+ssh://git@github.com/AhaLabs/js-stellar-sdk.git#9b572797bdd5b33b24b84494586892e5980740f0", "license": "Apache-2.0", "dependencies": { "@stellar/stellar-base": "^11.0.0", diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh index 621be2bc3f..976ac91a6e 100755 --- a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh +++ b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh @@ -28,52 +28,20 @@ exe() { echo"${@/eval/}" ; "$@" ; } function fund_all() { exe eval "./soroban keys generate root" exe eval "./soroban keys fund root" - exe eval "./soroban keys generate alice" - exe eval "./soroban keys fund alice" - exe eval "./soroban keys generate bob" - exe eval "./soroban keys fund bob" -} -function upload() { - exe eval "(./soroban contract $1 --source root --wasm $2 --ignore-checks) > $3" } function deploy() { - exe eval "(./soroban contract deploy --source root --wasm-hash $(cat $1) --ignore-checks) > $2" + exe eval "(./soroban contract deploy --source root --wasm $1 --ignore-checks) > $2" } function deploy_all() { - upload deploy ../../../../target/wasm32-unknown-unknown/test-wasms/test_custom_types.wasm contract-id-custom-types.txt - upload deploy ../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm contract-id-hello-world.txt - upload deploy ../../../../target/wasm32-unknown-unknown/test-wasms/test_swap.wasm contract-id-swap.txt - upload install ../../../../target/wasm32-unknown-unknown/test-wasms/test_token.wasm contract-token-hash.txt - deploy contract-token-hash.txt contract-id-token-a.txt - deploy contract-token-hash.txt contract-id-token-b.txt -} -function initialize() { - exe eval "./soroban contract invoke --source root --id $(cat $1) -- initialize --admin $(./soroban keys address root) --decimal 0 --name 'Token $2' --symbol '$2'" -} -function initialize_all() { - initialize contract-id-token-a.txt A - initialize contract-id-token-b.txt B + deploy ../../../../target/wasm32-unknown-unknown/test-wasms/test_custom_types.wasm contract-id-custom-types.txt } function bind() { exe eval "./soroban contract bindings typescript --contract-id $(cat $1) --output-dir ./node_modules/$2 --overwrite" } function bind_all() { bind contract-id-custom-types.txt test-custom-types - bind contract-id-hello-world.txt test-hello-world - bind contract-id-swap.txt test-swap - bind contract-id-token-a.txt token -} - -function mint() { - exe eval "./soroban contract invoke --source root --id $(cat $1) -- mint --amount 2000000 --to $(./soroban keys address $2)" -} -function mint_all() { - mint contract-id-token-a.txt alice - mint contract-id-token-b.txt bob } fund_all deploy_all -initialize_all -mint_all bind_all diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-deserialized-transaction.ts b/cmd/crates/soroban-spec-typescript/ts-tests/src/test-deserialized-transaction.ts index 8ce2cb933c..1208c65c73 100644 --- a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-deserialized-transaction.ts +++ b/cmd/crates/soroban-spec-typescript/ts-tests/src/test-deserialized-transaction.ts @@ -1,22 +1,20 @@ import test from "ava" import { rpcUrl, root, signer } from "./util.js" -import { Client, networks } from "test-hello-world" +import { Client, networks } from "test-custom-types" const contract = new Client({ + ...networks.standalone, rpcUrl, allowHttp: true, publicKey: root.keypair.publicKey(), ...signer, - ...networks.standalone, }) test("has correctly-typed result", async (t) => { - const initial = await contract.hello({ world: "tests" }) - t.is(initial.result[0], "Hello") - t.is(initial.result[1], "tests") + const initial = await contract.hello({ hello: "tests" }) + t.is(initial.result, "tests") const serialized = initial.toJSON() const deserialized = contract.fromJSON.hello(serialized) - t.is(deserialized.result[0], "Hello") // throws TS error if `result` is of type `unknown` - t.is(deserialized.result[1], "tests") // throws TS error if `result` is of type `unknown` + t.is(deserialized.result, "tests") // throws TS error if `result` is of type `unknown` }); diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-hello-world.ts b/cmd/crates/soroban-spec-typescript/ts-tests/src/test-hello-world.ts deleted file mode 100644 index 7dd9ff4b37..0000000000 --- a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-hello-world.ts +++ /dev/null @@ -1,32 +0,0 @@ -import test from "ava"; -import { root, signer, rpcUrl } from "./util.js"; -import { Client, networks } from "test-hello-world"; - -const contract = new Client({ - ...networks.standalone, - rpcUrl, - allowHttp: true, - publicKey: root.keypair.publicKey(), - ...signer, -}) - -test("hello", async (t) => { - t.deepEqual((await contract.hello({ world: "tests" })).result, ["Hello", "tests"]); -}); - -test("auth", async (t) => { - t.deepEqual( - (await contract.auth({ - addr: root.keypair.publicKey(), - world: 'lol' - })).result, - root.keypair.publicKey() - ) -}); - -test("inc", async (t) => { - const { result: startingBalance } = await contract.get_count() - const inc = await contract.inc() - t.is((await inc.signAndSend()).result, startingBalance + 1) - t.is((await contract.get_count()).result, startingBalance + 1) -}); diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-methods-as-args.ts b/cmd/crates/soroban-spec-typescript/ts-tests/src/test-methods-as-args.ts deleted file mode 100644 index ed805b4c97..0000000000 --- a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-methods-as-args.ts +++ /dev/null @@ -1,18 +0,0 @@ -import test from "ava"; -import { rpcUrl, root, signer } from "./util.js"; -import { Client, networks } from "test-hello-world"; - -const contract = new Client({ - ...networks.standalone, - rpcUrl, - allowHttp: true, - publicKey: root.keypair.publicKey(), - ...signer, -}) - -// this test checks that apps can pass methods as arguments to other methods and have them still work -const hello = contract.hello - -test("hello", async (t) => { - t.deepEqual((await hello({ world: "tests" })).result, ["Hello", "tests"]); -}); diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-swap.ts b/cmd/crates/soroban-spec-typescript/ts-tests/src/test-swap.ts deleted file mode 100644 index 57061d78d2..0000000000 --- a/cmd/crates/soroban-spec-typescript/ts-tests/src/test-swap.ts +++ /dev/null @@ -1,140 +0,0 @@ -import test from "ava" -import { SorobanRpc, xdr } from '@stellar/stellar-sdk' -import { signer, rpcUrl, alice, bob, networkPassphrase, root } from "./util.js" -import { Client as Token } from "token" -import { basicNodeSigner, AssembledTransaction, Client as Swap, networks } from "test-swap" -import fs from "node:fs" - -const tokenAId = fs.readFileSync(new URL("../contract-id-token-a.txt", import.meta.url), "utf8").trim() -const tokenBId = fs.readFileSync(new URL("../contract-id-token-b.txt", import.meta.url), "utf8").trim() - -// `root` is the invoker of all contracts -const tokenA = new Token({ - contractId: tokenAId, - networkPassphrase, - rpcUrl, - allowHttp: true, - publicKey: root.keypair.publicKey(), - ...signer, -}) -const tokenB = new Token({ - contractId: tokenBId, - networkPassphrase, - rpcUrl, - allowHttp: true, - publicKey: root.keypair.publicKey(), - ...signer, -}) -function swapContractAs(invoker: typeof root | typeof alice | typeof bob) { - return new Swap({ - ...networks.standalone, - rpcUrl, - allowHttp: true, - publicKey: invoker.keypair.publicKey(), - ...basicNodeSigner(invoker.keypair, networkPassphrase), - }) -} - -const amountAToSwap = 2n -const amountBToSwap = 1n -const alicePk = alice.keypair.publicKey() -const bobPk = bob.keypair.publicKey() - -test('calling `signAndSend()` too soon throws descriptive error', async t => { - const swapContract = swapContractAs(root) - const tx = await swapContract.swap({ - a: alicePk, - b: bobPk, - token_a: tokenAId, - token_b: tokenBId, - amount_a: amountAToSwap, - min_a_for_b: amountAToSwap, - amount_b: amountBToSwap, - 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}'`) - if (error) t.regex(error.message, /needsNonInvokerSigningBy/) -}) - -test('alice swaps bob 10 A for 1 B', async t => { - const swapContractAsRoot = swapContractAs(root) - const [ - { result: aliceStartingABalance }, - { result: aliceStartingBBalance }, - { result: bobStartingABalance }, - { result: bobStartingBBalance }, - ] = await Promise.all([ - tokenA.balance({ id: alicePk }), - tokenB.balance({ id: alicePk }), - tokenA.balance({ id: bobPk }), - tokenB.balance({ id: bobPk }), - ]) - t.true(aliceStartingABalance >= amountAToSwap, `alice does not have enough Token A! aliceStartingABalance: ${aliceStartingABalance}`) - t.true(bobStartingBBalance >= amountBToSwap, `bob does not have enough Token B! bobStartingBBalance: ${bobStartingBBalance}`) - - const tx = await swapContractAsRoot.swap({ - a: alicePk, - b: bobPk, - token_a: tokenAId, - token_b: tokenBId, - amount_a: amountAToSwap, - min_a_for_b: amountAToSwap, - amount_b: amountBToSwap, - min_b_for_a: amountBToSwap, - }) - - const needsNonInvokerSigningBy = await tx.needsNonInvokerSigningBy() - t.is(needsNonInvokerSigningBy.length, 2) - t.is(needsNonInvokerSigningBy.indexOf(alicePk), 0, 'needsNonInvokerSigningBy does not have alice\'s public key!') - t.is(needsNonInvokerSigningBy.indexOf(bobPk), 1, 'needsNonInvokerSigningBy does not have bob\'s public key!') - - - // root serializes & sends to alice - const jsonFromRoot = tx.toJSON() - const txAlice = swapContractAs(alice).fromJSON.swap(jsonFromRoot) - await txAlice.signAuthEntries() - - // alice serializes & sends to bob - const jsonFromAlice = txAlice.toJSON() - const txBob = swapContractAs(bob).fromJSON.swap(jsonFromAlice) - await txBob.signAuthEntries() - - // bob serializes & sends back to root - const jsonFromBob = txBob.toJSON() - const txRoot = swapContractAsRoot.fromJSON.swap(jsonFromBob) - const result = await txRoot.signAndSend() - - t.truthy(result.sendTransactionResponse, `tx failed: ${JSON.stringify(result, null, 2)}`) - t.is(result.sendTransactionResponse!.status, 'PENDING', `tx failed: ${JSON.stringify(result, null, 2)}`) - t.truthy(result.getTransactionResponseAll?.length, `tx failed: ${JSON.stringify(result.getTransactionResponseAll, null, 2)}`) - t.not(result.getTransactionResponse!.status, 'FAILED', `tx failed: ${JSON.stringify( - ((result.getTransactionResponse as SorobanRpc.Api.GetFailedTransactionResponse) - .resultXdr.result().value() as xdr.OperationResult[] - ).map(op => - op.value()?.value().switch() - ), null, 2)}` - ) - t.is( - result.getTransactionResponse!.status, - SorobanRpc.Api.GetTransactionStatus.SUCCESS, - `tx failed: ${JSON.stringify(result.getTransactionResponse, null, 2)}` - ) - - t.is( - (await tokenA.balance({ id: alicePk })).result, - aliceStartingABalance - amountAToSwap - ) - t.is( - (await tokenB.balance({ id: alicePk })).result, - aliceStartingBBalance + amountBToSwap - ) - t.is( - (await tokenA.balance({ id: bobPk })).result, - bobStartingABalance + amountAToSwap - ) - t.is( - (await tokenB.balance({ id: bobPk })).result, - bobStartingBBalance - amountBToSwap - ) -}) diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/src/util.ts b/cmd/crates/soroban-spec-typescript/ts-tests/src/util.ts index f5b6fbb8b0..c086847359 100644 --- a/cmd/crates/soroban-spec-typescript/ts-tests/src/util.ts +++ b/cmd/crates/soroban-spec-typescript/ts-tests/src/util.ts @@ -3,24 +3,12 @@ import { Address, Keypair } from "@stellar/stellar-sdk"; import { basicNodeSigner } from "@stellar/stellar-sdk/lib/contract_client/index.js"; const rootKeypair = Keypair.fromSecret(spawnSync("./soroban", ["keys", "show", "root"], { shell: true, encoding: "utf8" }).stdout.trim()); -const aliceKeypair = Keypair.fromSecret(spawnSync("./soroban", ["keys", "show", "alice"], { shell: true, encoding: "utf8" }).stdout.trim()); -const bobKeypair = Keypair.fromSecret(spawnSync("./soroban", ["keys", "show", "bob"], { shell: true, encoding: "utf8" }).stdout.trim()); export const root = { keypair: rootKeypair, address: Address.fromString(rootKeypair.publicKey()), } -export const alice = { - keypair: aliceKeypair, - address: Address.fromString(aliceKeypair.publicKey()), -} - -export const bob = { - keypair: bobKeypair, - address: Address.fromString(bobKeypair.publicKey()), -} - export const rpcUrl = process.env.SOROBAN_RPC_URL ?? "http://localhost:8000/"; export const networkPassphrase = process.env.SOROBAN_NETWORK_PASSPHRASE ?? "Standalone Network ; February 2017";