From 004db9d77b973ec2f603d64ab345d6a42f3ff7d5 Mon Sep 17 00:00:00 2001 From: peterlimg <54137706+peterlimg@users.noreply.github.com> Date: Mon, 5 Feb 2024 01:43:14 +0900 Subject: [PATCH] [wasm] Expose minersc - stakePool/unstakePool collect rewards (#1386) * replaced coingecko url with internal price url (#1379) * Add miner/sharder stake and unstake support * Add collect rewards for miner/sharder --------- Co-authored-by: Gaurang Patel --- core/tokenrate/coingecko.go | 28 ++------- wasmsdk/blobber.go | 2 +- zboxcore/sdk/sdk.go | 120 ++++++++++++++++++++++++++---------- 3 files changed, 92 insertions(+), 58 deletions(-) diff --git a/core/tokenrate/coingecko.go b/core/tokenrate/coingecko.go index 1f123f42a..440a77f30 100644 --- a/core/tokenrate/coingecko.go +++ b/core/tokenrate/coingecko.go @@ -6,9 +6,7 @@ import ( "errors" "fmt" "net/http" - "os" "strconv" - "strings" "github.com/0chain/gosdk/core/resty" ) @@ -20,26 +18,8 @@ func (qq *coingeckoQuoteQuery) getUSD(ctx context.Context, symbol string) (float var result coingeckoResponse - s := strings.ToLower(symbol) - var coinID string - // - switch s { - case "zcn": - coinID = "0chain" - case "eth": - coinID = "ethereum" - default: - envName := "COINGECKO_COINID_" + strings.ToUpper(symbol) - id, ok := os.LookupEnv(envName) - if !ok { - return 0, errors.New("coingecko: please configure coinid on environment variable [" + envName + "] first") - } - coinID = id - - } - r := resty.New() - r.DoGet(ctx, "https://api.coingecko.com/api/v3/coins/"+coinID+"?localization=false"). + r.DoGet(ctx, "https://zcnprices.zus.network/market"). Then(func(req *http.Request, resp *http.Response, respBody []byte, cf context.CancelFunc, err error) error { if err != nil { @@ -47,7 +27,7 @@ func (qq *coingeckoQuoteQuery) getUSD(ctx context.Context, symbol string) (float } if resp.StatusCode != http.StatusOK { - return errors.New("coingecko: " + strconv.Itoa(resp.StatusCode) + resp.Status) + return errors.New("market API: " + strconv.Itoa(resp.StatusCode) + resp.Status) } err = json.Unmarshal(respBody, &result) @@ -85,10 +65,10 @@ func (qq *coingeckoQuoteQuery) getUSD(ctx context.Context, symbol string) (float return rate, nil } - return 0, fmt.Errorf("coingecko: invalid response %s", result.Raw) + return 0, fmt.Errorf("market API: invalid response %s", result.Raw) } - return 0, fmt.Errorf("coingecko: %s price is not provided on coingecko apis", symbol) + return 0, fmt.Errorf("market API: %s price is not provided on internal https://zcnprices.zus.network/market api", symbol) } type coingeckoResponse struct { diff --git a/wasmsdk/blobber.go b/wasmsdk/blobber.go index 203cb4856..fcbc14c93 100644 --- a/wasmsdk/blobber.go +++ b/wasmsdk/blobber.go @@ -112,7 +112,7 @@ func updateBlobberSettings(blobberSettingsJson string) (*transaction.Transaction InputArgs: blobberSettings, } - _, _, _, txn, err := sdk.SmartContractTxn(sn) + _, _, _, txn, err := sdk.StorageSmartContractTxn(sn) return txn, err } diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 6c95745be..2fc200919 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -33,6 +33,8 @@ import ( ) const STORAGE_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7" +const MINERSC_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9" +const ZCNSC_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712e0" var sdkNotInitialized = errors.New("sdk_not_initialized", "SDK is not initialised") var allocationNotFound = errors.New("couldnt_find_allocation", "Couldn't find the allocation required for update") @@ -166,7 +168,7 @@ func CreateReadPool() (hash string, nonce int64, err error) { if !sdkInitialized { return "", 0, sdkNotInitialized } - hash, _, nonce, _, err = smartContractTxn(transaction.SmartContractTxnData{ + hash, _, nonce, _, err = storageSmartContractTxn(transaction.SmartContractTxnData{ Name: transaction.STORAGESC_CREATE_READ_POOL, }) return @@ -224,7 +226,7 @@ func ReadPoolLock(tokens, fee uint64) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_READ_POOL_LOCK, InputArgs: nil, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, tokens, fee) + hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) return } @@ -238,7 +240,7 @@ func ReadPoolUnlock(fee uint64) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_READ_POOL_UNLOCK, InputArgs: nil, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee) + hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) return } @@ -377,11 +379,24 @@ func StakePoolLock(providerType ProviderType, providerID string, value, fee uint ProviderType: providerType, ProviderID: providerID, } + var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_STAKE_POOL_LOCK, InputArgs: &spr, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, value, fee) + + var scAddress string + switch providerType { + case ProviderBlobber, ProviderValidator: + scAddress = STORAGE_SCADDRESS + sn.Name = transaction.STORAGESC_STAKE_POOL_LOCK + case ProviderMiner, ProviderSharder: + scAddress = MINERSC_SCADDRESS + sn.Name = transaction.MINERSC_LOCK + default: + return "", 0, errors.Newf("stake_pool_lock", "unsupported provider type: %v", providerType) + } + + hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, value, fee) return } @@ -419,12 +434,23 @@ func StakePoolUnlock(providerType ProviderType, providerID string, fee uint64) ( } var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_STAKE_POOL_UNLOCK, InputArgs: &spr, } + var scAddress string + switch providerType { + case ProviderBlobber, ProviderValidator: + scAddress = STORAGE_SCADDRESS + sn.Name = transaction.STORAGESC_STAKE_POOL_UNLOCK + case ProviderMiner, ProviderSharder: + scAddress = MINERSC_SCADDRESS + sn.Name = transaction.MINERSC_UNLOCK + default: + return 0, 0, errors.Newf("stake_pool_unlock", "unsupported provider type: %v", providerType) + } + var out string - if _, out, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee); err != nil { + if _, out, nonce, _, err = smartContractTxnValueFeeWithRetry(scAddress, sn, 0, fee); err != nil { return // an error } @@ -457,7 +483,8 @@ func WritePoolLock(allocID string, tokens, fee uint64) (hash string, nonce int64 Name: transaction.STORAGESC_WRITE_POOL_LOCK, InputArgs: &req, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, tokens, fee) + + hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, tokens, fee) return } @@ -478,7 +505,7 @@ func WritePoolUnlock(allocID string, fee uint64) (hash string, nonce int64, err Name: transaction.STORAGESC_WRITE_POOL_UNLOCK, InputArgs: &req, } - hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(sn, 0, fee) + hash, _, nonce, _, err = smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, 0, fee) return } @@ -1025,7 +1052,7 @@ func CreateAllocationForOwner( Name: transaction.NEW_ALLOCATION_REQUEST, InputArgs: allocationRequest, } - hash, _, nonce, txn, err = smartContractTxnValue(sn, lock) + hash, _, nonce, txn, err = storageSmartContractTxnValue(sn, lock) return } @@ -1163,7 +1190,7 @@ func AddFreeStorageAssigner(name, publicKey string, individualLimit, totalLimit Name: transaction.ADD_FREE_ALLOCATION_ASSIGNER, InputArgs: input, } - hash, _, n, _, err := smartContractTxn(sn) + hash, _, n, _, err := storageSmartContractTxn(sn) return hash, n, err } @@ -1191,7 +1218,7 @@ func CreateFreeAllocation(marker string, value uint64) (string, int64, error) { Name: transaction.NEW_FREE_ALLOCATION, InputArgs: input, } - hash, _, n, _, err := smartContractTxnValue(sn, value) + hash, _, n, _, err := storageSmartContractTxnValue(sn, value) return hash, n, err } @@ -1232,7 +1259,7 @@ func UpdateAllocation( Name: transaction.STORAGESC_UPDATE_ALLOCATION, InputArgs: updateAllocationRequest, } - hash, _, nonce, _, err = smartContractTxnValue(sn, lock) + hash, _, nonce, _, err = storageSmartContractTxnValue(sn, lock) return } @@ -1244,7 +1271,7 @@ func FinalizeAllocation(allocID string) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_FINALIZE_ALLOCATION, InputArgs: map[string]interface{}{"allocation_id": allocID}, } - hash, _, nonce, _, err = smartContractTxn(sn) + hash, _, nonce, _, err = storageSmartContractTxn(sn) return } @@ -1256,7 +1283,7 @@ func CancelAllocation(allocID string) (hash string, nonce int64, err error) { Name: transaction.STORAGESC_CANCEL_ALLOCATION, InputArgs: map[string]interface{}{"allocation_id": allocID}, } - hash, _, nonce, _, err = smartContractTxn(sn) + hash, _, nonce, _, err = storageSmartContractTxn(sn) return } @@ -1289,7 +1316,7 @@ func KillProvider(providerId string, providerType ProviderType) (string, int64, default: return "", 0, fmt.Errorf("kill provider type %v not implimented", providerType) } - hash, _, n, _, err := smartContractTxn(sn) + hash, _, n, _, err := storageSmartContractTxn(sn) return hash, n, err } @@ -1313,7 +1340,7 @@ func ShutdownProvider(providerType ProviderType, providerID string) (string, int default: return "", 0, fmt.Errorf("shutdown provider type %v not implimented", providerType) } - hash, _, n, _, err := smartContractTxn(sn) + hash, _, n, _, err := storageSmartContractTxn(sn) return hash, n, err } @@ -1326,11 +1353,27 @@ func CollectRewards(providerId string, providerType ProviderType) (string, int64 "provider_id": providerId, "provider_type": providerType, } + var sn = transaction.SmartContractTxnData{ - Name: transaction.STORAGESC_COLLECT_REWARD, InputArgs: input, } - hash, _, n, _, err := smartContractTxn(sn) + + var scAddress string + switch providerType { + case ProviderBlobber, ProviderValidator: + scAddress = STORAGE_SCADDRESS + sn.Name = transaction.STORAGESC_COLLECT_REWARD + case ProviderMiner, ProviderSharder: + scAddress = MINERSC_SCADDRESS + sn.Name = transaction.MINERSC_COLLECT_REWARD + // case ProviderAuthorizer: + // scAddress = ZCNSC_SCADDRESS + // sn.Name = transaction.ZCNSC_COLLECT_REWARD + default: + return "", 0, fmt.Errorf("collect rewards provider type %v not implimented", providerType) + } + + hash, _, n, _, err := smartContractTxn(scAddress, sn) return hash, n, err } @@ -1361,7 +1404,7 @@ func TransferAllocation(allocationId, newOwner, newOwnerPublicKey string) (strin Name: transaction.STORAGESC_UPDATE_ALLOCATION, InputArgs: allocationRequest, } - hash, _, n, _, err := smartContractTxn(sn) + hash, _, n, _, err := storageSmartContractTxn(sn) return hash, n, err } @@ -1373,7 +1416,7 @@ func UpdateBlobberSettings(blob *UpdateBlobber) (resp string, nonce int64, err e Name: transaction.STORAGESC_UPDATE_BLOBBER_SETTINGS, InputArgs: blob, } - resp, _, nonce, _, err = smartContractTxn(sn) + resp, _, nonce, _, err = storageSmartContractTxn(sn) return } @@ -1386,40 +1429,51 @@ func UpdateValidatorSettings(v *UpdateValidator) (resp string, nonce int64, err Name: transaction.STORAGESC_UPDATE_VALIDATOR_SETTINGS, InputArgs: v.ConvertToValidationNode(), } - resp, _, nonce, _, err = smartContractTxn(sn) + resp, _, nonce, _, err = storageSmartContractTxn(sn) return } -func SmartContractTxn(sn transaction.SmartContractTxnData) ( +func smartContractTxn(scAddress string, sn transaction.SmartContractTxnData) ( + hash, out string, nonce int64, txn *transaction.Transaction, err error) { + return smartContractTxnValue(scAddress, sn, 0) +} + +func StorageSmartContractTxn(sn transaction.SmartContractTxnData) ( + hash, out string, nonce int64, txn *transaction.Transaction, err error) { + + return storageSmartContractTxnValue(sn, 0) +} + +func storageSmartContractTxn(sn transaction.SmartContractTxnData) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return smartContractTxnValue(sn, 0) + return storageSmartContractTxnValue(sn, 0) } -func smartContractTxn(sn transaction.SmartContractTxnData) ( +func smartContractTxnValue(scAddress string, sn transaction.SmartContractTxnData, value uint64) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { - return smartContractTxnValue(sn, 0) + return smartContractTxnValueFeeWithRetry(scAddress, sn, value, client.TxnFee()) } -func smartContractTxnValue(sn transaction.SmartContractTxnData, value uint64) ( +func storageSmartContractTxnValue(sn transaction.SmartContractTxnData, value uint64) ( hash, out string, nonce int64, txn *transaction.Transaction, err error) { // Fee is set during sdk initialization. - return smartContractTxnValueFeeWithRetry(sn, value, client.TxnFee()) + return smartContractTxnValueFeeWithRetry(STORAGE_SCADDRESS, sn, value, client.TxnFee()) } -func smartContractTxnValueFeeWithRetry(sn transaction.SmartContractTxnData, +func smartContractTxnValueFeeWithRetry(scAddress string, sn transaction.SmartContractTxnData, value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { - hash, out, nonce, t, err = smartContractTxnValueFee(sn, value, fee) + hash, out, nonce, t, err = smartContractTxnValueFee(scAddress, sn, value, fee) if err != nil && strings.Contains(err.Error(), "invalid transaction nonce") { - return smartContractTxnValueFee(sn, value, fee) + return smartContractTxnValueFee(scAddress, sn, value, fee) } return } -func smartContractTxnValueFee(sn transaction.SmartContractTxnData, +func smartContractTxnValueFee(scAddress string, sn transaction.SmartContractTxnData, value, fee uint64) (hash, out string, nonce int64, t *transaction.Transaction, err error) { var requestBytes []byte @@ -1431,7 +1485,7 @@ func smartContractTxnValueFee(sn transaction.SmartContractTxnData, blockchain.GetChainID(), client.GetClientPublicKey(), nonce) txn.TransactionData = string(requestBytes) - txn.ToClientID = STORAGE_SCADDRESS + txn.ToClientID = scAddress txn.Value = value txn.TransactionFee = fee txn.TransactionType = transaction.TxnTypeSmartContract