Skip to content

Commit

Permalink
[wasm] Expose minersc - stakePool/unstakePool collect rewards (#1386)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
peterlimg and Zorro30 authored Feb 4, 2024
1 parent ccda3cd commit 004db9d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 58 deletions.
28 changes: 4 additions & 24 deletions core/tokenrate/coingecko.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"

"github.com/0chain/gosdk/core/resty"
)
Expand All @@ -20,34 +18,16 @@ 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 {
return err
}

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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion wasmsdk/blobber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
120 changes: 87 additions & 33 deletions zboxcore/sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 004db9d

Please sign in to comment.