Skip to content

Commit 818b5ed

Browse files
authored
Merge pull request #278 from kaleido-io/tokens-by-id
tokens-by-id getTokenPoolByNameOrID() and getTokenTransfersByID()
2 parents 34371c4 + db7e668 commit 818b5ed

12 files changed

+471
-0
lines changed

docs/swagger/swagger.yaml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5859,6 +5859,69 @@ paths:
58595859
description: Success
58605860
default:
58615861
description: ""
5862+
/namespaces/{ns}/tokens/pools/{nameOrID}:
5863+
get:
5864+
description: 'TODO: Description'
5865+
operationId: getTokenPoolByNameOrID
5866+
parameters:
5867+
- description: 'TODO: Description'
5868+
in: path
5869+
name: ns
5870+
required: true
5871+
schema:
5872+
example: default
5873+
type: string
5874+
- description: 'TODO: Description'
5875+
in: path
5876+
name: nameOrID
5877+
required: true
5878+
schema:
5879+
type: string
5880+
- description: Server-side request timeout (millseconds, or set a custom suffix
5881+
like 10s)
5882+
in: header
5883+
name: Request-Timeout
5884+
schema:
5885+
default: 120s
5886+
type: string
5887+
responses:
5888+
"200":
5889+
content:
5890+
application/json:
5891+
schema:
5892+
properties:
5893+
config:
5894+
additionalProperties: {}
5895+
type: object
5896+
connector:
5897+
type: string
5898+
created: {}
5899+
id: {}
5900+
key:
5901+
type: string
5902+
message: {}
5903+
name:
5904+
type: string
5905+
namespace:
5906+
type: string
5907+
protocolId:
5908+
type: string
5909+
standard:
5910+
type: string
5911+
symbol:
5912+
type: string
5913+
tx:
5914+
properties:
5915+
id: {}
5916+
type:
5917+
type: string
5918+
type: object
5919+
type:
5920+
type: string
5921+
type: object
5922+
description: Success
5923+
default:
5924+
description: ""
58625925
/namespaces/{ns}/tokens/transfers:
58635926
get:
58645927
description: 'TODO: Description'
@@ -6005,6 +6068,67 @@ paths:
60056068
description: Success
60066069
default:
60076070
description: ""
6071+
/namespaces/{ns}/tokens/transfers/{transferID}:
6072+
get:
6073+
description: 'TODO: Description'
6074+
operationId: getTokenTransferByID
6075+
parameters:
6076+
- description: 'TODO: Description'
6077+
in: path
6078+
name: ns
6079+
required: true
6080+
schema:
6081+
example: default
6082+
type: string
6083+
- description: 'TODO: Description'
6084+
in: path
6085+
name: transferID
6086+
required: true
6087+
schema:
6088+
type: string
6089+
- description: Server-side request timeout (millseconds, or set a custom suffix
6090+
like 10s)
6091+
in: header
6092+
name: Request-Timeout
6093+
schema:
6094+
default: 120s
6095+
type: string
6096+
responses:
6097+
"200":
6098+
content:
6099+
application/json:
6100+
schema:
6101+
properties:
6102+
amount: {}
6103+
connector:
6104+
type: string
6105+
created: {}
6106+
from:
6107+
type: string
6108+
key:
6109+
type: string
6110+
localId: {}
6111+
messageHash: {}
6112+
poolProtocolId:
6113+
type: string
6114+
protocolId:
6115+
type: string
6116+
to:
6117+
type: string
6118+
tokenIndex:
6119+
type: string
6120+
tx:
6121+
properties:
6122+
id: {}
6123+
type:
6124+
type: string
6125+
type: object
6126+
type:
6127+
type: string
6128+
type: object
6129+
description: Success
6130+
default:
6131+
description: ""
60086132
/namespaces/{ns}/transactions:
60096133
get:
60106134
description: 'TODO: Description'
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package apiserver
18+
19+
import (
20+
"net/http"
21+
22+
"github.com/hyperledger/firefly/internal/config"
23+
"github.com/hyperledger/firefly/internal/i18n"
24+
"github.com/hyperledger/firefly/internal/oapispec"
25+
"github.com/hyperledger/firefly/pkg/fftypes"
26+
)
27+
28+
var getTokenPoolByNameOrID = &oapispec.Route{
29+
Name: "getTokenPoolByNameOrID",
30+
Path: "namespaces/{ns}/tokens/pools/{nameOrID}",
31+
Method: http.MethodGet,
32+
PathParams: []*oapispec.PathParam{
33+
{Name: "ns", ExampleFromConf: config.NamespacesDefault, Description: i18n.MsgTBD},
34+
{Name: "nameOrID", Description: i18n.MsgTBD},
35+
},
36+
QueryParams: nil,
37+
FilterFactory: nil,
38+
Description: i18n.MsgTBD,
39+
JSONInputValue: nil,
40+
JSONOutputValue: func() interface{} { return &fftypes.TokenPool{} },
41+
JSONOutputCodes: []int{http.StatusOK},
42+
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
43+
output, err = r.Or.Assets().GetTokenPoolByNameOrID(r.Ctx, r.PP["ns"], r.PP["nameOrID"])
44+
return output, err
45+
},
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package apiserver
18+
19+
import (
20+
"net/http/httptest"
21+
"testing"
22+
23+
"github.com/hyperledger/firefly/mocks/assetmocks"
24+
"github.com/hyperledger/firefly/pkg/fftypes"
25+
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/mock"
27+
)
28+
29+
func TestGetTokenPoolByNameOrID(t *testing.T) {
30+
o, r := newTestAPIServer()
31+
mam := &assetmocks.Manager{}
32+
o.On("Assets").Return(mam)
33+
req := httptest.NewRequest("GET", "/api/v1/namespaces/ns1/tokens/pools/abc", nil)
34+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
35+
res := httptest.NewRecorder()
36+
37+
mam.On("GetTokenPoolByNameOrID", mock.Anything, "ns1", "abc").
38+
Return(&fftypes.TokenPool{}, nil)
39+
r.ServeHTTP(res, req)
40+
41+
assert.Equal(t, 200, res.Result().StatusCode)
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package apiserver
18+
19+
import (
20+
"net/http"
21+
22+
"github.com/hyperledger/firefly/internal/config"
23+
"github.com/hyperledger/firefly/internal/i18n"
24+
"github.com/hyperledger/firefly/internal/oapispec"
25+
"github.com/hyperledger/firefly/pkg/fftypes"
26+
)
27+
28+
var getTokenTransferByID = &oapispec.Route{
29+
Name: "getTokenTransferByID",
30+
Path: "namespaces/{ns}/tokens/transfers/{transferID}",
31+
Method: http.MethodGet,
32+
PathParams: []*oapispec.PathParam{
33+
{Name: "ns", ExampleFromConf: config.NamespacesDefault, Description: i18n.MsgTBD},
34+
{Name: "transferID", Description: i18n.MsgTBD},
35+
},
36+
QueryParams: nil,
37+
FilterFactory: nil,
38+
Description: i18n.MsgTBD,
39+
JSONInputValue: nil,
40+
JSONOutputValue: func() interface{} { return &fftypes.TokenTransfer{} },
41+
JSONOutputCodes: []int{http.StatusOK},
42+
JSONHandler: func(r *oapispec.APIRequest) (output interface{}, err error) {
43+
output, err = r.Or.Assets().GetTokenTransferByID(r.Ctx, r.PP["ns"], r.PP["transferID"])
44+
return output, err
45+
},
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package apiserver
18+
19+
import (
20+
"net/http/httptest"
21+
"testing"
22+
23+
"github.com/hyperledger/firefly/mocks/assetmocks"
24+
"github.com/hyperledger/firefly/pkg/fftypes"
25+
"github.com/stretchr/testify/assert"
26+
"github.com/stretchr/testify/mock"
27+
)
28+
29+
func TestGetTokenTransferByID(t *testing.T) {
30+
o, r := newTestAPIServer()
31+
mam := &assetmocks.Manager{}
32+
o.On("Assets").Return(mam)
33+
req := httptest.NewRequest("GET", "/api/v1/namespaces/ns1/tokens/transfers/id1", nil)
34+
req.Header.Set("Content-Type", "application/json; charset=utf-8")
35+
res := httptest.NewRecorder()
36+
37+
mam.On("GetTokenTransferByID", mock.Anything, "ns1", "id1").
38+
Return(&fftypes.TokenTransfer{}, nil)
39+
r.ServeHTTP(res, req)
40+
41+
assert.Equal(t, 200, res.Result().StatusCode)
42+
}

internal/apiserver/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ var routes = []*oapispec.Route{
8080
postTokenPool,
8181
getTokenPools,
8282
getTokenPoolsByType,
83+
getTokenPoolByNameOrID,
8384
getTokenPoolByName,
8485
getTokenAccounts,
8586
getTokenAccountsByPool,
8687
getTokenTransfers,
8788
getTokenTransfersByPool,
89+
getTokenTransferByID,
8890
postTokenMint,
8991
postTokenBurn,
9092
postTokenTransfer,

internal/assets/manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ type Manager interface {
3939
GetTokenPools(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.TokenPool, *database.FilterResult, error)
4040
GetTokenPoolsByType(ctx context.Context, ns, connector string, filter database.AndFilter) ([]*fftypes.TokenPool, *database.FilterResult, error)
4141
GetTokenPool(ctx context.Context, ns, connector, poolName string) (*fftypes.TokenPool, error)
42+
GetTokenPoolByNameOrID(ctx context.Context, ns string, poolNameOrID string) (*fftypes.TokenPool, error)
4243
ValidateTokenPoolTx(ctx context.Context, pool *fftypes.TokenPool, protocolTxID string) error
4344
GetTokenAccounts(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.TokenAccount, *database.FilterResult, error)
4445
GetTokenAccountsByPool(ctx context.Context, ns, connector, poolName string, filter database.AndFilter) ([]*fftypes.TokenAccount, *database.FilterResult, error)
4546
GetTokenTransfers(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.TokenTransfer, *database.FilterResult, error)
47+
GetTokenTransferByID(ctx context.Context, ns, id string) (*fftypes.TokenTransfer, error)
4648
GetTokenTransfersByPool(ctx context.Context, ns, connector, poolName string, filter database.AndFilter) ([]*fftypes.TokenTransfer, *database.FilterResult, error)
4749
NewTransfer(ns, connector, poolName string, transfer *fftypes.TokenTransferInput) sysmessaging.MessageSender
4850
MintTokens(ctx context.Context, ns, connector, poolName string, transfer *fftypes.TokenTransferInput, waitConfirm bool) (*fftypes.TokenTransfer, error)

internal/assets/token_pool.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,30 @@ func (am *assetManager) GetTokenPool(ctx context.Context, ns, connector, poolNam
157157
return pool, nil
158158
}
159159

160+
func (am *assetManager) GetTokenPoolByNameOrID(ctx context.Context, ns, poolNameOrID string) (*fftypes.TokenPool, error) {
161+
if err := fftypes.ValidateFFNameField(ctx, ns, "namespace"); err != nil {
162+
return nil, err
163+
}
164+
165+
var pool *fftypes.TokenPool
166+
167+
poolID, err := fftypes.ParseUUID(ctx, poolNameOrID)
168+
if err != nil {
169+
if err := fftypes.ValidateFFNameField(ctx, poolNameOrID, "name"); err != nil {
170+
return nil, err
171+
}
172+
if pool, err = am.database.GetTokenPool(ctx, ns, poolNameOrID); err != nil {
173+
return nil, err
174+
}
175+
} else if pool, err = am.database.GetTokenPoolByID(ctx, poolID); err != nil {
176+
return nil, err
177+
}
178+
if pool == nil {
179+
return nil, i18n.NewError(ctx, i18n.Msg404NotFound)
180+
}
181+
return pool, nil
182+
}
183+
160184
func (am *assetManager) ValidateTokenPoolTx(ctx context.Context, pool *fftypes.TokenPool, protocolTxID string) error {
161185
// TODO: validate that the given token pool was created with the given protocolTxId
162186
return nil

0 commit comments

Comments
 (0)