diff --git a/pkg/data/types.go b/pkg/data/types.go index 20869888..007fa2ae 100644 --- a/pkg/data/types.go +++ b/pkg/data/types.go @@ -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 diff --git a/pkg/solver/controller.go b/pkg/solver/controller.go index 4d1580cd..f49cbb74 100644 --- a/pkg/solver/controller.go +++ b/pkg/solver/controller.go @@ -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) diff --git a/pkg/web3/sdk.go b/pkg/web3/sdk.go index f4a28795..e3e8c657 100644 --- a/pkg/web3/sdk.go +++ b/pkg/web3/sdk.go @@ -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 +} diff --git a/pkg/web3/utils.go b/pkg/web3/utils.go index 5d93a376..9c2d44e4 100644 --- a/pkg/web3/utils.go +++ b/pkg/web3/utils.go @@ -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)