|
| 1 | +import "./imports/helper.fc"; |
| 2 | + |
| 3 | +@name(muldiv) |
| 4 | +native mulDiv(a: Int, b: Int, c: Int): Int; |
| 5 | + |
| 6 | +@name(fill_zeros) |
| 7 | +native fillZeros(jetton_wallets: map<Address, Address>): map<Address, Int>; |
| 8 | + |
| 9 | +message DexDeploy { |
| 10 | + query_id: Int as uint64; |
| 11 | + jetton_wallets: map<Address, Address>; |
| 12 | + assets: map<Address, Int>; |
| 13 | +} |
| 14 | + |
| 15 | +message(0x7362d09c) TokenNotification { |
| 16 | + query_id: Int as uint64; |
| 17 | + amount: Int as coins; |
| 18 | + sender: Address; |
| 19 | + forward_payload: Slice as remaining; |
| 20 | +} |
| 21 | + |
| 22 | +message(0x0f8a7ea5) JettonTransfer { |
| 23 | + query_id: Int as uint64; |
| 24 | + amount: Int as coins; |
| 25 | + destination: Address; |
| 26 | + response_destination: Address; |
| 27 | + custom_payload: Cell? = null; |
| 28 | + forward_ton_amount: Int as coins; |
| 29 | + forward_payload: Slice as remaining; |
| 30 | +} |
| 31 | + |
| 32 | +struct SystemInfo { |
| 33 | + id: Int as uint64; |
| 34 | + jetton_wallets: map<Address, Address>; |
| 35 | + assets: map<Address, Int>; // better to set type coins to values, but tact does not support it now :C |
| 36 | + owner_address: Address; // owner is not actually needed for current implementation, but let it be |
| 37 | +} |
| 38 | + |
| 39 | +contract MultitokenDex { |
| 40 | + const min_storage: Int = ton("0.05"); |
| 41 | + const forward_ton_amount: Int = ton("0.000001"); |
| 42 | + id: Int as uint64; // to create more than one pool |
| 43 | + jetton_wallets: map<Address, Address>; |
| 44 | + assets: map<Address, Int>; |
| 45 | + owner_address: Address; // owner is not actually needed for current implementation, but let it be |
| 46 | + |
| 47 | + init(id: Int, owner: Address) { |
| 48 | + self.id = id; |
| 49 | + self.owner_address = owner; |
| 50 | + self.assets = emptyMap(); |
| 51 | + self.jetton_wallets = emptyMap(); |
| 52 | + } |
| 53 | + |
| 54 | + receive(msg: DexDeploy) { |
| 55 | + require(sender() == self.owner_address, "Invalid sender"); |
| 56 | + require(self.jetton_wallets.asCell() == null, "Already set"); |
| 57 | + self.jetton_wallets = msg.jetton_wallets; // some jettons do not support TEP-89 |
| 58 | + self.assets = fillZeros(msg.jetton_wallets); |
| 59 | + nativeReserve(self.min_storage, 2); |
| 60 | + self.notify(null); |
| 61 | + } |
| 62 | + |
| 63 | + receive(msg: TokenNotification) { |
| 64 | + // todo |
| 65 | + // let ctx: Context = context(); |
| 66 | + // let old_balance_src: Int? = self.assets.get(ctx.sender); |
| 67 | + // let received: Int = msg.amount; |
| 68 | + |
| 69 | + // // unknown token |
| 70 | + // if (old_balance_src == null) { |
| 71 | + // self.transferJettonTo(ctx.sender, self.owner_address, |
| 72 | + // received, msg.query_id, "Unknown original jetton"); |
| 73 | + // return; |
| 74 | + // } |
| 75 | + |
| 76 | + // // insufficient value to process token |
| 77 | + // if (ctx.value <= ton("0.4")) { |
| 78 | + // self.transferJettonTo(ctx.sender, msg.from, received, |
| 79 | + // msg.query_id, "Insufficient value to process token"); |
| 80 | + // return; |
| 81 | + // } |
| 82 | + |
| 83 | + // let swap: Swap = msg.forwardPayload % Swap; |
| 84 | + // let other_jw: Address = |
| 85 | + // self.jetton_wallets.get(swap.otherJettonMaster)!!; |
| 86 | + // let old_balance_dst: Int = self.assets.get(other_jw)!!; |
| 87 | + |
| 88 | + // let swap_value: Int = self.calcSwap(old_balance_src, |
| 89 | + // old_balance_dst, received); // simple func received_token1 / token_1_balance * token2_balance |
| 90 | + |
| 91 | + // self.transferJettonTo(other_jw, msg.sender, swap_value, msg.query_id, "Swap completed"); |
| 92 | + // self.assets.set(ctx.sender, old_balance_src + received); |
| 93 | + // self.assets.set(other_jw, old_balance_dst - swap_value); |
| 94 | + } |
| 95 | + |
| 96 | + fun transferJettonTo(jetton_wallet: Address, destination: Address, amount: Int, query_id: Int, message: String) { |
| 97 | + if (amount > 0) { |
| 98 | + send(SendParameters{ |
| 99 | + to: jetton_wallet, |
| 100 | + value: 0, |
| 101 | + mode: 64, |
| 102 | + body: JettonTransfer{query_id: query_id, amount: amount, destination: destination, response_destination: destination, custom_payload: message.asComment(), forward_ton_amount: self.forward_ton_amount, forward_payload: emptySlice()}.toCell() |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + fun calcSwap(balance_token1: Int, balance_token2: Int, received_token1: Int): Int { |
| 108 | + return mulDiv(received_token1, balance_token2, balance_token1); |
| 109 | + } |
| 110 | + |
| 111 | + get fun get_system_info(): SystemInfo { |
| 112 | + return SystemInfo{id: self.id, jetton_wallets: self.jetton_wallets, assets: self.assets, owner_address: self.owner_address}; |
| 113 | + } |
| 114 | +} |
0 commit comments