github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/wasm/keeper/options.go (about) 1 package keeper 2 3 import ( 4 "fmt" 5 6 "github.com/prometheus/client_golang/prometheus" 7 8 "github.com/fibonacci-chain/fbc/x/wasm/types" 9 ) 10 11 type optsFn func(*Keeper) 12 13 func (f optsFn) apply(keeper *Keeper) { 14 f(keeper) 15 } 16 17 // WithWasmEngine is an optional constructor parameter to replace the default wasmVM engine with the 18 // given one. 19 func WithWasmEngine(x types.WasmerEngine) Option { 20 return optsFn(func(k *Keeper) { 21 k.wasmVM = x 22 }) 23 } 24 25 // WithMessageHandler is an optional constructor parameter to set a custom handler for wasmVM messages. 26 // This option should not be combined with Option `WithMessageEncoders` or `WithMessageHandlerDecorator` 27 func WithMessageHandler(x Messenger) Option { 28 return optsFn(func(k *Keeper) { 29 k.messenger = x 30 }) 31 } 32 33 // WithMessageHandlerDecorator is an optional constructor parameter to decorate the wasm handler for wasmVM messages. 34 // This option should not be combined with Option `WithMessageEncoders` or `WithMessageHandler` 35 func WithMessageHandlerDecorator(d func(old Messenger) Messenger) Option { 36 return optsFn(func(k *Keeper) { 37 k.messenger = d(k.messenger) 38 }) 39 } 40 41 // WithQueryHandler is an optional constructor parameter to set custom query handler for wasmVM requests. 42 // This option should not be combined with Option `WithQueryPlugins` or `WithQueryHandlerDecorator` 43 func WithQueryHandler(x WasmVMQueryHandler) Option { 44 return optsFn(func(k *Keeper) { 45 k.wasmVMQueryHandler = x 46 }) 47 } 48 49 // WithQueryHandlerDecorator is an optional constructor parameter to decorate the default wasm query handler for wasmVM requests. 50 // This option should not be combined with Option `WithQueryPlugins` or `WithQueryHandler` 51 func WithQueryHandlerDecorator(d func(old WasmVMQueryHandler) WasmVMQueryHandler) Option { 52 return optsFn(func(k *Keeper) { 53 k.wasmVMQueryHandler = d(k.wasmVMQueryHandler) 54 }) 55 } 56 57 // WithQueryPlugins is an optional constructor parameter to pass custom query plugins for wasmVM requests. 58 // This option expects the default `QueryHandler` set an should not be combined with Option `WithQueryHandler` or `WithQueryHandlerDecorator`. 59 func WithQueryPlugins(x *QueryPlugins) Option { 60 return optsFn(func(k *Keeper) { 61 q, ok := k.wasmVMQueryHandler.(QueryPlugins) 62 if !ok { 63 panic(fmt.Sprintf("Unsupported query handler type: %T", k.wasmVMQueryHandler)) 64 } 65 k.wasmVMQueryHandler = q.Merge(x) 66 }) 67 } 68 69 // WithMessageEncoders is an optional constructor parameter to pass custom message encoder to the default wasm message handler. 70 // This option expects the `DefaultMessageHandler` set and should not be combined with Option `WithMessageHandler` or `WithMessageHandlerDecorator`. 71 func WithMessageEncoders(x *MessageEncoders) Option { 72 return optsFn(func(k *Keeper) { 73 q, ok := k.messenger.(*MessageHandlerChain) 74 if !ok { 75 panic(fmt.Sprintf("Unsupported message handler type: %T", k.messenger)) 76 } 77 s, ok := q.handlers[0].(SDKMessageHandler) 78 if !ok { 79 panic(fmt.Sprintf("Unexpected message handler type: %T", q.handlers[0])) 80 } 81 e, ok := s.encoders.(MessageEncoders) 82 if !ok { 83 panic(fmt.Sprintf("Unsupported encoder type: %T", s.encoders)) 84 } 85 s.encoders = e.Merge(x) 86 q.handlers[0] = s 87 }) 88 } 89 90 // WithCoinTransferrer is an optional constructor parameter to set a custom coin transferrer 91 func WithCoinTransferrer(x CoinTransferrer) Option { 92 return optsFn(func(k *Keeper) { 93 k.bank = x 94 }) 95 } 96 97 func WithVMCacheMetrics(r prometheus.Registerer) Option { 98 return optsFn(func(k *Keeper) { 99 NewWasmVMMetricsCollector(k.wasmVM).Register(r) 100 }) 101 } 102 103 // WithGasRegister set a new gas register to implement custom gas costs. 104 // When the "gas multiplier" for wasmvm gas conversion is modified inside the new register, 105 // make sure to also use `WithApiCosts` option for non default values 106 func WithGasRegister(x GasRegister) Option { 107 return optsFn(func(k *Keeper) { 108 k.gasRegister = x 109 }) 110 } 111 112 // WithAPICosts sets custom api costs. Amounts are in cosmwasm gas Not SDK gas. 113 func WithAPICosts(human, canonical uint64) Option { 114 return optsFn(func(_ *Keeper) { 115 costHumanize = human 116 costCanonical = canonical 117 }) 118 } 119 120 // WithMaxQueryStackSize overwrites the default limit for maximum query stacks 121 func WithMaxQueryStackSize(m uint32) Option { 122 return optsFn(func(k *Keeper) { 123 k.maxQueryStackSize = m 124 }) 125 }