Skip to content

Commit 00236eb

Browse files
Maksandreashkuc
andauthored
SDK-2 docs (#172)
* add sponsoring docs * add token docs * add migration guide * minor fixes in SDK migration part * rename idOrAddress -> collectionId --------- Co-authored-by: ashkuc <[email protected]>
1 parent 0edb4eb commit 00236eb

File tree

7 files changed

+252
-17
lines changed

7 files changed

+252
-17
lines changed

docs/.vuepress/configs/sidebar.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export const sidebar: Record<string, SidebarConfig> = {
7373
'/build/sdk/examples-nesting.md',
7474
'/build/sdk/refungible.md',
7575
'/build/sdk/sponsoring.md',
76+
'/build/sdk/v2/migration.md'
7677
]
7778
},
7879
{

docs/build/sdk/v2/collections.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ await sdk.collection.setProperties({
103103
### Now, let's query our collection and check its properties
104104

105105
```ts:no-line-numbers
106-
const collection = await sdk.collection.get({idOrAddress: result.collectionId});
106+
const collection = await sdk.collection.get({collectionId: result.collectionId});
107107
108108
console.log(collection.properties);
109109
```
@@ -178,7 +178,7 @@ Every NFT token in the collection above could have three properties:
178178
The SDK also specifies some additional token properties related to Unique Schema. Let's check them.
179179

180180
```ts:no-line-numbers
181-
const collection = await sdk.collection.get({idOrAddress: collectionId})
181+
const collection = await sdk.collection.get({collectionId})
182182
183183
console.log(collection.tokenPropertyPermissions);
184184
```

docs/build/sdk/v2/migration.md

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# Migration from SDK V1 to SDK V2
2+
3+
This guide provides instructions on migrating from SDK V1 to SDK V2, highlighting key changes and improvements.
4+
5+
6+
## Working with Balances
7+
8+
The method signature for retrieving balances has remained the same.
9+
10+
```ts
11+
const alice = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY";
12+
13+
// V1
14+
const balanceV1 = await sdkV1.balance.get({ address: alice });
15+
16+
// V2
17+
const balanceV2 = await sdkV2.balance.get({ address: alice });
18+
```
19+
20+
However, the format of the returned balance object has been simplified. All balances are now returned in raw format, leaving it to the application to format the value as needed.
21+
22+
23+
```ts
24+
// V1
25+
console.log(balanceV1.availableBalance.amount); // 6994.40616
26+
27+
// V2
28+
console.log(balanceV2.available); // 6994406160000000000000
29+
```
30+
31+
For token transfers, SDK V2 requires the amount to be specified in raw format instead of a formatted number.
32+
33+
34+
```ts
35+
// V1
36+
sdkV1.balance.transfer({ amount: 2.3, destination: alice });
37+
38+
// V2
39+
sdkV2.balance.transfer({ amount: "2300000000000000000", to: alice });
40+
41+
// or
42+
sdkV2.balance.transfer({ amount: 2.3, to: alice, isAmountInCoins: true });
43+
```
44+
45+
## Working with Collections and Tokens
46+
47+
48+
### Reading Collections
49+
50+
There are only minor changes in how collections are retrieved.
51+
52+
```ts
53+
// V1
54+
const collectionV1 = await sdkV1.collection.get({ collectionId: 665 });
55+
56+
// V2
57+
const collectionV2 = await sdkV2.collection.get({ collectionId: 665 });
58+
59+
// or collection may be retrieved by address
60+
const collectionV2_ = await sdkV2.collection.get({ collectionId: '0x17C4E6453cc49aaAAeaCA894E6D9683E00000299' });
61+
62+
```
63+
64+
### Reading Tokens
65+
66+
The method signature for retrieving tokens has no changes.
67+
68+
```ts
69+
// V1
70+
const tokenV1 = await sdkV1.token.get({ collectionId: 1, tokenId: 1 });
71+
72+
// V2
73+
const tokenV2 = await sdkV2.token.get({ collectionId: 1, tokenId: 1 });
74+
75+
// collectionId may be replaced with collection address
76+
const tokenV2_ = await sdkV2.token.get({ collectionId: '0x17C4e6453cC49AAaaEaCA894E6D9683e00000001', tokenId: 1 });
77+
```
78+
79+
However, the response format has changed significantly. SDK V2 returns attributes in the Unique Schema V2 format, even for tokens created using Unique Schema V1.
80+
81+
82+
Attributes in SDK V1:
83+
84+
85+
```
86+
"0": {
87+
name: {
88+
_: "gender",
89+
},
90+
value: {
91+
en: "Female",
92+
_: "Female",
93+
},
94+
isArray: false,
95+
type: "string",
96+
rawValue: 0,
97+
isEnum: true,
98+
},
99+
"1": { ... }
100+
```
101+
102+
Attributes in SDK V2:
103+
104+
```
105+
[
106+
{
107+
trait_type: "gender",
108+
value: "Female",
109+
},
110+
{ ... }
111+
]
112+
```
113+
114+
### Extrinsics
115+
116+
The extrinsic methods for transferring, burning, and nesting tokens remain unchanged.
117+
118+
119+
## What If the Application Create Tokens in SDK-V1 Collections
120+
121+
If your application creates tokens in collections originally created with SDK-V1, you should continue using SDK-V1 for this purpose.
122+
123+
However, if your application also reads collections and tokens created by other users, it is recommended to use V2 methods for reading. This ensures that attributes are displayed correctly for new collections and tokens.
124+
125+
126+
```ts
127+
// Getting collections
128+
sdkV1.collection.get({ collectionId: 1 }); // before
129+
sdkV1.collection.getV2({ collectionId: 1 }); // after
130+
131+
// Getting tokens
132+
sdkV1.token.get({ collectionId: 1, tokenId: 1 }); // before
133+
sdkV1.token.getV2({ collectionId: 1, tokenId: 1 }); // after
134+
```
135+

docs/build/sdk/v2/quick-start.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ yarn add @unique-nft/sdk@alpha @unique-nft/sr25519
3838

3939
To begin using the Unique SDK, you need to import the required modules, set the base URL for the API, and optionally configure the default signer account.
4040

41+
You can find the list of public endpoints in the [reference section](../../../reference/sdk-endpoints.md).
42+
4143
<!-- TODO set production baseUrl -->
4244
```typescript:no-line-numbers
4345
import { UniqueChain } from "@unique-nft/sdk";

docs/build/sdk/v2/sponsoring.md

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ You also need to know how to [mint and transfer NFTs](./tokens.md).
2222
The process consists of two steps:
2323

2424
1. Set collection sponsor – only the collection owner or admin can do
25-
2. Confirm sponsorship. The sponsor should confirm willingness to sponsor collection
25+
2. Confirm sponsorship. The sponsor should confirm willingness to sponsor a collection
2626

2727
```ts:no-line-numbers
2828
// At this point we assume you already have a minted collection and NFT
@@ -34,19 +34,57 @@ await sdk.collection.confirmSponsorship({collectionId});
3434
```
3535

3636
At this point, every action with tokens of this collection, such as transfer or minting, will be sponsored by `account`.
37-
<!--
38-
TODO
39-
Let's check that an account without native tokens can perform NFT transfer.
37+
38+
## Setting limits
39+
40+
You may want to set limits on sponsorship to prevent your sponsorship account from being drained. We already discussed how to set limits in the [collection section](collections.md#collection-limits). Let's take a closet look on sponsorship-related limits.
41+
42+
<!-- TODO setVariableMetadata is not the case anymore -->
43+
44+
```ts:no-line-numbers
45+
await sdk.collection.create({
46+
name: "Test",
47+
description: "Test collection",
48+
symbol: "TST",
49+
limits: {
50+
// The time interval in blocks defines once per how long a non-privileged user approve transaction can be sponsored.
51+
sponsorApproveTimeout: 100,
52+
// Defines how many blocks need to pass between setVariableMetadata transactions in order for them to be sponsored
53+
sponsoredDataRateLimit: 100,
54+
// The maximum byte size of custom token data that can be sponsored when tokens are minted in sponsored mode
55+
sponsoredDataSize: 2048,
56+
// The time interval in blocks that defines once per how long a non-privileged user transfer or mint transaction can be sponsored
57+
sponsorTransferTimeout: 0,
58+
},
59+
});
60+
```
61+
62+
## Let's check that an account without native tokens can transfer NFT
4063

4164
```ts:no-line-numbers
4265
// generate a new account without OPL
4366
const emptyAccount = Sr25519Account.fromUri(Sr25519Account.generateMnemonic());
4467
45-
// mint new token to `emptyAccount`
46-
const {result} = await sdk.token.mintNFTs({collectionId, tokens: [
68+
// mint new token directly to `emptyAccount`
69+
const tokensCreation = await sdk.token.mintNFTs({collectionId, tokens: [
4770
{owner: account.address}
4871
]});
49-
50-
51-
52-
``` -->
72+
const [token] = tokensCreation.result;
73+
74+
// emptyAccount makes transfer
75+
await sdk.token.transfer(
76+
{
77+
to: alice.address,
78+
collectionId,
79+
tokenId: token.tokenId,
80+
},
81+
{ signerAddress: emptyAccount.address },
82+
emptyAccount,
83+
);
84+
85+
// Check alice is the new owner
86+
const { owner } = await sdk.token.get({
87+
collectionId: collectionId,
88+
tokenId: token.tokenId,
89+
});
90+
```

docs/build/sdk/v2/tokens.md

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Now, let's have a look at the newly created token.
118118

119119
```ts:no-line-numbers
120120
const nft = await sdk.token.get({
121-
collectionIdOrAddress: result.collectionId,
121+
collectionId: result.collectionId,
122122
tokenId: 1
123123
});
124124
@@ -148,7 +148,7 @@ Let's make a quick recap of how it can be done. Below, we set mutability for tok
148148
await sdk.collection.create({
149149
...
150150
tokenPropertyPermissions: [
151-
// This is how we specify token properties mutability during the collection creation
151+
// This is how we specify token properties' mutability during the collection creation
152152
{key: 'A', permission: {mutable: true, collectionAdmin: true, tokenOwner: true}},
153153
154154
...
@@ -164,6 +164,16 @@ await sdk.token.setProperties({
164164
});
165165
```
166166

167+
As far as deleted:
168+
169+
```ts:no-line-numbers
170+
await sdk.token.deleteProperties({
171+
collectionId,
172+
tokenId,
173+
keys: ['A'],
174+
});
175+
```
176+
167177
Attributes are part of `tokenData` property which is by default mutable for collection admin. You can override it during the collection creation.
168178

169179
The SDK provides the following method for attribute mutation:
@@ -206,6 +216,13 @@ const approvalTx = await sdk.token.approve({
206216
spender: alice.address,
207217
});
208218
219+
// Let's check token is approved
220+
const { isApproved } = await sdk.token.getApproved({
221+
collectionId,
222+
tokenId,
223+
spender: alice.address,
224+
});
225+
209226
// Now, Alice can transfer approved token
210227
const transferFromTx = await sdk.token.transfer(
211228
{
@@ -217,7 +234,7 @@ const transferFromTx = await sdk.token.transfer(
217234
{
218235
signerAddress: alice.address,
219236
},
220-
// This transaction performed by Alice
237+
// This transaction made by Alice
221238
alice,
222239
);
223240
```
@@ -233,6 +250,31 @@ await sdk.token.burn({
233250
});
234251
```
235252

253+
If token is approved for account this account can be burn token from:
254+
255+
```ts:no-line-numbers
256+
// Approve token for alice
257+
const approvalTx = await sdk.token.approve({
258+
collectionId,
259+
tokenId,
260+
spender: alice.address,
261+
});
262+
263+
// alice burns token directly without transfer
264+
await sdk.token.burn(
265+
{
266+
collectionId,
267+
tokenId: token1.tokenId,
268+
amount: 1,
269+
from: account.address,
270+
},
271+
{ signerAddress: alice.address },
272+
alice,
273+
);
274+
```
275+
276+
277+
236278
<!-- TODO add burn from docs -->
237279

238280
## Nesting
@@ -254,6 +296,15 @@ In the example above, `token1` will be nested to `token2`. This means:
254296
- Topmost token owner (real owner) of `token2` will be the owner of `token1`
255297
- if `token2` is transferred to a different account, this new account becomes the topmost owner for `token1`
256298

299+
Let's get token's topmost owner:
300+
301+
```ts:no-line-numbers
302+
const { topmostOwner } = await sdk.token.get({
303+
collectionId: collectionId,
304+
tokenId: token1.tokenId,
305+
});
306+
```
307+
257308
The topmost token owner can `unnest` tokens. In the example below, `token1` will be transferred from the `token2` address back to the topmost owner.
258309

259310
```ts:no-line-numbers
@@ -267,5 +318,3 @@ In Unique Network every collection and token have unique ID. At the same time th
267318
The concept of collections and token addresses is particularly useful when working with [smart contracts](../../evm/index.md).
268319
:::
269320

270-
<!-- TODO Add docs regarding getting topmostOwner -->
271-

docs/reference/sdk-endpoints.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Public endpoints
22

3+
## SDK-V2
4+
5+
| | Endpoint |
6+
|----------|----------|
7+
| Unique | https://rest.unique.network/v2/unique |
8+
| Quartz | https://rest.unique.network/v2/quartz |
9+
| Opal | https://rest.unique.network/v2/opal |
10+
11+
## SDK-V1
12+
313
| | Endpoint |
414
|----------|----------|
515
| Unique | https://rest.unique.network/unique/v1 |

0 commit comments

Comments
 (0)