github.com/FUSIONFoundation/efsn@v3.6.2-0.20200916075423-dbb5dd5d2cc7+incompatible/common/fsntypes.go (about) 1 package common 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/big" 7 ) 8 9 // SystemAssetID wacom 10 var SystemAssetID = HexToHash("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff") 11 12 // OwnerUSANAssetID wacom 13 var OwnerUSANAssetID = HexToHash("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe") 14 15 var ( 16 // FSNCallAddress wacom 17 FSNCallAddress = HexToAddress("0xffffffffffffffffffffffffffffffffffffffff") 18 19 // TicketLogAddress wacom (deprecated) 20 TicketLogAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffffe") 21 22 // NotationKeyAddress wacom 23 NotationKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffffd") 24 25 // AssetKeyAddress wacom 26 AssetKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffffc") 27 28 // TicketKeyAddress wacom 29 TicketKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffffb") 30 31 // SwapKeyAddress wacom 32 SwapKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffffa") 33 34 // MultiSwapKeyAddress wacom 35 MultiSwapKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffff9") 36 37 // ReportIllegalAddress wacom 38 ReportKeyAddress = HexToAddress("0xfffffffffffffffffffffffffffffffffffffff8") 39 ) 40 41 func (addr Address) IsSpecialKeyAddress() bool { 42 return addr == TicketKeyAddress || 43 addr == NotationKeyAddress || 44 addr == AssetKeyAddress || 45 addr == SwapKeyAddress || 46 addr == MultiSwapKeyAddress || 47 addr == ReportKeyAddress 48 } 49 50 var ( 51 // AutoBuyTicket wacom 52 AutoBuyTicket = false 53 // AutoBuyTicketChan wacom 54 AutoBuyTicketChan = make(chan int, 10) 55 56 // ReportIllegal wacom 57 ReportIllegalChan = make(chan []byte) 58 ) 59 60 // FSNCallFunc wacom 61 type FSNCallFunc uint8 62 63 const ( 64 // GenNotationFunc wacom 65 GenNotationFunc = iota 66 // GenAssetFunc wacom 67 GenAssetFunc 68 // SendAssetFunc wacom 69 SendAssetFunc 70 // TimeLockFunc wacom 71 TimeLockFunc 72 // BuyTicketFunc wacom 73 BuyTicketFunc 74 // OldAssetValueChangeFunc wacom (deprecated) 75 OldAssetValueChangeFunc 76 // MakeSwapFunc wacom 77 MakeSwapFunc 78 // RecallSwapFunc wacom 79 RecallSwapFunc 80 // TakeSwapFunc wacom 81 TakeSwapFunc 82 // EmptyFunc wacom 83 EmptyFunc 84 // MakeSwapFuncExt wacom 85 MakeSwapFuncExt 86 // TakeSwapFuncExt wacom 87 TakeSwapFuncExt 88 // AssetValueChangeFunc wacom 89 AssetValueChangeFunc 90 // MakeMultiSwapFunc wacom 91 MakeMultiSwapFunc 92 // RecallMultiSwapFunc wacom 93 RecallMultiSwapFunc 94 // TakeMultiSwapFunc wacom 95 TakeMultiSwapFunc 96 // ReportIllegalFunc wacom 97 ReportIllegalFunc 98 // UnknownFunc 99 UnknownFunc = 0xff 100 ) 101 102 func (f FSNCallFunc) Name() string { 103 switch f { 104 case GenNotationFunc: 105 return "GenNotationFunc" 106 case GenAssetFunc: 107 return "GenAssetFunc" 108 case SendAssetFunc: 109 return "SendAssetFunc" 110 case TimeLockFunc: 111 return "TimeLockFunc" 112 case BuyTicketFunc: 113 return "BuyTicketFunc" 114 case OldAssetValueChangeFunc: 115 return "OldAssetValueChangeFunc" 116 case MakeSwapFunc: 117 return "MakeSwapFunc" 118 case RecallSwapFunc: 119 return "RecallSwapFunc" 120 case TakeSwapFunc: 121 return "TakeSwapFunc" 122 case EmptyFunc: 123 return "EmptyFunc" 124 case MakeSwapFuncExt: 125 return "MakeSwapFuncExt" 126 case TakeSwapFuncExt: 127 return "TakeSwapFuncExt" 128 case AssetValueChangeFunc: 129 return "AssetValueChangeFunc" 130 case MakeMultiSwapFunc: 131 return "MakeMultiSwapFunc" 132 case RecallMultiSwapFunc: 133 return "RecallMultiSwapFunc" 134 case TakeMultiSwapFunc: 135 return "TakeMultiSwapFunc" 136 case ReportIllegalFunc: 137 return "ReportIllegalFunc" 138 } 139 return "Unknown" 140 } 141 142 func IsFsnCall(to *Address) bool { 143 return to != nil && *to == FSNCallAddress 144 } 145 146 func GetFsnCallFee(to *Address, funcType FSNCallFunc) *big.Int { 147 fee := big.NewInt(0) 148 if !IsFsnCall(to) { 149 return fee 150 } 151 switch funcType { 152 case GenNotationFunc: 153 fee = big.NewInt(100000000000000000) // 0.1 FSN 154 case GenAssetFunc: 155 fee = big.NewInt(10000000000000000) // 0.01 FSN 156 case MakeSwapFunc, MakeSwapFuncExt, MakeMultiSwapFunc: 157 fee = big.NewInt(1000000000000000) // 0.001 FSN 158 case TimeLockFunc: 159 fee = big.NewInt(1000000000000000) // 0.001 FSN 160 } 161 return fee 162 } 163 164 // ToAsset wacom 165 func (p *GenAssetParam) ToAsset() Asset { 166 return Asset{ 167 Name: p.Name, 168 Symbol: p.Symbol, 169 Decimals: p.Decimals, 170 Total: p.Total, 171 CanChange: p.CanChange, 172 Description: p.Description, 173 } 174 } 175 176 // Asset wacom 177 type Asset struct { 178 ID Hash 179 Owner Address 180 Name string 181 Symbol string 182 Decimals uint8 183 Total *big.Int `json:",string"` 184 CanChange bool 185 Description string 186 } 187 188 func (u *Asset) MarshalJSON() ([]byte, error) { 189 return json.Marshal(&struct { 190 ID Hash 191 Owner Address 192 Name string 193 Symbol string 194 Decimals uint8 195 Total string 196 CanChange bool 197 Description string 198 }{ 199 ID: u.ID, 200 Owner: u.Owner, 201 Name: u.Name, 202 Symbol: u.Symbol, 203 Decimals: u.Decimals, 204 Total: u.Total.String(), 205 CanChange: u.CanChange, 206 Description: u.Description, 207 }) 208 } 209 210 // SystemAsset wacom 211 var SystemAsset = Asset{ 212 Name: "Fusion", 213 Symbol: "FSN", 214 Decimals: 18, 215 Total: new(big.Int).Mul(big.NewInt(81920000), big.NewInt(1000000000000000000)), 216 ID: SystemAssetID, 217 Description: "https://fusion.org", 218 } 219 220 // Swap wacom 221 type Swap struct { 222 ID Hash 223 Owner Address 224 FromAssetID Hash 225 FromStartTime uint64 226 FromEndTime uint64 227 MinFromAmount *big.Int `json:",string"` 228 ToAssetID Hash 229 ToStartTime uint64 230 ToEndTime uint64 231 MinToAmount *big.Int `json:",string"` 232 SwapSize *big.Int `json:",string"` 233 Targes []Address 234 Time *big.Int // Provides information for TIME 235 Description string 236 Notation uint64 237 } 238 239 // MultiSwap wacom 240 type MultiSwap struct { 241 ID Hash 242 Owner Address 243 FromAssetID []Hash 244 FromStartTime []uint64 245 FromEndTime []uint64 246 MinFromAmount []*big.Int `json:",string"` 247 ToAssetID []Hash 248 ToStartTime []uint64 249 ToEndTime []uint64 250 MinToAmount []*big.Int `json:",string"` 251 SwapSize *big.Int `json:",string"` 252 Targes []Address 253 Time *big.Int // Provides information for TIME 254 Description string 255 Notation uint64 256 } 257 258 func CheckSwapTargets(targets []Address, addr Address) error { 259 if len(targets) == 0 { 260 return nil 261 } 262 for _, target := range targets { 263 if addr == target { 264 return nil 265 } 266 } 267 return fmt.Errorf("swap taker does not match the specified targets") 268 } 269 270 // KeyValue wacom 271 type KeyValue struct { 272 Key string 273 Value interface{} 274 } 275 276 // NewKeyValue wacom 277 func NewKeyValue(name string, v interface{}) *KeyValue { 278 return &KeyValue{Key: name, Value: v} 279 }