/
mint.go
77 lines (65 loc) 路 1.78 KB
/
mint.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
package stealcamoor
import (
"context"
"encoding/hex"
"log"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
)
func (sc *Stealcamoor) tryMint(creator common.Address, ids []uint64) {
idsForSigs := make([]uint64, 0)
sigs := make([][]byte, 0)
// Fetch all signatures for the ids to mint
for _, id := range ids {
m, err := sc.apiClient.GetMemory(id)
if err != nil {
log.Printf("Cannot get memory %d: %v", id, err)
// avoid DDoSing the Stealcam API
time.Sleep(10 * time.Millisecond)
continue
}
signature, err := hex.DecodeString(m.Signature)
if err != nil {
log.Printf("Cannot decode signature %s: %v", m.Signature, err)
// avoid DDoSing the Stealcam API
time.Sleep(10 * time.Millisecond)
continue
}
idsForSigs = append(idsForSigs, id)
sigs = append(sigs, signature)
}
if len(idsForSigs) != 0 {
sc.mint(creator, idsForSigs, sigs)
}
}
func (sc *Stealcamoor) mint(creator common.Address, ids []uint64, sigs [][]byte) {
nonce, err := sc.client.PendingNonceAt(context.Background(), sc.ourAddress)
if err != nil {
log.Printf("Cannot get pending nonce: %v", err)
return
}
log.Printf("User nonce: %v", nonce)
gasPrice, err := sc.client.SuggestGasPrice(context.Background())
if err != nil {
log.Printf("Cannot get gas price: %v", err)
return
}
for i, id := range ids {
sc.txOpts.Nonce = big.NewInt(int64(nonce + uint64(i)))
sc.txOpts.GasPrice = gasPrice
sc.txOpts.Value = big.NewInt(0)
// TODO: Handle reverts
tx, err := sc.stealcamContract.StealcamTransactor.Mint(
sc.txOpts, big.NewInt(int64(id)), creator, sigs[i],
)
if err != nil {
log.Printf("Failed to mint memory %d: %v", id, err)
continue
}
log.Printf("Transaction broadcasted: %s", tx.Hash().Hex())
go func(id uint64) {
sc.reveal(creator, id)
}(id)
}
}