/
tokens.go
609 lines (599 loc) · 20.9 KB
/
tokens.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
package api
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"github.com/vocdoni/census3/db/annotations"
queries "github.com/vocdoni/census3/db/sqlc"
"github.com/vocdoni/census3/internal"
"github.com/vocdoni/census3/internal/lexer"
"github.com/vocdoni/census3/scanner/providers"
"github.com/vocdoni/census3/scanner/providers/web3"
"go.vocdoni.io/dvote/httprouter"
api "go.vocdoni.io/dvote/httprouter/apirest"
"go.vocdoni.io/dvote/log"
)
func (capi *census3API) initTokenHandlers() error {
if err := capi.endpoint.RegisterMethod("/tokens", "GET",
api.MethodAccessTypePublic, capi.getTokens); err != nil {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens", "POST",
api.MethodAccessTypePublic, capi.createToken); err != nil {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens/{tokenID}", "GET",
api.MethodAccessTypePublic, capi.getToken); err != nil {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens/{tokenID}", "DELETE",
api.MethodAccessTypeAdmin, capi.deleteToken); err != nil {
return err
}
if err := capi.endpoint.RegisterMethod("/tokens/{tokenID}/holders/{holderID}", "GET",
api.MethodAccessTypePublic, capi.getTokenHolder); err != nil {
return err
}
return capi.endpoint.RegisterMethod("/tokens/types", "GET",
api.MethodAccessTypePublic, capi.getTokenTypes)
}
// getTokens function handler returns the registered tokens information from the
// database. It returns a 204 response if no tokens are registered or a 500
// error if something fails.
func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
// get pagination information from the request
pageSize, dbPageSize, cursor, goForward, err := paginationFromCtx(ctx)
if err != nil {
return ErrMalformedPagination.WithErr(err)
}
// if there is a cursor, decode it to bytes
bCursor := []byte{}
if cursor != "" {
bCursor = common.HexToAddress(cursor).Bytes()
}
// init context with timeout and database transaction
internalCtx, cancel := context.WithTimeout(context.Background(), getTokensTimeout)
defer cancel()
tx, err := capi.db.RO.BeginTx(internalCtx, nil)
if err != nil {
return ErrCantGetTokens.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil {
log.Errorw(err, "error rolling back tokens transaction")
}
}()
qtx := capi.db.QueriesRO.WithTx(tx)
// get the tokens from the database using the provided cursor, get the
// following or previous page depending on the direction of the cursor
var rows []queries.Token
if goForward {
rows, err = qtx.NextTokensPage(internalCtx, queries.NextTokensPageParams{
PageCursor: bCursor,
Limit: dbPageSize,
})
} else {
rows, err = qtx.PrevTokensPage(internalCtx, queries.PrevTokensPageParams{
PageCursor: bCursor,
Limit: dbPageSize,
})
}
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNoTokens.WithErr(err)
}
return ErrCantGetTokens.WithErr(err)
}
if len(rows) == 0 {
return ErrNoTokens
}
// init response struct with the initial pagination information and empty
// list of tokens
tokensResponse := GetTokensResponse{
Tokens: []GetTokensItemResponse{},
Pagination: &Pagination{PageSize: pageSize},
}
rows, nextCursorRow, prevCursorRow := paginationToRequest(rows, dbPageSize, cursor, goForward)
if nextCursorRow != nil {
tokensResponse.Pagination.NextCursor = common.BytesToAddress(nextCursorRow.ID).String()
}
if prevCursorRow != nil {
tokensResponse.Pagination.PrevCursor = common.BytesToAddress(prevCursorRow.ID).String()
}
// parse results from database to the response format
for _, tokenData := range rows {
tokensResponse.Tokens = append(tokensResponse.Tokens, GetTokensItemResponse{
ID: common.BytesToAddress(tokenData.ID).String(),
Type: providers.TokenTypeName(tokenData.TypeID),
Decimals: tokenData.Decimals,
Name: tokenData.Name,
StartBlock: uint64(tokenData.CreationBlock),
Tags: tokenData.Tags,
Symbol: tokenData.Symbol,
ChainID: tokenData.ChainID,
ChainAddress: tokenData.ChainAddress,
ExternalID: tokenData.ExternalID,
Synced: tokenData.Synced,
DefaultStrategy: tokenData.DefaultStrategy,
TotalSupply: string(tokenData.TotalSupply),
IconURI: tokenData.IconUri,
})
}
// encode the response and send it
res, err := json.Marshal(tokensResponse)
if err != nil {
return ErrEncodeTokens.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
// createDefaultTokenStrategy function creates a default strategy for the given
// token. It creates a strategy with a single token and the predicate of the
// token symbol. It returns the ID of the created strategy or an error if
// something fails. It also uploads the strategy to IPFS and updates the
// database with the IPFS URI and the default strategy of the token.
func (capi *census3API) createDefaultTokenStrategy(ctx context.Context, qtx *queries.Queries,
address common.Address, chainID uint64, chainAddress, symbol, externalID string,
) (uint64, error) {
// create a default strategy to support censuses over the holders of this
// single token
alias := fmt.Sprintf("Default strategy for token %s", symbol)
predicate := lexer.ScapeTokenSymbol(symbol)
res, err := qtx.CreateStategy(ctx, queries.CreateStategyParams{
Alias: alias,
Predicate: predicate,
})
if err != nil {
return 0, err
}
strategyID, err := res.LastInsertId()
if err != nil {
return 0, err
}
// create a strategy token to link the token with the strategy
if _, err := qtx.CreateStrategyToken(ctx, queries.CreateStrategyTokenParams{
StrategyID: uint64(strategyID),
TokenID: address.Bytes(),
ChainID: chainID,
MinBalance: big.NewInt(1).String(),
ExternalID: externalID,
}); err != nil {
return 0, err
}
// encode and compose final strategy data using the response of GET
// strategy endpoint
strategyDump, err := json.Marshal(GetStrategyResponse{
ID: uint64(strategyID),
Alias: alias,
Predicate: predicate,
Tokens: map[string]*StrategyToken{
symbol: {
ID: address.String(),
ChainID: chainID,
MinBalance: "0",
ExternalID: externalID,
ChainAddress: chainAddress,
},
},
})
if err != nil {
return 0, err
}
// publish the strategy to IPFS and update the database
uri, err := capi.storage.Publish(ctx, strategyDump)
if err != nil {
return 0, err
}
if _, err := qtx.UpdateStrategyIPFSUri(ctx, queries.UpdateStrategyIPFSUriParams{
ID: uint64(strategyID),
Uri: capi.storage.URIprefix() + uri,
}); err != nil {
return 0, err
}
// update the token default strategy
if _, err := qtx.UpdateTokenDefaultStrategy(ctx, queries.UpdateTokenDefaultStrategyParams{
ID: address.Bytes(),
DefaultStrategy: uint64(strategyID),
ChainID: chainID,
ExternalID: externalID,
}); err != nil {
return 0, err
}
return uint64(strategyID), nil
}
// createToken function creates a new token in the current database instance. It
// first gets the token information from the network and then stores it in the
// database. The new token created will be scanned from the block number
// provided as argument. It returns a 400 error if the provided inputs are
// wrong or empty or a 500 error if something fails.
func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
req := CreateTokenRequest{}
if err := json.Unmarshal(msg.Data, &req); err != nil {
log.Errorf("error unmarshalling token information: %s", err)
return ErrMalformedToken.WithErr(err)
}
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), createTokenTimeout)
defer cancel()
// get the correct holder provider for the token type
tokenType := providers.TokenTypeID(req.Type)
provider, exists := capi.holderProviders[tokenType]
if !exists {
return ErrCantCreateCensus.With("token type not supported")
}
if !provider.IsExternal() {
if err := provider.SetRef(web3.Web3ProviderRef{
HexAddress: req.ID,
ChainID: req.ChainID,
}); err != nil {
return ErrInitializingWeb3.WithErr(err)
}
}
// get token information from the external provider
address := provider.Address([]byte(req.ExternalID))
name, err := provider.Name([]byte(req.ExternalID))
if err != nil {
return ErrCantGetToken.WithErr(err)
}
symbol, err := provider.Symbol([]byte(req.ExternalID))
if err != nil {
return ErrCantGetToken.WithErr(err)
}
decimals, err := provider.Decimals([]byte(req.ExternalID))
if err != nil {
return ErrCantGetToken.WithErr(err)
}
totalSupply, err := provider.TotalSupply([]byte(req.ExternalID))
if err != nil {
return ErrCantGetToken.WithErr(err)
}
iconURI, err := provider.IconURI([]byte(req.ExternalID))
if err != nil {
return ErrCantGetToken.WithErr(err)
}
// init db transaction
tx, err := capi.db.RW.BeginTx(internalCtx, nil)
if err != nil {
return ErrCantCreateStrategy.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil && !errors.Is(sql.ErrTxDone, err) {
log.Errorw(err, "create strategy transaction rollback failed")
}
}()
// get the chain address for the token based on the chainID and tokenID
chainAddress, ok := capi.w3p.ChainAddress(req.ChainID, address.String())
if !ok {
return ErrChainIDNotSupported.Withf("chainID: %d, tokenID: %s", req.ChainID, req.ID)
}
sTotalSupply := big.NewInt(0).String()
if totalSupply != nil {
sTotalSupply = totalSupply.String()
}
qtx := capi.db.QueriesRW.WithTx(tx)
_, err = qtx.CreateToken(internalCtx, queries.CreateTokenParams{
ID: address.Bytes(),
Name: name,
Symbol: symbol,
Decimals: decimals,
TotalSupply: annotations.BigInt(sTotalSupply),
CreationBlock: 0,
TypeID: tokenType,
Synced: false,
Tags: req.Tags,
ChainID: req.ChainID,
ChainAddress: chainAddress,
ExternalID: req.ExternalID,
IconUri: iconURI,
})
if err != nil {
if strings.Contains(err.Error(), "UNIQUE constraint failed") {
return ErrTokenAlreadyExists.WithErr(err)
}
return ErrCantCreateToken.WithErr(err)
}
strategyID, err := capi.createDefaultTokenStrategy(internalCtx, qtx,
address, req.ChainID, chainAddress, symbol, req.ExternalID)
if err != nil {
return ErrCantCreateToken.WithErr(err)
}
if _, err := qtx.UpdateTokenDefaultStrategy(internalCtx, queries.UpdateTokenDefaultStrategyParams{
ID: address.Bytes(),
DefaultStrategy: uint64(strategyID),
ChainID: req.ChainID,
ExternalID: req.ExternalID,
}); err != nil {
return ErrCantGetToken.WithErr(err)
}
if err := tx.Commit(); err != nil {
return ErrCantGetToken.WithErr(err)
}
// update metrics
internal.TotalNumberOfTokens.Inc()
internal.NewTokensByTime.Update(1)
internal.TotalNumberOfStrategies.Inc()
internal.NewStrategiesByTime.Update(1)
return ctx.Send([]byte("Ok"), api.HTTPstatusOK)
}
// deleteToken function handler deletes the token with the given ID from the
// database. It returns a 400 error if the provided ID is wrong or empty, a 404
// error if the token is not found or a 500 error if something fails. This
// endpoint is protected for admin.
func (capi *census3API) deleteToken(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
// get contract address from the tokenID query param and decode check if
// it is provided, if not return an error
strAddress := ctx.URLParam("tokenID")
if strAddress == "" {
return ErrMalformedToken.With("tokenID is required")
}
address := common.HexToAddress(strAddress)
// get chainID from query params and decode it as integer, if it's not
// provided or it's not a valid integer return an error
strChainID := ctx.Request.URL.Query().Get("chainID")
if strChainID == "" {
return ErrMalformedChainID.With("chainID is required")
}
chainID, err := strconv.Atoi(strChainID)
if err != nil {
return ErrMalformedChainID.WithErr(err)
} else if chainID < 0 {
return ErrMalformedChainID.With("chainID must be a positive number")
}
// get externalID from query params and decode it as string, it is optional
// so if it's not provided continue
externalID := ctx.Request.URL.Query().Get("externalID")
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), deleteTokenTimeout)
defer cancel()
tx, err := capi.db.RW.BeginTx(internalCtx, nil)
if err != nil {
return ErrCantGetTokens.WithErr(err)
}
defer func() {
if err := tx.Rollback(); err != nil && !errors.Is(sql.ErrTxDone, err) {
log.Errorw(err, "error rolling back tokens transaction")
}
}()
qtx := capi.db.QueriesRO.WithTx(tx)
// check if the token exists in the database
if _, err := qtx.ExistsAndUnique(internalCtx, queries.ExistsAndUniqueParams{
ID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
}); err != nil {
return ErrNotFoundToken.WithErr(err)
}
// delete the token holders
if _, err := qtx.DeleteTokenHolder(internalCtx, queries.DeleteTokenHolderParams{
TokenID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
}); err != nil {
return ErrCantDeleteToken.WithErr(err)
}
// delete strategies tokens
if _, err := qtx.DeleteStrategyTokensByToken(internalCtx,
queries.DeleteStrategyTokensByTokenParams{
TokenID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
}); err != nil {
return ErrCantDeleteToken.WithErr(err)
}
// delete its strategies
res, err := qtx.DeleteStrategiesByToken(internalCtx, queries.DeleteStrategiesByTokenParams{
TokenID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
})
if err != nil {
return ErrCantDeleteToken.WithErr(err)
}
deletedStrategies, err := res.RowsAffected()
if err != nil {
return ErrCantDeleteToken.WithErr(err)
}
currentStrategies := internal.TotalNumberOfStrategies.Get()
if uDeletedStrategies := uint64(deletedStrategies); currentStrategies > uDeletedStrategies {
currentStrategies -= uDeletedStrategies
} else {
currentStrategies = 0
}
// delete the token
if _, err := qtx.DeleteToken(internalCtx, queries.DeleteTokenParams{
ID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
}); err != nil {
return ErrCantDeleteToken.WithErr(err)
}
if err := tx.Commit(); err != nil {
return ErrCantDeleteToken.WithErr(err)
}
// update metrics
internal.TotalNumberOfTokens.Dec()
internal.TotalNumberOfStrategies.Set(currentStrategies)
return ctx.Send([]byte("Ok"), api.HTTPstatusOK)
}
// getToken function handler returns the information of the given token address
// from the database. It returns a 400 error if the provided ID is wrong or
// empty, a 404 error if the token is not found or a 500 error if something
// fails.
func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
// get contract address from the tokenID query param and decode check if
// it is provided, if not return an error
strAddress := ctx.URLParam("tokenID")
if strAddress == "" {
return ErrMalformedToken.With("tokenID is required")
}
address := common.HexToAddress(strAddress)
// get chainID from query params and decode it as integer, if it's not
// provided or it's not a valid integer return an error
strChainID := ctx.Request.URL.Query().Get("chainID")
if strChainID == "" {
return ErrMalformedChainID.With("chainID is required")
}
chainID, err := strconv.Atoi(strChainID)
if err != nil {
return ErrMalformedChainID.WithErr(err)
} else if chainID < 0 {
return ErrMalformedChainID.With("chainID must be a positive number")
}
// get externalID from query params and decode it as string, it is optional
// so if it's not provided continue
externalID := ctx.Request.URL.Query().Get("externalID")
// get token information from the database
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokenTimeout)
defer cancel()
tokenData, err := capi.db.QueriesRO.GetToken(internalCtx,
queries.GetTokenParams{
ID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrNotFoundToken.WithErr(err)
}
return ErrCantGetToken.WithErr(err)
}
// if the token is not synced, get the last block of the network to
// calculate the current scan progress
atBlock := uint64(tokenData.LastBlock)
tokenProgress := 100
if !tokenData.Synced {
provider, exists := capi.holderProviders[tokenData.TypeID]
if !exists {
return ErrCantCreateCensus.With("token type not supported")
}
if !provider.IsExternal() {
if err := provider.SetRef(web3.Web3ProviderRef{
HexAddress: common.Bytes2Hex(tokenData.ID),
ChainID: tokenData.ChainID,
}); err != nil {
return ErrInitializingWeb3.WithErr(err)
}
}
// fetch the last block header and calculate progress
lastBlockNumber, err := provider.LatestBlockNumber(internalCtx, []byte(tokenData.ExternalID))
if err != nil {
return ErrCantGetLastBlockNumber.WithErr(err)
}
lastBlockNumber -= uint64(tokenData.CreationBlock)
currentBlockNumber := atBlock - uint64(tokenData.CreationBlock)
tokenProgress = int(float64(currentBlockNumber) / float64(lastBlockNumber) * 100)
}
// get token holders count
holders, err := capi.db.QueriesRO.CountTokenHolders(internalCtx,
queries.CountTokenHoldersParams{
TokenID: address.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
Balance: big.NewInt(1).String(),
})
if err != nil {
return ErrCantGetTokenCount.WithErr(err)
}
// build response
tokenResponse := GetTokenResponse{
ID: address.String(),
Type: providers.TokenTypeName(tokenData.TypeID),
Decimals: tokenData.Decimals,
Size: uint64(holders),
Name: tokenData.Name,
Symbol: tokenData.Symbol,
TotalSupply: string(tokenData.TotalSupply),
StartBlock: uint64(tokenData.CreationBlock),
Status: &GetTokenStatusResponse{
AtBlock: atBlock,
Synced: tokenData.Synced,
Progress: tokenProgress,
},
Tags: tokenData.Tags,
DefaultStrategy: tokenData.DefaultStrategy,
ChainID: tokenData.ChainID,
ChainAddress: tokenData.ChainAddress,
ExternalID: tokenData.ExternalID,
}
res, err := json.Marshal(tokenResponse)
if err != nil {
return ErrEncodeToken.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
func (capi *census3API) getTokenHolder(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
// get contract address from the tokenID query param and decode check if
// it is provided, if not return an error
strAddress := ctx.URLParam("tokenID")
if strAddress == "" {
return ErrMalformedToken.With("tokenID is required")
}
address := common.HexToAddress(strAddress)
// get chainID from query params and decode it as integer, if it's not
// provided or it's not a valid integer return an error
strChainID := ctx.Request.URL.Query().Get("chainID")
if strChainID == "" {
return ErrMalformedChainID.With("chainID is required")
}
chainID, err := strconv.Atoi(strChainID)
if err != nil {
return ErrMalformedChainID.WithErr(err)
} else if chainID < 0 {
return ErrMalformedChainID.With("chainID must be a positive number")
}
// get externalID from query params and decode it as string, it is optional
// so if it's not provided continue
externalID := ctx.Request.URL.Query().Get("externalID")
// get holder address from the holderID query param and decode check if
// it is provided, if not return an error
strHolderID := ctx.URLParam("holderID")
if strHolderID == "" {
return ErrMalformedHolder.With("holderID is required")
}
holderID := common.HexToAddress(strHolderID)
internalCtx, cancel := context.WithTimeout(ctx.Request.Context(), getTokenTimeout)
defer cancel()
// get token holder information from the database
holder, err := capi.db.QueriesRO.GetTokenHolder(internalCtx, queries.GetTokenHolderParams{
TokenID: address.Bytes(),
HolderID: holderID.Bytes(),
ChainID: uint64(chainID),
ExternalID: externalID,
})
if err != nil {
// if the error is sql.ErrNoRows, return a 404 error, otherwise return
// a 500 error
if errors.Is(err, sql.ErrNoRows) {
return ErrNoTokenHolderFound.WithErr(err)
}
return ErrCantGetTokenHolders.WithErr(err)
}
balance, ok := new(big.Int).SetString(holder.Balance, 10)
if !ok {
return ErrCantGetTokenHolders.With("error parsing balance")
}
// build response and send it
res, err := json.Marshal(&GetTokenHolderResponse{
Balance: balance.String(),
})
if err != nil {
return ErrEncodeTokenHolders.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}
// getTokenTypes handler returns the list of string names of the currently
// supported types of token contracts.
func (capi *census3API) getTokenTypes(msg *api.APIdata, ctx *httprouter.HTTPContext) error {
supportedTypes := []string{}
for _, provider := range capi.holderProviders {
supportedTypes = append(supportedTypes, provider.TypeName())
}
res, err := json.Marshal(TokenTypesResponse{supportedTypes})
if err != nil {
return ErrEncodeTokenTypes.WithErr(err)
}
return ctx.Send(res, api.HTTPstatusOK)
}