@@ -104,17 +104,26 @@ func (validator *balanceValidator) CheckToken(ctx context.Context, ethToken comm
104104 return err
105105 }
106106
107+ // Convert ethAmount (ETH decimals) to KDA decimals for comparison
108+ // Since KC balances are tracked in KDA decimals, we need to convert ETH amount to match
109+ ethAmountInKdaDecimals , err := validator .kcClient .ConvertEthToKdaAmount (ctx , kdaToken , ethAmount )
110+ if err != nil {
111+ return err
112+ }
113+
107114 validator .log .Debug ("balanceValidator.CheckToken" ,
108115 "ERC20 token" , ethToken .String (),
109- "ERC20 balance" , ethAmount .String (),
116+ "ERC20 balance (ETH decimals)" , ethAmount .String (),
117+ "ERC20 balance (KDA decimals)" , ethAmountInKdaDecimals .String (),
110118 "KDA token" , kdaToken ,
111119 "KDA balance" , kdaAmount .String (),
112120 "amount" , amount .String (),
113121 )
114122
115- if ethAmount .Cmp (kdaAmount ) != 0 {
116- return fmt .Errorf ("%w, balance for ERC20 token %s is %s and the balance for KDA token %s is %s, direction %s" ,
117- ErrBalanceMismatch , ethToken .String (), ethAmount .String (), kdaToken , kdaAmount .String (), direction )
123+ // Compare both amounts in KDA decimals
124+ if ethAmountInKdaDecimals .Cmp (kdaAmount ) != 0 {
125+ return fmt .Errorf ("%w, balance for ERC20 token %s is %s (converted: %s) and the balance for KDA token %s is %s, direction %s" ,
126+ ErrBalanceMismatch , ethToken .String (), ethAmount .String (), ethAmountInKdaDecimals .String (), kdaToken , kdaAmount .String (), direction )
118127 }
119128 return nil
120129}
@@ -216,6 +225,7 @@ func (validator *balanceValidator) computeKdaAmount(
216225 isMintBurn bool ,
217226 isNative bool ,
218227) (* big.Int , error ) {
228+ // kdaAmountInPendingBatches is already in KDA decimals (uses ConvertedAmount from batch)
219229 kdaAmountInPendingBatches , err := validator .getTotalTransferAmountInPendingKlvBatches (ctx , token )
220230 if err != nil {
221231 return nil , err
@@ -268,6 +278,22 @@ func getTotalAmountFromBatch(batch *bridgeCore.TransferBatch, token []byte) *big
268278 return amount
269279}
270280
281+ // getConvertedTotalAmountFromBatch uses ConvertedAmount (KDA decimals) for KC-originated batches
282+ func getConvertedTotalAmountFromBatch (batch * bridgeCore.TransferBatch , token []byte ) (* big.Int , error ) {
283+ amount := big .NewInt (0 )
284+ for _ , deposit := range batch .Deposits {
285+ if deposit .ConvertedAmount == nil {
286+ return nil , fmt .Errorf ("%w for deposit nonce %d" , clients .ErrMissingConvertedAmount , deposit .Nonce )
287+ }
288+
289+ if bytes .Equal (deposit .SourceTokenBytes , token ) {
290+ amount .Add (amount , deposit .ConvertedAmount )
291+ }
292+ }
293+
294+ return amount , nil
295+ }
296+
271297func (validator * balanceValidator ) getTotalTransferAmountInPendingKlvBatches (ctx context.Context , kdaToken []byte ) (* big.Int , error ) {
272298 batchID , err := validator .kcClient .GetLastKCBatchID (ctx )
273299 if err != nil {
@@ -293,7 +319,12 @@ func (validator *balanceValidator) getTotalTransferAmountInPendingKlvBatches(ctx
293319 return amount , nil
294320 }
295321
296- amountFromBatch := getTotalAmountFromBatch (batch , kdaToken )
322+ // Use ConvertedAmount (KDA decimals) to match KC balances which are also in KDA decimals
323+ amountFromBatch , err := getConvertedTotalAmountFromBatch (batch , kdaToken )
324+ if err != nil {
325+ return nil , err
326+ }
327+
297328 amount .Add (amount , amountFromBatch )
298329 batchID -- // go to the previous batch
299330 }
0 commit comments