Commit 02f466b7 authored by hezhengjun's avatar hezhengjun

correct withdraw configuration

parent d63b29b1
......@@ -1176,22 +1176,25 @@ func CfgWithdrawCmd() *cobra.Command {
func addCfgWithdrawFlags(cmd *cobra.Command) {
cmd.Flags().StringP("symbol", "s", "", "symbol")
_ = cmd.MarkFlagRequired("symbol")
cmd.Flags().Int64P("fee", "f", 0, "fee amount")
cmd.Flags().Float64P("fee", "f", 0, "fee amount")
_ = cmd.MarkFlagRequired("fee")
cmd.Flags().Int64P("amount", "a", 0, "accumulative amount allowed to be withdrew per day")
cmd.Flags().Float64P("amount", "a", 0, "accumulative amount allowed to be withdrew per day")
_ = cmd.MarkFlagRequired("amount")
cmd.Flags().Int8P("decimal", "d", 0, "token decimal")
_ = cmd.MarkFlagRequired("decimal")
}
func CfgWithdraw(cmd *cobra.Command, _ []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
symbol, _ := cmd.Flags().GetString("symbol")
fee, _ := cmd.Flags().GetInt64("fee")
amount, _ := cmd.Flags().GetInt64("amount")
fee, _ := cmd.Flags().GetFloat64("fee")
amount, _ := cmd.Flags().GetFloat64("amount")
decimal, _ := cmd.Flags().GetInt8("decimal")
req := &ebTypes.CfgWithdrawReq{
Symbol: symbol,
FeeAmount: fee,
AmountPerDay: amount,
FeeAmount: utils.ToWei(fee, int64(decimal)).String(),
AmountPerDay: utils.ToWei(amount, int64(decimal)).String(),
}
var res rpctypes.Reply
......
......@@ -247,14 +247,14 @@ message ResendChain33EventReq {
}
message CfgWithdrawReq {
string symbol = 1;
int64 feeAmount = 2;
int64 amountPerDay = 3;
string symbol = 1;
string feeAmount = 2;
string amountPerDay = 3;
}
message withdrawPara {
int64 fee = 1;
int64 amountPerDay = 2;
string fee = 1;
string amountPerDay = 2;
}
message WithdrawSymbol2Para {
......@@ -265,7 +265,7 @@ message WithdrawTx {
string chain33Sender = 1;
string ethereumReceiver = 2;
string symbol = 4;
int64 amount = 5;
string amount = 5;
int64 nonce = 6;
string txHashOnChain33 = 7;
string txHashOnEthereum = 8;
......
......@@ -76,7 +76,7 @@ type Relayer4Ethereum struct {
symbol2Addr map[string]common.Address
symbol2LockAddr map[string]ebTypes.TokenAddress
mulSignAddr string
withdrawFee map[string]*ebTypes.WithdrawPara
withdrawFee map[string]*WithdrawFeeAndQuota
}
var (
......@@ -100,6 +100,11 @@ type EthereumStartPara struct {
ProcessWithDraw bool
}
type WithdrawFeeAndQuota struct {
Fee *big.Int
AmountPerDay *big.Int
}
//StartEthereumRelayer ///
func StartEthereumRelayer(startPara *EthereumStartPara) *Relayer4Ethereum {
if 0 == startPara.BlockInterval {
......@@ -134,7 +139,7 @@ func StartEthereumRelayer(startPara *EthereumStartPara) *Relayer4Ethereum {
ethRelayer.eventLogIndex = ethRelayer.getLastBridgeBankProcessedHeight()
ethRelayer.initBridgeBankTx()
ethRelayer.mulSignAddr = ethRelayer.getMultiSignAddress()
ethRelayer.withdrawFee = ethRelayer.restoreWithdrawFee()
ethRelayer.withdrawFee = ethRelayer.restoreWithdrawFeeInINt()
// Start clientSpec with infura ropsten provider
relayerLog.Info("Relayer4Ethereum proc", "Started Ethereum websocket with provider:", ethRelayer.provider)
......@@ -522,20 +527,22 @@ func (ethRelayer *Relayer4Ethereum) handleChain33Msg(chain33Msg *events.Chain33M
return
}
func (ethRelayer *Relayer4Ethereum) checkPermissionWithinOneDay(withdrawTx *ebTypes.WithdrawTx) (int64, error) {
func (ethRelayer *Relayer4Ethereum) checkPermissionWithinOneDay(withdrawTx *ebTypes.WithdrawTx) (*big.Int, error) {
totalAlready, err := ethRelayer.getWithdrawsWithinSameDay(withdrawTx)
if nil != err {
relayerLog.Error("checkPermissionWithinOneDay", "Failed to getWithdrawsWithinSameDay due to", err.Error())
return 0, errors.New("ErrGetWithdrawsWithinSameDay")
return nil, errors.New("ErrGetWithdrawsWithinSameDay")
}
withdrawPara, ok := ethRelayer.withdrawFee[withdrawTx.Symbol]
if !ok {
relayerLog.Error("checkPermissionWithinOneDay", "No withdraw parameter configured for symbol ", withdrawTx.Symbol)
return 0, errors.New("ErrNoWithdrawParaCfged")
return nil, errors.New("ErrNoWithdrawParaCfged")
}
if totalAlready+withdrawTx.Amount > withdrawPara.AmountPerDay {
AmountInt, _ := big.NewInt(0).SetString(withdrawTx.Amount, 0)
totalAlready.Add(totalAlready, AmountInt)
if totalAlready.Cmp(withdrawPara.AmountPerDay) > 0 {
relayerLog.Error("checkPermissionWithinOneDay", "No withdraw parameter configured for symbol ", withdrawTx.Symbol)
return 0, errors.New("ErrWithdrawAmountTooBig")
return nil, errors.New("ErrWithdrawAmountTooBig")
}
relayerLog.Info("checkPermissionWithinOneDay", "total withdraw already", totalAlready, "Chain33Sender", withdrawTx.Chain33Sender,
"Symbol", withdrawTx.Symbol)
......@@ -562,7 +569,7 @@ func (ethRelayer *Relayer4Ethereum) handleLogWithdraw(chain33Msg *events.Chain33
Chain33Sender: chain33Msg.Chain33Sender.String(),
EthereumReceiver: chain33Msg.EthereumReceiver.String(),
Symbol: chain33Msg.Symbol,
Amount: chain33Msg.Amount.Int64(),
Amount: chain33Msg.Amount.String(),
TxHashOnChain33: common.Bytes2Hex(chain33Msg.TxHash),
Nonce: chain33Msg.Nonce,
Year: int32(year),
......@@ -571,7 +578,7 @@ func (ethRelayer *Relayer4Ethereum) handleLogWithdraw(chain33Msg *events.Chain33
}
//检查用户提币权限是否得到满足:比如是否超过累计提币额度
var feeAmount int64
var feeAmount *big.Int
var err error
if feeAmount, err = ethRelayer.checkPermissionWithinOneDay(withdrawTx); nil != err {
withdrawTx.Status = err.Error()
......@@ -605,8 +612,11 @@ func (ethRelayer *Relayer4Ethereum) handleLogWithdraw(chain33Msg *events.Chain33
}
relayerLog.Info("handleLogWithdraw", "token address", tokenAddr.String(), "amount", chain33Msg.Amount.String(),
"Receiver on Ethereum", chain33Msg.EthereumReceiver.String())
amount2transfer := big.NewInt(chain33Msg.Amount.Int64() - feeAmount)
if chain33Msg.Amount.Cmp(feeAmount) < 0 {
relayerLog.Error("handleLogWithdraw", "does support for decimal, %d", withdrawFromChain33TokenInfo.Decimal)
return
}
amount2transfer := chain33Msg.Amount.Sub(chain33Msg.Amount, feeAmount)
value := big.NewInt(0)
//此处需要完成在以太坊发送以太或者ERC20数字资产的操作
......@@ -1382,14 +1392,22 @@ func (ethRelayer *Relayer4Ethereum) SetMultiSignAddr(address string) {
ethRelayer.setMultiSignAddress(address)
}
func (ethRelayer *Relayer4Ethereum) CfgWithdraw(symbol string, feeAmount, amountPerDay int64) error {
withdrawPara := &ebTypes.WithdrawPara{
Fee: feeAmount,
AmountPerDay: amountPerDay,
func (ethRelayer *Relayer4Ethereum) CfgWithdraw(symbol string, feeAmount, amountPerDay string) error {
fee, _ := big.NewInt(0).SetString(feeAmount, 10)
amountPerDayInt, _ := big.NewInt(0).SetString(amountPerDay, 10)
withdrawPara := &WithdrawFeeAndQuota{
Fee: fee,
AmountPerDay: amountPerDayInt,
}
ethRelayer.rwLock.Lock()
ethRelayer.withdrawFee[symbol] = withdrawPara
ethRelayer.rwLock.Unlock()
return ethRelayer.setWithdrawFee(ethRelayer.withdrawFee)
WithdrawPara := ethRelayer.restoreWithdrawFee()
WithdrawPara[symbol] = &ebTypes.WithdrawPara{
Fee: feeAmount,
AmountPerDay: amountPerDay,
}
return ethRelayer.setWithdrawFee(WithdrawPara)
}
......@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
......@@ -411,6 +412,20 @@ func (ethRelayer *Relayer4Ethereum) restoreWithdrawFee() map[string]*ebTypes.Wit
return withdrawSymbol2Para.Symbol2Para
}
func (ethRelayer *Relayer4Ethereum) restoreWithdrawFeeInINt() map[string]*WithdrawFeeAndQuota {
withdrawPara := ethRelayer.restoreWithdrawFee()
res := make(map[string]*WithdrawFeeAndQuota)
for symbol, para := range withdrawPara {
feeInt, _ := big.NewInt(0).SetString(para.Fee, 10)
amountPerDayInt, _ := big.NewInt(0).SetString(para.AmountPerDay, 10)
res[symbol] = &WithdrawFeeAndQuota{
Fee: feeInt,
AmountPerDay: amountPerDayInt,
}
}
return res
}
func calcWithdrawKey(chain33Sender, symbol string, year, month, day int, nonce int64) []byte {
return []byte(fmt.Sprintf("%s-%s-%s-%d-%d-%d-%d", withdrawTokenPrefix, chain33Sender, symbol, year, month, day, nonce))
}
......@@ -432,7 +447,7 @@ func (ethRelayer *Relayer4Ethereum) setWithdraw(withdrawTx *ebTypes.WithdrawTx)
return ethRelayer.db.Set(key, bytes)
}
func (ethRelayer *Relayer4Ethereum) getWithdrawsWithinSameDay(withdrawTx *ebTypes.WithdrawTx) (int64, error) {
func (ethRelayer *Relayer4Ethereum) getWithdrawsWithinSameDay(withdrawTx *ebTypes.WithdrawTx) (*big.Int, error) {
chain33Sender := withdrawTx.Chain33Sender
symbol := withdrawTx.Symbol
year := withdrawTx.Year
......@@ -443,17 +458,18 @@ func (ethRelayer *Relayer4Ethereum) getWithdrawsWithinSameDay(withdrawTx *ebType
helper := dbm.NewListHelper(ethRelayer.db)
datas := helper.List(prefix, nil, 100, dbm.ListASC)
if nil == datas {
return 0, nil
return big.NewInt(0), nil
}
withdrawTotal := int64(0)
withdrawTotal := big.NewInt(0)
for _, data := range datas {
var info ebTypes.WithdrawTx
err := chain33Types.Decode(data, &info)
if nil != err {
return 0, err
return big.NewInt(0), err
}
withdrawTotal += info.Amount
AmountInt, _ := big.NewInt(0).SetString(info.Amount, 0)
withdrawTotal.Add(withdrawTotal, AmountInt)
}
return withdrawTotal, nil
}
......@@ -2437,8 +2437,8 @@ type CfgWithdrawReq struct {
unknownFields protoimpl.UnknownFields
Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"`
FeeAmount int64 `protobuf:"varint,2,opt,name=feeAmount,proto3" json:"feeAmount,omitempty"`
AmountPerDay int64 `protobuf:"varint,3,opt,name=amountPerDay,proto3" json:"amountPerDay,omitempty"`
FeeAmount string `protobuf:"bytes,2,opt,name=feeAmount,proto3" json:"feeAmount,omitempty"`
AmountPerDay string `protobuf:"bytes,3,opt,name=amountPerDay,proto3" json:"amountPerDay,omitempty"`
}
func (x *CfgWithdrawReq) Reset() {
......@@ -2480,18 +2480,18 @@ func (x *CfgWithdrawReq) GetSymbol() string {
return ""
}
func (x *CfgWithdrawReq) GetFeeAmount() int64 {
func (x *CfgWithdrawReq) GetFeeAmount() string {
if x != nil {
return x.FeeAmount
}
return 0
return ""
}
func (x *CfgWithdrawReq) GetAmountPerDay() int64 {
func (x *CfgWithdrawReq) GetAmountPerDay() string {
if x != nil {
return x.AmountPerDay
}
return 0
return ""
}
type WithdrawPara struct {
......@@ -2499,8 +2499,8 @@ type WithdrawPara struct {
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Fee int64 `protobuf:"varint,1,opt,name=fee,proto3" json:"fee,omitempty"`
AmountPerDay int64 `protobuf:"varint,2,opt,name=amountPerDay,proto3" json:"amountPerDay,omitempty"`
Fee string `protobuf:"bytes,1,opt,name=fee,proto3" json:"fee,omitempty"`
AmountPerDay string `protobuf:"bytes,2,opt,name=amountPerDay,proto3" json:"amountPerDay,omitempty"`
}
func (x *WithdrawPara) Reset() {
......@@ -2535,18 +2535,18 @@ func (*WithdrawPara) Descriptor() ([]byte, []int) {
return file_relayer_proto_rawDescGZIP(), []int{37}
}
func (x *WithdrawPara) GetFee() int64 {
func (x *WithdrawPara) GetFee() string {
if x != nil {
return x.Fee
}
return 0
return ""
}
func (x *WithdrawPara) GetAmountPerDay() int64 {
func (x *WithdrawPara) GetAmountPerDay() string {
if x != nil {
return x.AmountPerDay
}
return 0
return ""
}
type WithdrawSymbol2Para struct {
......@@ -2604,7 +2604,7 @@ type WithdrawTx struct {
Chain33Sender string `protobuf:"bytes,1,opt,name=chain33Sender,proto3" json:"chain33Sender,omitempty"`
EthereumReceiver string `protobuf:"bytes,2,opt,name=ethereumReceiver,proto3" json:"ethereumReceiver,omitempty"`
Symbol string `protobuf:"bytes,4,opt,name=symbol,proto3" json:"symbol,omitempty"`
Amount int64 `protobuf:"varint,5,opt,name=amount,proto3" json:"amount,omitempty"`
Amount string `protobuf:"bytes,5,opt,name=amount,proto3" json:"amount,omitempty"`
Nonce int64 `protobuf:"varint,6,opt,name=nonce,proto3" json:"nonce,omitempty"`
TxHashOnChain33 string `protobuf:"bytes,7,opt,name=txHashOnChain33,proto3" json:"txHashOnChain33,omitempty"`
TxHashOnEthereum string `protobuf:"bytes,8,opt,name=txHashOnEthereum,proto3" json:"txHashOnEthereum,omitempty"`
......@@ -2667,11 +2667,11 @@ func (x *WithdrawTx) GetSymbol() string {
return ""
}
func (x *WithdrawTx) GetAmount() int64 {
func (x *WithdrawTx) GetAmount() string {
if x != nil {
return x.Amount
}
return 0
return ""
}
func (x *WithdrawTx) GetNonce() int64 {
......@@ -3074,14 +3074,14 @@ var file_relayer_proto_rawDesc = []byte{
0x68, 0x74, 0x22, 0x6a, 0x0a, 0x0e, 0x43, 0x66, 0x67, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61,
0x77, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x1c, 0x0a, 0x09,
0x66, 0x65, 0x65, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52,
0x66, 0x65, 0x65, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52,
0x09, 0x66, 0x65, 0x65, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x22, 0x0a, 0x0c, 0x61, 0x6d,
0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x44, 0x61, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03,
0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x44, 0x61, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09,
0x52, 0x0c, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x44, 0x61, 0x79, 0x22, 0x44,
0x0a, 0x0c, 0x77, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61, 0x77, 0x50, 0x61, 0x72, 0x61, 0x12, 0x10,
0x0a, 0x03, 0x66, 0x65, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x03, 0x66, 0x65, 0x65,
0x0a, 0x03, 0x66, 0x65, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x66, 0x65, 0x65,
0x12, 0x22, 0x0a, 0x0c, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65, 0x72, 0x44, 0x61, 0x79,
0x18, 0x02, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65,
0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x50, 0x65,
0x72, 0x44, 0x61, 0x79, 0x22, 0xb9, 0x01, 0x0a, 0x13, 0x57, 0x69, 0x74, 0x68, 0x64, 0x72, 0x61,
0x77, 0x53, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x32, 0x50, 0x61, 0x72, 0x61, 0x12, 0x4d, 0x0a, 0x0b,
0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x32, 0x50, 0x61, 0x72, 0x61, 0x18, 0x01, 0x20, 0x03, 0x28,
......@@ -3102,7 +3102,7 @@ var file_relayer_proto_rawDesc = []byte{
0x10, 0x65, 0x74, 0x68, 0x65, 0x72, 0x65, 0x75, 0x6d, 0x52, 0x65, 0x63, 0x65, 0x69, 0x76, 0x65,
0x72, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x04, 0x20, 0x01, 0x28,
0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x16, 0x0a, 0x06, 0x61, 0x6d, 0x6f,
0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e,
0x75, 0x6e, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x61, 0x6d, 0x6f, 0x75, 0x6e,
0x74, 0x12, 0x14, 0x0a, 0x05, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03,
0x52, 0x05, 0x6e, 0x6f, 0x6e, 0x63, 0x65, 0x12, 0x28, 0x0a, 0x0f, 0x74, 0x78, 0x48, 0x61, 0x73,
0x68, 0x4f, 0x6e, 0x43, 0x68, 0x61, 0x69, 0x6e, 0x33, 0x33, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09,
......
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