-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify_vnft_owner.go
99 lines (81 loc) · 2.59 KB
/
verify_vnft_owner.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
88
89
90
91
92
93
94
95
96
97
98
99
// description:
// @author renshiwei
// Date: 2023/6/7
package exitscan
import (
"context"
"github.com/NodeDAO/operator-sdk/contracts"
"github.com/NodeDAO/operator-sdk/eth1"
"github.com/pkg/errors"
"math/big"
)
type VnftOwnerVerify struct {
// param
network string
// init
elClient *eth1.EthClient
// contracts
vnftContract *contracts.VnftContract
}
// NewVnftOwnerVerify new vnft owner verify
func NewVnftOwnerVerify(ctx context.Context, network, elAddr string) (*VnftOwnerVerify, error) {
var err error
elClient, err := eth1.NewEthClient(ctx, elAddr)
if err != nil {
return nil, err
}
vnftContract, err := contracts.NewVnftContract(network, elClient.Client)
if err != nil {
return nil, errors.Wrapf(err, "Fail to new vnftContract. network:%s", network)
}
return &VnftOwnerVerify{
network: network,
elClient: elClient,
vnftContract: vnftContract,
}, nil
}
// VerifyStakeType Verify that the stakeType of vnft tokenIds
func (e *VnftOwnerVerify) VerifyStakeType(network string, tokenId *big.Int) (StakeType, error) {
nETHOwnerExpected := contracts.LiqContractAddress(network)
vnftOwner, err := e.vnftContract.Contract.OwnerOf(nil, tokenId)
if err != nil {
return 0, errors.Wrapf(err, "Fail to get vnft owner. network:%s", e.network)
}
if vnftOwner.String() != nETHOwnerExpected {
return NETH, nil
}
return VNFT, nil
}
// VerifyVnftOwner Verify that the stakeType of vnft tokenIds and vnftOwner match
// ----------------------------------------------------------------
// The relationship between StakeType and VnftOwner is as follows:
// ----------------------
// StakeType | VnftOwner
// ----------------------
// VNFT | USER
// NETH | LiquidStaking
func (e *VnftOwnerVerify) VerifyVnftOwner(network string, stakeType StakeType, vnftOwner VnftOwner, tokenIds []*big.Int) (bool, error) {
if uint32(stakeType) != uint32(vnftOwner) {
return false, errors.New("stakeType and vnftOwner do not match")
}
var nETHOwnerExpected string
if stakeType == NETH {
nETHOwnerExpected = contracts.LiqContractAddress(network)
}
for _, tokenId := range tokenIds {
vnftOwner, err := e.vnftContract.Contract.OwnerOf(nil, tokenId)
if err != nil {
return false, errors.Wrapf(err, "Fail to get vnft owner. network:%s", e.network)
}
if stakeType == NETH {
if vnftOwner.String() != nETHOwnerExpected {
return false, errors.New("stakeType == NETH, vnft owner is not LiquidStaking")
}
} else if stakeType == VNFT {
if vnftOwner.String() == nETHOwnerExpected {
return false, errors.New("stakeType == VNFT, vnft owner is LiquidStaking")
}
}
}
return true, nil
}