Skip to content

Commit

Permalink
feat: Add ETH Balance Check on RP Matching (#298)
Browse files Browse the repository at this point in the history
* feat: Add WeiToEther method on utils.go

* feat: Add GetBalance method on sdk.go

* feat: Check resource provider's ETH balance before adding resource offer

* chore: Update comment to be more accurate

* feat: Use InstructionPrice for balance comparison

---------

Co-authored-by: Alvin Reyes <me@areyes.onl>
  • Loading branch information
apquinit and alvin-reyes authored Aug 25, 2024
1 parent e3d2aae commit 561a64a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ type ResourceOffer struct {
ID string `json:"id"`
// this is basically a nonce so we don't have one ID pointing at multiple offers
CreatedAt int `json:"created_at"`
// the address of the job creator
// the address of the resource provider
ResourceProvider string `json:"resource_provider"`
// allows a resource provider to manage multiple offers
// that are essentially the same
Expand Down
12 changes: 12 additions & 0 deletions pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ func (controller *SolverController) addResourceOffer(resourceOffer data.Resource
}
resourceOffer.ID = id

// Check the resource provider's ETH balance
balance, err := controller.web3SDK.GetBalance(resourceOffer.ResourceProvider)
if err != nil {
return nil, fmt.Errorf("failed to retrieve ETH balance for resource provider: %v", err)
}
// Convert InstructionPrice from ETH to Wei
requiredBalanceWei := web3.EtherToWei(float64(resourceOffer.DefaultPricing.InstructionPrice))
// If the balance is less than the required balance, don't add the resource offer
if balance.Cmp(requiredBalanceWei) < 0 {
return nil, err
}

controller.log.Info("add resource offer", resourceOffer)

metricsDashboard.TrackNodeInfo(resourceOffer)
Expand Down
13 changes: 13 additions & 0 deletions pkg/web3/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,16 @@ func (sdk *Web3SDK) WaitTx(ctx context.Context, tx *types.Transaction) (*types.R
func (sdk *Web3SDK) GetAddress() common.Address {
return crypto.PubkeyToAddress(GetPublicKey(sdk.PrivateKey))
}

func (sdk *Web3SDK) GetBalance(address string) (*big.Int, error) {
// Convert the string address to common.Address
ethAddress := common.HexToAddress(address)

// Get the balance using the converted address
balance, err := sdk.Client.BalanceAt(context.Background(), ethAddress, nil)
if err != nil {
log.Error().Msgf("error for GetBalance: %s", err.Error())
return nil, err
}
return balance, nil
}
4 changes: 4 additions & 0 deletions pkg/web3/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func EtherToWeiUint64(etherAmount float64) uint64 {
wei := EtherToWei(etherAmount)
return wei.Uint64()
}
func WeiToEther(wei *big.Int) *big.Float {
ethValue := new(big.Float).Quo(new(big.Float).SetInt(wei), big.NewFloat(1e18))
return ethValue
}

func ConvertStringToBigInt(st string) big.Int {
bigInt, _ := big.NewInt(0).SetString(st, 10)
Expand Down

0 comments on commit 561a64a

Please sign in to comment.