code.vegaprotocol.io/vega@v0.79.0/core/datasource/external/ethcall/common/trigger.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package common 17 18 import ( 19 "fmt" 20 21 vegapb "code.vegaprotocol.io/vega/protos/vega" 22 ) 23 24 type Trigger interface { 25 IntoTriggerProto() *vegapb.EthCallTrigger 26 DeepClone() Trigger 27 String() string 28 } 29 30 func TriggerFromProto(proto *vegapb.EthCallTrigger) (Trigger, error) { 31 if proto == nil { 32 return nil, fmt.Errorf("trigger proto is nil") 33 } 34 35 switch t := proto.Trigger.(type) { 36 case *vegapb.EthCallTrigger_TimeTrigger: 37 return TimeTriggerFromProto(t.TimeTrigger), nil 38 default: 39 return nil, fmt.Errorf("unknown trigger type: %T", proto.Trigger) 40 } 41 } 42 43 type TimeTrigger struct { 44 Initial uint64 45 Every uint64 // 0 = don't repeat 46 Until uint64 // 0 = forever 47 } 48 49 func (e TimeTrigger) IntoProto() *vegapb.EthTimeTrigger { 50 var initial, every, until *uint64 51 52 if e.Initial != 0 { 53 initial = &e.Initial 54 } 55 56 if e.Every != 0 { 57 every = &e.Every 58 } 59 60 if e.Until != 0 { 61 until = &e.Until 62 } 63 64 return &vegapb.EthTimeTrigger{ 65 Initial: initial, 66 Every: every, 67 Until: until, 68 } 69 } 70 71 func (e TimeTrigger) DeepClone() Trigger { 72 return e 73 } 74 75 func (e TimeTrigger) IntoTriggerProto() *vegapb.EthCallTrigger { 76 return &vegapb.EthCallTrigger{ 77 Trigger: &vegapb.EthCallTrigger_TimeTrigger{ 78 TimeTrigger: e.IntoProto(), 79 }, 80 } 81 } 82 83 func (e TimeTrigger) String() string { 84 return fmt.Sprintf( 85 "initial(%d) every(%d) until(%d)", 86 e.Initial, 87 e.Every, 88 e.Until, 89 ) 90 } 91 92 func TimeTriggerFromProto(protoTrigger *vegapb.EthTimeTrigger) TimeTrigger { 93 trigger := TimeTrigger{} 94 if protoTrigger == nil { 95 return trigger 96 } 97 98 if protoTrigger.Initial != nil { 99 trigger.Initial = *protoTrigger.Initial 100 } 101 if protoTrigger.Every != nil { 102 trigger.Every = *protoTrigger.Every 103 } 104 if protoTrigger.Until != nil { 105 trigger.Until = *protoTrigger.Until 106 } 107 return trigger 108 }