github.com/neatio-net/neatio@v1.7.3-0.20231114194659-f4d7a2226baa/neatabi/abi/abi.go (about) 1 package abi 2 3 import ( 4 "math/big" 5 "strings" 6 7 "github.com/neatio-net/neatio/chain/accounts/abi" 8 "github.com/neatio-net/neatio/utilities/common" 9 ) 10 11 type FunctionType struct { 12 id int 13 cross bool 14 main bool 15 side bool 16 } 17 18 var ( 19 CreateSideChain = FunctionType{0, true, true, false} 20 JoinSideChain = FunctionType{1, true, true, false} 21 DepositInMainChain = FunctionType{2, true, true, false} 22 DepositInSideChain = FunctionType{3, true, false, true} 23 WithdrawFromSideChain = FunctionType{4, true, false, true} 24 WithdrawFromMainChain = FunctionType{5, true, true, false} 25 SaveDataToMainChain = FunctionType{6, true, true, false} 26 SetBlockReward = FunctionType{7, true, false, true} 27 28 VoteNextEpoch = FunctionType{10, false, true, true} 29 RevealVote = FunctionType{11, false, true, true} 30 Delegate = FunctionType{12, false, true, true} 31 UnDelegate = FunctionType{13, false, true, true} 32 Register = FunctionType{14, false, true, true} 33 UnRegister = FunctionType{15, false, true, true} 34 EditValidator = FunctionType{16, false, true, true} 35 WithdrawReward = FunctionType{17, false, true, true} 36 UnBanned = FunctionType{18, false, true, true} 37 SetCommission = FunctionType{19, false, true, true} 38 SetAddress = FunctionType{20, false, true, true} 39 40 Unknown = FunctionType{-1, false, false, false} 41 ) 42 43 func (t FunctionType) IsCrossChainType() bool { 44 return t.cross 45 } 46 47 func (t FunctionType) AllowInMainChain() bool { 48 return t.main 49 } 50 51 func (t FunctionType) AllowInSideChain() bool { 52 return t.side 53 } 54 55 func (t FunctionType) RequiredGas() uint64 { 56 switch t { 57 case CreateSideChain: 58 return 42000 59 case JoinSideChain: 60 return 21000 61 case DepositInMainChain: 62 return 42000 63 case DepositInSideChain: 64 return 0 65 case WithdrawFromSideChain: 66 return 42000 67 case WithdrawFromMainChain: 68 return 0 69 case SaveDataToMainChain: 70 return 0 71 case VoteNextEpoch: 72 return 21000 73 case RevealVote: 74 return 21000 75 case Delegate, UnDelegate, Register, UnRegister: 76 return 21000 77 case SetBlockReward: 78 return 21000 79 case EditValidator: 80 return 21000 81 case WithdrawReward: 82 return 21000 83 case UnBanned: 84 return 21000 85 case SetCommission: 86 return 21000 87 case SetAddress: 88 return 21000 89 default: 90 return 0 91 } 92 } 93 94 func (t FunctionType) String() string { 95 switch t { 96 case CreateSideChain: 97 return "CreateSideChain" 98 case JoinSideChain: 99 return "JoinSideChain" 100 case DepositInMainChain: 101 return "DepositInMainChain" 102 case DepositInSideChain: 103 return "DepositInSideChain" 104 case WithdrawFromSideChain: 105 return "WithdrawFromSideChain" 106 case WithdrawFromMainChain: 107 return "WithdrawFromMainChain" 108 case SaveDataToMainChain: 109 return "SaveDataToMainChain" 110 case VoteNextEpoch: 111 return "VoteNextEpoch" 112 case RevealVote: 113 return "RevealVote" 114 case Delegate: 115 return "Delegate" 116 case UnDelegate: 117 return "UnDelegate" 118 case Register: 119 return "Register" 120 case UnRegister: 121 return "UnRegister" 122 case SetBlockReward: 123 return "SetBlockReward" 124 case EditValidator: 125 return "EditValidator" 126 case WithdrawReward: 127 return "WithdrawReward" 128 case UnBanned: 129 return "UnBanned" 130 case SetCommission: 131 return "SetCommission" 132 case SetAddress: 133 return "SetAddress" 134 default: 135 return "UnKnown" 136 } 137 } 138 139 func StringToFunctionType(s string) FunctionType { 140 switch s { 141 case "CreateSideChain": 142 return CreateSideChain 143 case "JoinSideChain": 144 return JoinSideChain 145 case "DepositInMainChain": 146 return DepositInMainChain 147 case "DepositInSideChain": 148 return DepositInSideChain 149 case "WithdrawFromSideChain": 150 return WithdrawFromSideChain 151 case "WithdrawFromMainChain": 152 return WithdrawFromMainChain 153 case "SaveDataToMainChain": 154 return SaveDataToMainChain 155 case "VoteNextEpoch": 156 return VoteNextEpoch 157 case "RevealVote": 158 return RevealVote 159 case "Delegate": 160 return Delegate 161 case "UnDelegate": 162 return UnDelegate 163 case "Register": 164 return Register 165 case "UnRegister": 166 return UnRegister 167 case "SetBlockReward": 168 return SetBlockReward 169 case "EditValidator": 170 return EditValidator 171 case "WithdrawReward": 172 return WithdrawReward 173 case "UnBanned": 174 return UnBanned 175 case "SetCommission": 176 return SetCommission 177 case "SetAddress": 178 return SetAddress 179 default: 180 return Unknown 181 } 182 } 183 184 type CreateSideChainArgs struct { 185 ChainId string 186 MinValidators uint16 187 MinDepositAmount *big.Int 188 StartBlock *big.Int 189 EndBlock *big.Int 190 } 191 192 type JoinSideChainArgs struct { 193 PubKey []byte 194 ChainId string 195 Signature []byte 196 } 197 198 type DepositInMainChainArgs struct { 199 ChainId string 200 } 201 202 type DepositInSideChainArgs struct { 203 ChainId string 204 TxHash common.Hash 205 } 206 207 type WithdrawFromSideChainArgs struct { 208 ChainId string 209 } 210 211 type WithdrawFromMainChainArgs struct { 212 ChainId string 213 Amount *big.Int 214 TxHash common.Hash 215 } 216 217 type VoteNextEpochArgs struct { 218 VoteHash common.Hash 219 } 220 221 type RevealVoteArgs struct { 222 PubKey []byte 223 Amount *big.Int 224 Salt string 225 Signature []byte 226 } 227 228 type DelegateArgs struct { 229 Candidate common.Address 230 } 231 232 type UnDelegateArgs struct { 233 Candidate common.Address 234 Amount *big.Int 235 } 236 237 type RegisterArgs struct { 238 Pubkey []byte 239 Signature []byte 240 Commission uint8 241 } 242 243 type SetBlockRewardArgs struct { 244 ChainId string 245 Reward *big.Int 246 } 247 248 type EditValidatorArgs struct { 249 Moniker string 250 Website string 251 Identity string 252 Details string 253 } 254 255 type WithdrawRewardArgs struct { 256 DelegateAddress common.Address 257 Amount *big.Int 258 } 259 260 type UnBannedArgs struct { 261 } 262 263 type SetCommissionArgs struct { 264 Commission uint8 265 } 266 267 type SetAddressArgs struct { 268 FAddress common.Address 269 } 270 271 const jsonChainABI = ` 272 [ 273 { 274 "type": "function", 275 "name": "CreateSideChain", 276 "constant": false, 277 "inputs": [ 278 { 279 "name": "chainId", 280 "type": "string" 281 }, 282 { 283 "name": "minValidators", 284 "type": "uint16" 285 }, 286 { 287 "name": "minDepositAmount", 288 "type": "uint256" 289 }, 290 { 291 "name": "startBlock", 292 "type": "uint256" 293 }, 294 { 295 "name": "endBlock", 296 "type": "uint256" 297 } 298 ] 299 }, 300 { 301 "type": "function", 302 "name": "JoinSideChain", 303 "constant": false, 304 "inputs": [ 305 { 306 "name": "pubKey", 307 "type": "bytes" 308 }, 309 { 310 "name": "chainId", 311 "type": "string" 312 }, 313 { 314 "name": "signature", 315 "type": "bytes" 316 } 317 ] 318 }, 319 { 320 "type": "function", 321 "name": "DepositInMainChain", 322 "constant": false, 323 "inputs": [ 324 { 325 "name": "chainId", 326 "type": "string" 327 } 328 ] 329 }, 330 { 331 "type": "function", 332 "name": "DepositInSideChain", 333 "constant": false, 334 "inputs": [ 335 { 336 "name": "chainId", 337 "type": "string" 338 }, 339 { 340 "name": "txHash", 341 "type": "bytes32" 342 } 343 ] 344 }, 345 { 346 "type": "function", 347 "name": "WithdrawFromSideChain", 348 "constant": false, 349 "inputs": [ 350 { 351 "name": "chainId", 352 "type": "string" 353 } 354 ] 355 }, 356 { 357 "type": "function", 358 "name": "WithdrawFromMainChain", 359 "constant": false, 360 "inputs": [ 361 { 362 "name": "chainId", 363 "type": "string" 364 }, 365 { 366 "name": "amount", 367 "type": "uint256" 368 }, 369 { 370 "name": "txHash", 371 "type": "bytes32" 372 } 373 ] 374 }, 375 { 376 "type": "function", 377 "name": "SaveDataToMainChain", 378 "constant": false, 379 "inputs": [ 380 { 381 "name": "data", 382 "type": "bytes" 383 } 384 ] 385 }, 386 { 387 "type": "function", 388 "name": "VoteNextEpoch", 389 "constant": false, 390 "inputs": [ 391 { 392 "name": "voteHash", 393 "type": "bytes32" 394 } 395 ] 396 }, 397 { 398 "type": "function", 399 "name": "RevealVote", 400 "constant": false, 401 "inputs": [ 402 { 403 "name": "pubKey", 404 "type": "bytes" 405 }, 406 { 407 "name": "amount", 408 "type": "uint256" 409 }, 410 { 411 "name": "salt", 412 "type": "string" 413 }, 414 { 415 "name": "signature", 416 "type": "bytes" 417 } 418 ] 419 }, 420 { 421 "type": "function", 422 "name": "Delegate", 423 "constant": false, 424 "inputs": [ 425 { 426 "name": "candidate", 427 "type": "address" 428 } 429 ] 430 }, 431 { 432 "type": "function", 433 "name": "UnDelegate", 434 "constant": false, 435 "inputs": [ 436 { 437 "name": "candidate", 438 "type": "address" 439 }, 440 { 441 "name": "amount", 442 "type": "uint256" 443 } 444 ] 445 }, 446 { 447 "type": "function", 448 "name": "Register", 449 "constant": false, 450 "inputs": [ 451 { 452 "name": "pubkey", 453 "type": "bytes" 454 }, 455 { 456 "name": "signature", 457 "type": "bytes" 458 }, 459 { 460 "name": "commission", 461 "type": "uint8" 462 } 463 ] 464 }, 465 { 466 "type": "function", 467 "name": "UnRegister", 468 "constant": false, 469 "inputs": [] 470 }, 471 { 472 "type": "function", 473 "name": "SetBlockReward", 474 "constant": false, 475 "inputs": [ 476 { 477 "name": "chainId", 478 "type": "string" 479 }, 480 { 481 "name": "reward", 482 "type": "uint256" 483 } 484 ] 485 }, 486 { 487 "type": "function", 488 "name": "EditValidator", 489 "constant": false, 490 "inputs": [ 491 { 492 "name": "moniker", 493 "type": "string" 494 }, 495 { 496 "name": "website", 497 "type": "string" 498 }, 499 { 500 "name": "identity", 501 "type": "string" 502 }, 503 { 504 "name": "details", 505 "type": "string" 506 } 507 ] 508 }, 509 { 510 "type": "function", 511 "name": "WithdrawReward", 512 "constant": false, 513 "inputs": [ 514 { 515 "name": "delegateAddress", 516 "type": "address" 517 }, 518 { 519 "name": "amount", 520 "type": "uint256" 521 } 522 ] 523 }, 524 { 525 "type": "function", 526 "name": "UnBanned", 527 "constant": false, 528 "inputs": [] 529 }, 530 { 531 "type": "function", 532 "name": "SetCommission", 533 "constant": false, 534 "inputs": [ 535 { 536 "name": "commission", 537 "type": "uint8" 538 } 539 ] 540 }, 541 { 542 "type": "function", 543 "name": "SetAddress", 544 "constant": false, 545 "inputs": [ 546 { 547 "name": "fAddress", 548 "type": "address" 549 } 550 ] 551 } 552 ]` 553 554 var NeatioSideChainsAddress = common.HexToAddress("0x0000000000000000000000000000000000001010") 555 556 var NeatioSmartContractAddress = common.HexToAddress("0x0000000000000000000000000000000000001001") 557 558 var ChainABI abi.ABI 559 560 func init() { 561 var err error 562 ChainABI, err = abi.JSON(strings.NewReader(jsonChainABI)) 563 if err != nil { 564 panic("fail to create the chain ABI: " + err.Error()) 565 } 566 } 567 568 func IsNeatChainContractAddr(addr *common.Address) bool { 569 return addr != nil && *addr == NeatioSmartContractAddress 570 } 571 572 func FunctionTypeFromId(sigdata []byte) (FunctionType, error) { 573 m, err := ChainABI.MethodById(sigdata) 574 if err != nil { 575 return Unknown, err 576 } 577 578 return StringToFunctionType(m.Name), nil 579 }