Skip to content

Commit

Permalink
Merge pull request #91 from kaleido-io/fabric-api-fix
Browse files Browse the repository at this point in the history
[fabric-api-fix] register contract api with fabric bug fix
  • Loading branch information
dechdev authored May 16, 2022
2 parents cb201e3 + c78d0b5 commit eeda7ba
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 5 deletions.
20 changes: 20 additions & 0 deletions server/src/controllers/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,26 @@ export class ContractsController {
return { type: 'message', id: api.message };
}

@Post('/apifabric')
@HttpCode(202)
@ResponseSchema(AsyncResponse)
@OpenAPI({ summary: 'Define a new contract API with Fabric' })
async createAPIFabric(@Body() body: ContractAPI): Promise<AsyncResponse> {
// See ContractsTemplateController and keep template code up to date.
const api = await firefly.createContractAPI({
name: body.name,
interface: {
name: body.interfaceName,
version: body.interfaceVersion,
},
location: {
chaincode: body.chaincode,
channel: body.channel,
},
});
return { type: 'message', id: api.message };
}

@Get('/interface')
@ResponseSchema(ContractInterfaceLookup, { isArray: true })
@OpenAPI({ summary: 'List contract interfaces' })
Expand Down
11 changes: 10 additions & 1 deletion server/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,16 @@ export class ContractAPI {
interfaceVersion: string;

@IsString()
address: string;
@IsOptional()
address?: string;

@IsString()
@IsOptional()
channel?: string;

@IsString()
@IsOptional()
chaincode?: string;
}

export class ContractAPIURLs {
Expand Down
28 changes: 28 additions & 0 deletions server/test/contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,34 @@ describe('Smart Contracts', () => {
});
});

test('Create contract API with Fabric', async () => {
const req: ContractAPI = {
name: 'my-api-fabric',
channel: '0x123',
chaincode: 'chaincode',
interfaceName: 'my-contract',
interfaceVersion: '1.0',
};
const api = {
name: 'my-api-fabric',
message: 'msg1',
} as FireFlyContractAPIResponse;

mockFireFly.createContractAPI.mockResolvedValueOnce(api);

await request(server)
.post('/api/contracts/apifabric')
.send(req)
.expect(202)
.expect({ type: 'message', id: 'msg1' });

expect(mockFireFly.createContractAPI).toHaveBeenCalledWith({
interface: { name: 'my-contract', version: '1.0' },
location: { chaincode: 'chaincode', channel: '0x123' },
name: 'my-api-fabric',
});
});

test('Get contract Interfaces', async () => {
const int = [
{
Expand Down
18 changes: 15 additions & 3 deletions ui/src/components/Buttons/RunButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ArrowForwardIos } from '@mui/icons-material';
import { Button, CircularProgress, Grid, Typography } from '@mui/material';
import { useContext } from 'react';
import { useTranslation } from 'react-i18next';
import { SDK_PATHS } from '../../constants/SDK_PATHS';
import {
TUTORIAL_CATEGORIES,
TUTORIAL_FORMS,
Expand All @@ -10,6 +11,7 @@ import { ApplicationContext } from '../../contexts/ApplicationContext';
import { EventContext } from '../../contexts/EventContext';
import { FormContext } from '../../contexts/FormContext';
import { SnackbarContext } from '../../contexts/SnackbarContext';
import { BLOCKCHAIN_TYPE } from '../../enums/enums';
import { DEFAULT_BORDER_RADIUS } from '../../theme';
import { isSuccessfulResponse } from '../../utils/strings';

Expand All @@ -22,8 +24,12 @@ interface Props {
export const RunButton: React.FC<Props> = ({ endpoint, payload, disabled }) => {
const { t } = useTranslation();
// const [showSnackbar, setShowSnackbar] = useState(false);
const { setApiStatus, setApiResponse, payloadMissingFields } =
useContext(ApplicationContext);
const {
blockchainPlugin,
setApiStatus,
setApiResponse,
payloadMissingFields,
} = useContext(ApplicationContext);
const { addAwaitedEventID, awaitedEventID } = useContext(EventContext);
const { categoryID, isBlob, poolObject } = useContext(FormContext);
const { setMessage, setMessageType } = useContext(SnackbarContext);
Expand All @@ -34,7 +40,13 @@ export const RunButton: React.FC<Props> = ({ endpoint, payload, disabled }) => {
setApiStatus(undefined);
setApiResponse({});
managePayload();
const postEndpoint = isBlob ? endpoint + 'blob' : endpoint;
let postEndpoint = isBlob ? endpoint + 'blob' : endpoint;
if (
blockchainPlugin === BLOCKCHAIN_TYPE.FABRIC &&
endpoint === SDK_PATHS.contractsApi
) {
postEndpoint = endpoint + BLOCKCHAIN_TYPE.FABRIC;
}
const reqDetails: any = {
method: 'POST',
body: isBlob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export const RegisterContractApiForm: React.FC = () => {
contractInterfaces[contractInterfaceIdx]?.version || '',
chaincode: chaincode,
channel: channel,
address: undefined,
address: '',
});
}
}, [
Expand Down

0 comments on commit eeda7ba

Please sign in to comment.