-
Notifications
You must be signed in to change notification settings - Fork 228
/
send_to_account.go
87 lines (72 loc) · 2.25 KB
/
send_to_account.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package evmhandler
import (
"fmt"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
cronoskeeper "github.com/crypto-org-chain/cronos/x/cronos/keeper"
"github.com/crypto-org-chain/cronos/x/cronos/types"
)
var _ types.EvmLogHandler = SendToAccountHandler{}
const SendToAccountEventName = "__CronosSendToAccount"
// SendToAccountEvent represent the signature of
// `event __CronosSendToAccount(address recipient, uint256 amount)`
var SendToAccountEvent abi.Event
func init() {
addressType, _ := abi.NewType("address", "", nil)
uint256Type, _ := abi.NewType("uint256", "", nil)
SendToAccountEvent = abi.NewEvent(
SendToAccountEventName,
SendToAccountEventName,
false,
abi.Arguments{abi.Argument{
Name: "recipient",
Type: addressType,
Indexed: false,
}, abi.Argument{
Name: "amount",
Type: uint256Type,
Indexed: false,
}},
)
}
// SendToAccountHandler handles `__CronosSendToAccount` log
type SendToAccountHandler struct {
bankKeeper types.BankKeeper
cronosKeeper cronoskeeper.Keeper
}
func NewSendToAccountHandler(bankKeeper types.BankKeeper, cronosKeeper cronoskeeper.Keeper) *SendToAccountHandler {
return &SendToAccountHandler{
bankKeeper: bankKeeper,
cronosKeeper: cronosKeeper,
}
}
func (h SendToAccountHandler) EventID() common.Hash {
return SendToAccountEvent.ID
}
func (h SendToAccountHandler) Handle(
ctx sdk.Context,
contract common.Address,
data []byte,
_ func(contractAddress common.Address, logSig common.Hash, logData []byte),
) error {
unpacked, err := SendToAccountEvent.Inputs.Unpack(data)
if err != nil {
// log and ignore
h.cronosKeeper.Logger(ctx).Error("log signature matches but failed to decode", "error", err)
return nil
}
denom, found := h.cronosKeeper.GetDenomByContract(ctx, contract)
if !found {
return fmt.Errorf("contract %s is not connected to native token", contract)
}
contractAddr := sdk.AccAddress(contract.Bytes())
recipient := sdk.AccAddress(unpacked[0].(common.Address).Bytes())
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewIntFromBigInt(unpacked[1].(*big.Int))))
err = h.bankKeeper.SendCoins(ctx, contractAddr, recipient, coins)
if err != nil {
return err
}
return nil
}