github.com/0chain/gosdk@v1.17.11/zcncore/transactionauth.go (about) 1 //go:build !mobile 2 // +build !mobile 3 4 package zcncore 5 6 import ( 7 "encoding/json" 8 "fmt" 9 "math" 10 "time" 11 12 "github.com/0chain/errors" 13 "github.com/0chain/gosdk/core/transaction" 14 ) 15 16 func newTransactionWithAuth(cb TransactionCallback, txnFee uint64, nonce int64) (*TransactionWithAuth, error) { 17 ta := &TransactionWithAuth{} 18 var err error 19 ta.t, err = newTransaction(cb, txnFee, nonce) 20 return ta, err 21 } 22 23 func (ta *TransactionWithAuth) ExecuteSmartContract(address, methodName string, 24 input interface{}, val uint64, feeOpts ...FeeOption) (*transaction.Transaction, error) { 25 err := ta.t.createSmartContractTxn(address, methodName, input, val, feeOpts...) 26 if err != nil { 27 return nil, err 28 } 29 go func() { 30 ta.submitTxn() 31 }() 32 return ta.t.txn, nil 33 } 34 35 func (ta *TransactionWithAuth) Send(toClientID string, val uint64, desc string) error { 36 txnData, err := json.Marshal(transaction.SmartContractTxnData{Name: "transfer", InputArgs: SendTxnData{Note: desc}}) 37 if err != nil { 38 return errors.New("", "Could not serialize description to transaction_data") 39 } 40 41 ta.t.txn.TransactionType = transaction.TxnTypeSend 42 ta.t.txn.ToClientID = toClientID 43 ta.t.txn.Value = val 44 ta.t.txn.TransactionData = string(txnData) 45 if ta.t.txn.TransactionFee == 0 { 46 fee, err := transaction.EstimateFee(ta.t.txn, _config.chain.Miners, 0.2) 47 if err != nil { 48 return err 49 } 50 ta.t.txn.TransactionFee = fee 51 } 52 53 go func() { 54 ta.submitTxn() 55 }() 56 57 return nil 58 } 59 60 func (ta *TransactionWithAuth) VestingAdd(ar *VestingAddRequest, 61 value uint64) (err error) { 62 63 err = ta.t.createSmartContractTxn(VestingSmartContractAddress, 64 transaction.VESTING_ADD, ar, value) 65 if err != nil { 66 logging.Error(err) 67 return 68 } 69 go func() { ta.submitTxn() }() 70 return 71 } 72 73 func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType Provider, lock uint64) error { 74 if lock > math.MaxInt64 { 75 return errors.New("invalid_lock", "int64 overflow on lock value") 76 } 77 78 pr := &stakePoolRequest{ 79 ProviderID: providerId, 80 ProviderType: providerType, 81 } 82 err := ta.t.createSmartContractTxn(MinerSmartContractAddress, 83 transaction.MINERSC_LOCK, pr, lock) 84 if err != nil { 85 logging.Error(err) 86 return err 87 } 88 go func() { ta.submitTxn() }() 89 return nil 90 } 91 92 func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType Provider) error { 93 pr := &stakePoolRequest{ 94 ProviderID: providerId, 95 ProviderType: providerType, 96 } 97 err := ta.t.createSmartContractTxn(MinerSmartContractAddress, 98 transaction.MINERSC_LOCK, pr, 0) 99 if err != nil { 100 logging.Error(err) 101 return err 102 } 103 go func() { ta.submitTxn() }() 104 return err 105 } 106 107 // FinalizeAllocation transaction. 108 func (ta *TransactionWithAuth) FinalizeAllocation(allocID string) ( 109 err error) { 110 111 type finiRequest struct { 112 AllocationID string `json:"allocation_id"` 113 } 114 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 115 transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{ 116 AllocationID: allocID, 117 }, 0) 118 if err != nil { 119 logging.Error(err) 120 return 121 } 122 go func() { ta.submitTxn() }() 123 return 124 } 125 126 // CancelAllocation transaction. 127 func (ta *TransactionWithAuth) CancelAllocation(allocID string) ( 128 err error) { 129 130 type cancelRequest struct { 131 AllocationID string `json:"allocation_id"` 132 } 133 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 134 transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{ 135 AllocationID: allocID, 136 }, 0) 137 if err != nil { 138 logging.Error(err) 139 return 140 } 141 go func() { ta.submitTxn() }() 142 return 143 } 144 145 // CreateAllocation transaction. 146 func (ta *TransactionWithAuth) CreateAllocation(car *CreateAllocationRequest, 147 lock uint64) (err error) { 148 149 if lock > math.MaxInt64 { 150 return errors.New("invalid_lock", "int64 overflow on lock value") 151 } 152 153 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 154 transaction.STORAGESC_CREATE_ALLOCATION, car, lock) 155 if err != nil { 156 logging.Error(err) 157 return 158 } 159 go func() { ta.submitTxn() }() 160 return 161 } 162 163 // CreateReadPool for current user. 164 func (ta *TransactionWithAuth) CreateReadPool() (err error) { 165 166 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 167 transaction.STORAGESC_CREATE_READ_POOL, nil, 0) 168 if err != nil { 169 logging.Error(err) 170 return 171 } 172 go func() { ta.submitTxn() }() 173 return 174 } 175 176 // ReadPoolLock locks tokens for current user and given allocation, using given 177 // duration. If blobberID is not empty, then tokens will be locked for given 178 // allocation->blobber only. 179 func (ta *TransactionWithAuth) ReadPoolLock(allocID, blobberID string, 180 duration int64, lock uint64) (err error) { 181 182 if lock > math.MaxInt64 { 183 return errors.New("invalid_lock", "int64 overflow on lock value") 184 } 185 186 type lockRequest struct { 187 Duration time.Duration `json:"duration"` 188 AllocationID string `json:"allocation_id"` 189 BlobberID string `json:"blobber_id,omitempty"` 190 } 191 192 var lr lockRequest 193 lr.Duration = time.Duration(duration) 194 lr.AllocationID = allocID 195 lr.BlobberID = blobberID 196 197 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 198 transaction.STORAGESC_READ_POOL_LOCK, &lr, lock) 199 if err != nil { 200 logging.Error(err) 201 return 202 } 203 go func() { ta.submitTxn() }() 204 return 205 } 206 207 // ReadPoolUnlock for current user and given pool. 208 func (ta *TransactionWithAuth) ReadPoolUnlock() ( 209 err error) { 210 211 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 212 transaction.STORAGESC_READ_POOL_UNLOCK, nil, 0) 213 if err != nil { 214 logging.Error(err) 215 return 216 } 217 go func() { ta.submitTxn() }() 218 return 219 } 220 221 // StakePoolLock used to lock tokens in a stake pool of a blobber. 222 func (ta *TransactionWithAuth) StakePoolLock(providerId string, providerType Provider, lock uint64) error { 223 224 if lock > math.MaxInt64 { 225 return errors.New("invalid_lock", "int64 overflow on lock value") 226 } 227 228 type stakePoolRequest struct { 229 ProviderType Provider `json:"provider_type,omitempty"` 230 ProviderID string `json:"provider_id,omitempty"` 231 } 232 233 spr := stakePoolRequest{ 234 ProviderType: providerType, 235 ProviderID: providerId, 236 } 237 238 err := ta.t.createSmartContractTxn(StorageSmartContractAddress, 239 transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lock) 240 if err != nil { 241 logging.Error(err) 242 return err 243 } 244 go func() { ta.submitTxn() }() 245 return nil 246 } 247 248 // StakePoolUnlock by blobberID 249 func (ta *TransactionWithAuth) StakePoolUnlock(providerId string, providerType Provider) error { 250 251 type stakePoolRequest struct { 252 ProviderType Provider `json:"provider_type,omitempty"` 253 ProviderID string `json:"provider_id,omitempty"` 254 } 255 256 spr := stakePoolRequest{ 257 ProviderType: providerType, 258 ProviderID: providerId, 259 } 260 261 err := ta.t.createSmartContractTxn(StorageSmartContractAddress, 262 transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, 0) 263 if err != nil { 264 logging.Error(err) 265 return err 266 } 267 go func() { ta.submitTxn() }() 268 return nil 269 } 270 271 // UpdateBlobberSettings update settings of a blobber. 272 func (ta *TransactionWithAuth) UpdateBlobberSettings(blob *Blobber) ( 273 err error) { 274 275 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 276 transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, blob, 0) 277 if err != nil { 278 logging.Error(err) 279 return 280 } 281 go func() { ta.submitTxn() }() 282 return 283 } 284 285 // UpdateValidatorSettings update settings of a validator. 286 func (ta *TransactionWithAuth) UpdateValidatorSettings(v *Validator) ( 287 err error) { 288 289 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 290 transaction.STORAGESC_UPDATE_VALIDATOR_SETTINGS, v, 0) 291 if err != nil { 292 logging.Error(err) 293 return 294 } 295 go func() { ta.submitTxn() }() 296 return 297 } 298 299 // UpdateAllocation transaction. 300 func (ta *TransactionWithAuth) UpdateAllocation(allocID string, sizeDiff int64, 301 expirationDiff int64, lock uint64) (err error) { 302 303 if lock > math.MaxInt64 { 304 return errors.New("invalid_lock", "int64 overflow on lock value") 305 } 306 307 type updateAllocationRequest struct { 308 ID string `json:"id"` // allocation id 309 Size int64 `json:"size"` // difference 310 Expiration int64 `json:"expiration_date"` // difference 311 } 312 313 var uar updateAllocationRequest 314 uar.ID = allocID 315 uar.Size = sizeDiff 316 uar.Expiration = expirationDiff 317 318 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 319 transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lock) 320 if err != nil { 321 logging.Error(err) 322 return 323 } 324 go func() { ta.submitTxn() }() 325 return 326 } 327 328 // WritePoolLock locks tokens for current user and given allocation, using given 329 // duration. If blobberID is not empty, then tokens will be locked for given 330 // allocation->blobber only. 331 func (ta *TransactionWithAuth) WritePoolLock(allocID, blobberID string, 332 duration int64, lock uint64) (err error) { 333 334 if lock > math.MaxInt64 { 335 return errors.New("invalid_lock", "int64 overflow on lock value") 336 } 337 338 type lockRequest struct { 339 Duration time.Duration `json:"duration"` 340 AllocationID string `json:"allocation_id"` 341 BlobberID string `json:"blobber_id,omitempty"` 342 } 343 344 var lr lockRequest 345 lr.Duration = time.Duration(duration) 346 lr.AllocationID = allocID 347 lr.BlobberID = blobberID 348 349 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 350 transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lock) 351 if err != nil { 352 logging.Error(err) 353 return 354 } 355 go func() { ta.submitTxn() }() 356 return 357 } 358 359 // WritePoolUnlock for current user and given pool. 360 func (ta *TransactionWithAuth) WritePoolUnlock(allocID string) (err error) { 361 type unlockRequest struct { 362 AllocationID string `json:"allocation_id"` 363 } 364 365 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 366 transaction.STORAGESC_WRITE_POOL_UNLOCK, &unlockRequest{ 367 AllocationID: allocID, 368 }, 0) 369 if err != nil { 370 logging.Error(err) 371 return 372 } 373 go func() { ta.submitTxn() }() 374 return 375 } 376 377 func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType Provider) error { 378 pr := &scCollectReward{ 379 ProviderId: providerId, 380 ProviderType: int(providerType), 381 } 382 err := ta.t.createSmartContractTxn(MinerSmartContractAddress, 383 transaction.MINERSC_COLLECT_REWARD, pr, 0) 384 if err != nil { 385 logging.Error(err) 386 return err 387 } 388 go ta.submitTxn() 389 return err 390 } 391 392 func (ta *TransactionWithAuth) MinerSCKill(providerId string, providerType Provider) error { 393 pr := &scCollectReward{ 394 ProviderId: providerId, 395 ProviderType: int(providerType), 396 } 397 var name string 398 switch providerType { 399 case ProviderMiner: 400 name = transaction.MINERSC_KILL_MINER 401 case ProviderSharder: 402 name = transaction.MINERSC_KILL_SHARDER 403 default: 404 return fmt.Errorf("kill provider type %v not implimented", providerType) 405 } 406 err := ta.t.createSmartContractTxn(MinerSmartContractAddress, name, pr, 0) 407 if err != nil { 408 logging.Error(err) 409 return err 410 } 411 go ta.submitTxn() 412 return err 413 } 414 415 func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, providerType Provider) error { 416 pr := &scCollectReward{ 417 ProviderId: providerId, 418 ProviderType: int(providerType), 419 } 420 err := ta.t.createSmartContractTxn(StorageSmartContractAddress, 421 transaction.STORAGESC_COLLECT_REWARD, pr, 0) 422 if err != nil { 423 logging.Error(err) 424 return err 425 } 426 go func() { ta.submitTxn() }() 427 return err 428 } 429 430 func (ta *TransactionWithAuth) VestingUpdateConfig(ip *InputMap) (err error) { 431 err = ta.t.createSmartContractTxn(VestingSmartContractAddress, 432 transaction.VESTING_UPDATE_SETTINGS, ip, 0) 433 if err != nil { 434 logging.Error(err) 435 return 436 } 437 go func() { ta.submitTxn() }() 438 return 439 } 440 441 // faucet smart contract 442 443 func (ta *TransactionWithAuth) FaucetUpdateConfig(ip *InputMap) (err error) { 444 err = ta.t.createSmartContractTxn(FaucetSmartContractAddress, 445 transaction.FAUCETSC_UPDATE_SETTINGS, ip, 0) 446 if err != nil { 447 logging.Error(err) 448 return 449 } 450 go func() { ta.submitTxn() }() 451 return 452 } 453 454 func (ta *TransactionWithAuth) MinerScUpdateConfig(ip *InputMap) (err error) { 455 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 456 transaction.MINERSC_UPDATE_SETTINGS, ip, 0) 457 if err != nil { 458 logging.Error(err) 459 return 460 } 461 go func() { ta.submitTxn() }() 462 return 463 } 464 465 func (ta *TransactionWithAuth) MinerScUpdateGlobals(ip *InputMap) (err error) { 466 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 467 transaction.MINERSC_UPDATE_GLOBALS, ip, 0) 468 if err != nil { 469 logging.Error(err) 470 return 471 } 472 go func() { ta.submitTxn() }() 473 return 474 } 475 476 func (ta *TransactionWithAuth) StorageScUpdateConfig(ip *InputMap) (err error) { 477 err = ta.t.createSmartContractTxn(StorageSmartContractAddress, 478 transaction.STORAGESC_UPDATE_SETTINGS, ip, 0) 479 if err != nil { 480 logging.Error(err) 481 return 482 } 483 go func() { ta.submitTxn() }() 484 return 485 } 486 487 func (t *TransactionWithAuth) AddHardfork(ip *InputMap) (err error) { 488 err = t.t.createSmartContractTxn(MinerSmartContractAddress, 489 transaction.ADD_HARDFORK, ip, 0) 490 if err != nil { 491 logging.Error(err) 492 return 493 } 494 go func() { t.submitTxn() }() 495 return 496 } 497 498 func (ta *TransactionWithAuth) ZCNSCUpdateGlobalConfig(ip *InputMap) (err error) { 499 err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, 0) 500 if err != nil { 501 logging.Error(err) 502 return 503 } 504 go ta.submitTxn() 505 return 506 } 507 508 func (ta *TransactionWithAuth) GetVerifyConfirmationStatus() ConfirmationStatus { 509 return ta.t.GetVerifyConfirmationStatus() //nolint 510 } 511 512 func (ta *TransactionWithAuth) MinerSCMinerSettings(info *MinerSCMinerInfo) ( 513 err error) { 514 515 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 516 transaction.MINERSC_MINER_SETTINGS, info, 0) 517 if err != nil { 518 logging.Error(err) 519 return 520 } 521 go func() { ta.submitTxn() }() 522 return 523 } 524 525 func (ta *TransactionWithAuth) MinerSCSharderSettings(info *MinerSCMinerInfo) ( 526 err error) { 527 528 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 529 transaction.MINERSC_SHARDER_SETTINGS, info, 0) 530 if err != nil { 531 logging.Error(err) 532 return 533 } 534 go func() { ta.submitTxn() }() 535 return 536 } 537 538 func (ta *TransactionWithAuth) MinerSCDeleteMiner(info *MinerSCMinerInfo) ( 539 err error) { 540 541 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 542 transaction.MINERSC_MINER_DELETE, info, 0) 543 if err != nil { 544 logging.Error(err) 545 return 546 } 547 go func() { ta.submitTxn() }() 548 return 549 } 550 551 func (ta *TransactionWithAuth) MinerSCDeleteSharder(info *MinerSCMinerInfo) ( 552 err error) { 553 554 err = ta.t.createSmartContractTxn(MinerSmartContractAddress, 555 transaction.MINERSC_SHARDER_DELETE, info, 0) 556 if err != nil { 557 logging.Error(err) 558 return 559 } 560 go func() { ta.submitTxn() }() 561 return 562 } 563 564 func (ta *TransactionWithAuth) ZCNSCUpdateAuthorizerConfig(ip *AuthorizerNode) (err error) { 565 err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, 0) 566 if err != nil { 567 logging.Error(err) 568 return 569 } 570 go ta.submitTxn() 571 return 572 } 573 574 func (ta *TransactionWithAuth) ZCNSCAddAuthorizer(ip *AddAuthorizerPayload) (err error) { 575 err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, 0) 576 if err != nil { 577 logging.Error(err) 578 return 579 } 580 go ta.submitTxn() 581 return 582 } 583 584 func (ta *TransactionWithAuth) ZCNSCAuthorizerHealthCheck(ip *AuthorizerHealthCheckPayload) (err error) { 585 err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, ip, 0) 586 if err != nil { 587 logging.Error(err) 588 return 589 } 590 go ta.t.setNonceAndSubmit() 591 return 592 } 593 594 func (ta *TransactionWithAuth) ZCNSCDeleteAuthorizer(ip *DeleteAuthorizerPayload) (err error) { 595 err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_DELETE_AUTHORIZER, ip, 0) 596 if err != nil { 597 logging.Error(err) 598 return 599 } 600 go ta.submitTxn() 601 return 602 } 603 604 func (ta *TransactionWithAuth) ZCNSCCollectReward(providerId string, providerType Provider) error { 605 pr := &scCollectReward{ 606 ProviderId: providerId, 607 ProviderType: int(providerType), 608 } 609 err := ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, 610 transaction.ZCNSC_COLLECT_REWARD, pr, 0) 611 if err != nil { 612 logging.Error(err) 613 return err 614 } 615 go func() { ta.t.setNonceAndSubmit() }() 616 return err 617 } 618 619 // ========================================================================== // 620 // vesting pool // 621 // ========================================================================== // 622 623 func (ta *TransactionWithAuth) VestingTrigger(poolID string) (err error) { 624 err = ta.t.vestingPoolTxn(transaction.VESTING_TRIGGER, poolID, 0) 625 if err != nil { 626 logging.Error(err) 627 return 628 } 629 go func() { ta.submitTxn() }() 630 return 631 } 632 633 func (ta *TransactionWithAuth) VestingStop(sr *VestingStopRequest) (err error) { 634 err = ta.t.createSmartContractTxn(VestingSmartContractAddress, 635 transaction.VESTING_STOP, sr, 0) 636 if err != nil { 637 logging.Error(err) 638 return 639 } 640 go func() { ta.submitTxn() }() 641 return 642 } 643 644 func (ta *TransactionWithAuth) VestingUnlock(poolID string) (err error) { 645 646 err = ta.t.vestingPoolTxn(transaction.VESTING_UNLOCK, poolID, 0) 647 if err != nil { 648 logging.Error(err) 649 return 650 } 651 go func() { ta.submitTxn() }() 652 return 653 } 654 655 func (ta *TransactionWithAuth) VestingDelete(poolID string) (err error) { 656 err = ta.t.vestingPoolTxn(transaction.VESTING_DELETE, poolID, 0) 657 if err != nil { 658 logging.Error(err) 659 return 660 } 661 go func() { ta.submitTxn() }() 662 return 663 }