Skip to content

Commit f62a8e1

Browse files
committed
chore: update mocks with decimal conversion
1 parent d328d12 commit f62a8e1

File tree

4 files changed

+107
-31
lines changed

4 files changed

+107
-31
lines changed

integrationTests/mock/KcMock.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,16 @@ func (mock *KleverBlockchainMock) AddTokensPair(erc20 common.Address, ticker str
161161
mock.addTokensPair(erc20, ticker, isNativeToken, isMintBurnToken, totalBalance, mintBalances, burnBalances)
162162
}
163163

164+
// SetDecimalConversion sets the decimal conversion configuration for a token
165+
// The conversion is calculated as: convertedAmount = (amount * multiplier) / divisor
166+
// For example, ETH 18 decimals to KDA 6 decimals: multiplier=1, divisor=10^12
167+
func (mock *KleverBlockchainMock) SetDecimalConversion(ticker string, multiplier, divisor *big.Int) {
168+
mock.mutState.Lock()
169+
defer mock.mutState.Unlock()
170+
171+
mock.setDecimalConversion(ticker, multiplier, divisor)
172+
}
173+
164174
// SetLastExecutedEthBatchID -
165175
func (mock *KleverBlockchainMock) SetLastExecutedEthBatchID(lastExecutedEthBatchId uint64) {
166176
mock.mutState.Lock()

integrationTests/mock/KleverContractStateMock.go

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ type kleverBlockchainProposedTransfer struct {
2828

2929
// Transfer -
3030
type Transfer struct {
31-
From []byte
32-
To []byte
33-
Token string
34-
Amount *big.Int
35-
Nonce *big.Int
36-
Data []byte
31+
From []byte
32+
To []byte
33+
Token string
34+
Amount *big.Int
35+
ConvertedAmount *big.Int
36+
Nonce *big.Int
37+
Data []byte
3738
}
3839

3940
// KleverBlockchainPendingBatch -
@@ -44,11 +45,12 @@ type KleverBlockchainPendingBatch struct {
4445

4546
// KleverBlockchainDeposit -
4647
type KleverBlockchainDeposit struct {
47-
From address.Address
48-
To common.Address
49-
Ticker string
50-
Amount *big.Int
51-
DepositNonce uint64
48+
From address.Address
49+
To common.Address
50+
Ticker string
51+
Amount *big.Int
52+
ConvertedAmount *big.Int
53+
DepositNonce uint64
5254
}
5355

5456
// kleverBlockchainContractStateMock is not concurrent safe
@@ -193,26 +195,32 @@ func (mock *kleverBlockchainContractStateMock) createProposedTransfer(dataSplit
193195
panic(errDecode)
194196
}
195197

196-
nonceBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+4])
198+
convertedAmountBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+4])
197199
if errDecode != nil {
198200
panic(errDecode)
199201
}
200202

201-
dataBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+5])
203+
nonceBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+5])
204+
if errDecode != nil {
205+
panic(errDecode)
206+
}
207+
208+
dataBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+6])
202209
if errDecode != nil {
203210
panic(errDecode)
204211
}
205212

206213
t := Transfer{
207-
From: from,
208-
To: to,
209-
Token: dataSplit[currentIndex+2],
210-
Amount: big.NewInt(0).SetBytes(amountBytes),
211-
Nonce: big.NewInt(0).SetBytes(nonceBytes),
212-
Data: dataBytes,
214+
From: from,
215+
To: to,
216+
Token: dataSplit[currentIndex+2],
217+
Amount: big.NewInt(0).SetBytes(amountBytes),
218+
ConvertedAmount: big.NewInt(0).SetBytes(convertedAmountBytes),
219+
Nonce: big.NewInt(0).SetBytes(nonceBytes),
220+
Data: dataBytes,
213221
}
214222

215-
indexIncrementValue := 6
223+
indexIncrementValue := 7
216224
transfer.Transfers = append(transfer.Transfers, t)
217225
currentIndex += indexIncrementValue
218226
}
@@ -283,6 +291,8 @@ func (mock *kleverBlockchainContractStateMock) processVmRequests(vmRequest *mode
283291
return mock.vmRequestGetBurnBalances(vmRequest), nil
284292
case "getLastBatchId":
285293
return mock.vmRequestGetLastBatchId(vmRequest), nil
294+
case "convertEthToKdaAmount":
295+
return mock.vmRequestConvertEthToKdaAmount(vmRequest), nil
286296
}
287297

288298
return nil, fmt.Errorf("unimplemented function: %s", vmRequest.FuncName)
@@ -464,6 +474,11 @@ func (mock *kleverBlockchainContractStateMock) responseWithPendingBatch() *model
464474
args = append(args, deposit.To.Bytes())
465475
args = append(args, []byte(deposit.Ticker))
466476
args = append(args, deposit.Amount.Bytes())
477+
convertedAmount := deposit.ConvertedAmount
478+
if convertedAmount == nil {
479+
convertedAmount = deposit.Amount // default to same as Amount if not set
480+
}
481+
args = append(args, convertedAmount.Bytes())
467482
}
468483
return createOkVmResponse(args)
469484
}
@@ -553,6 +568,23 @@ func (mock *kleverBlockchainContractStateMock) vmRequestGetLastBatchId(_ *models
553568
return createOkVmResponse([][]byte{mock.pendingBatch.Nonce.Bytes()})
554569
}
555570

