Skip to content

Commit e08407f

Browse files
committed
update distributor to correct send tokens correctly
1 parent 05df452 commit e08407f

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

starship/faucet/distributor.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,11 @@ func (a *Account) queryTxWithRetry(txhash string, maxRetries int) (map[string]in
327327
return nil, fmt.Errorf("transaction not found after retries: txhash: %s", txhash)
328328
}
329329

330-
// sendTokens performs chain binary send txn from account
331-
func (a *Account) sendTokens(address string, denom string, amount string) error {
330+
// sendTokens performs chain binary send txn from account, returns txhash
331+
func (a *Account) sendTokens(address string, denom string, amount string) (string, error) {
332332
ok := a.mu.TryLock()
333333
if !ok {
334-
return fmt.Errorf("account %s busy: %w", a, ErrResourceInUse)
334+
return "", fmt.Errorf("account %s busy: %w", a, ErrResourceInUse)
335335
}
336336
defer a.mu.Unlock()
337337

@@ -340,29 +340,35 @@ func (a *Account) sendTokens(address string, denom string, amount string) error
340340
output, err := runCommand(cmdStr)
341341
if err != nil {
342342
a.logger.Error("send token failed", zap.String("cmd", cmdStr), zap.Error(err))
343-
return err
343+
return "", err
344344
}
345345
a.logger.Info("ran cmd to send tokens", zap.String("cmd", cmdStr), zap.String("stdout", string(output)))
346346

347347
// Find the JSON line and extract the txhash using a regular expression
348348
txhash, err := extractTxHash(string(output))
349349
if err != nil {
350350
a.logger.Error("failed to extract txhash", zap.Error(err))
351-
return err
351+
return "", err
352352
}
353353

354354
a.logger.Debug("send tokens txhash", zap.String("txhash", txhash))
355355

356+
return txhash, nil
357+
}
358+
359+
// confirmTx checks if the tx has gone through by checking the event
360+
func (a *Account) confirmTx(txhash, eventType string) error {
356361
// query tx to check if the tx was successful
357362
txMap, err := a.queryTxWithRetry(txhash, 3)
358363
if err != nil {
359364
return err
360365
}
361366

362367
// check if the tx has the event
363-
err = hasEvent(txMap, "transfer")
368+
err = hasEvent(txMap, eventType)
364369
if err != nil {
365-
a.logger.Error("transfer event not found in tx",
370+
a.logger.Error("event not found in tx",
371+
zap.String("eventType", eventType),
366372
zap.String("txhash", txhash),
367373
zap.Any("txMap", txMap))
368374
return err
@@ -373,16 +379,23 @@ func (a *Account) sendTokens(address string, denom string, amount string) error
373379

374380
// SendTokens will perform send tokens with retries based on errors
375381
func (a *Account) SendTokens(address string, denom string, amount string) error {
376-
err := a.sendTokens(address, denom, amount)
382+
txHash, err := a.sendTokens(address, denom, amount)
377383
if err == nil {
378384
return nil
379385
}
386+
380387
if strings.Contains(err.Error(), "account sequence mismatch") {
381388
// retry sendTokens
382389
a.logger.Debug("got account sequence missmatch error, retrying send tokens recursively")
383390
return a.SendTokens(address, denom, amount)
384391
}
385392

393+
// Confirm tx has gone through
394+
err = a.confirmTx(txHash, "transfer")
395+
if err != nil {
396+
return err
397+
}
398+
386399
return err
387400
}
388401

0 commit comments

Comments
 (0)