-
Notifications
You must be signed in to change notification settings - Fork 1
/
post_note_for_character.go
88 lines (81 loc) · 2.56 KB
/
post_note_for_character.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
package chain
import (
"context"
"errors"
"fmt"
"github.com/Crossbell-Box/OperatorSync/app/worker/consts"
"github.com/Crossbell-Box/OperatorSync/app/worker/global"
crossbellContract "github.com/Crossbell-Box/contracts.go"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethTypes "github.com/ethereum/go-ethereum/core/types"
"math/big"
"strconv"
)
func PostNoteForCharacter(characterIdStr string, metadataUri string, forURI string, forNoteCharacterId int64, forNoteNoteId int64) (string, int64, int64, error) {
characterId, err := strconv.ParseInt(characterIdStr, 10, 64)
if err != nil {
global.Logger.Errorf("Failed to parse character id with error: %s", err.Error())
return "", 0, 0, err
}
// Prepare contract instance
client, contractInstance, operatorAuth, err := Prepare()
if err != nil {
global.Logger.Errorf("Failed to prepare eth contract instance")
return "", 0, 0, err
}
var tx *ethTypes.Transaction
noteData := crossbellContract.DataTypesPostNoteData{
CharacterId: big.NewInt(characterId),
ContentUri: metadataUri,
}
if forURI != "" {
if forNoteCharacterId > 0 && forNoteNoteId > 0 {
// For note
tx, err = contractInstance.PostNote4Note(
operatorAuth,
noteData,
crossbellContract.DataTypesNoteStruct{
CharacterId: big.NewInt(forNoteCharacterId),
NoteId: big.NewInt(forNoteNoteId),
},
)
} else {
// For normal URI
tx, err = contractInstance.PostNote4AnyUri(
operatorAuth,
noteData,
forURI,
)
}
} else {
tx, err = contractInstance.PostNote(
operatorAuth,
noteData,
)
}
if err != nil {
global.Logger.Errorf("Failed to create transaction: %s", err.Error())
return "", 0, 0, err
}
// Wait till transaction finish or timeout
ctx, cancel := context.WithTimeout(context.Background(), consts.CONFIG_TRANSACTION_TIMEOUT)
defer cancel()
rcpt, err := bind.WaitMined(ctx, client, tx) // Warn: Not stable -> for other evm chains, we should query this later (like for transaction revert etc.)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
global.Logger.Errorf("Transaction %s timed out!", tx.Hash().Hex())
}
return "", 0, 0, err
}
if rcpt.Status == ethTypes.ReceiptStatusFailed {
global.Logger.Errorf("Transaction %s failed", tx.Hash().Hex())
return "", 0, 0, fmt.Errorf("transaction failed")
}
// Get return value
noteId, err := ExtractNoteID(rcpt)
if err != nil {
global.Logger.Errorf("Failed to extract note id for transaction %s with error: %v", tx.Hash().Hex(), err)
return "", 0, 0, err
}
return tx.Hash().Hex(), characterId, noteId, nil
}