Skip to content

Commit 2fa34cd

Browse files
authored
feat: refactor tests and environment (#4)
1 parent ef6bcf3 commit 2fa34cd

File tree

15 files changed

+711
-552
lines changed

15 files changed

+711
-552
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@ton/sandbox": "^0.27.0",
2828
"@ton/test-utils": "^0.4.2",
2929
"@ton/ton": "^13.9.0",
30+
"@tondevwallet/traces": "^0.1.6",
3031
"@types/jest": "^29.2.4",
3132
"@types/node": "^18.11.14",
3233
"@types/qs": "^6.9.7",

sources/contracts/ammPool.tact

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ message(0x178d4519) MintViaJettonTransferInternal {
4444
contract AmmPool(
4545
leftVault: Address,
4646
rightVault: Address, // To be deterministic, rightVault address must be greater than leftVault address
47-
leftSide: Int as uint256, // Should be deployed with 0
48-
rightSide: Int as uint256, // Should be deployed with 0
47+
leftSideReserve: Int as uint256, // Should be deployed with 0
48+
rightSideReserve: Int as uint256, // Should be deployed with 0
4949
// LP tokens-related fields
5050
totalSupply: Int as coins, // Should be deployed with 0
5151
) {
@@ -68,18 +68,20 @@ contract AmmPool(
6868
let leftSideReceived = msg.leftAmount;
6969
let rightSideReceived = msg.rightAmount;
7070
// Both sides are 0 in this case.
71-
if (self.leftSide == 0) {
72-
self.leftSide = msg.leftAmount;
73-
self.rightSide = msg.rightAmount;
71+
if (self.leftSideReserve == 0) {
72+
// TODO: check bug here
73+
self.leftSideReserve = msg.leftAmount;
74+
self.rightSideReserve = msg.rightAmount;
7475
} else {
7576
// Liquidity provide contract does not allow to add 0 tokens on one side.
7677
// Probably the rate has changed, so we must return extra tokens.
77-
if (msg.rightAmount / msg.leftAmount > self.rightSide / self.leftSide) {
78-
let expectedRightAmount = muldiv(msg.leftAmount, self.rightSide, self.leftSide);
78+
if (msg.rightAmount / msg.leftAmount > self.rightSideReserve / self.leftSideReserve) {
79+
let expectedRightAmount = muldiv(msg.leftAmount, self.rightSideReserve, self.leftSideReserve);
7980
rightSideReceived = expectedRightAmount;
8081
message(MessageParameters {
81-
value: 0,
82-
mode: SendRemainingValue,
82+
// TODO: think about the value here and in lp minting
83+
value: context().value / 2,
84+
mode: SendPayFwdFeesSeparately,
8385
to: self.rightVault,
8486
body: PayoutFromPool {
8587
otherVault: self.leftVault,
@@ -88,11 +90,11 @@ contract AmmPool(
8890
}.toCell(),
8991
});
9092
} else {
91-
let expectedLeftAmount = muldiv(msg.rightAmount, self.leftSide, self.rightSide);
93+
let expectedLeftAmount = muldiv(msg.rightAmount, self.leftSideReserve, self.rightSideReserve);
9294
leftSideReceived = expectedLeftAmount;
9395
message(MessageParameters {
94-
value: 0,
95-
mode: SendRemainingValue,
96+
value: context().value / 2,
97+
mode: SendPayFwdFeesSeparately,
9698
to: self.leftVault,
9799
body: PayoutFromPool {
98100
otherVault: self.rightVault,
@@ -103,7 +105,7 @@ contract AmmPool(
103105
}
104106
}
105107

106-
//TODO Consider checking overflow
108+
// TODO Consider checking overflow
107109
let liquidityTokensToMint = sqrt(leftSideReceived * rightSideReceived);
108110

109111
self.totalSupply += liquidityTokensToMint;
@@ -114,11 +116,11 @@ contract AmmPool(
114116

115117
self.totalSupply += liquidityTokensToMint;
116118

117-
//Mint LP tokens
119+
// Mint LP tokens, amm pool acts as the jetton minter for lp jettons
118120
deploy(DeployParameters {
119-
value: 0,
121+
value: context().value / 2 - 1,
120122
bounce: true,
121-
mode: SendRemainingValue,
123+
mode: SendPayFwdFeesSeparately,
122124
init: getJettonWalletInit(msg.depositor),
123125
body: MintViaJettonTransferInternal {
124126
queryId: 0,
@@ -138,8 +140,8 @@ contract AmmPool(
138140
// Workchain 0 is basechain
139141
require(sender.workchain == Basechain && sender.address == wallet.hash!!, "Invalid sender of JettonBurn notification");
140142

141-
let amountOfLeftToPay = muldiv(msg.amount, self.leftSide, self.totalSupply);
142-
let amountOfRightToPay = muldiv(msg.amount, self.rightSide, self.totalSupply);
143+
let amountOfLeftToPay = muldiv(msg.amount, self.leftSideReserve, self.totalSupply);
144+
let amountOfRightToPay = muldiv(msg.amount, self.rightSideReserve, self.totalSupply);
143145

144146
self.totalSupply -= msg.amount;
145147
let halfOfMsgValue = context().value / 2;
@@ -189,13 +191,13 @@ contract AmmPool(
189191
if (sender == self.leftVault) {
190192
inVault = self.leftVault;
191193
outVault = self.rightVault;
192-
inBalance = self.leftSide;
193-
outBalance = self.rightSide;
194+
inBalance = self.leftSideReserve;
195+
outBalance = self.rightSideReserve;
194196
} else {
195197
inVault = self.rightVault;
196198
outVault = self.leftVault;
197-
inBalance = self.rightSide;
198-
outBalance = self.leftSide;
199+
inBalance = self.rightSideReserve;
200+
outBalance = self.leftSideReserve;
199201
vaultOrderSwap = true;
200202
}
201203

@@ -237,20 +239,20 @@ contract AmmPool(
237239
});
238240

239241
if (vaultOrderSwap) {
240-
self.leftSide = newAmountOut;
241-
self.rightSide = newAmountIn;
242+
self.leftSideReserve = newAmountOut;
243+
self.rightSideReserve = newAmountIn;
242244
} else {
243-
self.leftSide = newAmountIn;
244-
self.rightSide = newAmountOut;
245+
self.leftSideReserve = newAmountIn;
246+
self.rightSideReserve = newAmountOut;
245247
}
246248
}
247249

248250
get fun getLeftSide(): Int {
249-
return self.leftSide;
251+
return self.leftSideReserve;
250252
}
251253

252254
get fun getRightSide(): Int {
253-
return self.rightSide;
255+
return self.rightSideReserve;
254256
}
255257

256258
get fun get_wallet_address(ownerAddress: Address): Address {
@@ -266,13 +268,13 @@ contract AmmPool(
266268
if (vaultAddress == self.leftVault) {
267269
inVault = self.leftVault;
268270
outVault = self.rightVault;
269-
inBalance = self.leftSide;
270-
outBalance = self.rightSide;
271+
inBalance = self.leftSideReserve;
272+
outBalance = self.rightSideReserve;
271273
} else {
272274
inVault = self.rightVault;
273275
outVault = self.leftVault;
274-
inBalance = self.rightSide;
275-
outBalance = self.leftSide;
276+
inBalance = self.rightSideReserve;
277+
outBalance = self.leftSideReserve;
276278
}
277279
let amountInWithFee = muldiv(amountIn, 1000 - self.PoolFee, 1000);
278280
let newAmountIn = inBalance + amountInWithFee;

sources/contracts/liquidityDeposit.tact

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ message(0xe7a3475f) PartHasBeenDeposited {
88
contract LiquidityDepositContract(
99
leftVault: Address, // To be deterministic, leftVault address must be less than rightVault address
1010
rightVault: Address,
11-
leftSide: Int as uint256,
12-
rightSide: Int as uint256,
11+
leftSideAmount: Int as uint256,
12+
rightSideAmount: Int as uint256,
1313
depositor: Address, // This and the next field are kind of salt, so several similar contracts can exist
1414
contractId: Int as uint64,
1515
status: Int as uint3, // Should be deployed with 0.
@@ -23,12 +23,12 @@ contract LiquidityDepositContract(
2323
require(sender == self.leftVault || sender == self.rightVault, "Sender must be a vault");
2424
if (sender == self.leftVault) {
2525
// TODO maybe here we should check that it is not already filled and revert on errors.
26-
require(msg.amount == self.leftSide, "Amount must be equal to leftSide");
26+
require(msg.amount == self.leftSideAmount, "Amount must be equal to leftSide");
2727
require(msg.depositor == self.depositor, "Depositor must be the same");
2828
self.status |= 1;
2929
} else {
3030
// TODO maybe here we should check that it is not already filled and revert on errors.
31-
require(msg.amount == self.rightSide, "Amount must be equal to rightSide");
31+
require(msg.amount == self.rightSideAmount, "Amount must be equal to rightSide");
3232
require(msg.depositor == self.depositor, "Depositor must be the same");
3333
self.status |= 2;
3434
}
@@ -39,8 +39,8 @@ contract LiquidityDepositContract(
3939
mode: SendRemainingBalance + SendDestroyIfZero, // We don't need to keep this contract alive
4040
init: initOf AmmPool(self.leftVault, self.rightVault, 0, 0, 0),
4141
body: LiquidityDeposit {
42-
leftAmount: self.leftSide,
43-
rightAmount: self.rightSide,
42+
leftAmount: self.leftSideAmount,
43+
rightAmount: self.rightSideAmount,
4444
depositor: self.depositor,
4545
contractId: self.contractId,
4646
}.toCell(),

sources/contracts/math.tact

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
struct SortedAddresses{
1+
struct SortedAddresses {
22
lower: Address;
33
higher: Address;
44
}
55

66
inline fun sortAddresses(a: Address, b: Address): SortedAddresses {
7-
if(parseStdAddress(a.asSlice()).address < parseStdAddress(b.asSlice()).address) {
8-
return SortedAddresses{lower: a, higher: b};
7+
if (parseStdAddress(a.asSlice()).address < parseStdAddress(b.asSlice()).address) {
8+
return SortedAddresses { lower: a, higher: b };
99
}
10-
return SortedAddresses{lower: b, higher: a};
10+
return SortedAddresses { lower: b, higher: a };
1111
}

sources/contracts/vanityContract.tact

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
message initVanityContract {
1+
message(0xedafb633) InitVanityContract {
22
code: Cell;
33
data: Cell;
44
}
55

66
contract VanityContract(
7-
codeDataHash: Int as uint256;
7+
codeDataHash: Int as uint256,
88
) {
9-
receive(msg: initVanityContract) {
9+
receive(msg: InitVanityContract) {
1010
require(msg.code.hash() ^ msg.data.hash() == self.codeDataHash, "Code or data not matched");
1111
setCode(msg.code);
1212
setData(msg.data);

sources/contracts/vaults/jettonVault.tact

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct JettonMasterState {
88
jettonMasterData: Cell?;
99
}
1010

11-
// TEP-74 JettonNotify
11+
// Compatible with TEP-74 JettonNotify, but with forwardPayload serialized as expected fields
1212
message(0x7362d09c) JettonNotifyWithActionRequest {
1313
queryId: Int as uint64;
1414
amount: Int as coins;
@@ -19,7 +19,7 @@ message(0x7362d09c) JettonNotifyWithActionRequest {
1919
actionPayload: Cell; // Obligatory ref
2020
}
2121

22-
// TEP-74 JettonTransfer
22+
// TEP-74 JettonTransfer with better DEX naming
2323
message(0xf8a7ea5) SendViaJettonTransfer {
2424
queryId: Int as uint64;
2525
amount: Int as coins;
@@ -40,6 +40,7 @@ contract JettonVault(
4040
if (!self.inited) {
4141
// Theoretically, data can be null, but I think it's not possible, so we won't accept such jettons
4242
if (msg.init.jettonMasterCode != null && msg.init.jettonMasterData != null) {
43+
// on-chain hack, dependent on the jetton minter state init
4344
let myJettonWallet = calculateJettonWallet(myAddress(), msg.init.jettonMasterData, msg.init.jettonMasterCode, self.jettonMaster);
4445

4546
if (myJettonWallet == sender() &&
@@ -67,8 +68,10 @@ contract JettonVault(
6768

6869
receive(msg: PayoutFromPool) {
6970
require(self.inited, "Jetton vault not inited");
71+
7072
let sortedAddresses = sortAddresses(myAddress(), msg.otherVault);
7173
let poolInit = initOf AmmPool(sortedAddresses.lower, sortedAddresses.higher, 0, 0, 0);
74+
7275
require(poolInit.hasSameBasechainAddress(sender()), "Sender must be pool");
7376

7477
message(MessageParameters {
@@ -82,6 +85,7 @@ contract JettonVault(
8285
destination: msg.receiver,
8386
forwardTonAmount: 1,
8487
forwardPayload: sliceWithOneZeroBit(),
88+
customPayload: null,
8589
}.toCell(),
8690
});
8791
}

sources/contracts/vaults/vaultInterface.tact

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ struct SwapRequest {
44
// Or we can specify ammPool address here, which I think is better
55
destinationVault: Address;
66
minAmountOut: Int as uint256;
7+
// amountIn is part of the jetton notification
78
timeout: Int as uint32;
89
}
910

sources/scripts/deploy.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {JettonVault} from "../output/DEX_JettonVault"
88
import {LiquidityDepositContract} from "../output/DEX_LiquidityDepositContract"
99
import {ExtendedJettonWallet} from "../wrappers/ExtendedJettonWallet"
1010
import {storeJettonTransfer} from "../output/Jetton_JettonMinter"
11-
import {createJettonVaultLiquidityDeposit} from "../utils/testUtils"
11+
import {createJettonVaultLiquidityDepositPayload} from "../utils/testUtils"
1212
import {AmmPool} from "../output/DEX_AmmPool"
1313

1414
const main = async () => {
@@ -158,7 +158,7 @@ const main = async () => {
158158
responseDestination: deployerWallet.address,
159159
customPayload: null,
160160
forwardTonAmount: toNano(0.5),
161-
forwardPayload: createJettonVaultLiquidityDeposit(
161+
forwardPayload: createJettonVaultLiquidityDepositPayload(
162162
LPprovider.address,
163163
jettonMinterACode!!,
164164
jettonMinterAData!!,
@@ -177,7 +177,7 @@ const main = async () => {
177177
responseDestination: deployerWallet.address,
178178
customPayload: null,
179179
forwardTonAmount: toNano(0.5),
180-
forwardPayload: createJettonVaultLiquidityDeposit(
180+
forwardPayload: createJettonVaultLiquidityDepositPayload(
181181
LPprovider.address,
182182
jettonMinterBCode!!,
183183
jettonMinterBData!!,

0 commit comments

Comments
 (0)