Skip to content

Commit eeda7ba

Browse files
authored
Merge pull request #91 from kaleido-io/fabric-api-fix
[fabric-api-fix] register contract api with fabric bug fix
2 parents cb201e3 + c78d0b5 commit eeda7ba

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

server/src/controllers/contracts.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,26 @@ export class ContractsController {
6969
return { type: 'message', id: api.message };
7070
}
7171

72+
@Post('/apifabric')
73+
@HttpCode(202)
74+
@ResponseSchema(AsyncResponse)
75+
@OpenAPI({ summary: 'Define a new contract API with Fabric' })
76+
async createAPIFabric(@Body() body: ContractAPI): Promise<AsyncResponse> {
77+
// See ContractsTemplateController and keep template code up to date.
78+
const api = await firefly.createContractAPI({
79+
name: body.name,
80+
interface: {
81+
name: body.interfaceName,
82+
version: body.interfaceVersion,
83+
},
84+
location: {
85+
chaincode: body.chaincode,
86+
channel: body.channel,
87+
},
88+
});
89+
return { type: 'message', id: api.message };
90+
}
91+
7292
@Get('/interface')
7393
@ResponseSchema(ContractInterfaceLookup, { isArray: true })
7494
@OpenAPI({ summary: 'List contract interfaces' })

server/src/interfaces.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,16 @@ export class ContractAPI {
250250
interfaceVersion: string;
251251

252252
@IsString()
253-
address: string;
253+
@IsOptional()
254+
address?: string;
255+
256+
@IsString()
257+
@IsOptional()
258+
channel?: string;
259+
260+
@IsString()
261+
@IsOptional()
262+
chaincode?: string;
254263
}
255264

256265
export class ContractAPIURLs {

server/test/contracts.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,34 @@ describe('Smart Contracts', () => {
101101
});
102102
});
103103

104+
test('Create contract API with Fabric', async () => {
105+
const req: ContractAPI = {
106+
name: 'my-api-fabric',
107+
channel: '0x123',
108+
chaincode: 'chaincode',
109+
interfaceName: 'my-contract',
110+
interfaceVersion: '1.0',
111+
};
112+
const api = {
113+
name: 'my-api-fabric',
114+
message: 'msg1',
115+
} as FireFlyContractAPIResponse;
116+
117+
mockFireFly.createContractAPI.mockResolvedValueOnce(api);
118+
119+
await request(server)
120+
.post('/api/contracts/apifabric')
121+
.send(req)
122+
.expect(202)
123+
.expect({ type: 'message', id: 'msg1' });
124+
125+
expect(mockFireFly.createContractAPI).toHaveBeenCalledWith({
126+
interface: { name: 'my-contract', version: '1.0' },
127+
location: { chaincode: 'chaincode', channel: '0x123' },
128+
name: 'my-api-fabric',
129+
});
130+
});
131+
104132
test('Get contract Interfaces', async () => {
105133
const int = [
106134
{

ui/src/components/Buttons/RunButton.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ArrowForwardIos } from '@mui/icons-material';
22
import { Button, CircularProgress, Grid, Typography } from '@mui/material';
33
import { useContext } from 'react';
44
import { useTranslation } from 'react-i18next';
5+
import { SDK_PATHS } from '../../constants/SDK_PATHS';
56
import {
67
TUTORIAL_CATEGORIES,
78
TUTORIAL_FORMS,
@@ -10,6 +11,7 @@ import { ApplicationContext } from '../../contexts/ApplicationContext';
1011
import { EventContext } from '../../contexts/EventContext';
1112
import { FormContext } from '../../contexts/FormContext';
1213
import { SnackbarContext } from '../../contexts/SnackbarContext';
14+
import { BLOCKCHAIN_TYPE } from '../../enums/enums';
1315
import { DEFAULT_BORDER_RADIUS } from '../../theme';
1416
import { isSuccessfulResponse } from '../../utils/strings';
1517

@@ -22,8 +24,12 @@ interface Props {
2224
export const RunButton: React.FC<Props> = ({ endpoint, payload, disabled }) => {
2325
const { t } = useTranslation();
2426
// const [showSnackbar, setShowSnackbar] = useState(false);
25-
const { setApiStatus, setApiResponse, payloadMissingFields } =
26-
useContext(ApplicationContext);
27+
const {
28+
blockchainPlugin,
29+
setApiStatus,
30+
setApiResponse,
31+
payloadMissingFields,
32+
} = useContext(ApplicationContext);
2733
const { addAwaitedEventID, awaitedEventID } = useContext(EventContext);
2834
const { categoryID, isBlob, poolObject } = useContext(FormContext);
2935
const { setMessage, setMessageType } = useContext(SnackbarContext);
@@ -34,7 +40,13 @@ export const RunButton: React.FC<Props> = ({ endpoint, payload, disabled }) => {
3440
setApiStatus(undefined);
3541
setApiResponse({});
3642
managePayload();
37-
const postEndpoint = isBlob ? endpoint + 'blob' : endpoint;
43+
let postEndpoint = isBlob ? endpoint + 'blob' : endpoint;
44+
if (
45+
blockchainPlugin === BLOCKCHAIN_TYPE.FABRIC &&
46+
endpoint === SDK_PATHS.contractsApi
47+
) {
48+
postEndpoint = endpoint + BLOCKCHAIN_TYPE.FABRIC;
49+
}
3850
const reqDetails: any = {
3951
method: 'POST',
4052
body: isBlob

ui/src/components/Forms/Contracts/RegisterContractApiForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export const RegisterContractApiForm: React.FC = () => {
7070
contractInterfaces[contractInterfaceIdx]?.version || '',
7171
chaincode: chaincode,
7272
channel: channel,
73-
address: undefined,
73+
address: '',
7474
});
7575
}
7676
}, [

0 commit comments

Comments
 (0)