github.com/koko1123/flow-go-1@v0.29.6/fvm/blueprints/fees.go (about) 1 package blueprints 2 3 import ( 4 _ "embed" 5 "encoding/hex" 6 "fmt" 7 8 "github.com/onflow/cadence" 9 jsoncdc "github.com/onflow/cadence/encoding/json" 10 "github.com/onflow/cadence/runtime/common" 11 "github.com/onflow/flow-core-contracts/lib/go/contracts" 12 "github.com/onflow/flow-core-contracts/lib/go/templates" 13 14 "github.com/koko1123/flow-go-1/model/flow" 15 ) 16 17 var TransactionFeesExecutionEffortWeightsPath = cadence.Path{ 18 Domain: common.PathDomainStorage.Identifier(), 19 Identifier: "executionEffortWeights", 20 } 21 var TransactionFeesExecutionMemoryWeightsPath = cadence.Path{ 22 Domain: common.PathDomainStorage.Identifier(), 23 Identifier: "executionMemoryWeights", 24 } 25 var TransactionFeesExecutionMemoryLimitPath = cadence.Path{ 26 Domain: common.PathDomainStorage.Identifier(), 27 Identifier: "executionMemoryLimit", 28 } 29 30 //go:embed scripts/deployTxFeesTransactionTemplate.cdc 31 var deployTxFeesTransactionTemplate string 32 33 //go:embed scripts/setupParametersTransactionTemplate.cdc 34 var setupParametersTransactionTemplate string 35 36 //go:embed scripts/setupStorageForServiceAccountsTemplate.cdc 37 var setupStorageForServiceAccountsTemplate string 38 39 //go:embed scripts/setupFeesTransactionTemplate.cdc 40 var setupFeesTransactionTemplate string 41 42 //go:embed scripts/setExecutionMemoryLimit.cdc 43 var setExecutionMemoryLimit string 44 45 func DeployTxFeesContractTransaction(service, fungibleToken, flowToken, storageFees, flowFees flow.Address) *flow.TransactionBody { 46 contract := contracts.FlowFees( 47 fungibleToken.HexWithPrefix(), 48 flowToken.HexWithPrefix(), 49 storageFees.HexWithPrefix(), 50 ) 51 52 return flow.NewTransactionBody(). 53 SetScript([]byte(deployTxFeesTransactionTemplate)). 54 AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))). 55 AddAuthorizer(flowFees). 56 AddAuthorizer(service) 57 } 58 59 func DeployStorageFeesContractTransaction(service flow.Address, contract []byte) *flow.TransactionBody { 60 return DeployContractTransaction(service, contract, "FlowStorageFees") 61 } 62 63 func SetupParametersTransaction( 64 service flow.Address, 65 addressCreationFee, 66 minimumStorageReservation, 67 storagePerFlow cadence.UFix64, 68 restrictedAccountCreationEnabled cadence.Bool, 69 ) *flow.TransactionBody { 70 addressCreationFeeArg, err := jsoncdc.Encode(addressCreationFee) 71 if err != nil { 72 panic(fmt.Sprintf("failed to encode address creation fee: %s", err.Error())) 73 } 74 minimumStorageReservationArg, err := jsoncdc.Encode(minimumStorageReservation) 75 if err != nil { 76 panic(fmt.Sprintf("failed to encode minimum storage reservation: %s", err.Error())) 77 } 78 storagePerFlowArg, err := jsoncdc.Encode(storagePerFlow) 79 if err != nil { 80 panic(fmt.Sprintf("failed to encode storage ratio: %s", err.Error())) 81 } 82 restrictedAccountCreationEnabledArg, err := jsoncdc.Encode(restrictedAccountCreationEnabled) 83 if err != nil { 84 panic(fmt.Sprintf("failed to encode restrictedAccountCreationEnabled: %s", err.Error())) 85 } 86 87 return flow.NewTransactionBody(). 88 SetScript([]byte(templates.ReplaceAddresses(setupParametersTransactionTemplate, 89 templates.Environment{ 90 StorageFeesAddress: service.Hex(), 91 ServiceAccountAddress: service.Hex(), 92 })), 93 ). 94 AddArgument(addressCreationFeeArg). 95 AddArgument(minimumStorageReservationArg). 96 AddArgument(storagePerFlowArg). 97 AddArgument(restrictedAccountCreationEnabledArg). 98 AddAuthorizer(service) 99 } 100 101 func SetupStorageForServiceAccountsTransaction( 102 service, fungibleToken, flowToken, feeContract flow.Address, 103 ) *flow.TransactionBody { 104 return flow.NewTransactionBody(). 105 SetScript([]byte(templates.ReplaceAddresses(setupStorageForServiceAccountsTemplate, 106 templates.Environment{ 107 ServiceAccountAddress: service.Hex(), 108 StorageFeesAddress: service.Hex(), 109 FungibleTokenAddress: fungibleToken.Hex(), 110 FlowTokenAddress: flowToken.Hex(), 111 })), 112 ). 113 AddAuthorizer(service). 114 AddAuthorizer(fungibleToken). 115 AddAuthorizer(flowToken). 116 AddAuthorizer(feeContract) 117 } 118 119 func SetupFeesTransaction( 120 service flow.Address, 121 flowFees flow.Address, 122 surgeFactor, 123 inclusionEffortCost, 124 executionEffortCost cadence.UFix64, 125 ) *flow.TransactionBody { 126 surgeFactorArg, err := jsoncdc.Encode(surgeFactor) 127 if err != nil { 128 panic(fmt.Sprintf("failed to encode surge factor: %s", err.Error())) 129 } 130 inclusionEffortCostArg, err := jsoncdc.Encode(inclusionEffortCost) 131 if err != nil { 132 panic(fmt.Sprintf("failed to encode inclusion effort cost: %s", err.Error())) 133 } 134 executionEffortCostArg, err := jsoncdc.Encode(executionEffortCost) 135 if err != nil { 136 panic(fmt.Sprintf("failed to encode execution effort cost: %s", err.Error())) 137 } 138 139 return flow.NewTransactionBody(). 140 SetScript([]byte(templates.ReplaceAddresses(setupFeesTransactionTemplate, 141 templates.Environment{ 142 FlowFeesAddress: flowFees.Hex(), 143 })), 144 ). 145 AddArgument(surgeFactorArg). 146 AddArgument(inclusionEffortCostArg). 147 AddArgument(executionEffortCostArg). 148 AddAuthorizer(service) 149 } 150 151 // SetExecutionEffortWeightsTransaction creates a transaction that sets up weights for the weighted Meter. 152 func SetExecutionEffortWeightsTransaction( 153 service flow.Address, 154 weights map[uint]uint64, 155 ) (*flow.TransactionBody, error) { 156 return setExecutionWeightsTransaction( 157 service, 158 weights, 159 TransactionFeesExecutionEffortWeightsPath, 160 ) 161 } 162 163 // SetExecutionMemoryWeightsTransaction creates a transaction that sets up weights for the weighted Meter. 164 func SetExecutionMemoryWeightsTransaction( 165 service flow.Address, 166 weights map[uint]uint64, 167 ) (*flow.TransactionBody, error) { 168 return setExecutionWeightsTransaction( 169 service, 170 weights, 171 TransactionFeesExecutionMemoryWeightsPath, 172 ) 173 } 174 175 func setExecutionWeightsTransaction( 176 service flow.Address, 177 weights map[uint]uint64, 178 path cadence.Path, 179 ) (*flow.TransactionBody, error) { 180 newWeightsKeyValuePairs := make([]cadence.KeyValuePair, len(weights)) 181 i := 0 182 for k, w := range weights { 183 newWeightsKeyValuePairs[i] = cadence.KeyValuePair{ 184 Key: cadence.UInt64(k), 185 Value: cadence.UInt64(w), 186 } 187 i += 1 188 } 189 newWeights, err := jsoncdc.Encode(cadence.NewDictionary(newWeightsKeyValuePairs)) 190 if err != nil { 191 return nil, err 192 } 193 194 storagePath, err := jsoncdc.Encode(path) 195 if err != nil { 196 return nil, err 197 } 198 199 tx := flow.NewTransactionBody(). 200 SetScript([]byte(setExecutionWeightsScript)). 201 AddArgument(newWeights). 202 AddArgument(storagePath). 203 AddAuthorizer(service) 204 205 return tx, nil 206 } 207 208 //go:embed scripts/setExecutionWeightsScript.cdc 209 var setExecutionWeightsScript string 210 211 func SetExecutionMemoryLimitTransaction( 212 service flow.Address, 213 limit uint64, 214 ) (*flow.TransactionBody, error) { 215 newLimit, err := jsoncdc.Encode(cadence.UInt64(limit)) 216 if err != nil { 217 return nil, err 218 } 219 220 storagePath, err := jsoncdc.Encode(TransactionFeesExecutionMemoryLimitPath) 221 if err != nil { 222 return nil, err 223 } 224 225 tx := flow.NewTransactionBody(). 226 SetScript([]byte(setExecutionMemoryLimit)). 227 AddArgument(newLimit). 228 AddArgument(storagePath). 229 AddAuthorizer(service) 230 231 return tx, nil 232 }