Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: adding handler to return all assets of an account #5290

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion daemon/algod/api/algod.oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
"description": "An asset identifier",
"name": "asset-id",
"in": "path",
"required": true
"required": false
},
{
"$ref": "#/parameters/format"
Expand Down
47 changes: 47 additions & 0 deletions daemon/algod/api/server/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,53 @@ func (v2 *Handlers) AccountAssetInformation(ctx echo.Context, address string, as
ledger := v2.Node.LedgerForAPI()

lastRound := ledger.Latest()

// highjack the endpoint when 0 is passed to get all assets
// TODO: this is a hack, we should have a separate endpoint for this
if assetID == 0 {
// First lookup the account
acct, _, _, err := ledger.LookupLatest(addr)
if err != nil {
return internalError(ctx, err, fmt.Sprintf("could not retrieve account information for last round (%d)", lastRound), v2.Log)
}

// Just stealing the current response struct for this
responses := []model.AccountAssetResponse{}

// For each asset, lookup the asset creator then asset params
for assetID, holding := range acct.Assets {

creator, ok, err := ledger.GetCreator(basics.CreatableIndex(assetID), basics.AssetCreatable)
if err != nil {
return internalError(ctx, err, errFailedLookingUpLedger, v2.Log)
}
if !ok {
return notFound(ctx, errors.New(errAssetDoesNotExist), errAssetDoesNotExist, v2.Log)
}

record, err := ledger.LookupAsset(lastRound, creator, basics.AssetIndex(assetID))
if err != nil {
return internalError(ctx, err, errFailedLookingUpLedger, v2.Log)
}
if record.AssetParams == nil {
return notFound(ctx, errors.New(errAccountAssetDoesNotExist), errAccountAssetDoesNotExist, v2.Log)
}
asset := AssetParamsToAsset(creator.String(), basics.AssetIndex(assetID), record.AssetParams)
recordResponse := model.AccountAssetResponse{
CreatedAsset: &asset.Params,
AssetHolding: &model.AssetHolding{
Amount: holding.Amount,
AssetID: uint64(assetID),
IsFrozen: holding.Frozen,
},
}
responses = append(responses, recordResponse)
}

return ctx.JSON(http.StatusOK, responses)

}

record, err := ledger.LookupAsset(lastRound, addr, basics.AssetIndex(assetID))
if err != nil {
return internalError(ctx, err, errFailedLookingUpLedger, v2.Log)
Expand Down