-
Notifications
You must be signed in to change notification settings - Fork 13
/
cancel_sendtoexternal.go
49 lines (40 loc) · 1.51 KB
/
cancel_sendtoexternal.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
package crosschain
import (
"errors"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/functionx/fx-core/v4/x/evm/types"
)
func (c *Contract) CancelSendToExternal(ctx sdk.Context, evm *vm.EVM, contract *vm.Contract, readonly bool) ([]byte, error) {
if readonly {
return nil, errors.New("cancel send to external method not readonly")
}
// args
var args CancelSendToExternalArgs
if err := types.ParseMethodArgs(CancelSendToExternalMethod, &args, contract.Input[4:]); err != nil {
return nil, err
}
sender := contract.Caller()
route, has := c.router.GetRoute(args.Chain)
if !has {
return nil, fmt.Errorf("chain not support: %s", args.Chain)
}
originDenom := c.evmKeeper.GetParams(ctx).EvmDenom
// NOTE: must be get relation before cancel, cancel will delete it if relation exist
hasRelation := c.erc20Keeper.HasOutgoingTransferRelation(ctx, args.Chain, args.TxID.Uint64())
refundCoin, err := route.PrecompileCancelSendToExternal(ctx, args.TxID.Uint64(), sender.Bytes())
if err != nil {
return nil, err
}
if !hasRelation && refundCoin.Denom == originDenom {
// add refund to sender in evm state db, because bank keeper add refund to sender
evm.StateDB.AddBalance(sender, refundCoin.Amount.BigInt())
}
// add event log
if err := c.AddLog(evm, CancelSendToExternalEvent, []common.Hash{sender.Hash()}, args.Chain, args.TxID); err != nil {
return nil, err
}
return CancelSendToExternalMethod.Outputs.Pack(true)
}