Commit 9e2e6eb8 authored by hezhengjun's avatar hezhengjun

support different decimal

parent 5b964942
...@@ -406,10 +406,25 @@ func (chain33Relayer *Relayer4Chain33) relayLockBurnToChain33(claim *ebTypes.Eth ...@@ -406,10 +406,25 @@ func (chain33Relayer *Relayer4Chain33) relayLockBurnToChain33(claim *ebTypes.Eth
} }
} }
//因为发行的合约的精度为8,所以需要缩小,在进行burn的时候,再进行倍乘,在函数ParseBurnLock4chain33进行 //因为发行的合约的精度为8,所以需要进行相应的缩放
if 18 == claim.Decimal { if 8 != claim.Decimal {
bigAmount.Div(bigAmount, big.NewInt(int64(1e10))) if claim.Decimal > 8 {
claim.Amount = bigAmount.String() dist := claim.Decimal - 8
value, exist := utils.Decimal2value[int(dist)]
if !exist {
panic(fmt.Sprintf("does support for decimal, %d", claim.Decimal))
}
bigAmount.Div(bigAmount, big.NewInt(value))
claim.Amount = bigAmount.String()
} else {
dist := 8 - claim.Decimal
value, exist := utils.Decimal2value[int(dist)]
if !exist {
panic(fmt.Sprintf("does support for decimal, %d", claim.Decimal))
}
bigAmount.Mul(bigAmount, big.NewInt(value))
claim.Amount = bigAmount.String()
}
} }
parameter := fmt.Sprintf("newOracleClaim(%d, %s, %s, %s, %s, %s, %s, %s)", parameter := fmt.Sprintf("newOracleClaim(%d, %s, %s, %s, %s, %s, %s, %s)",
......
...@@ -496,16 +496,34 @@ func (ethRelayer *Relayer4Ethereum) handleChain33Msg(chain33Msg *events.Chain33M ...@@ -496,16 +496,34 @@ func (ethRelayer *Relayer4Ethereum) handleChain33Msg(chain33Msg *events.Chain33M
tokenAddr = common.HexToAddress(addr) tokenAddr = common.HexToAddress(addr)
} }
} else { } else {
lockedTokenInfo, exist := ethRelayer.symbol2LockAddr[prophecyClaim.Symbol] burnFromChain33TokenInfo, exist := ethRelayer.symbol2LockAddr[prophecyClaim.Symbol]
if !exist { if !exist {
//因为是burn操作,必须从允许lock的token地址中进行查询 //因为是burn操作,必须从允许lock的token地址中进行查询
relayerLog.Error("handleChain33Msg", "Failed to fetch locked Token Info for symbol", prophecyClaim.Symbol) relayerLog.Error("handleChain33Msg", "Failed to fetch locked Token Info for symbol", prophecyClaim.Symbol)
return return
} }
tokenAddr = common.HexToAddress(lockedTokenInfo.Address) tokenAddr = common.HexToAddress(burnFromChain33TokenInfo.Address)
if lockedTokenInfo.Decimal == 18 { //if lockedTokenInfo.Decimal == 18 {
prophecyClaim.Amount = prophecyClaim.Amount.Mul(prophecyClaim.Amount, big.NewInt(int64(1e10))) // prophecyClaim.Amount = prophecyClaim.Amount.Mul(prophecyClaim.Amount, big.NewInt(int64(1e10)))
//}
//从chain33进行withdraw回来的token需要根据精度进行相应的缩放
if 8 != burnFromChain33TokenInfo.Decimal {
if burnFromChain33TokenInfo.Decimal > 8 {
dist := burnFromChain33TokenInfo.Decimal - 8
value, exist := utils.Decimal2value[int(dist)]
if !exist {
panic(fmt.Sprintf("does support for decimal, %d", burnFromChain33TokenInfo.Decimal))
}
prophecyClaim.Amount.Mul(prophecyClaim.Amount, big.NewInt(value))
} else {
dist := 8 - burnFromChain33TokenInfo.Decimal
value, exist := utils.Decimal2value[int(dist)]
if !exist {
panic(fmt.Sprintf("does support for decimal, %d", burnFromChain33TokenInfo.Decimal))
}
prophecyClaim.Amount.Div(prophecyClaim.Amount, big.NewInt(value))
}
} }
} }
......
...@@ -34,6 +34,20 @@ const ( ...@@ -34,6 +34,20 @@ const (
nullAddress = "0x0000000000000000000000000000000000000000" nullAddress = "0x0000000000000000000000000000000000000000"
) )
var Decimal2value = map[int]int64{
1: 1e1,
2: 1e2,
3: 1e3,
4: 1e4,
5: 1e5,
6: 1e6,
7: 1e7,
8: 1e8,
9: 1e9,
10: 1e10,
11: 1e11,
}
var log = log15.New("module", "utils") var log = log15.New("module", "utils")
// IsZeroAddress : checks an Ethereum address and returns a bool which indicates if it is the null address // IsZeroAddress : checks an Ethereum address and returns a bool which indicates if it is the null address
......
package utils
import (
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func Test_decimals(t *testing.T) {
value2comp := 10
for i := 1; i <= 10; i++ {
value, ok := Decimal2value[i]
require.Equal(t, value, int64(value2comp))
require.Equal(t, ok, true)
fmt.Println("value=", value)
value2comp = value2comp * 10
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment