@@ -8,27 +8,99 @@ import (
8
8
"github.com/onflow/flow-go/fvm/flex/storage"
9
9
)
10
10
11
- type BaseFlexContractHandler struct {
11
+ type FOAHandler struct {
12
+ fch FlexContractHandler
13
+ address * models.FlexAddress
14
+ // TODO inject gas meter
15
+ }
16
+
17
+ // Address returns the flex address associated with the FOA account
18
+ func (h * FOAHandler ) Address () * models.FlexAddress {
19
+ return h .address
20
+ }
21
+
22
+ // Deposit deposits the token from the given vault into the Flex main vault
23
+ // and update the FOA balance with the new amount
24
+ func (h * FOAHandler ) Deposit (models.FlowTokenVault ) {
25
+ panic ("not implemented" )
26
+ }
27
+
28
+ // Withdraw deducts the balance from the FOA account and
29
+ // withdraw and return flow token from the Flex main vault.
30
+ func (h * FOAHandler ) Withdraw (models.Balance ) models.FlowTokenVault {
31
+ panic ("not implemented" )
32
+ }
33
+
34
+ // Deploy deploys a contract to the Flex environment
35
+ // the new deployed contract would be at the returned address and
36
+ // the contract data is not controlled by the FOA accounts
37
+ func (h * FOAHandler ) Deploy (code models.Code , gaslimit models.Gaslimit , balance models.Balance ) models.FlexAddress {
38
+ config := env .NewFlexConfig (
39
+ env .WithBlockNumber (env .BlockNumberForEVMRules ))
40
+ env , err := env .NewEnvironment (config , h .fch .db )
41
+ // TODO improve this
42
+ if err != nil {
43
+ panic (err )
44
+ }
45
+ // TODO check gas limit against what has been left on the transaction side
46
+ env .Deploy (h .address .ToCommon (), code , uint64 (gaslimit ), balance .InAttoFlow ())
47
+ if env .Result .Failed {
48
+ // TODO panic with a handlable error
49
+ panic ("deploy failed" )
50
+ }
51
+ return models .FlexAddress (env .Result .DeployedContractAddress )
52
+ }
53
+
54
+ // Call calls a smart contract function with the given data
55
+ // it would limit the gas used according to the limit provided
56
+ // given it doesn't goes beyond what Flow transaction allows.
57
+ // the balance would be deducted from the OFA account and would be transferred to the target address
58
+ // contract data is not controlled by the FOA accounts
59
+ func (h * FOAHandler ) Call (to models.FlexAddress , data models.Data , gaslimit models.Gaslimit , balance models.Balance ) models.Data {
60
+ config := env .NewFlexConfig (
61
+ env .WithBlockNumber (env .BlockNumberForEVMRules ))
62
+ env , err := env .NewEnvironment (config , h .fch .db )
63
+ // TODO improve this
64
+ if err != nil {
65
+ panic (err )
66
+ }
67
+ // TODO check gas limit against what has been left on the transaction side
68
+ env .Call (h .address .ToCommon (), to .ToCommon (), data , uint64 (gaslimit ), balance .InAttoFlow ())
69
+ if env .Result .Failed {
70
+ // TODO panic with a handlable error
71
+ panic ("call failed" )
72
+ }
73
+ return env .Result .RetValue
74
+ }
75
+
76
+ var _ models.FlowOwnedAccount = & FOAHandler {}
77
+
78
+ type FlexContractHandler struct {
12
79
db * storage.Database
80
+ // TODO inject what captures how much gas has been used
13
81
}
14
82
15
- var _ models.FlexContractHandler = & BaseFlexContractHandler {}
83
+ var _ models.FlexContractHandler = & FlexContractHandler {}
16
84
17
- func NewBaseFlexContractHandler (ledger atree.Ledger ) * BaseFlexContractHandler {
18
- return & BaseFlexContractHandler {
85
+ func NewFlexContractHandler (ledger atree.Ledger ) * FlexContractHandler {
86
+ return & FlexContractHandler {
19
87
db : storage .NewDatabase (ledger ),
20
88
}
21
89
}
22
90
23
- func (h BaseFlexContractHandler ) NewFlowOwnedAccount () models.FlowOwnedAccount {
91
+ func (h FlexContractHandler ) NewFlowOwnedAccount () models.FlowOwnedAccount {
92
+ // allocate a new address
93
+ // TODO check for collission
94
+ // from 20 bytes, the first 12 could be zero, the next 8 could be the output of a uuid generator (resourceID?)
95
+ // does this leads to trie depth issue?
24
96
panic ("not implemented yet" )
25
97
}
26
98
27
- func (h BaseFlexContractHandler ) LastExecutedBlock () models.FlexBlock {
99
+ func (h FlexContractHandler ) LastExecutedBlock () models.FlexBlock {
28
100
panic ("not implemented yet" )
29
101
}
30
102
31
- func (h BaseFlexContractHandler ) Run (tx []byte , coinbase models.FlexAddress ) bool {
103
+ func (h FlexContractHandler ) Run (tx []byte , coinbase models.FlexAddress ) bool {
32
104
config := env .NewFlexConfig (
33
105
env .WithCoinbase (common .Address (coinbase )),
34
106
env .WithBlockNumber (env .BlockNumberForEVMRules ))
0 commit comments