Skip to content

Commit

Permalink
faucet
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed Feb 4, 2025
1 parent 69d8a69 commit 975a0e8
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 685 deletions.
32 changes: 19 additions & 13 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,44 +224,50 @@ func (c *APIClient) GetSimpleSigLockedOutputs(addr ledger.AddressED25519, maxOut
return ret, &retLRBID, nil
}

func (c *APIClient) GetOutputsForAmount(addr ledger.AddressED25519, amount uint64) ([]*ledger.OutputWithID, *ledger.TransactionID, error) {
func (c *APIClient) GetOutputsForAmount(addr ledger.AddressED25519, amount uint64) ([]*ledger.OutputWithID, *ledger.TransactionID, uint64, error) {
path := fmt.Sprintf(api.PathGetOutputsForAmount+"?addr=%s&amount=%d", addr.Source(), amount)
body, err := c.getBody(path)
if err != nil {
return nil, nil, err
return nil, nil, 0, err
}

var res api.OutputList
err = json.Unmarshal(body, &res)
if err != nil {
return nil, nil, err
return nil, nil, 0, err
}
if res.Error.Error != "" {
return nil, nil, fmt.Errorf("from server: %s", res.Error.Error)
return nil, nil, 0, fmt.Errorf("from server: %s", res.Error.Error)
}

retLRBID, err := ledger.TransactionIDFromHexString(res.LRBID)
if err != nil {
return nil, nil, fmt.Errorf("while parsing transaction ID: %s", res.Error.Error)
return nil, nil, 0, fmt.Errorf("while parsing transaction ID: %s", res.Error.Error)
}

ret := make([]*ledger.OutputWithID, 0, len(res.Outputs))

sum := uint64(0)
for idStr, dataStr := range res.Outputs {
id, err := ledger.OutputIDFromHexString(idStr)
if err != nil {
return nil, nil, fmt.Errorf("wrong output ID data from server: %s: '%w'", idStr, err)
return nil, nil, 0, fmt.Errorf("wrong output ID data from server: %s: '%w'", idStr, err)
}
o, err := ledger.OutputFromHexString(dataStr)
if err != nil {
return nil, nil, fmt.Errorf("wrong output data from server: %s: '%w'", dataStr, err)
return nil, nil, 0, fmt.Errorf("wrong output data from server: %s: '%w'", dataStr, err)
}
ret = append(ret, &ledger.OutputWithID{
ID: id,
Output: o,
})
sum += o.Amount()
}
return ret, &retLRBID, nil
if sum < amount {
// double check
return nil, nil, 0, fmt.Errorf("inconsistency: server returned not enough tokens")
}
return ret, &retLRBID, sum, nil
}

// GetChainedOutputs fetches all outputs of the account. Optionally sorts them on the server
Expand Down Expand Up @@ -635,18 +641,18 @@ func (c *APIClient) TransferFromED25519Wallet(par TransferFromED25519WalletParam
return nil, fmt.Errorf("minimum transfer amount is %d", minimumTransferAmount)
}
walletAccount := ledger.AddressED25519FromPrivateKey(par.WalletPrivateKey)
nowisTs := ledger.TimeNow()

walletOutputs, _, _, err := c.GetTransferableOutputs(walletAccount, par.MaxOutputs)

walletOutputs, _, _, err := c.GetOutputsForAmount(walletAccount, par.Amount+par.TagAlongFee)
if err != nil {
return nil, err
}
txBytes, err := MakeTransferTransaction(MakeTransferTransactionParams{
Inputs: walletOutputs,
Target: par.Target,
Amount: par.Amount,
PrivateKey: par.WalletPrivateKey,
TagAlongSeqID: par.TagAlongSeqID,
TagAlongFee: par.TagAlongFee,
Timestamp: nowisTs,
Timestamp: ledger.TimeNow(),
})
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (srv *server) getOutputsForAmount(w http.ResponseWriter, r *http.Request) {
})

if sum < uint64(amount) {
writeErr(w, "not enough tokens")
writeErr(w, fmt.Sprintf("not enough tokens: < than requested %s", util.Th(amount)))
return
}

Expand Down
2 changes: 1 addition & 1 deletion proxi/glb/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func GetTagAlongSequencerID() *ledger.ChainID {
Infof("using tag_along sequencer: %s", seqIDStr)
}
if seqIDStr == "" {
return nil
return GetOwnSequencerID()
}
ret, err := ledger.ChainIDFromHexString(seqIDStr)
AssertNoError(err)
Expand Down
Loading

0 comments on commit 975a0e8

Please sign in to comment.