/
metascheduler_backend.go
108 lines (96 loc) · 3.24 KB
/
metascheduler_backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2024 DeepSquare Association
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
// Package metascheduler defines APIs for interacting with the DeepSquare Grid.
package metascheduler
import (
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
metaschedulerabi "github.com/deepsquare-io/grid/cli/types/abi/metascheduler"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
// ErrNewRequestJobNotFound happens when submitting a job, but the job is not found.
//
// This would typically be used for panicking.
ErrNewRequestJobNotFound = errors.New("new request job event not found")
)
var (
metaschedulerABI *abi.ABI
ierc20ABI *abi.ABI
jobsABI *abi.ABI
newJobRequestEvent abi.Event
jobTransitionEvent abi.Event
transferEvent abi.Event
approvalEvent abi.Event
)
func init() {
var err error
metaschedulerABI, err = metaschedulerabi.MetaSchedulerMetaData.GetAbi()
if err != nil {
panic(fmt.Errorf("failed to parse metascheduler contract ABI: %w", err))
}
ierc20ABI, err = metaschedulerabi.IERC20MetaData.GetAbi()
if err != nil {
panic(fmt.Errorf("failed to parse erc20 contract ABI: %w", err))
}
jobsABI, err = metaschedulerabi.IJobRepositoryMetaData.GetAbi()
if err != nil {
panic(fmt.Errorf("failed to parse jobRepository contract ABI: %w", err))
}
// Find the event signature dynamically
var ok bool
newJobRequestEvent, ok = metaschedulerABI.Events["NewJobRequestEvent"]
if !ok {
panic(fmt.Errorf("failed to get NewJobRequestEvent: %w", err))
}
jobTransitionEvent, ok = jobsABI.Events["JobTransitionEvent"]
if !ok {
panic(fmt.Errorf("failed to get JobTransitionEvent: %w", err))
}
transferEvent, ok = ierc20ABI.Events["Transfer"]
if !ok {
panic(fmt.Errorf("failed to get Transfer: %w", err))
}
approvalEvent, ok = ierc20ABI.Events["Approval"]
if !ok {
panic(fmt.Errorf("failed to get Approval: %w", err))
}
}
// EthereumBackend combines [bind.ContractBackend] and [bind.DeployBackend].
type EthereumBackend interface {
bind.ContractBackend
bind.DeployBackend
}
// Backend is a wrapper around the EthereumBackend used to interact with the Meta-Scheduler smart-contract.
type Backend struct {
MetaschedulerAddress common.Address
// EthereumBackend is the Ethereum Client.
EthereumBackend
// ChainID of the blockchain.
ChainID *big.Int
// PrivateKey of the user.
UserPrivateKey *ecdsa.PrivateKey
}
func (b *Backend) from() (addr common.Address) {
if b.UserPrivateKey == nil {
return addr
}
return crypto.PubkeyToAddress(b.UserPrivateKey.PublicKey)
}