-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
61 lines (50 loc) · 1.4 KB
/
relay.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
package gaudius
import (
"context"
"crypto/ecdsa"
"fmt"
"github.com/alecsavvy/gaudius/gen/contracts"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/go-resty/resty/v2"
)
func (sdk *AudiusSdk) relay(encodedabi string) (*contracts.EntityManagerManageEntity, error) {
c := resty.New()
_, err := c.NewRequest().SetBody(nil).Post(fmt.Sprintf("%s/relay", sdk.Discovery.SelectedNode))
if err != nil {
return nil, err
}
txhash, err := sdk.genTxHash(encodedabi)
if err != nil {
return nil, err
}
sub := sdk.TxSubscriber(nil, *txhash)
return <-sub, nil
}
func (sdk *AudiusSdk) genTxHash(encodedAbi string) (*common.Hash, error) {
ctx := context.TODO()
// https://goethereumbook.org/wallet-generate/
privateKey, err := crypto.GenerateKey()
if err != nil {
return nil, err
}
publicKey := privateKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
address := crypto.PubkeyToAddress(*publicKeyECDSA).Hex()
sender := common.HexToAddress(address)
// gather tx params
client := sdk.AcdcClient
nonce, err := client.NonceAt(ctx, sender, nil)
if err != nil {
return nil, err
}
em := sdk.EntityManagerAddress
tx := types.NewTransaction(nonce, *em, nil, 2000000, nil, []byte(encodedAbi))
hash := tx.Hash()
err = client.SendTransaction(ctx, tx)
if err != nil {
return nil, err
}
return &hash, nil
}