Skip to content

Commit

Permalink
Add ts bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
tupui committed Jul 25, 2024
1 parent bed45ee commit a7d1a54
Show file tree
Hide file tree
Showing 7 changed files with 785 additions and 18 deletions.
43 changes: 25 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ contract_build-release: contract_build
stellar contract optimize --wasm target/wasm32-unknown-unknown/release/versioning.wasm
@ls -l target/wasm32-unknown-unknown/release/*.wasm


contract_bindings: contract_build-release
stellar contract bindings typescript \
--network $(network) \
--contract-id $(shell cat .soroban/soroban_versioning_id) \
--output-dir web/bindings/ts \
--overwrite

contract_deploy: contract_test contract_build-release ## Deploy Soroban contract to testnet
stellar contract deploy \
--wasm target/wasm32-unknown-unknown/release/versioning.optimized.wasm \
Expand All @@ -72,32 +80,41 @@ contract_deploy: contract_test contract_build-release ## Deploy Soroban contrac
> .soroban/soroban_versioning_id && \
cat .soroban/soroban_versioning_id

# --------- CONTRACT USAGE --------- #
contract_init:
stellar contract invoke \
--source-account mando-$(network) \
--network $(network) \
--id $(shell cat .soroban/soroban_versioning_id) \
-- \
init \
--admin $(shell soroban keys address mando-$(network))

contract_help:
contract_upgrade: contract_build-release
stellar contract invoke \
--source-account mando-$(network) \
--network $(network) \
--id $(shell cat .soroban/soroban_versioning_id) \
-- \
--help
upgrade \
--new_wasm_hash $(shell stellar contract install --source-account mando-$(network) --network $(network) --wasm target/wasm32-unknown-unknown/release/versioning.optimized.wasm)

contract_version:
# --------- CONTRACT USAGE --------- #

contract_help:
stellar contract invoke \
--source-account mando-$(network) \
--network $(network) \
--id $(shell cat .soroban/soroban_versioning_id) \
-- \
version
--help

contract_init:
contract_version:
stellar contract invoke \
--source-account mando-$(network) \
--network $(network) \
--id $(shell cat .soroban/soroban_versioning_id) \
-- \
init \
--admin $(shell soroban keys address mando-$(network))
version

contract_register:
stellar contract invoke \
Expand Down Expand Up @@ -131,13 +148,3 @@ contract_get_commit:
-- \
get_commit \
--project_key 37ae83c06fde1043724743335ac2f3919307892ee6307cce8c0c63eaa549e156


contract_upgrade: contract_build-release
stellar contract invoke \
--source-account mando-$(network) \
--network $(network) \
--id $(shell cat .soroban/soroban_versioning_id) \
-- \
upgrade \
--new_wasm_hash $(shell stellar contract install --source-account mando-$(network) --network $(network) --wasm target/wasm32-unknown-unknown/release/versioning.optimized.wasm)
2 changes: 2 additions & 0 deletions web/bindings/ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
out/
54 changes: 54 additions & 0 deletions web/bindings/ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# ts JS

JS library for interacting with [Soroban](https://soroban.stellar.org/) smart contract `ts` via Soroban RPC.

This library was automatically generated by Soroban CLI using a command similar to:

```bash
soroban contract bindings ts \
--rpc-url https://soroban-testnet.stellar.org:443 \
--network-passphrase "Test SDF Network ; September 2015" \
--contract-id CC3JCYWHNMPMQTOQUNDCJSCFSWRFZIE2JVSAUEXEG56DMKOMI3RL7VOH \
--output-dir ./path/to/ts
```

The network passphrase and contract ID are exported from [index.ts](./src/index.ts) in the `networks` constant. If you are the one who generated this library and you know that this contract is also deployed to other networks, feel free to update `networks` with other valid options. This will help your contract consumers use this library more easily.

# To publish or not to publish

This library is suitable for publishing to NPM. You can publish it to NPM using the `npm publish` command.

But you don't need to publish this library to NPM to use it. You can add it to your project's `package.json` using a file path:

```json
"dependencies": {
"ts": "./path/to/this/folder"
}
```

However, we've actually encountered [frustration](https://github.com/stellar/soroban-example-dapp/pull/117#discussion_r1232873560) using local libraries with NPM in this way. Though it seems a bit messy, we suggest generating the library directly to your `node_modules` folder automatically after each install by using a `postinstall` script. We've had the least trouble with this approach. NPM will automatically remove what it sees as erroneous directories during the `install` step, and then regenerate them when it gets to your `postinstall` step, which will keep the library up-to-date with your contract.

```json
"scripts": {
"postinstall": "soroban contract bindings ts --rpc-url https://soroban-testnet.stellar.org:443 --network-passphrase \"Test SDF Network ; September 2015\" --id CC3JCYWHNMPMQTOQUNDCJSCFSWRFZIE2JVSAUEXEG56DMKOMI3RL7VOH --name ts"
}
```

Obviously you need to adjust the above command based on the actual command you used to generate the library.

# Use it

Now that you have your library up-to-date and added to your project, you can import it in a file and see inline documentation for all of its exported methods:

```js
import { Contract, networks } from "ts"

const contract = new Contract({
...networks.futurenet, // for example; check which networks this library exports
rpcUrl: '...', // use your own, or find one for testing at https://soroban.stellar.org/docs/reference/rpc#public-rpc-providers
})

contract.|
```

As long as your editor is configured to show JavaScript/TypeScript documentation, you can pause your typing at that `|` to get a list of all exports and inline-documentation for each. It exports a separate [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) function for each method in the smart contract, with documentation for each generated from the comments the contract's author included in the original source code.
Loading

0 comments on commit a7d1a54

Please sign in to comment.