github.com/stafiprotocol/go-substrate-rpc-client@v1.4.7/client/stafi_event.go (about) 1 package client 2 3 import ( 4 "encoding/json" 5 "errors" 6 "fmt" 7 "math/big" 8 "reflect" 9 10 "github.com/ethereum/go-ethereum/common/hexutil" 11 scale "github.com/itering/scale.go" 12 "github.com/itering/scale.go/utiles" 13 "github.com/shopspring/decimal" 14 "github.com/stafiprotocol/go-substrate-rpc-client/pkg/utils" 15 "github.com/stafiprotocol/go-substrate-rpc-client/types" 16 ) 17 18 var ( 19 ErrValueNotStringSlice = errors.New("value not string slice") 20 ErrValueNotString = errors.New("value not string") 21 ErrValueNotFloat = errors.New("value not float64") 22 ErrValueNotMap = errors.New("value not map") 23 ErrValueNotU32 = errors.New("value not u32") 24 ) 25 26 func parseBytes(value interface{}) ([]byte, error) { 27 val, ok := value.(string) 28 if !ok { 29 return nil, ErrValueNotString 30 } 31 32 bz, err := hexutil.Decode(utiles.AddHex(val)) 33 if err != nil { 34 if err.Error() == hexutil.ErrSyntax.Error() { 35 return []byte(val), nil 36 } 37 return nil, err 38 } 39 40 return bz, nil 41 } 42 43 func parseAccountId(value interface{}) (types.AccountID, error) { 44 val, ok := value.(string) 45 if !ok { 46 return types.NewAccountID([]byte{}), ErrValueNotString 47 } 48 ac, err := hexutil.Decode(utiles.AddHex(val)) 49 if err != nil { 50 return types.NewAccountID([]byte{}), err 51 } 52 53 return types.NewAccountID(ac), nil 54 } 55 56 func parseRsymbol(value interface{}) (RSymbol, error) { 57 sym, ok := value.(string) 58 if !ok { 59 return RSymbol(""), ErrValueNotString 60 } 61 62 return RSymbol(sym), nil 63 } 64 65 func parseHash(value interface{}) (types.Hash, error) { 66 val, ok := value.(string) 67 if !ok { 68 return types.NewHash([]byte{}), ErrValueNotString 69 } 70 71 hash, err := types.NewHashFromHexString(utiles.AddHex(val)) 72 if err != nil { 73 return types.NewHash([]byte{}), err 74 } 75 76 return hash, err 77 } 78 79 func parseU128(value interface{}) (types.U128, error) { 80 val, ok := value.(string) 81 if !ok { 82 return types.U128{}, ErrValueNotString 83 } 84 deci, err := decimal.NewFromString(val) 85 if err != nil { 86 return types.U128{}, err 87 } 88 return types.NewU128(*deci.BigInt()), nil 89 } 90 91 func parseU8(value interface{}) (types.U8, error) { 92 val, ok := value.(float64) 93 if !ok { 94 return types.U8(0), ErrValueNotFloat 95 } 96 return types.NewU8(uint8(decimal.NewFromFloat(val).IntPart())), nil 97 } 98 99 func ParseVecBytes(value interface{}) ([]types.Bytes, error) { 100 vals, ok := value.([]interface{}) 101 if !ok { 102 return nil, ErrValueNotStringSlice 103 } 104 result := make([]types.Bytes, 0) 105 for _, val := range vals { 106 bz, err := parseBytes(val) 107 if err != nil { 108 return nil, err 109 } 110 111 result = append(result, bz) 112 } 113 114 return result, nil 115 } 116 117 func parseBigint(value interface{}) (*big.Int, error) { 118 val, ok := value.(string) 119 if !ok { 120 return nil, ErrValueNotString 121 } 122 123 i, ok := utils.StringToBigint(val) 124 if !ok { 125 return nil, fmt.Errorf("string to bigint error: %s", val) 126 } 127 128 return i, nil 129 } 130 131 func parseBoolean(value interface{}) (bool, error) { 132 val, ok := value.(bool) 133 if !ok { 134 return false, fmt.Errorf("value not bool type") 135 } 136 137 return val, nil 138 } 139 140 func parseU64(value interface{}) (types.U64, error) { 141 val, ok := value.(float64) 142 if !ok { 143 return 0, ErrValueNotString 144 } 145 146 ret := types.NewU64(uint64(val)) 147 148 return ret, nil 149 } 150 151 func parseU32(value interface{}) (types.U32, error) { 152 valueType := reflect.TypeOf(value) 153 var ret types.U32 154 switch valueType.String() { 155 case "float64": 156 val, ok := value.(float64) 157 if !ok { 158 return 0, ErrValueNotString 159 } 160 161 ret = types.NewU32(uint32(val)) 162 default: 163 return 0, ErrValueNotFloat 164 } 165 166 return ret, nil 167 } 168 169 func ParseLiquidityBondAndSwapEvent(evt *ChainEvent) (*EvtExecuteBondAndSwap, error) { 170 if len(evt.Params) != 6 { 171 return nil, fmt.Errorf("LiquidityBondEventData params number not right: %d, expected: 6", len(evt.Params)) 172 } 173 accountId, err := parseAccountId(evt.Params[0].Value) 174 if err != nil { 175 return nil, fmt.Errorf("LiquidityBondEventData params[0] -> AccountId error: %s", err) 176 } 177 symbol, err := parseRsymbol(evt.Params[1].Value) 178 if err != nil { 179 return nil, fmt.Errorf("LiquidityBondEventData params[1] -> RSymbol error: %s", err) 180 } 181 bondId, err := parseHash(evt.Params[2].Value) 182 if err != nil { 183 return nil, fmt.Errorf("LiquidityBondEventData params[2] -> BondId error: %s", err) 184 } 185 amount, err := parseU128(evt.Params[3].Value) 186 if err != nil { 187 return nil, fmt.Errorf("LiquidityBondEventData params[3] -> BondId error: %s", err) 188 } 189 destRecipient, err := parseBytes(evt.Params[4].Value) 190 if err != nil { 191 return nil, fmt.Errorf("LiquidityBondEventData params[4] -> BondId error: %s", err) 192 } 193 destId, err := parseU8(evt.Params[5].Value) 194 if err != nil { 195 return nil, fmt.Errorf("LiquidityBondEventData params[5] -> BondId error: %s", err) 196 } 197 198 return &EvtExecuteBondAndSwap{ 199 AccountId: accountId, 200 Symbol: symbol, 201 BondId: bondId, 202 Amount: amount, 203 DestRecipient: types.NewBytes(destRecipient), 204 DestId: destId, 205 }, nil 206 } 207 208 func EventRateSetData(evt *ChainEvent) (*RateSet, error) { 209 switch len(evt.Params) { 210 case 1: 211 rate, err := parseU64(evt.Params[0].Value) 212 if err != nil { 213 return nil, fmt.Errorf("EventRateSetData params[1] -> rate error: %s", err) 214 } 215 216 return &RateSet{ 217 Symbol: RFIS, 218 Rate: rate, 219 }, nil 220 case 2: 221 symbol, err := parseRsymbol(evt.Params[0].Value) 222 if err != nil { 223 return nil, fmt.Errorf("EventRateSetData params[0] -> RSymbol error: %s", err) 224 } 225 226 rate, err := parseU64(evt.Params[1].Value) 227 if err != nil { 228 return nil, fmt.Errorf("EventRateSetData params[1] -> rate error: %s", err) 229 } 230 231 return &RateSet{ 232 Symbol: symbol, 233 Rate: rate, 234 }, nil 235 } 236 237 return nil, fmt.Errorf("EventRateSetData params number not right: %d, expected:1 or 2", len(evt.Params)) 238 } 239 240 func EventEraPayoutData(evt *ChainEvent) (*EraPayout, error) { 241 if len(evt.Params) != 3 { 242 return nil, fmt.Errorf("EventTransferData params number not right: %d, expected: 4", len(evt.Params)) 243 } 244 eraIndex, err := parseU32(evt.Params[0].Value) 245 if err != nil { 246 return nil, fmt.Errorf("EventTransferData params[0] -> from error: %s", err) 247 } 248 balance, err := parseBigint(evt.Params[1].Value) 249 if err != nil { 250 return nil, fmt.Errorf("EventTransferData params[3] -> value error: %s", err) 251 } 252 balance2, err := parseBigint(evt.Params[2].Value) 253 if err != nil { 254 return nil, fmt.Errorf("EventTransferData params[3] -> value error: %s", err) 255 } 256 257 return &EraPayout{ 258 EraIndex: eraIndex, 259 Balance: types.NewU128(*balance), 260 Balance2: types.NewU128(*balance2), 261 }, nil 262 } 263 264 func EventUnbondData(evt *ChainEvent) (*Unbond, error) { 265 if len(evt.Params) != 7 { 266 return nil, fmt.Errorf("EventUnbondData params number not right: %d, expected: 7", len(evt.Params)) 267 } 268 from, err := parseAccountId(evt.Params[0].Value) 269 if err != nil { 270 return nil, fmt.Errorf("EventUnbondData params[0] -> from error: %s", err) 271 } 272 273 symbol, err := parseRsymbol(evt.Params[1].Value) 274 if err != nil { 275 return nil, fmt.Errorf("EventUnbondData params[1] -> RSymbol error: %s", err) 276 } 277 278 pool, err := parseBytes(evt.Params[2].Value) 279 if err != nil { 280 return nil, fmt.Errorf("EventUnbondData params[2] -> value error: %s", err) 281 } 282 283 value, err := parseBigint(evt.Params[3].Value) 284 if err != nil { 285 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 286 } 287 288 leftValue, err := parseBigint(evt.Params[4].Value) 289 if err != nil { 290 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 291 } 292 balance, err := parseBigint(evt.Params[5].Value) 293 if err != nil { 294 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 295 } 296 recipient, err := parseBytes(evt.Params[6].Value) 297 if err != nil { 298 return nil, fmt.Errorf("EventUnbondData params[6] -> value error: %s", err) 299 } 300 301 return &Unbond{ 302 From: from, 303 Symbol: symbol, 304 Pool: pool, 305 Value: types.NewU128(*value), 306 LeftValue: types.NewU128(*leftValue), 307 Balance: types.NewU128(*balance), 308 Recipient: recipient, 309 }, nil 310 } 311 312 func EventRdexSwapData(evt *ChainEvent) (*RdexSwap, error) { 313 if len(evt.Params) != 8 { 314 return nil, fmt.Errorf("EventUnbondData params number not right: %d, expected: 7", len(evt.Params)) 315 } 316 from, err := parseAccountId(evt.Params[0].Value) 317 if err != nil { 318 return nil, fmt.Errorf("EventUnbondData params[0] -> from error: %s", err) 319 } 320 321 symbol, err := parseRsymbol(evt.Params[1].Value) 322 if err != nil { 323 return nil, fmt.Errorf("EventUnbondData params[1] -> RSymbol error: %s", err) 324 } 325 326 inputAmount, err := parseBigint(evt.Params[2].Value) 327 if err != nil { 328 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 329 } 330 outputAmount, err := parseBigint(evt.Params[3].Value) 331 if err != nil { 332 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 333 } 334 feeAmount, err := parseBigint(evt.Params[4].Value) 335 if err != nil { 336 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 337 } 338 339 inputIsFis, err := parseBoolean(evt.Params[5].Value) 340 if err != nil { 341 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 342 } 343 344 fisBalance, err := parseBigint(evt.Params[6].Value) 345 if err != nil { 346 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 347 } 348 rTokenBalance, err := parseBigint(evt.Params[7].Value) 349 if err != nil { 350 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 351 } 352 353 return &RdexSwap{ 354 From: from, 355 Symbol: symbol, 356 InputAmount: types.NewU128(*inputAmount), 357 OutputAmount: types.NewU128(*outputAmount), 358 FeeAmount: types.NewU128(*feeAmount), 359 InputIsFis: inputIsFis, 360 FisBalance: types.NewU128(*fisBalance), 361 RTokenBalance: types.NewU128(*rTokenBalance), 362 }, nil 363 } 364 365 func EventRdexAddLiquidityData(evt *ChainEvent) (*RdexAddLiquidity, error) { 366 if len(evt.Params) != 8 { 367 return nil, fmt.Errorf("EventUnbondData params number not right: %d, expected: 7", len(evt.Params)) 368 } 369 from, err := parseAccountId(evt.Params[0].Value) 370 if err != nil { 371 return nil, fmt.Errorf("EventUnbondData params[0] -> from error: %s", err) 372 } 373 374 symbol, err := parseRsymbol(evt.Params[1].Value) 375 if err != nil { 376 return nil, fmt.Errorf("EventUnbondData params[1] -> RSymbol error: %s", err) 377 } 378 379 fisAmount, err := parseBigint(evt.Params[2].Value) 380 if err != nil { 381 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 382 } 383 rTokenAmount, err := parseBigint(evt.Params[3].Value) 384 if err != nil { 385 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 386 } 387 newTotalUnit, err := parseBigint(evt.Params[4].Value) 388 if err != nil { 389 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 390 } 391 392 addUnit, err := parseBigint(evt.Params[5].Value) 393 if err != nil { 394 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 395 } 396 397 fisBalance, err := parseBigint(evt.Params[6].Value) 398 if err != nil { 399 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 400 } 401 rTokenBalance, err := parseBigint(evt.Params[7].Value) 402 if err != nil { 403 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 404 } 405 406 return &RdexAddLiquidity{ 407 From: from, 408 Symbol: symbol, 409 FisAmount: types.NewU128(*fisAmount), 410 RTokenAmount: types.NewU128(*rTokenAmount), 411 NewTotalUnit: types.NewU128(*newTotalUnit), 412 AddUnit: types.NewU128(*addUnit), 413 FisBalance: types.NewU128(*fisBalance), 414 RTokenBalance: types.NewU128(*rTokenBalance), 415 }, nil 416 } 417 418 func EventRdexRemoveLiquidityData(evt *ChainEvent) (*RdexRemoveLiquidity, error) { 419 if len(evt.Params) != 9 { 420 return nil, fmt.Errorf("EventUnbondData params number not right: %d, expected: 7", len(evt.Params)) 421 } 422 from, err := parseAccountId(evt.Params[0].Value) 423 if err != nil { 424 return nil, fmt.Errorf("EventUnbondData params[0] -> from error: %s", err) 425 } 426 427 symbol, err := parseRsymbol(evt.Params[1].Value) 428 if err != nil { 429 return nil, fmt.Errorf("EventUnbondData params[1] -> RSymbol error: %s", err) 430 } 431 432 removeUnit, err := parseBigint(evt.Params[2].Value) 433 if err != nil { 434 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 435 } 436 437 swapUnit, err := parseBigint(evt.Params[3].Value) 438 if err != nil { 439 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 440 } 441 removefisAmount, err := parseBigint(evt.Params[4].Value) 442 if err != nil { 443 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 444 } 445 removeRTokenAmount, err := parseBigint(evt.Params[5].Value) 446 if err != nil { 447 return nil, fmt.Errorf("EventUnbondData params[3] -> value error: %s", err) 448 } 449 450 inputIsFis, err := parseBoolean(evt.Params[6].Value) 451 if err != nil { 452 return nil, fmt.Errorf("EventUnbondData params[4] -> value error: %s", err) 453 } 454 455 fisBalance, err := parseBigint(evt.Params[7].Value) 456 if err != nil { 457 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 458 } 459 rTokenBalance, err := parseBigint(evt.Params[8].Value) 460 if err != nil { 461 return nil, fmt.Errorf("EventUnbondData params[5] -> value error: %s", err) 462 } 463 464 return &RdexRemoveLiquidity{ 465 From: from, 466 Symbol: symbol, 467 RemoveUnit: types.NewU128(*removeUnit), 468 SwapUnit: types.NewU128(*swapUnit), 469 RemoveFisAmount: types.NewU128(*removefisAmount), 470 RemoveRTokenAmount: types.NewU128(*removeRTokenAmount), 471 InputIsFis: inputIsFis, 472 FisBalance: types.NewU128(*fisBalance), 473 RTokenBalance: types.NewU128(*rTokenBalance), 474 }, nil 475 } 476 477 func EventRFisUnbondData(evt *ChainEvent) (*RFisUnbond, error) { 478 if len(evt.Params) != 5 { 479 return nil, fmt.Errorf("EventRFisUnbondData params number not right: %d, expected: 5", len(evt.Params)) 480 } 481 from, err := parseAccountId(evt.Params[0].Value) 482 if err != nil { 483 return nil, fmt.Errorf("EventRFisUnbondData params[0] -> from error: %s", err) 484 } 485 486 pool, err := parseBytes(evt.Params[1].Value) 487 if err != nil { 488 return nil, fmt.Errorf("EventRFisUnbondData params[1] -> value error: %s", err) 489 } 490 491 value, err := parseBigint(evt.Params[2].Value) 492 if err != nil { 493 return nil, fmt.Errorf("EventRFisUnbondData params[2] -> value error: %s", err) 494 } 495 496 leftValue, err := parseBigint(evt.Params[3].Value) 497 if err != nil { 498 return nil, fmt.Errorf("EventRFisUnbondData params[3] -> value error: %s", err) 499 } 500 balance, err := parseBigint(evt.Params[4].Value) 501 if err != nil { 502 return nil, fmt.Errorf("EventRFisUnbondData params[4] -> value error: %s", err) 503 } 504 505 return &RFisUnbond{ 506 From: from, 507 Pool: pool, 508 Value: types.NewU128(*value), 509 LeftValue: types.NewU128(*leftValue), 510 Balance: types.NewU128(*balance), 511 }, nil 512 } 513 514 func EventTransferData(evt *ChainEvent) (*Transfer, error) { 515 if len(evt.Params) != 4 { 516 return nil, fmt.Errorf("EventTransferData params number not right: %d, expected: 4", len(evt.Params)) 517 } 518 from, err := parseAccountId(evt.Params[0].Value) 519 if err != nil { 520 return nil, fmt.Errorf("EventTransferData params[0] -> from error: %s", err) 521 } 522 523 to, err := parseAccountId(evt.Params[1].Value) 524 if err != nil { 525 return nil, fmt.Errorf("EventTransferData params[1] -> to error: %s", err) 526 } 527 528 symbol, err := parseRsymbol(evt.Params[2].Value) 529 if err != nil { 530 return nil, fmt.Errorf("EventTransferData params[2] -> RSymbol error: %s", err) 531 } 532 value, err := parseBigint(evt.Params[3].Value) 533 if err != nil { 534 return nil, fmt.Errorf("EventTransferData params[3] -> value error: %s", err) 535 } 536 537 return &Transfer{ 538 From: from, 539 To: to, 540 Symbol: symbol, 541 Value: types.NewU128(*value), 542 }, nil 543 } 544 545 func EventWithdrawUnbondData(evt *ChainEvent) (*LiquidityWithdrawUnbond, error) { 546 if len(evt.Params) != 3 { 547 return nil, fmt.Errorf("EventWithdrawUnbondData params number not right: %d, expected: 4", len(evt.Params)) 548 } 549 from, err := parseAccountId(evt.Params[0].Value) 550 if err != nil { 551 return nil, fmt.Errorf("EventWithdrawUnbondData params[0] -> from error: %s", err) 552 } 553 554 to, err := parseAccountId(evt.Params[1].Value) 555 if err != nil { 556 return nil, fmt.Errorf("EventWithdrawUnbondData params[1] -> to error: %s", err) 557 } 558 559 value, err := parseBigint(evt.Params[2].Value) 560 if err != nil { 561 return nil, fmt.Errorf("EventWithdrawUnbondData params[3] -> value error: %s", err) 562 } 563 564 return &LiquidityWithdrawUnbond{ 565 From: from, 566 To: to, 567 Value: types.NewU128(*value), 568 }, nil 569 } 570 571 func EventMintedData(evt *ChainEvent) (*Minted, error) { 572 if len(evt.Params) != 3 { 573 return nil, fmt.Errorf("EventMintedData params number not right: %d, expected: 4", len(evt.Params)) 574 } 575 576 to, err := parseAccountId(evt.Params[0].Value) 577 if err != nil { 578 return nil, fmt.Errorf("EventMintedData params[1] -> to error: %s", err) 579 } 580 581 symbol, err := parseRsymbol(evt.Params[1].Value) 582 if err != nil { 583 return nil, fmt.Errorf("EventMintedData params[2] -> RSymbol error: %s", err) 584 } 585 value, err := parseBigint(evt.Params[2].Value) 586 if err != nil { 587 return nil, fmt.Errorf("EventMintedData params[3] -> value error: %s", err) 588 } 589 590 return &Minted{ 591 To: to, 592 Symbol: symbol, 593 Value: types.NewU128(*value), 594 }, nil 595 } 596 597 func EventBurnedData(evt *ChainEvent) (*Burned, error) { 598 if len(evt.Params) != 3 { 599 return nil, fmt.Errorf("EventBurnedData params number not right: %d, expected: 4", len(evt.Params)) 600 } 601 from, err := parseAccountId(evt.Params[0].Value) 602 if err != nil { 603 return nil, fmt.Errorf("EventBurnedData params[0] -> from error: %s", err) 604 } 605 606 symbol, err := parseRsymbol(evt.Params[1].Value) 607 if err != nil { 608 return nil, fmt.Errorf("EventBurnedData params[1] -> RSymbol error: %s", err) 609 } 610 value, err := parseBigint(evt.Params[2].Value) 611 if err != nil { 612 return nil, fmt.Errorf("EventBurnedData params[2] -> value error: %s", err) 613 } 614 615 return &Burned{ 616 From: from, 617 Symbol: symbol, 618 Value: types.NewU128(*value), 619 }, nil 620 } 621 622 func EventEraUpdatedData(evt *ChainEvent) (*EraUpdated, error) { 623 if len(evt.Params) != 3 { 624 return nil, fmt.Errorf("EraPoolUpdatedData params number not right: %d, expected: 4", len(evt.Params)) 625 } 626 627 symbol, err := parseRsymbol(evt.Params[0].Value) 628 if err != nil { 629 return nil, fmt.Errorf("EraPoolUpdatedData params[0] -> RSymbol error: %s", err) 630 } 631 632 oldEra, err := parseEra(evt.Params[1]) 633 if err != nil { 634 return nil, fmt.Errorf("EraPoolUpdatedData params[1] -> era error: %s", err) 635 } 636 newEra, err := parseEra(evt.Params[2]) 637 if err != nil { 638 return nil, fmt.Errorf("EraPoolUpdatedData params[2] -> era error: %s", err) 639 } 640 641 return &EraUpdated{ 642 Symbol: symbol, 643 OldEra: oldEra.Value, 644 NewEra: newEra.Value, 645 }, nil 646 } 647 648 func EventBondingDurationData(evt *ChainEvent) (*BondingDuration, error) { 649 if len(evt.Params) != 3 { 650 return nil, fmt.Errorf("EventBondingDurationData params number not right: %d, expected: 3", len(evt.Params)) 651 } 652 653 symbol, err := parseRsymbol(evt.Params[0].Value) 654 if err != nil { 655 return nil, fmt.Errorf("EventBondingDurationData params[0] -> RSymbol error: %s", err) 656 } 657 658 oldDuration, err := parseU32(evt.Params[1].Value) 659 if err != nil { 660 return nil, fmt.Errorf("EventBondingDurationData params[1] -> old duration error: %s", err) 661 } 662 newDuration, err := parseU32(evt.Params[2].Value) 663 if err != nil { 664 return nil, fmt.Errorf("EventBondingDurationData params[2] -> new duration error: %s", err) 665 } 666 667 return &BondingDuration{ 668 Symbol: symbol, 669 OldDuration: oldDuration, 670 NewDuration: newDuration, 671 }, nil 672 } 673 674 func EventNewMultisigData(evt *ChainEvent) (*EventNewMultisig, error) { 675 if len(evt.Params) != 3 { 676 return nil, fmt.Errorf("EventNewMultisigData params number not right: %d, expected: 3", len(evt.Params)) 677 } 678 who, err := parseAccountId(evt.Params[0].Value) 679 if err != nil { 680 return nil, fmt.Errorf("EventNewMultisig params[0] -> who error: %s", err) 681 } 682 683 id, err := parseAccountId(evt.Params[1].Value) 684 if err != nil { 685 return nil, fmt.Errorf("EventNewMultisig params[1] -> id error: %s", err) 686 } 687 688 hash, err := parseHash(evt.Params[2].Value) 689 if err != nil { 690 return nil, fmt.Errorf("EventNewMultisig params[2] -> hash error: %s", err) 691 } 692 693 return &EventNewMultisig{ 694 Who: who, 695 ID: id, 696 CallHash: hash, 697 }, nil 698 } 699 700 func EventMultisigExecutedData(evt *ChainEvent) (*EventMultisigExecuted, error) { 701 if len(evt.Params) != 5 { 702 return nil, fmt.Errorf("EventMultisigExecuted params number not right: %d, expected: 5", len(evt.Params)) 703 } 704 705 approving, err := parseAccountId(evt.Params[0].Value) 706 if err != nil { 707 return nil, fmt.Errorf("EventMultisigExecuted params[0] -> approving error: %s", err) 708 } 709 710 tp, err := parseTimePoint(evt.Params[1].Value) 711 if err != nil { 712 return nil, fmt.Errorf("EventMultisigExecuted params[1] -> timepoint error: %s", err) 713 } 714 715 id, err := parseAccountId(evt.Params[2].Value) 716 if err != nil { 717 return nil, fmt.Errorf("EventMultisigExecuted params[2] -> id error: %s", err) 718 } 719 720 hash, err := parseHash(evt.Params[3].Value) 721 if err != nil { 722 return nil, fmt.Errorf("EventMultisigExecuted params[3] -> hash error: %s", err) 723 } 724 725 ok, err := parseDispatchResult(evt.Params[4].Value) 726 if err != nil { 727 return nil, fmt.Errorf("EventMultisigExecuted params[4] -> dispatchresult error: %s", err) 728 } 729 730 return &EventMultisigExecuted{ 731 Who: approving, 732 TimePoint: tp, 733 ID: id, 734 CallHash: hash, 735 Result: ok, 736 }, nil 737 } 738 739 func parseEra(param scale.EventParam) (*Era, error) { 740 bz, err := json.Marshal(param) 741 if err != nil { 742 return nil, err 743 } 744 745 era := new(Era) 746 err = json.Unmarshal(bz, era) 747 if err != nil { 748 return nil, err 749 } 750 751 return era, nil 752 } 753 754 func parseTimePoint(value interface{}) (types.TimePoint, error) { 755 bz, err := json.Marshal(value) 756 if err != nil { 757 return types.TimePoint{}, err 758 } 759 760 var tp types.TimePoint 761 err = json.Unmarshal(bz, &tp) 762 if err != nil { 763 return types.TimePoint{}, err 764 } 765 766 return tp, nil 767 } 768 769 func parseDispatchResult(value interface{}) (bool, error) { 770 result, ok := value.(map[string]interface{}) 771 if !ok { 772 return false, ErrValueNotMap 773 } 774 _, ok = result["Ok"] 775 return ok, nil 776 }