github.com/0chain/gosdk@v1.17.11/zcncore/transactionauth_mobile.go (about)

     1  //go:build mobile
     2  // +build mobile
     3  
     4  package zcncore
     5  
     6  import (
     7  	"encoding/json"
     8  	"time"
     9  
    10  	"github.com/0chain/errors"
    11  	"github.com/0chain/gosdk/core/transaction"
    12  )
    13  
    14  func newTransactionWithAuth(cb TransactionCallback, txnFee string, nonce int64) (*TransactionWithAuth, error) {
    15  	ta := &TransactionWithAuth{}
    16  	var err error
    17  	ta.t, err = newTransaction(cb, txnFee, nonce)
    18  	return ta, err
    19  }
    20  
    21  func (ta *TransactionWithAuth) GetDetails() *transaction.Transaction {
    22  	return ta.t.txn
    23  }
    24  
    25  func (ta *TransactionWithAuth) ExecuteSmartContract(address string, methodName string, input string, val string) error {
    26  	err := ta.t.createSmartContractTxn(address, methodName, input, val)
    27  	if err != nil {
    28  		return err
    29  	}
    30  	go func() {
    31  		ta.submitTxn()
    32  	}()
    33  
    34  	return nil
    35  }
    36  
    37  func (ta *TransactionWithAuth) Send(toClientID string, val string, desc string) error {
    38  	txnData, err := json.Marshal(SendTxnData{Note: desc})
    39  	if err != nil {
    40  		return errors.New("", "Could not serialize description to transaction_data")
    41  	}
    42  	go func() {
    43  		ta.t.txn.TransactionType = transaction.TxnTypeSend
    44  		ta.t.txn.ToClientID = toClientID
    45  		ta.t.txn.Value = val
    46  		ta.t.txn.TransactionData = string(txnData)
    47  		ta.submitTxn()
    48  	}()
    49  	return nil
    50  }
    51  
    52  func (ta *TransactionWithAuth) VestingAdd(ar VestingAddRequest, value string) error {
    53  	err := ta.t.createSmartContractTxn(VestingSmartContractAddress,
    54  		transaction.VESTING_ADD, ar, value)
    55  	if err != nil {
    56  		logging.Error(err)
    57  		return err
    58  	}
    59  	go func() { ta.submitTxn() }()
    60  	return nil
    61  }
    62  
    63  func (ta *TransactionWithAuth) MinerSCLock(providerId string, providerType int, lock string) error {
    64  	pr := stakePoolRequest{
    65  		ProviderType: providerType,
    66  		ProviderID:   providerId,
    67  	}
    68  
    69  	err := ta.t.createSmartContractTxn(MinerSmartContractAddress,
    70  		transaction.MINERSC_LOCK, &pr, lock)
    71  	if err != nil {
    72  		logging.Error(err)
    73  		return err
    74  	}
    75  	go func() { ta.submitTxn() }()
    76  	return nil
    77  }
    78  
    79  func (ta *TransactionWithAuth) MinerSCUnlock(providerId string, providerType int) error {
    80  	pr := &stakePoolRequest{
    81  		ProviderID:   providerId,
    82  		ProviderType: providerType,
    83  	}
    84  	err := ta.t.createSmartContractTxn(MinerSmartContractAddress,
    85  		transaction.MINERSC_LOCK, pr, "0")
    86  	if err != nil {
    87  		logging.Error(err)
    88  		return err
    89  	}
    90  	go func() { ta.submitTxn() }()
    91  	return err
    92  }
    93  
    94  // FinalizeAllocation transaction.
    95  func (ta *TransactionWithAuth) FinalizeAllocation(allocID string) error {
    96  	type finiRequest struct {
    97  		AllocationID string `json:"allocation_id"`
    98  	}
    99  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   100  		transaction.STORAGESC_FINALIZE_ALLOCATION, &finiRequest{
   101  			AllocationID: allocID,
   102  		}, "0")
   103  	if err != nil {
   104  		logging.Error(err)
   105  		return err
   106  	}
   107  	go func() { ta.submitTxn() }()
   108  	return nil
   109  }
   110  
   111  // CancelAllocation transaction.
   112  func (ta *TransactionWithAuth) CancelAllocation(allocID string) error {
   113  	type cancelRequest struct {
   114  		AllocationID string `json:"allocation_id"`
   115  	}
   116  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   117  		transaction.STORAGESC_CANCEL_ALLOCATION, &cancelRequest{
   118  			AllocationID: allocID,
   119  		}, "0")
   120  	if err != nil {
   121  		logging.Error(err)
   122  		return err
   123  	}
   124  	go func() { ta.submitTxn() }()
   125  	return nil
   126  }
   127  
   128  // CreateAllocation transaction.
   129  func (ta *TransactionWithAuth) CreateAllocation(car *CreateAllocationRequest,
   130  	lock string) error {
   131  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   132  		transaction.STORAGESC_CREATE_ALLOCATION, car, lock)
   133  	if err != nil {
   134  		logging.Error(err)
   135  		return err
   136  	}
   137  	go func() { ta.submitTxn() }()
   138  	return nil
   139  }
   140  
   141  // CreateReadPool for current user.
   142  func (ta *TransactionWithAuth) CreateReadPool() error {
   143  	if err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   144  		transaction.STORAGESC_CREATE_READ_POOL, nil, "0"); err != nil {
   145  		logging.Error(err)
   146  		return err
   147  	}
   148  	go func() { ta.submitTxn() }()
   149  	return nil
   150  }
   151  
   152  // ReadPoolLock locks tokens for current user and given allocation, using given
   153  // duration. If blobberID is not empty, then tokens will be locked for given
   154  // allocation->blobber only.
   155  func (ta *TransactionWithAuth) ReadPoolLock(allocID, blobberID string,
   156  	duration int64, lock string) error {
   157  	type lockRequest struct {
   158  		Duration     time.Duration `json:"duration"`
   159  		AllocationID string        `json:"allocation_id"`
   160  		BlobberID    string        `json:"blobber_id,omitempty"`
   161  	}
   162  
   163  	var lr lockRequest
   164  	lr.Duration = time.Duration(duration)
   165  	lr.AllocationID = allocID
   166  	lr.BlobberID = blobberID
   167  
   168  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   169  		transaction.STORAGESC_READ_POOL_LOCK, &lr, lock)
   170  	if err != nil {
   171  		logging.Error(err)
   172  		return err
   173  	}
   174  	go func() { ta.submitTxn() }()
   175  	return nil
   176  }
   177  
   178  // ReadPoolUnlock for current user and given pool.
   179  func (ta *TransactionWithAuth) ReadPoolUnlock() error {
   180  	if err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   181  		transaction.STORAGESC_READ_POOL_UNLOCK, nil, "0"); err != nil {
   182  		logging.Error(err)
   183  		return err
   184  	}
   185  	go func() { ta.submitTxn() }()
   186  	return nil
   187  }
   188  
   189  // StakePoolLock used to lock tokens in a stake pool of a blobber.
   190  func (ta *TransactionWithAuth) StakePoolLock(providerId string, providerType int,
   191  	lock string) error {
   192  	type stakePoolRequest struct {
   193  		ProviderType int    `json:"provider_type,omitempty"`
   194  		ProviderID   string `json:"provider_id,omitempty"`
   195  	}
   196  
   197  	spr := stakePoolRequest{
   198  		ProviderType: providerType,
   199  		ProviderID:   providerId,
   200  	}
   201  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   202  		transaction.STORAGESC_STAKE_POOL_LOCK, &spr, lock)
   203  	if err != nil {
   204  		logging.Error(err)
   205  		return err
   206  	}
   207  	go func() { ta.submitTxn() }()
   208  	return nil
   209  }
   210  
   211  // StakePoolUnlock by blobberID
   212  func (ta *TransactionWithAuth) StakePoolUnlock(providerId string, providerType int) error {
   213  	spr := stakePoolRequest{
   214  		ProviderType: providerType,
   215  		ProviderID:   providerId,
   216  	}
   217  
   218  	if err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   219  		transaction.STORAGESC_STAKE_POOL_UNLOCK, &spr, "0"); err != nil {
   220  		logging.Error(err)
   221  		return err
   222  	}
   223  	go func() { ta.submitTxn() }()
   224  	return nil
   225  }
   226  
   227  // UpdateBlobberSettings update settings of a blobber.
   228  func (ta *TransactionWithAuth) UpdateBlobberSettings(blob Blobber) error {
   229  	if err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   230  		transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, blob, "0"); err != nil {
   231  		logging.Error(err)
   232  		return err
   233  	}
   234  	go func() { ta.submitTxn() }()
   235  	return nil
   236  }
   237  
   238  // UpdateAllocation transaction.
   239  func (ta *TransactionWithAuth) UpdateAllocation(allocID string, sizeDiff int64,
   240  	expirationDiff int64, lock string) error {
   241  	type updateAllocationRequest struct {
   242  		ID         string `json:"id"`              // allocation id
   243  		Size       int64  `json:"size"`            // difference
   244  		Expiration int64  `json:"expiration_date"` // difference
   245  	}
   246  
   247  	var uar updateAllocationRequest
   248  	uar.ID = allocID
   249  	uar.Size = sizeDiff
   250  	uar.Expiration = expirationDiff
   251  
   252  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   253  		transaction.STORAGESC_UPDATE_ALLOCATION, &uar, lock)
   254  	if err != nil {
   255  		logging.Error(err)
   256  		return err
   257  	}
   258  	go func() { ta.submitTxn() }()
   259  	return nil
   260  }
   261  
   262  // WritePoolLock locks tokens for current user and given allocation, using given
   263  // duration. If blobberID is not empty, then tokens will be locked for given
   264  // allocation->blobber only.
   265  func (ta *TransactionWithAuth) WritePoolLock(allocID, lock string) error {
   266  	var lr = struct {
   267  		AllocationID string `json:"allocation_id"`
   268  	}{
   269  		AllocationID: allocID,
   270  	}
   271  
   272  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   273  		transaction.STORAGESC_WRITE_POOL_LOCK, &lr, lock)
   274  	if err != nil {
   275  		logging.Error(err)
   276  		return err
   277  	}
   278  	go func() { ta.submitTxn() }()
   279  	return nil
   280  }
   281  
   282  // WritePoolUnlock for current user and given pool.
   283  func (ta *TransactionWithAuth) WritePoolUnlock(allocID string) error {
   284  	var ur = struct {
   285  		AllocationID string `json:"allocation_id"`
   286  	}{
   287  		AllocationID: allocID,
   288  	}
   289  
   290  	if err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   291  		transaction.STORAGESC_WRITE_POOL_UNLOCK, &ur, "0"); err != nil {
   292  		logging.Error(err)
   293  		return err
   294  	}
   295  	go func() { ta.submitTxn() }()
   296  	return nil
   297  }
   298  
   299  func (ta *TransactionWithAuth) MinerSCCollectReward(providerId string, providerType int) error {
   300  	pr := &scCollectReward{
   301  		ProviderId:   providerId,
   302  		ProviderType: providerType,
   303  	}
   304  	err := ta.t.createSmartContractTxn(MinerSmartContractAddress,
   305  		transaction.MINERSC_COLLECT_REWARD, pr, "0")
   306  	if err != nil {
   307  		logging.Error(err)
   308  		return err
   309  	}
   310  	go ta.submitTxn()
   311  	return err
   312  }
   313  
   314  func (ta *TransactionWithAuth) StorageSCCollectReward(providerId string, providerType int) error {
   315  	pr := &scCollectReward{
   316  		ProviderId:   providerId,
   317  		ProviderType: providerType,
   318  	}
   319  	err := ta.t.createSmartContractTxn(StorageSmartContractAddress,
   320  		transaction.STORAGESC_COLLECT_REWARD, pr, "0")
   321  	if err != nil {
   322  		logging.Error(err)
   323  		return err
   324  	}
   325  	go func() { ta.submitTxn() }()
   326  	return err
   327  }
   328  
   329  func (ta *TransactionWithAuth) VestingUpdateConfig(ip InputMap) (err error) {
   330  	err = ta.t.createSmartContractTxn(VestingSmartContractAddress,
   331  		transaction.VESTING_UPDATE_SETTINGS, ip, "0")
   332  	if err != nil {
   333  		logging.Error(err)
   334  		return
   335  	}
   336  	go func() { ta.submitTxn() }()
   337  	return
   338  }
   339  
   340  // faucet smart contract
   341  
   342  func (ta *TransactionWithAuth) FaucetUpdateConfig(ip InputMap) (err error) {
   343  	err = ta.t.createSmartContractTxn(FaucetSmartContractAddress,
   344  		transaction.FAUCETSC_UPDATE_SETTINGS, ip, "0")
   345  	if err != nil {
   346  		logging.Error(err)
   347  		return
   348  	}
   349  	go func() { ta.submitTxn() }()
   350  	return
   351  }
   352  
   353  func (ta *TransactionWithAuth) MinerScUpdateConfig(ip InputMap) (err error) {
   354  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   355  		transaction.MINERSC_UPDATE_SETTINGS, ip, "0")
   356  	if err != nil {
   357  		logging.Error(err)
   358  		return
   359  	}
   360  	go func() { ta.submitTxn() }()
   361  	return
   362  }
   363  
   364  func (ta *TransactionWithAuth) MinerScUpdateGlobals(ip InputMap) (err error) {
   365  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   366  		transaction.MINERSC_UPDATE_GLOBALS, ip, "0")
   367  	if err != nil {
   368  		logging.Error(err)
   369  		return
   370  	}
   371  	go func() { ta.submitTxn() }()
   372  	return
   373  }
   374  
   375  func (ta *TransactionWithAuth) StorageScUpdateConfig(ip InputMap) (err error) {
   376  	err = ta.t.createSmartContractTxn(StorageSmartContractAddress,
   377  		transaction.STORAGESC_UPDATE_SETTINGS, ip, "0")
   378  	if err != nil {
   379  		logging.Error(err)
   380  		return
   381  	}
   382  	go func() { ta.submitTxn() }()
   383  	return
   384  }
   385  
   386  func (ta *TransactionWithAuth) ZCNSCUpdateGlobalConfig(ip InputMap) (err error) {
   387  	err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress,
   388  		transaction.ZCNSC_UPDATE_GLOBAL_CONFIG, ip, "0")
   389  	if err != nil {
   390  		logging.Error(err)
   391  		return
   392  	}
   393  	go ta.submitTxn()
   394  	return
   395  }
   396  
   397  func (ta *TransactionWithAuth) GetVerifyConfirmationStatus() int {
   398  	return ta.t.GetVerifyConfirmationStatus()
   399  }
   400  
   401  func (ta *TransactionWithAuth) MinerSCMinerSettings(info MinerSCMinerInfo) (
   402  	err error) {
   403  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   404  		transaction.MINERSC_MINER_SETTINGS, info, "0")
   405  	if err != nil {
   406  		logging.Error(err)
   407  		return
   408  	}
   409  	go func() { ta.submitTxn() }()
   410  	return
   411  }
   412  
   413  func (ta *TransactionWithAuth) MinerSCSharderSettings(info MinerSCMinerInfo) (
   414  	err error) {
   415  
   416  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   417  		transaction.MINERSC_SHARDER_SETTINGS, info, "0")
   418  	if err != nil {
   419  		logging.Error(err)
   420  		return
   421  	}
   422  	go func() { ta.submitTxn() }()
   423  	return
   424  }
   425  
   426  func (ta *TransactionWithAuth) MinerSCDeleteMiner(info MinerSCMinerInfo) (
   427  	err error) {
   428  
   429  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   430  		transaction.MINERSC_MINER_DELETE, info, "0")
   431  	if err != nil {
   432  		logging.Error(err)
   433  		return
   434  	}
   435  	go func() { ta.submitTxn() }()
   436  	return
   437  }
   438  
   439  func (ta *TransactionWithAuth) MinerSCDeleteSharder(info MinerSCMinerInfo) (
   440  	err error) {
   441  
   442  	err = ta.t.createSmartContractTxn(MinerSmartContractAddress,
   443  		transaction.MINERSC_SHARDER_DELETE, info, "0")
   444  	if err != nil {
   445  		logging.Error(err)
   446  		return
   447  	}
   448  	go func() { ta.submitTxn() }()
   449  	return
   450  }
   451  
   452  func (ta *TransactionWithAuth) ZCNSCUpdateAuthorizerConfig(ip AuthorizerNode) (err error) {
   453  	err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_UPDATE_AUTHORIZER_CONFIG, ip, "0")
   454  	if err != nil {
   455  		logging.Error(err)
   456  		return
   457  	}
   458  	go ta.submitTxn()
   459  	return
   460  }
   461  
   462  func (ta *TransactionWithAuth) ZCNSCAddAuthorizer(ip AddAuthorizerPayload) (err error) {
   463  	err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_ADD_AUTHORIZER, ip, "0")
   464  	if err != nil {
   465  		logging.Error(err)
   466  		return
   467  	}
   468  	go ta.submitTxn()
   469  	return
   470  }
   471  
   472  func (ta *TransactionWithAuth) ZCNSCAuthorizerHealthCheck(ip *AuthorizerHealthCheckPayload) (err error) {
   473  	err = ta.t.createSmartContractTxn(ZCNSCSmartContractAddress, transaction.ZCNSC_AUTHORIZER_HEALTH_CHECK, ip, "0")
   474  	if err != nil {
   475  		logging.Error(err)
   476  		return
   477  	}
   478  	go ta.t.setNonceAndSubmit()
   479  	return
   480  }
   481  
   482  func (ta *TransactionWithAuth) VestingTrigger(poolID string) (err error) {
   483  	err = ta.t.vestingPoolTxn(transaction.VESTING_TRIGGER, poolID, "0")
   484  	if err != nil {
   485  		logging.Error(err)
   486  		return
   487  	}
   488  	go func() { ta.submitTxn() }()
   489  	return
   490  }
   491  
   492  func (ta *TransactionWithAuth) VestingStop(sr *VestingStopRequest) (err error) {
   493  	err = ta.t.createSmartContractTxn(VestingSmartContractAddress,
   494  		transaction.VESTING_STOP, sr, "0")
   495  	if err != nil {
   496  		logging.Error(err)
   497  		return
   498  	}
   499  	go func() { ta.submitTxn() }()
   500  	return
   501  }
   502  
   503  func (ta *TransactionWithAuth) VestingUnlock(poolID string) (err error) {
   504  
   505  	err = ta.t.vestingPoolTxn(transaction.VESTING_UNLOCK, poolID, "0")
   506  	if err != nil {
   507  		logging.Error(err)
   508  		return
   509  	}
   510  	go func() { ta.submitTxn() }()
   511  	return
   512  }
   513  
   514  func (ta *TransactionWithAuth) VestingDelete(poolID string) (err error) {
   515  	err = ta.t.vestingPoolTxn(transaction.VESTING_DELETE, poolID, "0")
   516  	if err != nil {
   517  		logging.Error(err)
   518  		return
   519  	}
   520  	go func() { ta.submitTxn() }()
   521  	return
   522  }