@mysten/sui 2.17.0 → 2.18.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 +18 -0
- package/dist/grpc/proto/sui/rpc/v2/ledger_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/move_package_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/name_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/state_service.client.d.mts +4 -4
- package/dist/grpc/proto/sui/rpc/v2/subscription_service.client.d.mts +4 -4
- package/dist/transactions/Transaction.d.mts +16 -2
- package/dist/transactions/Transaction.d.mts.map +1 -1
- package/dist/transactions/Transaction.mjs +8 -3
- package/dist/transactions/Transaction.mjs.map +1 -1
- package/dist/transactions/index.d.mts +2 -2
- package/dist/version.mjs +1 -1
- package/dist/version.mjs.map +1 -1
- package/dist/zklogin/jwt-utils.d.mts.map +1 -1
- package/dist/zklogin/jwt-utils.mjs.map +1 -1
- package/dist/zklogin/utils.d.mts.map +1 -1
- package/dist/zklogin/utils.mjs +9 -0
- package/dist/zklogin/utils.mjs.map +1 -1
- package/docs/bcs.md +11 -6
- package/docs/clients/core.md +10 -9
- package/docs/clients/grpc.md +1 -1
- package/docs/index.md +176 -22
- package/docs/llms-index.md +6 -9
- package/docs/migrations/0.38.md +2 -2
- package/docs/migrations/sui-1.0.md +2 -2
- package/docs/migrations/sui-2.0/json-rpc-migration.md +76 -33
- package/docs/plugins.md +29 -5
- package/docs/transactions/basics.md +279 -0
- package/docs/transactions/coins-and-balances.md +293 -0
- package/docs/transactions/offline.md +192 -0
- package/docs/transactions/reference.md +380 -0
- package/docs/transactions/signing-and-execution.md +401 -0
- package/package.json +26 -26
- package/src/transactions/Transaction.ts +36 -5
- package/src/transactions/index.ts +1 -0
- package/src/version.ts +1 -1
- package/src/zklogin/jwt-utils.ts +3 -0
- package/src/zklogin/utils.ts +17 -0
- package/docs/faucet.md +0 -26
- package/docs/hello-sui.md +0 -115
- package/docs/install.md +0 -61
- package/docs/transaction-building/basics.md +0 -299
- package/docs/transaction-building/gas.md +0 -61
- package/docs/transaction-building/intents.md +0 -62
- package/docs/transaction-building/offline.md +0 -73
- package/docs/transaction-building/sponsored-transactions.md +0 -22
package/docs/hello-sui.md
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
# Hello Sui
|
|
2
|
-
|
|
3
|
-
> Build your first Sui application with the TypeScript SDK.
|
|
4
|
-
|
|
5
|
-
This basic example introduces you to the Sui TypeScript SDK. The Node.js example mints SUI on a Sui
|
|
6
|
-
network and then queries the address to get a sum for the owned SUI. You don't need to use an IDE to
|
|
7
|
-
complete the example, but one like Microsoft Visual Studio Code helps centralize more advanced
|
|
8
|
-
projects.
|
|
9
|
-
|
|
10
|
-
## Before you begin
|
|
11
|
-
|
|
12
|
-
You need an address on a Sui development network (Devnet, Testnet, or local). If you don't already
|
|
13
|
-
have an address, use the [Sui Client CLI](https://docs.sui.io/references/cli/client) or the
|
|
14
|
-
[Sui Wallet browser extension](https://docs.mystenlabs.com) to create one.
|
|
15
|
-
|
|
16
|
-
You also need [Node.js](https://nodejs.org/en/download/current) and a package manager like
|
|
17
|
-
[pnpm](https://pnpm.io/installation) to follow this example, so install them on your system if you
|
|
18
|
-
haven't already.
|
|
19
|
-
|
|
20
|
-
## Start a project
|
|
21
|
-
|
|
22
|
-
Using a Terminal or Console, create a folder on your system (`hello-sui` in this example) and make
|
|
23
|
-
it the working directory.
|
|
24
|
-
|
|
25
|
-
```sh
|
|
26
|
-
mkdir hello-sui
|
|
27
|
-
cd hello-sui
|
|
28
|
-
```
|
|
29
|
-
|
|
30
|
-
When you use a package manager to install the necessary packages, it downloads the modules to your
|
|
31
|
-
`node_modules` folder and adds the references to your `package.json` file, creating the file if it
|
|
32
|
-
doesn't already exist. For this example, you need only the Sui TypeScript SDK:
|
|
33
|
-
|
|
34
|
-
```sh npm2yarn
|
|
35
|
-
npm i -D @mysten/sui
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
The SDK is published as an ESM only package, so you also need to set `"type": "module"` in your
|
|
39
|
-
`package.json`. Your `package.json` file should look like this:
|
|
40
|
-
|
|
41
|
-
```json
|
|
42
|
-
{
|
|
43
|
-
"type": "module",
|
|
44
|
-
"dependencies": {
|
|
45
|
-
"@mysten/sui": "^<VERSION_NUMBER>"
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Get some SUI for your account
|
|
51
|
-
|
|
52
|
-
Instead of a 'Hello World' output to your console, this example introduces some SUI to your wallet
|
|
53
|
-
address. You must be on Devnet, Testnet, or a local network to use a faucet for minting SUI.
|
|
54
|
-
|
|
55
|
-
Create a new `index.js` file in the root of your project with the following code.
|
|
56
|
-
|
|
57
|
-
```js
|
|
58
|
-
|
|
59
|
-
// replace <YOUR_SUI_ADDRESS> with your actual address, which is in the form 0x123...
|
|
60
|
-
const MY_ADDRESS = '<YOUR_SUI_ADDRESS>';
|
|
61
|
-
|
|
62
|
-
// create a new SuiGrpcClient object pointing to the network you want to use
|
|
63
|
-
const suiClient = new SuiGrpcClient({
|
|
64
|
-
network: 'devnet',
|
|
65
|
-
baseUrl: 'https://fullnode.devnet.sui.io:443',
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// Convert MIST to Sui
|
|
69
|
-
const balance = (balance) => {
|
|
70
|
-
return Number.parseInt(balance.totalBalance) / Number(MIST_PER_SUI);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
// store the JSON representation for the SUI the address owns before using faucet
|
|
74
|
-
const suiBefore = await suiClient.getBalance({
|
|
75
|
-
owner: MY_ADDRESS,
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
await requestSuiFromFaucetV2({
|
|
79
|
-
// use getFaucetHost to make sure you're using correct faucet address
|
|
80
|
-
// you can also just use the address (see Sui TypeScript SDK Quick Start for values)
|
|
81
|
-
host: getFaucetHost('devnet'),
|
|
82
|
-
recipient: MY_ADDRESS,
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// store the JSON representation for the SUI the address owns after using faucet
|
|
86
|
-
const suiAfter = await suiClient.getBalance({
|
|
87
|
-
owner: MY_ADDRESS,
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
// Output result to console.
|
|
91
|
-
console.log(
|
|
92
|
-
`Balance before faucet: ${balance(suiBefore)} SUI. Balance after: ${balance(
|
|
93
|
-
suiAfter,
|
|
94
|
-
)} SUI. Hello, SUI!`,
|
|
95
|
-
);
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
Save the file, then use Node.js to run it in your Console or Terminal:
|
|
99
|
-
|
|
100
|
-
```sh
|
|
101
|
-
node index.js
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
The code imports the `requestSuiFromFaucetV2` function from the SDK and calls it to mint SUI for the
|
|
105
|
-
provided address. The code also imports `SuiGrpcClient` to create a new client on the Sui network
|
|
106
|
-
that it uses to query the address and output the amount of SUI the address owns before and after
|
|
107
|
-
using the faucet. You can check the total SUI for your address using the Sui Wallet or Sui Client
|
|
108
|
-
CLI.
|
|
109
|
-
|
|
110
|
-
> **Note:** Faucets on Devnet and Testnet are rate limited. If you run the script too many times, you surpass
|
|
111
|
-
> the limit and must wait to successfully run it again. For Testnet, the best way to get SUI is
|
|
112
|
-
> through the Web UI: `faucet.sui.io`.
|
|
113
|
-
|
|
114
|
-
You can also use the [Sui Client CLI](https://docs.sui.io/references/cli/client) to perform client
|
|
115
|
-
calls on a Sui network.
|
package/docs/install.md
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
# Install Sui TypeScript SDK
|
|
2
|
-
|
|
3
|
-
> Install the @mysten/sui package and configure your project.
|
|
4
|
-
|
|
5
|
-
The Sui TypeScript SDK is available in the
|
|
6
|
-
[Sui TS SDK monorepo](https://github.com/MystenLabs/ts-sdks) and NPM.
|
|
7
|
-
|
|
8
|
-
## Install from NPM
|
|
9
|
-
|
|
10
|
-
To use the Sui TypeScript SDK in your project, run the following command in your project root:
|
|
11
|
-
|
|
12
|
-
```sh npm2yarn
|
|
13
|
-
npm i @mysten/sui
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
The SDK is published as an ESM only package. Make sure your `package.json` includes
|
|
17
|
-
`"type": "module"`:
|
|
18
|
-
|
|
19
|
-
```json
|
|
20
|
-
{
|
|
21
|
-
"type": "module"
|
|
22
|
-
}
|
|
23
|
-
```
|
|
24
|
-
|
|
25
|
-
If you are using TypeScript, your `tsconfig.json` should use a compatible `moduleResolution` setting
|
|
26
|
-
such as `"NodeNext"`, `"Node16"`, or `"Bundler"`. See the
|
|
27
|
-
[2.0 migration guide](/sui/migrations/sui-2.0#esm-migration) for more details.
|
|
28
|
-
|
|
29
|
-
## Experimental tag for use with a local Sui network
|
|
30
|
-
|
|
31
|
-
Projects developing against one of the onchain Sui networks (Devnet, Testnet, Mainnet) should use
|
|
32
|
-
the base SDK published in the NPM registry (previous section) because the code aligns with the
|
|
33
|
-
relevant JSON-RPC. If you are developing against a
|
|
34
|
-
[local network](https://docs.sui.io/guides/developer/getting-started/local-network) built from the
|
|
35
|
-
`main` branch of the Sui monorepo, however, you should use the `experimental`-tagged SDK package as
|
|
36
|
-
it contains the latest features (or a local build detailed in the section that follows).
|
|
37
|
-
|
|
38
|
-
```sh npm2yarn
|
|
39
|
-
npm i @mysten/sui@experimental
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
## Install from local build
|
|
43
|
-
|
|
44
|
-
To build the SDK from the Sui monorepo, you must use [pnpm](https://pnpm.io/). With pnpm installed,
|
|
45
|
-
run the following command from the `sui` root directory:
|
|
46
|
-
|
|
47
|
-
```bash
|
|
48
|
-
# Install all dependencies
|
|
49
|
-
pnpm install
|
|
50
|
-
# Run the build for the TypeScript SDK
|
|
51
|
-
pnpm sdk build
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
With the SDK built, you can import the library from your `sui` project. To do so, use a path to the
|
|
55
|
-
`ts-sdks/packages/sui` directory that is relative to your project. For example, if you created a
|
|
56
|
-
folder `my-sui-project` at the same level as `sui`, use the following to import the locally built
|
|
57
|
-
Sui TypeScript package:
|
|
58
|
-
|
|
59
|
-
```bash
|
|
60
|
-
pnpm add ../ts-sdks/packages/sui
|
|
61
|
-
```
|
|
@@ -1,299 +0,0 @@
|
|
|
1
|
-
# Sui Programmable Transaction Basics
|
|
2
|
-
|
|
3
|
-
> Construct programmable transaction blocks with the Transaction API.
|
|
4
|
-
|
|
5
|
-
This example starts by constructing a transaction to send SUI. To construct transactions, import the
|
|
6
|
-
`Transaction` class and construct it:
|
|
7
|
-
|
|
8
|
-
```tsx
|
|
9
|
-
|
|
10
|
-
const tx = new Transaction();
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
You can then add commands to the transaction .
|
|
14
|
-
|
|
15
|
-
```tsx
|
|
16
|
-
// create a new coin with balance 100, based on the coins used as gas payment
|
|
17
|
-
// you can define any balance here
|
|
18
|
-
const [coin] = tx.splitCoins(tx.gas, [100]);
|
|
19
|
-
|
|
20
|
-
// transfer the split coin to a specific address
|
|
21
|
-
tx.transferObjects([coin], '0xSomeSuiAddress');
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
You can attach multiple commands of the same type to a transaction, as well. For example, to get a
|
|
25
|
-
list of transfers and iterate over them to transfer coins to each of them:
|
|
26
|
-
|
|
27
|
-
```tsx
|
|
28
|
-
interface Transfer {
|
|
29
|
-
to: string;
|
|
30
|
-
amount: number;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// procure a list of some Sui transfers to make
|
|
34
|
-
const transfers: Transfer[] = getTransfers();
|
|
35
|
-
|
|
36
|
-
const tx = new Transaction();
|
|
37
|
-
|
|
38
|
-
// first, split the gas coin into multiple coins
|
|
39
|
-
const coins = tx.splitCoins(
|
|
40
|
-
tx.gas,
|
|
41
|
-
transfers.map((transfer) => transfer.amount),
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
// next, create a transfer command for each coin
|
|
45
|
-
transfers.forEach((transfer, index) => {
|
|
46
|
-
tx.transferObjects([coins[index]], transfer.to);
|
|
47
|
-
});
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
After you have the transaction defined, you can directly execute it with a signer using
|
|
51
|
-
`signAndExecuteTransaction`.
|
|
52
|
-
|
|
53
|
-
```tsx
|
|
54
|
-
const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx });
|
|
55
|
-
|
|
56
|
-
// IMPORTANT: Always check the transaction status
|
|
57
|
-
// Transactions can execute but still fail (e.g., insufficient gas, move errors)
|
|
58
|
-
if (result.$kind === 'FailedTransaction') {
|
|
59
|
-
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
## Observing the results of a transaction
|
|
64
|
-
|
|
65
|
-
When you use `client.signAndExecuteTransaction` or `client.executeTransactionBlock`, the transaction
|
|
66
|
-
will be finalized on the blockchain before the function resolves, but the effects of the transaction
|
|
67
|
-
might not be immediately observable.
|
|
68
|
-
|
|
69
|
-
There are 2 ways to observe the results of a transaction. Methods like
|
|
70
|
-
`client.signAndExecuteTransaction` accept an `options` object with options like `showObjectChanges`
|
|
71
|
-
and `showBalanceChanges` (see
|
|
72
|
-
[the SuiJsonRpcClient docs for more details](/sui/clients/json-rpc#arguments)). These options will
|
|
73
|
-
cause the request to contain additional details about the effects of the transaction that can be
|
|
74
|
-
immediately displayed to the user, or used for further processing in your application.
|
|
75
|
-
|
|
76
|
-
The other way effects of transactions can be observed is by querying other RPC methods like
|
|
77
|
-
`client.getBalances` that return objects or balances owned by a specific address. These RPC calls
|
|
78
|
-
depend on the RPC node having indexed the effects of the transaction, which might not have happened
|
|
79
|
-
immediately after a transaction has been executed. To ensure that effects of a transaction are
|
|
80
|
-
represented in future RPC calls, you can use the `waitForTransaction` method on the client:
|
|
81
|
-
|
|
82
|
-
```typescript
|
|
83
|
-
const result = await client.signAndExecuteTransaction({ signer: keypair, transaction: tx });
|
|
84
|
-
|
|
85
|
-
// Check transaction status
|
|
86
|
-
if (result.$kind === 'FailedTransaction') {
|
|
87
|
-
throw new Error(`Transaction failed: ${result.FailedTransaction.status.error?.message}`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
await client.waitForTransaction({ result });
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
Once `waitForTransaction` resolves, any future RPC calls will be guaranteed to reflect the effects
|
|
94
|
-
of the transaction.
|
|
95
|
-
|
|
96
|
-
## Transactions
|
|
97
|
-
|
|
98
|
-
Programmable Transactions have two key concepts: inputs and commands.
|
|
99
|
-
|
|
100
|
-
Commands are steps of execution in the transaction. Each command in a Transaction takes a set of
|
|
101
|
-
inputs, and produces results. The inputs for a transaction depend on the kind of command. Sui
|
|
102
|
-
supports following commands:
|
|
103
|
-
|
|
104
|
-
- `tx.splitCoins(coin, amounts)`: Creates new coins with the defined amounts, split from the
|
|
105
|
-
provided coin. Returns the coins so that it can be used in subsequent transactions.
|
|
106
|
-
- Example: `tx.splitCoins(tx.gas, [100, 200])`
|
|
107
|
-
- `tx.mergeCoins(destinationCoin, sourceCoins)`: Merges the sourceCoins into the destinationCoin.
|
|
108
|
-
- Example: `tx.mergeCoins(tx.object(coin1), [tx.object(coin2), tx.object(coin3)])`
|
|
109
|
-
- `tx.transferObjects(objects, address)`: Transfers a list of objects to the specified address.
|
|
110
|
-
- Example: `tx.transferObjects([tx.object(thing1), tx.object(thing2)], myAddress)`
|
|
111
|
-
- `tx.moveCall({ target, arguments, typeArguments })`: Executes a Move call. Returns whatever the
|
|
112
|
-
Sui Move call returns.
|
|
113
|
-
- Example:
|
|
114
|
-
`tx.moveCall({ target: '0x2::devnet_nft::mint', arguments: [tx.pure.string(name), tx.pure.string(description), tx.pure.string(image)] })`
|
|
115
|
-
- `tx.makeMoveVec({ type, elements })`: Constructs a vector of objects that can be passed into a
|
|
116
|
-
`moveCall`. This is required as there’s no way to define a vector as an input.
|
|
117
|
-
- Example: `tx.makeMoveVec({ elements: [tx.object(id1), tx.object(id2)] })`
|
|
118
|
-
- `tx.publish(modules, dependencies)`: Publishes a Move package. Returns the upgrade capability
|
|
119
|
-
object.
|
|
120
|
-
|
|
121
|
-
## Passing inputs to a command
|
|
122
|
-
|
|
123
|
-
Command inputs can be provided in a number of different ways, depending on the command, and the type
|
|
124
|
-
of value being provided.
|
|
125
|
-
|
|
126
|
-
#### JavaScript values
|
|
127
|
-
|
|
128
|
-
For specific command arguments (`amounts` in `splitCoins`, and `address` in `transferObjects`) the
|
|
129
|
-
expected type is known ahead of time, and you can directly pass raw javascript values when calling
|
|
130
|
-
the command method. appropriate Move type automatically.
|
|
131
|
-
|
|
132
|
-
```ts
|
|
133
|
-
// the amount to split off the gas coin is provided as a pure javascript number
|
|
134
|
-
const [coin] = tx.splitCoins(tx.gas, [100]);
|
|
135
|
-
// the address for the transfer is provided as a pure javascript string
|
|
136
|
-
tx.transferObjects([coin], '0xSomeSuiAddress');
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
#### Pure values
|
|
140
|
-
|
|
141
|
-
When providing inputs that are not on chain objects, the values must be serialized as
|
|
142
|
-
|
|
143
|
-
[BCS](https://sdk.mystenlabs.com/bcs), which can be done using `tx.pure`, for example
|
|
144
|
-
`tx.pure.address(address)` or `tx.pure(bcs.vector(bcs.U8).serialize(bytes))`.
|
|
145
|
-
|
|
146
|
-
`tx.pure` can be called as a function that accepts a SerializedBcs object, or as a namespace that
|
|
147
|
-
contains functions for each of the supported types.
|
|
148
|
-
|
|
149
|
-
```ts
|
|
150
|
-
const [coin] = tx.splitCoins(tx.gas, [tx.pure.u64(100)]);
|
|
151
|
-
const [coin] = tx.splitCoins(tx.gas, [tx.pure(bcs.U64.serialize(100))]);
|
|
152
|
-
tx.transferObjects([coin], tx.pure.address('0xSomeSuiAddress'));
|
|
153
|
-
tx.transferObjects([coin], tx.pure(bcs.Address.serialize('0xSomeSuiAddress')));
|
|
154
|
-
```
|
|
155
|
-
|
|
156
|
-
To pass `vector` or `option` types, you can pass use the corresponding methods on `tx.pure`, use
|
|
157
|
-
tx.pure as a function with a type argument, or serialize the value before passing it to tx.pure
|
|
158
|
-
using the bcs sdk:
|
|
159
|
-
|
|
160
|
-
```ts
|
|
161
|
-
|
|
162
|
-
tx.moveCall({
|
|
163
|
-
target: '0x2::foo::bar',
|
|
164
|
-
arguments: [
|
|
165
|
-
// using vector and option methods
|
|
166
|
-
tx.pure.vector('u8', [1, 2, 3]),
|
|
167
|
-
tx.pure.option('u8', 1),
|
|
168
|
-
tx.pure.option('u8', null),
|
|
169
|
-
|
|
170
|
-
// Using pure with type arguments
|
|
171
|
-
tx.pure('vector<u8>', [1, 2, 3]),
|
|
172
|
-
tx.pure('option<u8>', 1),
|
|
173
|
-
tx.pure('option<u8>', null),
|
|
174
|
-
tx.pure('vector<option<u8>>', [1, null, 2]),
|
|
175
|
-
|
|
176
|
-
// Using bcs.serialize
|
|
177
|
-
tx.pure(bcs.vector(bcs.U8).serialize([1, 2, 3])),
|
|
178
|
-
tx.pure(bcs.option(bcs.U8).serialize(1)),
|
|
179
|
-
tx.pure(bcs.option(bcs.U8).serialize(null)),
|
|
180
|
-
tx.pure(bcs.vector(bcs.option(bcs.U8)).serialize([1, null, 2])),
|
|
181
|
-
],
|
|
182
|
-
});
|
|
183
|
-
```
|
|
184
|
-
|
|
185
|
-
#### Object references
|
|
186
|
-
|
|
187
|
-
To use an on chain object as a transaction input, you must pass a reference to that object. This can
|
|
188
|
-
be done by calling `tx.object` with the object id. Transaction arguments that only accept objects
|
|
189
|
-
(like `objects` in `transferObjects`) will automatically treat any provided strings as objects ids.
|
|
190
|
-
For methods like `moveCall` that accept both objects and other types, you must explicitly call
|
|
191
|
-
`tx.object` to convert the id to an object reference.
|
|
192
|
-
|
|
193
|
-
```ts
|
|
194
|
-
// Object IDs can be passed to some methods like (transferObjects) directly
|
|
195
|
-
tx.transferObjects(['0xSomeObject'], 'OxSomeAddress');
|
|
196
|
-
// tx.object can be used anywhere an object is accepted
|
|
197
|
-
tx.transferObjects([tx.object('0xSomeObject')], 'OxSomeAddress');
|
|
198
|
-
|
|
199
|
-
tx.moveCall({
|
|
200
|
-
target: '0x2::nft::mint',
|
|
201
|
-
// object IDs must be wrapped in moveCall arguments
|
|
202
|
-
arguments: [tx.object('0xSomeObject')],
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
// tx.object automatically converts the object ID to receiving transaction arguments if the moveCall expects it
|
|
206
|
-
tx.moveCall({
|
|
207
|
-
target: '0xSomeAddress::example::receive_object',
|
|
208
|
-
// 0xSomeAddress::example::receive_object expects a receiving argument and has a Move definition that looks like this:
|
|
209
|
-
// public fun receive_object<T: key>(parent_object: &mut ParentObjectType, receiving_object: Receiving<ChildObjectType>) { ... }
|
|
210
|
-
arguments: [tx.object('0xParentObjectID'), tx.object('0xReceivingObjectID')],
|
|
211
|
-
});
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
When building a transaction, Sui expects all objects to be fully resolved, including the object
|
|
215
|
-
version. The SDK automatically looks up the current version of objects for any provided object
|
|
216
|
-
reference when building a transaction. If the object reference is used as a receiving argument to a
|
|
217
|
-
`moveCall`, the object reference is automatically converted to a receiving transaction argument.
|
|
218
|
-
This greatly simplifies building transactions, but requires additional RPC calls. You can optimize
|
|
219
|
-
this process by providing a fully resolved object reference instead:
|
|
220
|
-
|
|
221
|
-
```ts
|
|
222
|
-
// for owned or immutable objects
|
|
223
|
-
tx.object(Inputs.ObjectRef({ digest, objectId, version }));
|
|
224
|
-
|
|
225
|
-
// for shared objects
|
|
226
|
-
tx.object(Inputs.SharedObjectRef({ objectId, initialSharedVersion, mutable }));
|
|
227
|
-
|
|
228
|
-
// for receiving objects
|
|
229
|
-
tx.object(Inputs.ReceivingRef({ digest, objectId, version }));
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
##### Object helpers
|
|
233
|
-
|
|
234
|
-
There are a handful of specific object types that can be referenced through helper methods on
|
|
235
|
-
tx.object:
|
|
236
|
-
|
|
237
|
-
```ts
|
|
238
|
-
tx.object.system(),
|
|
239
|
-
tx.object.clock(),
|
|
240
|
-
tx.object.random(),
|
|
241
|
-
tx.object.denyList(),
|
|
242
|
-
|
|
243
|
-
tx.object.option({
|
|
244
|
-
type: '0x123::example::Thing',
|
|
245
|
-
// value can be an Object ID, or any other object reference, or null for `none`
|
|
246
|
-
value: '0x456',
|
|
247
|
-
}),
|
|
248
|
-
```
|
|
249
|
-
|
|
250
|
-
#### Transaction results
|
|
251
|
-
|
|
252
|
-
You can also use the result of a command as an argument in a subsequent commands. Each method on the
|
|
253
|
-
transaction builder returns a reference to the transaction result.
|
|
254
|
-
|
|
255
|
-
```tsx
|
|
256
|
-
// split a coin object off of the gas object
|
|
257
|
-
const [coin] = tx.splitCoins(tx.gas, [100]);
|
|
258
|
-
// transfer the resulting coin object
|
|
259
|
-
tx.transferObjects([coin], address);
|
|
260
|
-
```
|
|
261
|
-
|
|
262
|
-
When a command returns multiple results, you can access the result at a specific index either using
|
|
263
|
-
destructuring, or array indexes.
|
|
264
|
-
|
|
265
|
-
```tsx
|
|
266
|
-
// destructuring (preferred, as it gives you logical local names)
|
|
267
|
-
const [nft1, nft2] = tx.moveCall({ target: '0x2::nft::mint_many' });
|
|
268
|
-
tx.transferObjects([nft1, nft2], address);
|
|
269
|
-
|
|
270
|
-
// array indexes
|
|
271
|
-
const mintMany = tx.moveCall({ target: '0x2::nft::mint_many' });
|
|
272
|
-
tx.transferObjects([mintMany[0], mintMany[1]], address);
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
## Get transaction bytes
|
|
276
|
-
|
|
277
|
-
If you need the transaction bytes, instead of signing or executing the transaction, you can use the
|
|
278
|
-
`build` method on the transaction builder itself.
|
|
279
|
-
|
|
280
|
-
You might need to explicitly call `setSender()` on the transaction to ensure that the `sender` field
|
|
281
|
-
is populated. This is normally done by the signer before signing the transaction, but will not be
|
|
282
|
-
done automatically if you’re building the transaction bytes yourself.
|
|
283
|
-
|
|
284
|
-
```tsx
|
|
285
|
-
const tx = new Transaction();
|
|
286
|
-
|
|
287
|
-
// ... add some transactions...
|
|
288
|
-
|
|
289
|
-
await tx.build({ client });
|
|
290
|
-
```
|
|
291
|
-
|
|
292
|
-
In most cases, building requires your SuiJsonRpcClient to fully resolve input values.
|
|
293
|
-
|
|
294
|
-
If you have transaction bytes, you can also convert them back into a `Transaction` class:
|
|
295
|
-
|
|
296
|
-
```tsx
|
|
297
|
-
const bytes = getTransactionBytesFromSomewhere();
|
|
298
|
-
const tx = Transaction.from(bytes);
|
|
299
|
-
```
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
# Paying for Sui Transactions with Gas Coins
|
|
2
|
-
|
|
3
|
-
> Configure gas budget, price, and coin selection for Sui transactions.
|
|
4
|
-
|
|
5
|
-
With Programmable Transactions, you can use the gas payment coin to construct coins with a set
|
|
6
|
-
balance using `splitCoin`. This is useful for Sui payments, and avoids the need for up-front coin
|
|
7
|
-
selection. You can use `tx.gas` to access the gas coin in a transaction, and it is valid as input
|
|
8
|
-
for any arguments, as long as it is used
|
|
9
|
-
[by-reference](https://docs.sui.io/guides/developer/sui-101/simulating-refs). Practically speaking,
|
|
10
|
-
this means you can also add to the gas coin with `mergeCoins` and borrow it for Move functions with
|
|
11
|
-
`moveCall`.
|
|
12
|
-
|
|
13
|
-
You can also transfer the gas coin using `transferObjects`, in the event that you want to transfer
|
|
14
|
-
all of your coin balance to another address.
|
|
15
|
-
|
|
16
|
-
## Gas configuration
|
|
17
|
-
|
|
18
|
-
The new transaction builder comes with default behavior for all gas logic, including automatically
|
|
19
|
-
setting the gas price, budget, and selecting coins to be used as gas. This behavior can be
|
|
20
|
-
customized.
|
|
21
|
-
|
|
22
|
-
### Gas price
|
|
23
|
-
|
|
24
|
-
By default, the gas price is set to the reference gas price of the network. You can also explicitly
|
|
25
|
-
set the gas price of the transaction by calling `setGasPrice` on the transaction builder.
|
|
26
|
-
|
|
27
|
-
```tsx
|
|
28
|
-
tx.setGasPrice(gasPrice);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
### Budget
|
|
32
|
-
|
|
33
|
-
By default, the gas budget is automatically derived by executing a dry-run of the transaction
|
|
34
|
-
beforehand. The dry run gas consumption is then used to determine a balance for the transaction. You
|
|
35
|
-
can override this behavior by explicitly setting a gas budget for the transaction, by calling
|
|
36
|
-
`setGasBudget` on the transaction builder.
|
|
37
|
-
|
|
38
|
-
The gas budget is represented in Sui and should take the gas price of the transaction into account.
|
|
39
|
-
|
|
40
|
-
```tsx
|
|
41
|
-
tx.setGasBudget(gasBudgetAmount);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
### Gas payment
|
|
45
|
-
|
|
46
|
-
By default, the gas payment is automatically determined by the SDK. The SDK selects all of the users
|
|
47
|
-
coins that are not used as inputs in the transaction.
|
|
48
|
-
|
|
49
|
-
The list of coins used as gas payment will be merged down into a single gas coin before executing
|
|
50
|
-
the transaction, and all but one of the gas objects will be deleted. The gas coin at the 0-index
|
|
51
|
-
will be the coin that all others are merged into.
|
|
52
|
-
|
|
53
|
-
```tsx
|
|
54
|
-
// you need to ensure that the coins do not overlap with any
|
|
55
|
-
// of the input objects for the transaction
|
|
56
|
-
tx.setGasPayment([coin1, coin2]);
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Gas coins should be objects containing the coins objectId, version, and digest.
|
|
60
|
-
|
|
61
|
-
<auto-type-table type="{ objectId: string, version: string | number, digest: string }" />
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
# Transaction Intents
|
|
2
|
-
|
|
3
|
-
> Use high-level intents to simplify transaction building.
|
|
4
|
-
|
|
5
|
-
Transaction Intents enable third-party SDKs and [Transaction Plugins](../plugins) to more easily add
|
|
6
|
-
operations to a Transaction. The TypeScript SDK currently only includes a single Intent
|
|
7
|
-
(CoinWithBalance), but more will be added in the future.
|
|
8
|
-
|
|
9
|
-
## The CoinWithBalance intent
|
|
10
|
-
|
|
11
|
-
The `CoinWithBalance` intent makes it easy to get a coin with a specific balance. For SUI, this has
|
|
12
|
-
generally been done by splitting the gas coin:
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
const tx = new Transaction();
|
|
16
|
-
|
|
17
|
-
const [coin] = tx.splitCoins(tx.gas, [100]);
|
|
18
|
-
|
|
19
|
-
tx.transferObjects([coin], recipient);
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
This approach works well for SUI, but can't be used for other coin types. The CoinWithBalance intent
|
|
23
|
-
solves this by providing a helper function that automatically adds the correct SplitCoins and
|
|
24
|
-
MergeCoins commands to the transaction:
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
|
|
28
|
-
const tx = new Transaction();
|
|
29
|
-
|
|
30
|
-
// Setting the sender is required for the CoinWithBalance intent to resolve coins when not using the gas coin
|
|
31
|
-
tx.setSender(keypair.toSuiAddress());
|
|
32
|
-
|
|
33
|
-
tx.transferObjects(
|
|
34
|
-
[
|
|
35
|
-
// Create a SUI coin (balance is in MIST)
|
|
36
|
-
coinWithBalance({ balance: 100 }),
|
|
37
|
-
// Create a coin of another type
|
|
38
|
-
coinWithBalance({ balance: 100, type: '0x123::foo:Bar' }),
|
|
39
|
-
],
|
|
40
|
-
recipient,
|
|
41
|
-
);
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
Splitting the gas coin also causes problems for sponsored transactions. When sponsoring
|
|
45
|
-
transactions, the gas coin comes from the sponsor instead of the transaction sender. Transaction
|
|
46
|
-
sponsors usually do not sponsor transactions that use the gas coin for anything other than gas. To
|
|
47
|
-
transfer SUI that does not use the gas coin, you can set the `useGasCoin` option to `false`:
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
const tx = new Transaction();
|
|
51
|
-
tx.transferObjects([coinWithBalance({ balance: 100, useGasCoin: false })], recipient);
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
It's important to only set `useGasCoin` option to false for sponsored transactions, otherwise the
|
|
55
|
-
coinWithBalance intent might use all the SUI coins, leaving no coins to use for gas.
|
|
56
|
-
|
|
57
|
-
## How it works
|
|
58
|
-
|
|
59
|
-
When the `CoinWithBalance` intent is resolved, it will look up the senders owned coins for each type
|
|
60
|
-
that needs to be created. It will then find a set of coins with sufficient balance to cover the
|
|
61
|
-
desired balance, to combine them into a single coin. This coin is then used in a `SplitCoins`
|
|
62
|
-
command to create the desired coin.
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
# Building Offline
|
|
2
|
-
|
|
3
|
-
> Build transactions without a network connection.
|
|
4
|
-
|
|
5
|
-
To build a transaction offline (with no `client` required), you need to fully define all of your
|
|
6
|
-
inputs, gas configuration, and expiration.
|
|
7
|
-
|
|
8
|
-
## Required configuration
|
|
9
|
-
|
|
10
|
-
When building offline, you must set the following:
|
|
11
|
-
|
|
12
|
-
- Sender address: The address that will execute the transaction
|
|
13
|
-
- Gas price: The price per gas unit (can be obtained from the network beforehand)
|
|
14
|
-
- Gas budget: The maximum gas to spend on this transaction
|
|
15
|
-
- Gas payment: One or more coin object references to use for gas, or an empty array for Address
|
|
16
|
-
Balances
|
|
17
|
-
- Expiration: Only needed when using address balances for gas
|
|
18
|
-
|
|
19
|
-
```tsx
|
|
20
|
-
|
|
21
|
-
const { referenceGasPrice } = await client.getReferenceGasPrice();
|
|
22
|
-
|
|
23
|
-
const tx = new Transaction();
|
|
24
|
-
|
|
25
|
-
tx.setSender('0x<your-address>');
|
|
26
|
-
tx.setGasPrice(referenceGasPrice);
|
|
27
|
-
tx.setGasBudget(50_000_000);
|
|
28
|
-
tx.setGasPayment([
|
|
29
|
-
{
|
|
30
|
-
objectId: '0x<gas-coin-object-id>',
|
|
31
|
-
version: '<object-version>',
|
|
32
|
-
digest: '<object-digest>',
|
|
33
|
-
},
|
|
34
|
-
]);
|
|
35
|
-
|
|
36
|
-
// Build the transaction without a client
|
|
37
|
-
const bytes = await tx.build();
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
## Object references
|
|
41
|
-
|
|
42
|
-
For objects used in your transaction, you must provide full object references using the `Inputs`
|
|
43
|
-
helper:
|
|
44
|
-
|
|
45
|
-
```tsx
|
|
46
|
-
|
|
47
|
-
// For owned or immutable objects
|
|
48
|
-
tx.object(
|
|
49
|
-
Inputs.ObjectRef({
|
|
50
|
-
objectId: '0x<object-id>',
|
|
51
|
-
version: '<object-version>',
|
|
52
|
-
digest: '<object-digest>',
|
|
53
|
-
}),
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
// For shared objects
|
|
57
|
-
tx.object(
|
|
58
|
-
Inputs.SharedObjectRef({
|
|
59
|
-
objectId: '0x<object-id>',
|
|
60
|
-
initialSharedVersion: '<initial-shared-version>',
|
|
61
|
-
mutable: true,
|
|
62
|
-
}),
|
|
63
|
-
);
|
|
64
|
-
|
|
65
|
-
// For receiving objects (objects being received by another object)
|
|
66
|
-
tx.object(
|
|
67
|
-
Inputs.ReceivingRef({
|
|
68
|
-
objectId: '0x<object-id>',
|
|
69
|
-
version: '<object-version>',
|
|
70
|
-
digest: '<object-digest>',
|
|
71
|
-
}),
|
|
72
|
-
);
|
|
73
|
-
```
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
# Sponsored Transactions
|
|
2
|
-
|
|
3
|
-
> Pay gas fees on behalf of other users with sponsored transactions.
|
|
4
|
-
|
|
5
|
-
The transaction builder can support sponsored transactions by using the `onlyTransactionKind` flag
|
|
6
|
-
when building the transaction.
|
|
7
|
-
|
|
8
|
-
```tsx
|
|
9
|
-
const tx = new Transaction();
|
|
10
|
-
|
|
11
|
-
// ... add some transactions...
|
|
12
|
-
|
|
13
|
-
const kindBytes = await tx.build({ provider, onlyTransactionKind: true });
|
|
14
|
-
|
|
15
|
-
// construct a sponsored transaction from the kind bytes
|
|
16
|
-
const sponsoredtx = Transaction.fromKind(kindBytes);
|
|
17
|
-
|
|
18
|
-
// you can now set the sponsored transaction data that is required
|
|
19
|
-
sponsoredtx.setSender(sender);
|
|
20
|
-
sponsoredtx.setGasOwner(sponsor);
|
|
21
|
-
sponsoredtx.setGasPayment(sponsorCoins);
|
|
22
|
-
```
|