-
Notifications
You must be signed in to change notification settings - Fork 199
/
esdtPause.go
116 lines (96 loc) · 2.94 KB
/
esdtPause.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package builtInFunctions
import (
"bytes"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data/state"
"github.com/ElrondNetwork/elrond-go/process"
"github.com/ElrondNetwork/elrond-go/vm"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
var _ process.BuiltinFunction = (*esdtPause)(nil)
type esdtPause struct {
keyPrefix []byte
pause bool
accounts state.AccountsAdapter
}
// NewESDTPauseFunc returns the esdt pause/un-pause built-in function component
func NewESDTPauseFunc(
accounts state.AccountsAdapter,
pause bool,
) (*esdtPause, error) {
if check.IfNil(accounts) {
return nil, process.ErrNilAccountsAdapter
}
e := &esdtPause{
keyPrefix: []byte(core.ElrondProtectedKeyPrefix + esdtKeyIdentifier),
pause: pause,
accounts: accounts,
}
return e, nil
}
// ProcessBuiltinFunction resolves ESDT pause function call
func (e *esdtPause) ProcessBuiltinFunction(
_, _ state.UserAccountHandler,
vmInput *vmcommon.ContractCallInput,
) (*vmcommon.VMOutput, error) {
if vmInput == nil {
return nil, process.ErrNilVmInput
}
if vmInput.CallValue.Cmp(zero) != 0 {
return nil, process.ErrBuiltInFunctionCalledWithValue
}
if len(vmInput.Arguments) != 1 {
return nil, process.ErrInvalidArguments
}
if !bytes.Equal(vmInput.CallerAddr, vm.ESDTSCAddress) {
return nil, process.ErrAddressIsNotESDTSystemSC
}
if !core.IsSystemAccountAddress(vmInput.RecipientAddr) {
return nil, process.ErrOnlySystemAccountAccepted
}
esdtTokenKey := append(e.keyPrefix, vmInput.Arguments[0]...)
log.Trace(vmInput.Function, "sender", vmInput.CallerAddr, "receiver", vmInput.RecipientAddr, "token", esdtTokenKey)
err := e.togglePause(esdtTokenKey)
if err != nil {
return nil, err
}
vmOutput := &vmcommon.VMOutput{}
return vmOutput, nil
}
func (e *esdtPause) togglePause(token []byte) error {
systemSCAccount, err := e.getSystemAccount()
if err != nil {
return err
}
val, _ := systemSCAccount.DataTrieTracker().RetrieveValue(token)
esdtMetaData := ESDTGlobalMetadataFromBytes(val)
esdtMetaData.Paused = e.pause
systemSCAccount.DataTrieTracker().SaveKeyValue(token, esdtMetaData.ToBytes())
return nil
}
func (e *esdtPause) getSystemAccount() (state.UserAccountHandler, error) {
systemSCAccount, err := e.accounts.LoadAccount(core.SystemAccountAddress)
if err != nil {
return nil, err
}
userAcc, ok := systemSCAccount.(state.UserAccountHandler)
if !ok {
return nil, process.ErrWrongTypeAssertion
}
return userAcc, nil
}
// IsPaused returns true if the token is paused
func (e *esdtPause) IsPaused(token []byte) bool {
systemSCAccount, err := e.getSystemAccount()
if err != nil {
return false
}
val, _ := systemSCAccount.DataTrieTracker().RetrieveValue(token)
esdtMetaData := ESDTGlobalMetadataFromBytes(val)
return esdtMetaData.Paused
}
// IsInterfaceNil returns true if underlying object in nil
func (e *esdtPause) IsInterfaceNil() bool {
return e == nil
}