@stellar/typescript-wallet-sdk-soroban 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.MD ADDED
@@ -0,0 +1,8 @@
1
+ # Release notes - Typescript Wallet SDK Key Soroban - 1.5.0
2
+
3
+ ### Added
4
+ * Init to the project, added soroban functionality
5
+ * getTokenInvocationArgs function
6
+ * Token parsing/formatting functions
7
+ * scValyByType function
8
+ * generic getInvocationDetails helper
package/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Stellar Typescript Wallet Soroban SDK [![npm version](https://badge.fury.io/js/@stellar%2Ftypescript-wallet-sdk-soroban.svg)](https://badge.fury.io/js/@stellar%2Ftypescript-wallet-sdk-soroban)
2
+
3
+ The Typescript Wallet Soroban SDK is a work-in-progress library that (currently)
4
+ allows developers to use soroban helpers in their wallet applications. It works
5
+ in conjuction with the main
6
+ [Typescript Wallet SDK](https://github.com/stellar/typescript-wallet-sdk) to
7
+ hold all the functionality a developer would need to create a wallet for the
8
+ stellar network.
9
+
10
+ ## Dependency
11
+
12
+ The library is available via npm. To import `typescript-wallet-sdk-soroban` you
13
+ need to add it as a dependency to your code:
14
+
15
+ yarn:
16
+
17
+ ```shell
18
+ yarn add @stellar/typescript-wallet-sdk-soroban
19
+ ```
20
+
21
+ npm:
22
+
23
+ ```shell
24
+ npm install @stellar/typescript-wallet-sdk-soroban
25
+ ```
26
+
27
+ ## Introduction
28
+
29
+ Here's some examples on how to use the Soroban helpers:
30
+
31
+ ```typescript
32
+ import {
33
+ getTokenInvocationArgs,
34
+ formatTokenAmount,
35
+ parseTokenAmount,
36
+ scValByType,
37
+ } from "@stellar/typescript-wallet-sdk-soroban";
38
+
39
+ const transaction = TransactionBuilder.fromXDR(
40
+ "AAAAAgAAAACM6IR9GHiRoVVAO78JJNksy2fKDQNs2jBn8bacsRLcrDucaFsAAAWIAAAAMQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAABHkEVdJ+UfDnWpBr/qF582IEoDQ0iW0WPzO9CEUdvvh8AAAAIdHJhbnNmZXIAAAADAAAAEgAAAAAAAAAAjOiEfRh4kaFVQDu/CSTZLMtnyg0DbNowZ/G2nLES3KwAAAASAAAAAAAAAADoFl2ACT9HZkbCeuaT9MAIdStpdf58wM3P24nl738AnQAAAAoAAAAAAAAAAAAAAAAAAAAFAAAAAQAAAAAAAAAAAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAACHRyYW5zZmVyAAAAAwAAABIAAAAAAAAAAIzohH0YeJGhVUA7vwkk2SzLZ8oNA2zaMGfxtpyxEtysAAAAEgAAAAAAAAAA6BZdgAk/R2ZGwnrmk/TACHUraXX+fMDNz9uJ5e9/AJ0AAAAKAAAAAAAAAAAAAAAAAAAABQAAAAAAAAABAAAAAAAAAAIAAAAGAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAAFAAAAAEAAAAHa35L+/RxV6EuJOVk78H5rCN+eubXBWtsKrRxeLnnpRAAAAACAAAABgAAAAEeQRV0n5R8OdakGv+oXnzYgSgNDSJbRY/M70IRR2++HwAAABAAAAABAAAAAgAAAA8AAAAHQmFsYW5jZQAAAAASAAAAAAAAAACM6IR9GHiRoVVAO78JJNksy2fKDQNs2jBn8bacsRLcrAAAAAEAAAAGAAAAAR5BFXSflHw51qQa/6hefNiBKA0NIltFj8zvQhFHb74fAAAAEAAAAAEAAAACAAAADwAAAAdCYWxhbmNlAAAAABIAAAAAAAAAAOgWXYAJP0dmRsJ65pP0wAh1K2l1/nzAzc/bieXvfwCdAAAAAQBkcwsAACBwAAABKAAAAAAAAB1kAAAAAA==",
41
+ Networks.FUTURENET,
42
+ ) as Transaction<Memo<MemoType>, Operation.InvokeHostFunction[]>;
43
+ const op = transaction.operations[0];
44
+
45
+ const args = getTokenInvocationArgs(op);
46
+ /*
47
+ extracts args from the invoke host function operation:
48
+ args = {
49
+ fnName: "transfer,
50
+ contractId: "CAPECFLUT6KHYOOWUQNP7KC6PTMICKANBURFWRMPZTXUEEKHN67B7UI2",
51
+ from: "GCGORBD5DB4JDIKVIA536CJE3EWMWZ6KBUBWZWRQM7Y3NHFRCLOKYVAL",
52
+ to: "GDUBMXMABE7UOZSGYJ5ONE7UYAEHKK3JOX7HZQGNZ7NYTZPPP4AJ2GQJ",
53
+ amount: 5
54
+ }
55
+ */
56
+
57
+ const formattedAmount = formatTokenAmount("10000123", 3);
58
+ // converts smart contract token amount into a displayable amount that can be
59
+ // used on client UI
60
+ // formattedAmount = "10000.123"
61
+
62
+ const parsedAmount = parseTokenAmount("10000.123", 3);
63
+ // converts an amount to a whole (bigint) number that can be used on
64
+ // smart contracts operations
65
+ // parsedAmount = 10000123
66
+
67
+ const accountAddress = xdr.ScVal.scvAddress(
68
+ xdr.ScAddress.scAddressTypeAccount(
69
+ xdr.PublicKey.publicKeyTypeEd25519(
70
+ StrKey.decodeEd25519PublicKey(
71
+ "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB",
72
+ ),
73
+ ),
74
+ ),
75
+ );
76
+
77
+ const addressString = scValByType(accountAddress);
78
+ // converts smart contract complex value into a simple string
79
+ // addressString = "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"
80
+ ```
@@ -0,0 +1,5 @@
1
+ const parentConfig = require("../../babel.config");
2
+
3
+ module.exports = {
4
+ ...parentConfig,
5
+ };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * https://github.com/stellar/js-stellar-base/blob/4b510113738aefb5decb31e2ae72c27da5dd7f5c/src/soroban.js
3
+ *
4
+ * Given a whole number smart contract amount of a token and an amount of
5
+ * decimal places (if the token has any), it returns a "display" value.
6
+ *
7
+ * All arithmetic inside the contract is performed on integers to avoid
8
+ * potential precision and consistency issues of floating-point.
9
+ *
10
+ * @param {string | bigint} amount the token amount you want to display
11
+ * @param {number} decimals specify how many decimal places a token has
12
+ *
13
+ * @returns {string} the display value
14
+ * @throws {TypeError} if the given amount has a decimal point already
15
+ * @example
16
+ * formatTokenAmount("123000", 4) === "12.3";
17
+ */
18
+ export declare const formatTokenAmount: (amount: string | bigint, decimals: number) => string;
@@ -0,0 +1,12 @@
1
+ import { xdr } from "@stellar/stellar-sdk";
2
+ import { InvocationArgs } from "Types";
3
+ /**
4
+ * Extract invocation args and params from a Soroban authorized invocation
5
+ * tree up to its immediate sub invocations.
6
+ *
7
+ * @param {xdr.SorobanAuthorizedInvocation} invocationTree - The invocation tree.
8
+ *
9
+ * @returns {InvocationArgs[]} A list of user friendly invocation args and params.
10
+ */
11
+ export declare const getInvocationDetails: (invocationTree: xdr.SorobanAuthorizedInvocation) => InvocationArgs[];
12
+ export declare const getInvocationArgs: (invocation: xdr.SorobanAuthorizedInvocation) => InvocationArgs | undefined;
@@ -0,0 +1,14 @@
1
+ import { Operation, xdr } from "@stellar/stellar-sdk";
2
+ import { ArgsForTokenInvocation, TokenInvocationArgs } from "../Types";
3
+ export declare const getArgsForTokenInvocation: (fnName: string, args: xdr.ScVal[]) => ArgsForTokenInvocation;
4
+ /**
5
+ * Get params and args related to the invoked contract. It must use a valid
6
+ * "transfer" or "mint" invocation otherwise it will return 'null'.
7
+ *
8
+ * @param {Operation.InvokeHostFunction} hostFn - The invoke host function.
9
+ *
10
+ * @returns {TokenInvocationArgs | null} Params and args related to the
11
+ * "transfer" or "mint" invocation like function name, contract id, from/to
12
+ * addresses and amount.
13
+ */
14
+ export declare const getTokenInvocationArgs: (hostFn: Operation.InvokeHostFunction) => TokenInvocationArgs | null;
@@ -0,0 +1,5 @@
1
+ export * from "./formatTokenAmount";
2
+ export * from "./getInvocationDetails";
3
+ export * from "./getTokenInvocationArgs";
4
+ export * from "./parseTokenAmount";
5
+ export * from "./scValByType";
@@ -0,0 +1,24 @@
1
+ import BigNumber from "bignumber.js";
2
+ /**
3
+ * https://github.com/stellar/js-stellar-base/blob/4b510113738aefb5decb31e2ae72c27da5dd7f5c/src/soroban.js
4
+ *
5
+ * Parse a token amount to use it on smart contract
6
+ *
7
+ * This function takes the display value and its decimals (if the token has
8
+ * any) and returns a string that'll be used within the smart contract.
9
+ *
10
+ * @param {string | number | BigNumber} amount the token amount you want to
11
+ * use in a smart contract which you've been displaying in a UI
12
+ * @param {number} decimals the number of decimal places expected in the
13
+ * display value (different than the "actual" number, because suffix zeroes
14
+ * might not be present)
15
+ *
16
+ * @returns {bigint} the whole number token amount represented by the display
17
+ * value with the decimal places shifted over
18
+ *
19
+ * @example
20
+ * const displayValueAmount = "123.4560"
21
+ * const parsedAmtForSmartContract = parseTokenAmount(displayValueAmount, 5);
22
+ * parsedAmtForSmartContract === "12345600"
23
+ */
24
+ export declare const parseTokenAmount: (amount: string | number | BigNumber, decimals: number) => bigint;
@@ -0,0 +1,23 @@
1
+ import { xdr } from "@stellar/stellar-sdk";
2
+ /**
3
+ * This function attempts to convert smart contract (complex) value types
4
+ * to common/simpler types like string, array, buffer, JSON string, etc.
5
+ *
6
+ * @param {xdr.ScVal} scVal the smart contract (complex) value
7
+ *
8
+ *
9
+ * @returns the smart contract value converted to a common/simpler
10
+ * value like string, array, buffer, JSON string, etc.
11
+ *
12
+ * @example
13
+ * const accountAddress = xdr.ScVal.scvAddress(
14
+ * xdr.ScAddress.scAddressTypeAccount(
15
+ * xdr.PublicKey.publicKeyTypeEd25519(
16
+ * StrKey.decodeEd25519PublicKey("GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"),
17
+ * ),
18
+ * )
19
+ * ); ===> complex object
20
+ *
21
+ * scValByType(accountAddress) returns "GBBM6BKZPEHWYO3E3YKREDPQXMS4VK35YLNU7NFBRI26RAN7GI5POFBB"
22
+ */
23
+ export declare const scValByType: (scVal: xdr.ScVal) => any;
@@ -0,0 +1,31 @@
1
+ import { xdr } from "@stellar/stellar-sdk";
2
+ export declare enum SorobanTokenInterface {
3
+ transfer = "transfer",
4
+ mint = "mint"
5
+ }
6
+ export type ArgsForTokenInvocation = {
7
+ from: string;
8
+ to: string;
9
+ amount: bigint | number;
10
+ };
11
+ export type TokenInvocationArgs = ArgsForTokenInvocation & {
12
+ fnName: SorobanTokenInterface;
13
+ contractId: string;
14
+ };
15
+ export interface FnArgsInvoke {
16
+ type: "invoke";
17
+ fnName: string;
18
+ contractId: string;
19
+ args: xdr.ScVal[];
20
+ }
21
+ export interface FnArgsCreateWasm {
22
+ type: "wasm";
23
+ salt: string;
24
+ hash: string;
25
+ address: string;
26
+ }
27
+ export interface FnArgsCreateSac {
28
+ type: "sac";
29
+ asset: string;
30
+ }
31
+ export type InvocationArgs = FnArgsInvoke | FnArgsCreateWasm | FnArgsCreateSac;