Skip to content

Commit 8aa8e85

Browse files
committed
contract init
1 parent 2635ec4 commit 8aa8e85

File tree

6 files changed

+150
-74
lines changed

6 files changed

+150
-74
lines changed

contracts/imports/helper.fc

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const slice zero_asset = "00000000000000000000000000000000"s;
2+
3+
cell fill_zeros(cell jetton_masters) {
4+
cell assets = new_dict();
5+
while (~ jetton_masters.dict_empty?()) {
6+
var(_, wallet_addr, _) = jetton_masters~dict::delete_get_min(267);
7+
assets~dict_set(267, wallet_addr, zero_asset);
8+
}
9+
return assets;
10+
}

contracts/multitoken_dex.tact

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

contracts/multitoken_dex.tact.txt

-42
This file was deleted.

package-lock.json

+18-25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
"name": "MulDEX",
33
"version": "0.0.1",
44
"scripts": {
5-
"test": "jest"
5+
"test": "jest",
6+
"build": "blueprint build"
67
},
78
"devDependencies": {
8-
"@ton-community/blueprint": "^0.10.0",
9+
"@ton-community/blueprint": "^0.12.0",
910
"@ton-community/sandbox": "^0.11.0",
10-
"@ton-community/test-utils": "^0.2.0",
11+
"@ton-community/test-utils": "^0.3.0",
1112
"@types/jest": "^29.5.0",
1213
"@types/node": "^20.2.5",
1314
"jest": "^29.5.0",
1415
"prettier": "^2.8.6",
15-
"ton": "^13.4.1",
16-
"ton-core": "^0.49.0",
16+
"ton": "^13.6.0",
17+
"ton-core": "^0.51.0",
1718
"ton-crypto": "^3.2.0",
1819
"ts-jest": "^29.0.5",
1920
"ts-node": "^10.9.1",

wrappers/MultitokenDex.compile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CompilerConfig } from '@ton-community/blueprint';
22

33
export const compile: CompilerConfig = {
4-
lang: 'func',
5-
targets: ['contracts/imports/stdlib.fc', 'contracts/multitoken_dex.fc'],
4+
lang: 'tact',
5+
target: 'contracts/multitoken_dex.tact'
66
};

0 commit comments

Comments
 (0)