571+
func (mock *kleverBlockchainContractStateMock) vmRequestConvertEthToKdaAmount(vmRequest *models.VmValueRequest) *models.VmValuesResponseData {
572+
// Parse the token from the first argument
573+
hexedToken := vmRequest.Args[0]
574+
575+
// Parse the amount from the second argument
576+
amountBytes, err := hex.DecodeString(vmRequest.Args[1])
577+
if err != nil {
578+
return createNokVmResponse(err)
579+
}
580+
amount := big.NewInt(0).SetBytes(amountBytes)
581+
582+
// Use the decimal conversion configuration from the token registry
583+
convertedAmount := mock.tokensRegistryMock.getConvertedAmount(hexedToken, amount)
584+
585+
return createOkVmResponse([][]byte{convertedAmount.Bytes()})
586+
}
587+
556588
func getBigIntFromString(data string) *big.Int {
557589
buff, err := hex.DecodeString(data)
558590
if err != nil {

integrationTests/mock/tokensRegistryMock.go

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,22 @@ import (
88
"github.com/klever-io/klv-bridge-eth-go/integrationTests"
99
)
1010

11+
// DecimalConversionConfig holds the multiplier and divisor for decimal conversion
12+
type DecimalConversionConfig struct {
13+
Multiplier *big.Int
14+
Divisor *big.Int
15+
}
16+
1117
// tokensRegistryMock is not concurrent safe
1218
type tokensRegistryMock struct {
13-
ethToKC map[common.Address]string
14-
kcToEth map[string]common.Address
15-
mintBurnTokens map[string]bool
16-
nativeTokens map[string]bool
17-
totalBalances map[string]*big.Int
18-
mintBalances map[string]*big.Int
19-
burnBalances map[string]*big.Int
19+
ethToKC map[common.Address]string
20+
kcToEth map[string]common.Address
21+
mintBurnTokens map[string]bool
22+
nativeTokens map[string]bool
23+
totalBalances map[string]*big.Int
24+
mintBalances map[string]*big.Int
25+
burnBalances map[string]*big.Int
26+
decimalConversion map[string]*DecimalConversionConfig // key is hex-encoded ticker
2027
}
2128

2229
func (mock *tokensRegistryMock) addTokensPair(erc20Address common.Address, ticker string, isNativeToken, isMintBurnToken bool, totalBalance, mintBalances, burnBalances *big.Int) {
@@ -41,6 +48,30 @@ func (mock *tokensRegistryMock) addTokensPair(erc20Address common.Address, ticke
4148
}
4249
}
4350

51+
// setDecimalConversion sets the decimal conversion configuration for a token
52+
// The conversion is calculated as: convertedAmount = (amount * multiplier) / divisor
53+
// For example, ETH 18 decimals to KDA 6 decimals: multiplier=1, divisor=10^12
54+
func (mock *tokensRegistryMock) setDecimalConversion(ticker string, multiplier, divisor *big.Int) {
55+
hexedTicker := hex.EncodeToString([]byte(ticker))
56+
mock.decimalConversion[hexedTicker] = &DecimalConversionConfig{
57+
Multiplier: multiplier,
58+
Divisor: divisor,
59+
}
60+
}
61+
62+
// getConvertedAmount converts an amount using the decimal conversion config for the token
63+
// If no conversion is configured, returns the same amount
64+
func (mock *tokensRegistryMock) getConvertedAmount(hexedTicker string, amount *big.Int) *big.Int {
65+
config, exists := mock.decimalConversion[hexedTicker]
66+
if !exists || config == nil {
67+
return amount
68+
}
69+
// convertedAmount = (amount * multiplier) / divisor
70+
result := new(big.Int).Mul(amount, config.Multiplier)
71+
result.Div(result, config.Divisor)
72+
return result
73+
}
74+
4475
func (mock *tokensRegistryMock) clearTokens() {
4576
mock.ethToKC = make(map[common.Address]string)
4677
mock.kcToEth = make(map[string]common.Address)
@@ -49,6 +80,7 @@ func (mock *tokensRegistryMock) clearTokens() {
4980
mock.totalBalances = make(map[string]*big.Int)
5081
mock.mintBalances = make(map[string]*big.Int)
5182
mock.burnBalances = make(map[string]*big.Int)
83+
mock.decimalConversion = make(map[string]*DecimalConversionConfig)
5284
}
5385

5486
func (mock *tokensRegistryMock) getTicker(erc20Address common.Address) string {

integrationTests/relayers/kcToEth_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,14 @@ func createTransactions(n int) ([]mock.KleverBlockchainDeposit, []common.Address
248248

249249
func createTransaction(index int) (mock.KleverBlockchainDeposit, common.Address) {
250250
tokenAddress := testsCommon.CreateRandomEthereumAddress()
251+
amount := big.NewInt(int64(index*1000) + 500) // 0 as amount is not relevant
251252

252253
return mock.KleverBlockchainDeposit{
253-
From: testsCommon.CreateRandomKCAddress(),
254-
To: testsCommon.CreateRandomEthereumAddress(),
255-
Ticker: fmt.Sprintf("tck-00000%d", index+1),
256-
Amount: big.NewInt(int64(index*1000) + 500), // 0 as amount is not relevant
254+
From: testsCommon.CreateRandomKCAddress(),
255+
To: testsCommon.CreateRandomEthereumAddress(),
256+
Ticker: fmt.Sprintf("tck-00000%d", index+1),
257+
Amount: amount,
258+
ConvertedAmount: amount, // default to same as Amount for now
257259
}, tokenAddress
258260
}
259261

0 commit comments

Comments
 (0)