Skip to content

Commit b19019c

Browse files
committed
fix(avail): error formatting
1 parent 71fe78f commit b19019c

File tree

1 file changed

+25
-30
lines changed

1 file changed

+25
-30
lines changed

avail/client.go

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ func NewClient(configPath string) (*Client, error) {
6060
a := Client{}
6161
err := a.Config.GetConfig(configPath)
6262
if err != nil {
63-
return nil, fmt.Errorf("cannot get config", err)
63+
return nil, fmt.Errorf("cannot get config:%w", err)
6464
}
6565

6666
a.API, err = gsrpc.NewSubstrateAPI(a.Config.WsRpcURL)
6767
if err != nil {
6868
// log.Error("cannot get api:%w", zap.Error(err))
69-
return nil, fmt.Errorf("cannot get api", err)
69+
return nil, fmt.Errorf("cannot get api:%w", err)
7070
}
7171

7272
a.Meta, err = a.API.RPC.State.GetMetadataLatest()
7373
if err != nil {
7474
// log.Error("cannot get metadata:%w", zap.Error(err))
75-
return nil, fmt.Errorf("cannot get metadata", err)
75+
return nil, fmt.Errorf("cannot get metadata:%w", err)
7676
}
7777

7878
a.AppID = 0
@@ -85,27 +85,21 @@ func NewClient(configPath string) (*Client, error) {
8585
a.GenesisHash, err = a.API.RPC.Chain.GetBlockHash(0)
8686
if err != nil {
8787
// log.Error("cannot get genesis hash:%w", zap.Error(err))
88-
return nil, fmt.Errorf("cannot get genesis hash", err)
88+
return nil, fmt.Errorf("cannot get genesis hash:%w", err)
8989
}
9090

9191
a.Rv, err = a.API.RPC.State.GetRuntimeVersionLatest()
9292
if err != nil {
9393
// log.Error("cannot get runtime version:%w", zap.Error(err))
94-
return nil, fmt.Errorf("cannot get runtime version", err)
94+
return nil, fmt.Errorf("cannot get runtime version:%w", err)
9595
}
9696

9797
a.KeyringPair, err = signature.KeyringPairFromSecret(a.Config.Seed, 42)
9898
if err != nil {
9999
// log.Error("cannot get keyring pair:%w", zap.Error(err))
100-
return nil, fmt.Errorf("cannot get keyring pair", err)
100+
return nil, fmt.Errorf("cannot get keyring pair:%w", err)
101101
}
102102

103-
// DestinationAddress, err = types.NewHashFromHexString(Config.DestinationAddress)
104-
// if err != nil {
105-
// log.Fatalf("cannot decode destination address:%w", err)
106-
// }
107-
108-
// DestinationDomain = types.NewUCompactFromUInt(uint64(Config.DestinationDomain))
109103
return &a, nil
110104
}
111105

@@ -120,20 +114,19 @@ func (c *Client) MaxBlobSize(ctx context.Context) (uint64, error) {
120114
func (a *Client) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64) ([]da.ID, []da.Proof, error) {
121115
// TODO: Add support for multiple blobs
122116
daBlob := daBlobs[0]
123-
log.Println("data", zap.Any("data", daBlob))
124-
log.Println("⚡️ Preparing to post data to Avail:%d bytes", zap.Int("data_size", len(daBlob)))
125-
fmt.Println(*a)
117+
log.Println("data", zap.String("data", string(daBlob)))
118+
log.Printf("⚡️ Preparing to post data to Avail:%d bytes", len(daBlob))
126119
newCall, err := types.NewCall(a.Meta, "DataAvailability.submit_data", types.NewBytes(daBlob))
127120
if err != nil {
128-
return nil, nil, fmt.Errorf("cannot create new call", err)
121+
return nil, nil, fmt.Errorf("cannot create new call:%w", err)
129122
}
130123

131124
// Create the extrinsic
132125
ext := types.NewExtrinsic(newCall)
133126

134127
nonce, err := a.GetAccountNextIndex()
135128
if err != nil {
136-
return nil, nil, fmt.Errorf("cannot get account next index", err)
129+
return nil, nil, fmt.Errorf("cannot get account next index:%w", err)
137130
}
138131

139132
options := types.SignatureOptions{
@@ -147,15 +140,17 @@ func (a *Client) Submit(ctx context.Context, daBlobs []da.Blob, gasPrice float64
147140
TransactionVersion: a.Rv.TransactionVersion,
148141
}
149142

143+
fmt.Println("options transaction version", options.TransactionVersion, "spec version", options.SpecVersion)
144+
150145
err = ext.Sign(a.KeyringPair, options)
151146
if err != nil {
152-
return nil, nil, fmt.Errorf("cannot sign extrinsic", err)
147+
return nil, nil, fmt.Errorf("cannot sign extrinsic:%w", err)
153148
}
154149

155150
// Send the extrinsic
156151
sub, err := a.API.RPC.Author.SubmitAndWatchExtrinsic(ext)
157152
if err != nil {
158-
return nil, nil, fmt.Errorf("cannot submit extrinsic", err)
153+
return nil, nil, fmt.Errorf("cannot submit extrinsic:%w", err)
159154
}
160155

161156
defer sub.Unsubscribe()
@@ -166,7 +161,7 @@ out:
166161
select {
167162
case status := <-sub.Chan():
168163
if status.IsInBlock {
169-
log.Println("📥 Submit data extrinsic included in block %v", zap.String("status in block", status.AsInBlock.Hex()))
164+
log.Printf("📥 Submit data extrinsic included in block %v", status.AsInBlock.Hex())
170165
}
171166
if status.IsFinalized {
172167
blockHash = status.AsFinalized
@@ -195,7 +190,7 @@ out:
195190

196191
block, err := a.API.RPC.Chain.GetBlock(blockHash)
197192
if err != nil {
198-
return nil, nil, fmt.Errorf("cannot get block", err)
193+
return nil, nil, fmt.Errorf("cannot get block:%w", err)
199194
}
200195

201196
var dataProofResp DataProofRPCResponse
@@ -259,10 +254,10 @@ func (a *Client) Get(ctx context.Context, ids []da.ID) ([]da.Blob, error) {
259254
// TODO: We are dealing with single blobs for now. We will need to handle multiple blobs in the future.
260255
ext, err := a.GetExtrinsic(ids[0])
261256
if err != nil {
262-
return nil, fmt.Errorf("cannot get extrinsic", err)
257+
return nil, fmt.Errorf("cannot get extrinsic:%w", err)
263258
}
264259
blobData := ext.Method.Args[2:]
265-
log.Println("📥 received data:%+v", zap.Any("data", blobData))
260+
log.Printf("📥 received data:%+v", blobData)
266261
return []da.Blob{blobData}, nil
267262
}
268263

@@ -329,13 +324,13 @@ func (a *Client) GetAccountNextIndex() (types.UCompact, error) {
329324
// TODO: Add context to the request
330325
resp, err := http.Post(a.Config.HttpApiURL, "application/json", strings.NewReader(fmt.Sprintf("{\"id\":1,\"jsonrpc\":\"2.0\",\"method\":\"system_accountNextIndex\",\"params\":[\"%v\"]}", a.KeyringPair.Address))) //nolint: noctx
331326
if err != nil {
332-
return types.NewUCompactFromUInt(0), fmt.Errorf("cannot post account next index request", err)
327+
return types.NewUCompactFromUInt(0), fmt.Errorf("cannot post account next index request:%w", err)
333328
}
334329
defer resp.Body.Close()
335330

336331
data, err := io.ReadAll(resp.Body)
337332
if err != nil {
338-
return types.NewUCompactFromUInt(0), fmt.Errorf("cannot read body", err)
333+
return types.NewUCompactFromUInt(0), fmt.Errorf("cannot read body:%w", err)
339334
}
340335
var accountNextIndex AccountNextIndexRPCResponse
341336
err = json.Unmarshal(data, &accountNextIndex)
@@ -365,18 +360,18 @@ type Config struct {
365360
func (c *Config) GetConfig(configFileName string) error {
366361
jsonFile, err := os.Open(configFileName)
367362
if err != nil {
368-
return fmt.Errorf("cannot open config file", err)
363+
return fmt.Errorf("cannot open config file:%w", err)
369364
}
370365
defer jsonFile.Close()
371366

372367
byteValue, err := io.ReadAll(jsonFile)
373368
if err != nil {
374-
return fmt.Errorf("cannot read config file", err)
369+
return fmt.Errorf("cannot read config file:%w", err)
375370
}
376371

377372
err = json.Unmarshal(byteValue, c)
378373
if err != nil {
379-
return fmt.Errorf("cannot unmarshal config file", err)
374+
return fmt.Errorf("cannot unmarshal config file:%w", err)
380375
}
381376

382377
return nil
@@ -389,11 +384,11 @@ func (a *Client) GetExtrinsic(id da.ID) (types.Extrinsic, error) {
389384
}
390385
blockHash, err := a.API.RPC.Chain.GetBlockHash(uint64(availID.Height))
391386
if err != nil {
392-
log.Fatalf("cannot get block hash:%w", err)
387+
return types.Extrinsic{}, fmt.Errorf("cannot get block hash:%w", err)
393388
}
394389
block, err := a.API.RPC.Chain.GetBlock(blockHash)
395390
if err != nil {
396-
log.Fatalf("cannot get block:%w", err)
391+
return types.Extrinsic{}, fmt.Errorf("cannot get block:%w", err)
397392
}
398393
return block.Block.Extrinsics[availID.ExtIndex], nil
399394
}

0 commit comments

Comments
 (0)