|
1 | 1 | # DeDust.io
|
2 | 2 |
|
3 |
| -import { Callout, Steps, Tabs } from 'nextra/components' |
| 3 | +import { Callout } from 'nextra-theme-docs'; |
4 | 4 |
|
5 | 5 | {/* See: https://nextra.site/docs/guide/built-ins */}
|
6 | 6 |
|
7 | 7 | [DeDust](https://dedust.io) is a decentralized exchange (DEX) and automated market maker (AMM) built natively on [TON Blockchain](https://ton.org) and [DeDust Protocol 2.0](https://docs.dedust.io/reference/tlb-schemes). DeDust is designed with a meticulous attention to user experience (UX), gas efficiency, and extensibility.
|
8 | 8 |
|
9 |
| -<Callout emoji="🚨"> |
| 9 | +## Swaps |
10 | 10 |
|
11 |
| - This page is a stub until it gets new content in [#146](https://github.com/tact-lang/tact-docs/issues/146). |
| 11 | +You can read more about swaps on [DeDust documentation](https://docs.dedust.io/docs/swaps). |
| 12 | + |
| 13 | +<Callout type="warning" emoji="⚠️"> |
| 14 | + |
| 15 | + It's important to ensure that contracts are deployed. |
| 16 | + Sending funds to an inactive contract could result in irretrievable loss. |
12 | 17 |
|
13 | 18 | </Callout>
|
| 19 | + |
| 20 | +All kinds of swaps shares same structures: |
| 21 | + |
| 22 | +```tact |
| 23 | +struct SwapStep { |
| 24 | + poolAddress: Address; |
| 25 | + reserved: Cell? = null; // NULL always, reserved. |
| 26 | + limit: Int as coins = 0; |
| 27 | + nextStep: Cell?; // should be `SwapStep`, but recursive types not supported in tact |
| 28 | +} |
| 29 | +
|
| 30 | +struct SwapParams { |
| 31 | + deadline: Int as uint32 = 0; |
| 32 | + recipientAddress: Address? = null; |
| 33 | + referralAddress: Address? = null; |
| 34 | + fulfillPayload: Cell? = null; |
| 35 | + rejectPayload: Cell? = null; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Swapping native coin |
| 40 | + |
| 41 | +```tact |
| 42 | +message(0xea06185d) NativeSwap { |
| 43 | + queryId: Int as uint64 = 0; |
| 44 | + amount: Int as coins; // TON amount for the swap |
| 45 | + poolAddress: Address; |
| 46 | + reserved: Cell? = null; // NULL always, reserved. |
| 47 | + limit: Int as coins = 0; |
| 48 | + nextStep: SwapStep? = null; |
| 49 | + swapParams: SwapParams; |
| 50 | +} |
| 51 | +
|
| 52 | +// address of ton vault to send message |
| 53 | +const TON_VAULT_ADDRESS: Address = address("EQDa4VOnTYlLvDJ0gZjNYm5PXfSmmtL6Vs6A_CZEtXCNICq_"); |
| 54 | +
|
| 55 | +// address of pool to swap. Pool is pair like TON/USDT |
| 56 | +const POOL_ADDRESS: Address = address("EQCY5ufIEA-wdv1L5Zw09OMDHywwHsqLKEAUBYtngwNnnal4"); |
| 57 | +
|
| 58 | +const TON_SWAP_GAS_AMOUNT: Int = ton("0.2"); |
| 59 | +
|
| 60 | +fun makeTonSwap() { |
| 61 | + let swapParams = SwapParams{ |
| 62 | + deadline: 0, |
| 63 | + recipientAddress: null, |
| 64 | + referralAddress: null, |
| 65 | + fulfillPayload: null, |
| 66 | + rejectPayload: null, |
| 67 | + }; |
| 68 | +
|
| 69 | + // ton amount to swap in nanotons |
| 70 | + let swapAmount = ...; |
| 71 | +
|
| 72 | + send(SendParameters{ |
| 73 | + to: TON_VAULT_ADDRESS, |
| 74 | + value: swapAmount + TON_SWAP_GAS_AMOUNT, |
| 75 | + body: NativeSwap{ |
| 76 | + queryId: 0, |
| 77 | + amount: swapAmount, |
| 78 | + poolAddress: POOL_ADDRESS, |
| 79 | + reserved: null, |
| 80 | + limit: 0, |
| 81 | + nextStep: null, |
| 82 | + swapParams, |
| 83 | + }.toCell() |
| 84 | + }); |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | + |
| 89 | +### Swapping jetton |
| 90 | + |
| 91 | +```tact |
| 92 | +message(0xf8a7ea5) TokenTransfer { |
| 93 | + queryId: Int as uint64; |
| 94 | + amount: Int as coins; |
| 95 | + destination: Address; |
| 96 | + responseDestination: Address; |
| 97 | + customPayload: Cell?; |
| 98 | + forwardTonAmount: Int as coins; |
| 99 | + forwardPayload: Cell?; |
| 100 | +} |
| 101 | +
|
| 102 | +message(0xe3a0d482) JettonSwapPayload { |
| 103 | + poolAddress: Address; |
| 104 | + reserved: Cell? = null; // NULL always, reserved. |
| 105 | + limit: Int as coins = 0; |
| 106 | + nextStep: SwapStep? = null; |
| 107 | + swapParams: SwapParams; |
| 108 | +} |
| 109 | +
|
| 110 | +// address of jetton master you want to swap |
| 111 | +const JETTON_MASTER_ADDRESS: Address = address("kQCxBOJjV31OIAa0NN9JbaYUp6OXOahcFCipuYnsujx1MDAY"); |
| 112 | +
|
| 113 | +// address of jetton vault to send message |
| 114 | +const JETTON_VAULT_ADDRESS: Address = address("EQCgne1aW1CmkokSeRjMIREmJWbCKFbJxxkRCvtLDiheTTF9"); |
| 115 | +
|
| 116 | +// address of pool to swap. Pool is pair like TON/USDT |
| 117 | +const POOL_ADDRESS: Address = address("EQCY5ufIEA-wdv1L5Zw09OMDHywwHsqLKEAUBYtngwNnnal4"); |
| 118 | +
|
| 119 | +const JETTON_SWAP_GAS_AMOUNT: Int = ton("0.3"); |
| 120 | +const JETTON_SWAP_FWD_AMOUNT: Int = ton("0.25"); |
| 121 | +
|
| 122 | +fun makeJettonSwap() { |
| 123 | + let swapParams = SwapParams{ |
| 124 | + deadline: 0, |
| 125 | + recipientAddress: null, |
| 126 | + referralAddress: null, |
| 127 | + fulfillPayload: null, |
| 128 | + rejectPayload: null, |
| 129 | + }; |
| 130 | +
|
| 131 | + let myJettonWalletAddress = ...; // calculate wallet address of jetton you want to swap |
| 132 | + let swapAmount = ...; // jetton amount for swap |
| 133 | +
|
| 134 | + send(SendParameters{ |
| 135 | + to: myJettonWalletAddress, |
| 136 | + value: JETTON_SWAP_GAS_AMOUNT, |
| 137 | + body: TokenTransfer{ |
| 138 | + queryId: 0, |
| 139 | + amount: swapAmount, |
| 140 | + destination: JETTON_VAULT_ADDRESS, |
| 141 | + responseDestination: myAddress(), |
| 142 | + customPayload: null, |
| 143 | + forwardTonAmount: JETTON_SWAP_FWD_AMOUNT, |
| 144 | + forwardPayload: JettonSwapPayload{ |
| 145 | + poolAddress: POOL_ADDRESS, |
| 146 | + reserved: null, |
| 147 | + limit: 0, |
| 148 | + nextStep: null, |
| 149 | + swapParams, |
| 150 | + }.toCell() |
| 151 | + }.toCell() |
| 152 | + }); |
| 153 | +} |
| 154 | +``` |
0 commit comments