@@ -44,8 +44,8 @@ message(0x178d4519) MintViaJettonTransferInternal {
4444contract 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 ;
0 commit comments