diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/.env b/cmd/crates/soroban-spec-typescript/ts-tests/.env index e69de29bb2..39b2454824 100644 --- a/cmd/crates/soroban-spec-typescript/ts-tests/.env +++ b/cmd/crates/soroban-spec-typescript/ts-tests/.env @@ -0,0 +1,2 @@ +SOROBAN_NETWORK_PASSPHRASE="Standalone Network ; February 2017" +SOROBAN_RPC_URL="http://localhost:8000/soroban/rpc" diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh new file mode 100755 index 0000000000..56749a406e --- /dev/null +++ b/cmd/crates/soroban-spec-typescript/ts-tests/initialize.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# read .env file, but prefer explicitly set environment variables +IFS=$'\n' +for l in $(cat .env); do + IFS='=' read -ra VARVAL <<< "$l" + # If variable with such name already exists, preserves its value + eval "export ${VARVAL[0]}=\${${VARVAL[0]}:-${VARVAL[1]}}" +done +unset IFS + +echo Network +echo " RPC: $SOROBAN_RPC_URL" +echo " Passphrase: \"$SOROBAN_NETWORK_PASSPHRASE\"" + +# Print command before executing, from https://stackoverflow.com/a/23342259/249801 +# Discussion: https://github.com/stellar/soroban-tools/pull/1034#pullrequestreview-1690667116 +exe() { echo"${@/eval/}" ; "$@" ; } + +function fund_all() { + exe eval "./soroban config identity fund" + exe eval "./soroban config identity generate alice" + exe eval "./soroban config identity fund alice" +} +function upload() { + exe eval "(./soroban contract $1 --wasm $2) > $3" +} +function deploy() { + exe eval "(./soroban contract deploy --wasm-hash $(cat $1)) > $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 --id $(cat $1) -- initialize --admin $(./soroban config identity address) --decimal 0 --name 'Token $2' --symbol '$2'" +} +function initialize_all() { + initialize contract-id-token-a.txt A + initialize contract-id-token-b.txt B +} +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 +} + +curl -X POST "http://localhost:8000/soroban/rpc" || { echo "Make sure you're running standalone RPC network on localhost:8000" && exit 1; } +fund_all +deploy_all +initialize_all +bind_all diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/package-lock.json b/cmd/crates/soroban-spec-typescript/ts-tests/package-lock.json index 5981685152..da8188ec34 100644 --- a/cmd/crates/soroban-spec-typescript/ts-tests/package-lock.json +++ b/cmd/crates/soroban-spec-typescript/ts-tests/package-lock.json @@ -9,6 +9,7 @@ "@ava/typescript": "^4.1.0", "@types/node": "^20.4.9", "ava": "^5.3.1", + "dotenv": "^16.3.1", "soroban-client": "1.0.0-beta.2", "typescript": "^5.1.6" } @@ -706,6 +707,18 @@ "node": ">=8" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "dev": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/eastasianwidth": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/eastasianwidth/-/eastasianwidth-0.2.0.tgz", diff --git a/cmd/crates/soroban-spec-typescript/ts-tests/package.json b/cmd/crates/soroban-spec-typescript/ts-tests/package.json index f184633b65..a05d198be8 100644 --- a/cmd/crates/soroban-spec-typescript/ts-tests/package.json +++ b/cmd/crates/soroban-spec-typescript/ts-tests/package.json @@ -2,42 +2,26 @@ "private": true, "type": "module", "scripts": { - "postinstall": "curl -X POST \"http://localhost:8000/soroban/rpc\" && npm run fund && npm run deploy && npm run initialize && npm run bindings || { echo \"Make sure you're running standalone RPC network on localhost:8000\" && exit 1; }", - "fund:root": "./soroban config identity fund", - "fund:alice": "./soroban config identity generate alice && ./soroban config identity fund alice", - "fund": "npm run fund:root && npm run fund:alice", - "bindings:custom-types": "./soroban contract bindings typescript --contract-id $(cat contract-id-custom-types.txt) --output-dir ./node_modules/test-custom-types --overwrite", - "bindings:hello-world": "./soroban contract bindings typescript --contract-id $(cat contract-id-hello-world.txt) --output-dir ./node_modules/test-hello-world --overwrite", - "bindings:swap": "./soroban contract bindings typescript --contract-id $(cat contract-id-swap.txt) --output-dir ./node_modules/test-swap --overwrite", - "bindings:token": "./soroban contract bindings typescript --contract-id $(cat contract-id-token-a.txt) --output-dir ./node_modules/token --overwrite", - "bindings": "npm run bindings:custom-types && npm run bindings:hello-world && npm run bindings:swap && npm run bindings:token", - "deploy:custom-types": "(./soroban contract deploy --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_custom_types.wasm) > contract-id-custom-types.txt", - "deploy:hello-world": "(./soroban contract deploy --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_hello_world.wasm) > contract-id-hello-world.txt", - "deploy:swap": "(./soroban contract deploy --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_swap.wasm) > contract-id-swap.txt", - "install:token": "(./soroban contract install --wasm ../../../../target/wasm32-unknown-unknown/test-wasms/test_token.wasm) > contract-token-hash.txt", - "deploy:token:a": "(./soroban contract deploy --wasm-hash $(cat contract-token-hash.txt)) > contract-id-token-a.txt", - "deploy:token:b": "(./soroban contract deploy --wasm-hash $(cat contract-token-hash.txt)) > contract-id-token-b.txt", - "deploy:token": "npm run install:token && npm run deploy:token:a && npm run deploy:token:b", - "deploy": "npm run deploy:custom-types && npm run deploy:hello-world && npm run deploy:swap && npm run deploy:token", - "initialize:token:a": "./soroban contract invoke --id $(cat contract-id-token-a.txt) -- initialize --admin $(./soroban config identity address) --decimal 0 --name 'Token A' --symbol 'A'", - "initialize:token:b": "./soroban contract invoke --id $(cat contract-id-token-b.txt) -- initialize --admin $(./soroban config identity address) --decimal 0 --name 'Token B' --symbol 'B'", - "initialize:tokens": "npm run initialize:token:a && npm run initialize:token:b", - "initialize": "npm run deploy:token && npm run initialize:tokens", + "postinstall": "./initialize.sh", "test": "ava" }, - "devDependencies": { - "@ava/typescript": "^4.1.0", - "@types/node": "^20.4.9", - "ava": "^5.3.1", - "soroban-client": "1.0.0-beta.2", - "typescript": "^5.1.6" - }, - "ava": { - "typescript": { - "rewritePaths": { - "src/": "build/" - }, - "compile": "tsc" - } - } + "devDependencies": { + "@ava/typescript": "^4.1.0", + "@types/node": "^20.4.9", + "ava": "^5.3.1", + "dotenv": "^16.3.1", + "soroban-client": "1.0.0-beta.2", + "typescript": "^5.1.6" + }, + "ava": { + "typescript": { + "rewritePaths": { + "src/": "build/" + }, + "compile": "tsc" + }, + "require": [ + "dotenv/config" + ] + } }