Skip to content

Commit

Permalink
feat: add examples with assets sdk for message builders
Browse files Browse the repository at this point in the history
  • Loading branch information
mlikhtar committed Jul 15, 2024
1 parent 71d5e2b commit 5d338c1
Showing 1 changed file with 179 additions and 1 deletion.
180 changes: 179 additions & 1 deletion docs/develop/dapps/ton-connect/message-builders.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Examples created based on @tonconnect/sdk and @tonconnect/ui:

```js
import { useTonConnectUI } from '@tonconnect/ui-react';
const [tonConnectUI] = useTonConnectUI();

const transaction = {
//transaction body
Expand Down Expand Up @@ -268,6 +267,14 @@ await connector.sendTransaction({

The `body` for Jetton Transfer([TEP-74](https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md#1-transfer)) typically should be done according the following way:

:::info
You can use `assets-sdk` library with the methods out of the box (even with `ton-connect`)
:::


<Tabs groupId="Jetton Transfer">
<TabItem value="@ton/ton" label="@ton/ton">

```js
import { beginCell, toNano, Address } from '@ton/ton'
// transfer#0f8a7ea5 query_id:uint64 amount:(VarUInteger 16) destination:MsgAddress
Expand All @@ -287,6 +294,7 @@ The `body` for Jetton Transfer([TEP-74](https://github.com/ton-blockchain/TEPs/b
.endCell();
```


Next, sending the transaction with this body to sender's jettonWalletContract executed:

<Tabs groupId="Jetton Transfer">
Expand Down Expand Up @@ -415,12 +423,48 @@ async function main() {
```

</details>
</TabItem>
<TabItem value="assets/sdk" label="assets/sdk">

:::tip
Note: For the browser, you need to set a polyfill for `Buffer`.
:::
For more examples check [documentation](https://github.com/ton-community/assets-sdk)

```js
const NETWORK = "testnet";
const api = await createApi(NETWORK);

// https://github.com/ton-community/assets-sdk/blob/main/examples/use-tonconnect.ts
const provider = new TonConnectUI();
const sender = new TonConnectProvider(provider);

const storage: PinataStorageParams = {
pinataApiKey: process.env.PINATA_API_KEY!,
pinataSecretKey: process.env.PINATA_SECRET!,
};

const sdk = AssetsSDK.create({
api,
storage,
sender,
});

const jetton = sdk.openJettonWallet(Address.parse("JETTON_ADDRESS"));
const RECEIVER_ADDRESS = Address.parse("RECIEVER_ADDRESS");

jetton.send(sender, RECEIVER_ADDRESS, toNano(10));
```

</TabItem>
</Tabs>


### Jetton Transfer with Comment

<Tabs groupId="Jetton Transfer with Comment">
<TabItem value="@ton/ton" label="@ton/ton">

The `messageBody` for Jetton Transfer([TEP-74](https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md#1-transfer)) with comment we should additionally to the regular transfer `body` serialize comment and pack this in the `forwardPayload`:

```js
Expand Down Expand Up @@ -582,10 +626,51 @@ Next, sending the transaction with this body to sender's jettonWalletContract ex
```

</details>
</TabItem>
<TabItem value="assets/sdk" label="assets/sdk">

:::tip
Note: For the browser, you need to set a polyfill for `Buffer`.
:::
For more examples check [documentation](https://github.com/ton-community/assets-sdk)

```js
const NETWORK = "testnet";
const api = await createApi(NETWORK);

// https://github.com/ton-community/assets-sdk/blob/main/examples/use-tonconnect.ts
const provider = new TonConnectUI();
const sender = new TonConnectProvider(provider);

const storage: PinataStorageParams = {
pinataApiKey: process.env.PINATA_API_KEY!,
pinataSecretKey: process.env.PINATA_SECRET!,
};

const sdk = AssetsSDK.create({
api,
storage,
sender,
});

const jetton = sdk.openJettonWallet(Address.parse("JETTON_ADDRESS"));

const forwardPayload = beginCell()
.storeUint(0, 32) // 0 opcode means we have a comment
.storeStringTail('Hello, TON!')
.endCell();

jetton.send(sender, RECEIVER_ADDRESS, toNano(10), { notify: { payload: forwardPayload } });
```

</TabItem>
</Tabs>

### Jetton Burn


<Tabs groupId="Jetton Burn">
<TabItem value="@ton/ton" label="@ton/ton">
The `body` for Jetton Burn([TEP-74](https://github.com/ton-blockchain/TEPs/blob/master/text/0074-jettons-standard.md#2-burn)) typically should be done according the following way:


Expand Down Expand Up @@ -685,8 +770,45 @@ await connector.sendTransaction({
- `amount` - Integer, amount of Toncoin for gas payments in nanotons.
- `body` - payload for the jetton wallet with the `burn#595f07bc` op code

</TabItem>
<TabItem value="assets/sdk" label="assets/sdk">

:::tip
Note: For the browser, you need to set a polyfill for `Buffer`.
:::
For more examples check [documentation](https://github.com/ton-community/assets-sdk)

```js
const NETWORK = "testnet";
const api = await createApi(NETWORK);

// https://github.com/ton-community/assets-sdk/blob/main/examples/use-tonconnect.ts
const provider = new TonConnectUI();
const sender = new TonConnectProvider(provider);

const storage: PinataStorageParams = {
pinataApiKey: process.env.PINATA_API_KEY!,
pinataSecretKey: process.env.PINATA_SECRET!,
};

const sdk = AssetsSDK.create({
api,
storage,
sender,
});

const jetton = sdk.openJettonWallet(Address.parse("JETTON_ADDRESS"));

jetton.sendBurn(sender, RECEIVER_ADDRESS, toNano(10));
```

</TabItem>
</Tabs>

### NFT Transfer

<Tabs groupId="NFT Transfer">
<TabItem value="@ton/ton" label="@ton/ton">
The `body` message typically should be done according the following way:

```js
Expand Down Expand Up @@ -786,6 +908,43 @@ await connector.sendTransaction({
- `balance` - Integer, amount of Toncoin for gas payments in nanotons.
- `body` - payload for the NFT contract

</TabItem>

<TabItem value="assets/sdk" label="assets/sdk">

:::tip
Note: For the browser, you need to set a polyfill for `Buffer`.
:::
For more examples check [documentation](https://github.com/ton-community/assets-sdk)

```js
const NETWORK = "testnet";
const api = await createApi(NETWORK);

// https://github.com/ton-community/assets-sdk/blob/main/examples/use-tonconnect.ts
const provider = new TonConnectUI();
const sender = new TonConnectProvider(provider);

const storage: PinataStorageParams = {
pinataApiKey: process.env.PINATA_API_KEY!,
pinataSecretKey: process.env.PINATA_SECRET!,
};

const sdk = AssetsSDK.create({
api,
storage,
sender,
});

const nft = sdk.openNftItem(Address.parse("NFT_ADDRESS"));
const RECEIVER_ADDRESS = Address.parse("RECIEVER_ADDRESS");

nft.send(sender, RECEIVER_ADDRESS);
```

</TabItem>
</Tabs>

### NFT Sale (GetGems)

Here is an example of preparing message and transaction for sale on GetGems marketplace, according to contract [nft-fixprice-sale-v3r2](https://github.com/getgems-io/nft-contracts/blob/main/packages/contracts/sources/nft-fixprice-sale-v3r2.fc).
Expand Down Expand Up @@ -956,6 +1115,9 @@ await connector.sendTransaction({

### NFT Buy (GetGems)

<Tabs groupId="NFT Buy tabs">
<TabItem value="@ton/ton" label="@ton/ton">

The process of buy NFT for [nft-fixprice-sale-v3r2](https://github.com/getgems-io/nft-contracts/blob/main/packages/contracts/sources/nft-fixprice-sale-v3r2.fc) sale contract could be carry out with regular transfer without payload, the only important thing is accurate TON amount, that calculates as follows:
`buyAmount = Nftprice TON + 1.0 TON`.

Expand Down Expand Up @@ -1030,6 +1192,22 @@ messages: [
</TabItem>
</Tabs>

</TabItem>
<TabItem value="assets/sdk" label="assets/sdk">

:::tip
Note: For the browser, you need to set a polyfill for `Buffer`.
:::
For more examples check [documentation](https://github.com/ton-community/assets-sdk)

```js
const nft = sdk.openNftSale(Address.parse("NFT_ADDRESS"));
nft.sendBuy(sdk.sender!, { queryId: BigInt(1) })
```

</TabItem>
</Tabs>

## TON Connect Python SDK

Python examples are using [PyTonConnect](https://github.com/XaBbl4/pytonconnect) and [pytoniq](https://github.com/yungwine/pytoniq).
Expand Down

0 comments on commit 5d338c1

Please sign in to comment.