Skip to content

Commit e73178a

Browse files
authored
[KLC-1933] Bridged tokens decimal conversion (#21)
* feat: token decimals conversion * chore: improve klever blockchain error handlig * chore: test cases for decimal conversion * chore: add deposit converted amount check to prevent panic * chore: update mocks with decimal conversion * chore: integration tests for decimal conversion
1 parent fc7ef27 commit e73178a

File tree

21 files changed

+1130
-68
lines changed

21 files changed

+1130
-68
lines changed

bridges/ethKC/bridgeExecutor.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,11 +456,34 @@ func (executor *bridgeExecutor) GetAndStoreBatchFromEthereum(ctx context.Context
456456
if err != nil {
457457
return err
458458
}
459+
460+
batch, err = executor.convertDepositAmountsToKda(ctx, batch)
461+
if err != nil {
462+
return err
463+
}
464+
459465
executor.batch = batch
460466

461467
return nil
462468
}
463469

470+
// convertDepositAmountsToKda converts the deposit amounts from Ethereum decimals to KDA decimals
471+
func (executor *bridgeExecutor) convertDepositAmountsToKda(ctx context.Context, batch *bridgeCore.TransferBatch) (*bridgeCore.TransferBatch, error) {
472+
if batch == nil {
473+
return nil, ErrNilBatch
474+
}
475+
476+
for i, deposit := range batch.Deposits {
477+
convertedAmount, err := executor.kcClient.ConvertEthToKdaAmount(ctx, deposit.DestinationTokenBytes, deposit.Amount)
478+
if err != nil {
479+
return nil, fmt.Errorf("%w while converting deposit amount for deposit index %d", err, i)
480+
}
481+
batch.Deposits[i].ConvertedAmount = convertedAmount
482+
}
483+
484+
return batch, nil
485+
}
486+
464487
// addBatchSCMetadata fetches the logs containing sc calls metadata for the current batch
465488
func (executor *bridgeExecutor) addBatchSCMetadata(ctx context.Context, transfers *bridgeCore.TransferBatch) (*bridgeCore.TransferBatch, error) {
466489
if transfers == nil {

bridges/ethKC/interface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type KCClient interface {
2626
GetLastExecutedEthTxID(ctx context.Context) (uint64, error)
2727
GetLastKCBatchID(ctx context.Context) (uint64, error)
2828
GetCurrentNonce(ctx context.Context) (uint64, error)
29+
ConvertEthToKdaAmount(ctx context.Context, token []byte, amount *big.Int) (*big.Int, error)
2930

3031
ProposeSetStatus(ctx context.Context, batch *bridgeCore.TransferBatch) (string, error)
3132
ProposeTransfer(ctx context.Context, batch *bridgeCore.TransferBatch) (string, error)

clients/balanceValidator/balanceValidator.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
271297
func (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

Comments
 (0)