Skip to content

Commit 5a70bd3

Browse files
committed
created functions endpoint
1 parent 7a364e6 commit 5a70bd3

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

src/api/service/controller/ContractController.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { logger } from '../../../Logger';
66
import { RunContractFunctionRequest } from '../request/RunContractFunctionRequest';
77
import { Web3Configuration } from '../../blockchain/Web3Configuration';
88
import { DeployContractRequest } from '../request/DeployContractRequest';
9+
import { StringBodyRequest } from '../request/StringBodyRequest';
910

1011
@Route('contract')
1112
@provideSingleton(ContractController)
@@ -17,14 +18,14 @@ export class ContractController extends Controller {
1718
super()
1819
}
1920

20-
@Get('abi')
21+
@Post('abi')
2122
async getAbi(
22-
@Query('source') source: string,
23+
@Body() source: StringBodyRequest,
2324
@Query('name') name: string,
2425
@Query('path') path: string
2526
){
2627
try {
27-
const abi = this.contractService.getAbi(name, source, path)
28+
const abi = this.contractService.getAbi(name, source.request, path)
2829
if (!abi) {
2930
throw new Error(`No abi found for contract ${name}`)
3031
}
@@ -35,6 +36,24 @@ export class ContractController extends Controller {
3536
}
3637
}
3738

39+
@Post('functions')
40+
async getFunctions(
41+
@Body() source: StringBodyRequest,
42+
@Query('name') name: string,
43+
@Query('path') path: string
44+
){
45+
try {
46+
const functions = this.contractService.getFunctions(name, source.request, path)
47+
if (!functions) {
48+
throw new Error(`No abi found for contract ${name}`)
49+
}
50+
return functions
51+
} catch (err) {
52+
logger.error(err)
53+
throw new Error(err.message)
54+
}
55+
}
56+
3857
@Post('deploy')
3958
async deploy(
4059
@Body() deployRequest: DeployContractRequest,

src/api/service/service/ContractService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ export class ContractService {
2020
return contract.abi
2121
}
2222

23+
getFunctions(contractName: string, source: string, path: string): any {
24+
const contract = this.compileContract(contractName, source, path)
25+
return contract.abi.filter(abi => abi.type === 'function' || abi.type === 'constructor')
26+
}
27+
2328
compileContract(contractName: string, source: string, path: string): any {
2429
const compileJson = this.generateCompileObject(contractName, source, path)
2530
const compiledContract = JSON.parse(this.solc.getInstance().compileStandardWrapper(JSON.stringify(compileJson)))

src/routes.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const models: TsoaRoute.Models = {
5050
"SaveFileRequest": {
5151
"properties": {
5252
"path": { "dataType": "string", "required": true },
53+
"name": { "dataType": "string", "required": true },
5354
"content": { "dataType": "string", "required": true },
5455
},
5556
},
@@ -322,10 +323,10 @@ export function RegisterRoutes(app: express.Express) {
322323
const promise = controller.getStorage.apply(controller, validatedArgs as any);
323324
promiseHandler(controller, promise, response, next);
324325
});
325-
app.get('/contract/abi',
326+
app.post('/contract/abi',
326327
function(request: any, response: any, next: any) {
327328
const args = {
328-
source: { "in": "query", "name": "source", "required": true, "dataType": "string" },
329+
source: { "in": "body", "name": "source", "required": true, "ref": "StringBodyRequest" },
329330
name: { "in": "query", "name": "name", "required": true, "dataType": "string" },
330331
path: { "in": "query", "name": "path", "required": true, "dataType": "string" },
331332
};
@@ -346,6 +347,30 @@ export function RegisterRoutes(app: express.Express) {
346347
const promise = controller.getAbi.apply(controller, validatedArgs as any);
347348
promiseHandler(controller, promise, response, next);
348349
});
350+
app.post('/contract/functions',
351+
function(request: any, response: any, next: any) {
352+
const args = {
353+
source: { "in": "body", "name": "source", "required": true, "ref": "StringBodyRequest" },
354+
name: { "in": "query", "name": "name", "required": true, "dataType": "string" },
355+
path: { "in": "query", "name": "path", "required": true, "dataType": "string" },
356+
};
357+
358+
let validatedArgs: any[] = [];
359+
try {
360+
validatedArgs = getValidatedArgs(args, request);
361+
} catch (err) {
362+
return next(err);
363+
}
364+
365+
const controller = iocContainer.get<ContractController>(ContractController);
366+
if (typeof controller['setStatus'] === 'function') {
367+
(<any>controller).setStatus(undefined);
368+
}
369+
370+
371+
const promise = controller.getFunctions.apply(controller, validatedArgs as any);
372+
promiseHandler(controller, promise, response, next);
373+
});
349374
app.post('/contract/deploy',
350375
function(request: any, response: any, next: any) {
351376
const args = {

0 commit comments

Comments
 (0)