-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdeployerSimpleSC.go
More file actions
123 lines (100 loc) · 3.2 KB
/
deployerSimpleSC.go
File metadata and controls
123 lines (100 loc) · 3.2 KB
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
117
118
119
120
121
122
123
package contracts
import (
"errors"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-go/vmhost"
"math/big"
mock "github.com/multiversx/mx-chain-vm-go/mock/context"
"github.com/multiversx/mx-chain-vm-go/testcommon"
"github.com/multiversx/mx-chain-vm-go/vmhost/vmhooks"
"github.com/stretchr/testify/require"
)
// DeployContractFromSourceMock -
func DeployContractFromSourceMock(instanceMock *mock.InstanceMock, _ interface{}) {
instanceMock.AddMockMethod("deployContractFromSource", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)
t := instance.T
arguments := host.Runtime().Arguments()
if len(arguments) < 3 {
host.Runtime().SignalUserError("wrong num of arguments")
return instance
}
sourceContractAddress := arguments[0]
codeMetadata := arguments[1]
gasForInit := big.NewInt(0).SetBytes(arguments[2])
newAddress, err :=
vmhooks.DeployFromSourceContractWithTypedArgs(
host,
sourceContractAddress,
codeMetadata,
big.NewInt(0),
[][]byte{},
gasForInit.Int64(),
)
if err != nil {
if errors.Is(err, vmhost.ErrNotEnoughGas) {
host.Output().SetReturnCode(vmcommon.OutOfGas)
} else {
host.Runtime().FailExecution(err)
}
return instance
}
require.NotNil(t, newAddress)
host.Output().Finish(newAddress)
return instance
})
}
// InitMockMethod -
func InitMockMethod(instanceMock *mock.InstanceMock, config interface{}) {
testConfig := config.(*testcommon.TestConfig)
instanceMock.AddMockMethod("init", testcommon.SimpleWasteGasMockMethod(instanceMock, testConfig.GasUsedByInit))
}
// UpgradeMockMethod -
func UpgradeMockMethod(instanceMock *mock.InstanceMock, config interface{}) {
testConfig := config.(*testcommon.TestConfig)
instanceMock.AddMockMethod("upgrade", testcommon.SimpleWasteGasMockMethod(instanceMock, testConfig.GasUsedByInit))
}
// CallbackTestConfig -
type CallbackTestConfig interface {
CallbackFails() bool
}
// CallbackMockMethodThatCouldFail -
func CallbackMockMethodThatCouldFail(instanceMock *mock.InstanceMock, config interface{}) {
testConfig := config.(*testcommon.TestConfig)
instanceMock.AddMockMethod("callBack", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)
if testConfig.CallbackFails {
host.Runtime().SignalUserError("fail")
return instance
}
return instance
})
}
// UpdateContractFromSourceMock -
func UpdateContractFromSourceMock(instanceMock *mock.InstanceMock, _ interface{}) {
instanceMock.AddMockMethod("updateContractFromSource", func() *mock.InstanceMock {
host := instanceMock.Host
instance := mock.GetMockInstance(host)
arguments := host.Runtime().Arguments()
if len(arguments) < 4 {
host.Runtime().SignalUserError("wrong num of arguments")
return instance
}
sourceContractAddress := arguments[0]
destinationContractAddress := arguments[1]
codeMetadata := arguments[2]
gasForInit := big.NewInt(0).SetBytes(arguments[3])
vmhooks.UpgradeFromSourceContractWithTypedArgs(
host,
sourceContractAddress,
destinationContractAddress,
big.NewInt(0).Bytes(),
[][]byte{},
gasForInit.Int64(),
codeMetadata,
)
return instance
})
}