gitlab.nesad.fit.vutbr.cz/blended/libblended@v0.0.0-20221202124402-2bee159339df/ethereum/contract_extensions.go (about)

     1  package ethereum
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"github.com/ethereum/go-ethereum/accounts/abi/bind"
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/core/types"
     9  	"github.com/ethereum/go-ethereum/ethclient"
    10  	"os"
    11  	"time"
    12  )
    13  
    14  type TaskState uint8
    15  
    16  const (
    17  	TaskStateInit TaskState = iota
    18  	TaskStateRequestsLocked TaskState = iota
    19  	TaskStateRunning TaskState = iota
    20  	TaskStateRunFailed TaskState = iota
    21  	TaskStateFinished TaskState = iota
    22  	TaskStateNacked TaskState = iota
    23  )
    24  
    25  func CreateTransactor(keyfile string, password string) (*bind.TransactOpts, error) {
    26  	key, err := os.Open(keyfile)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	defer key.Close()
    31  
    32  	return bind.NewTransactor(key, password)
    33  }
    34  
    35  func PollForTransaction(
    36  	client *ethclient.Client,
    37  	txHash common.Hash,
    38  	maxDuration time.Duration,
    39  ) (*types.Receipt, error) {
    40  	finish := time.Now().Add(maxDuration)
    41  	for time.Now().Before(finish) {
    42  		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    43  		defer cancel()
    44  		receipt, _ := client.TransactionReceipt(ctx, txHash)
    45  		if receipt != nil {
    46  			return receipt, nil
    47  		}
    48  		time.Sleep(time.Second)
    49  	}
    50  	return nil, errors.New("Transaction polling timed out")
    51  }
    52  
    53  func (_Task *TaskCaller) ResourceRequestsByAddress(opts *bind.CallOpts, owner common.Address) ([]TaskResourceRequest, error) {
    54  	requests, err := _Task.GetResourceRequests(opts)
    55  
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  
    60  	ret := make([]TaskResourceRequest, 0)
    61  
    62  	for _, request := range requests {
    63  		if request.Owner == owner {
    64  			ret = append(ret, request)
    65  		}
    66  	}
    67  	return ret, nil
    68  }