Skip to content

Commit 2f4de53

Browse files
Merge pull request #5 from nonam3e/master
Write correct deploy wrapper issue
2 parents 2635ec4 + e6f2fa0 commit 2f4de53

File tree

9 files changed

+174
-88
lines changed

9 files changed

+174
-88
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
temp
33
build
4+
.env

contracts/imports/helper.fc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
slice tact_fill_zeros() asm "x{0000000000000000000000000000000000000000000000000000000000000000} b{0} |+ PUSHSLICE";
2+
3+
cell fill_zeros(cell jetton_masters) {
4+
cell assets = new_dict();
5+
slice zero_cs = tact_fill_zeros();
6+
while (~ jetton_masters.dict_empty?()) {
7+
var(_, wallet_addr, _) = jetton_masters~dict::delete_get_min(267);
8+
assets~dict_set(267, wallet_addr, zero_cs);
9+
}
10+
return assets;
11+
}

contracts/multitoken_dex.tact

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

contracts/multitoken_dex.tact.txt

Lines changed: 0 additions & 42 deletions
This file was deleted.

package-lock.json

Lines changed: 18 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 5 deletions
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",

scripts/deployMultitokenDex.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
1-
import { toNano } from 'ton-core';
2-
import { MultitokenDex } from '../wrappers/MultitokenDex';
1+
import { Address, Cell, Dictionary, toNano } from 'ton-core';
2+
import { MultitokenDex, sendDeploy } from '../wrappers/MultitokenDex';
33
import { NetworkProvider } from '@ton-community/blueprint';
4+
import { inspect } from 'util';
45

56
export async function run(provider: NetworkProvider) {
6-
const multitokenDex = provider.open(await MultitokenDex.fromInit());
7+
const multitokenDex = provider.open(await MultitokenDex.fromInit(BigInt(1), provider.sender().address!));
78

8-
await multitokenDex.send(
9-
provider.sender(),
10-
{
11-
value: toNano('0.05'),
12-
},
13-
{
14-
$$type: 'Deploy',
15-
queryId: 0n,
16-
}
17-
);
189

19-
await provider.waitForDeploy(multitokenDex.address);
10+
await sendDeploy(multitokenDex, provider, ["EQAMcImLBgZHazWmradz51pI0uHZwvxMONlMQy0QwQTQInD5", "kQAbDiNBKXDn5l3AE8cx-j7zZneFITRRFM-FH-HfBJqsrTLh"].map(elem => Address.parse(elem)))
2011

12+
await provider.waitForDeploy(multitokenDex.address);
2113
// run methods on `multitokenDex`
2214
}

wrappers/MultitokenDex.compile.ts

Lines changed: 2 additions & 2 deletions
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
};

wrappers/MultitokenDex.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1+
import { Address, Dictionary, OpenedContract, TupleBuilder, toNano } from 'ton-core';
2+
import { MultitokenDex } from '../build/MultitokenDex/tact_MultitokenDex';
3+
import { NetworkProvider } from '@ton-community/blueprint';
4+
15
export * from '../build/MultitokenDex/tact_MultitokenDex';
6+
7+
export async function sendDeploy(contract: OpenedContract<MultitokenDex>, provider: NetworkProvider, masters: Address[]) {
8+
let dict: Dictionary<Address, Address> = Dictionary.empty();
9+
let tuple = new TupleBuilder();
10+
tuple.writeAddress(contract.address);
11+
for (let item of masters) {
12+
dict.set(item, (await provider.provider(item).get("get_wallet_address", tuple.build())).stack.readAddress());
13+
}
14+
await contract.send(provider.sender(), {value: toNano("0.1"), bounce: false}, {$$type: 'DexDeploy', query_id: 12n, jetton_wallets: dict});
15+
}

0 commit comments

Comments
 (0)