Skip to content

Commit

Permalink
Merge branch 'master' into 22-kcs5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Jan 31, 2025
2 parents e2aa466 + 24624f1 commit c9d230f
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 55 deletions.
35 changes: 35 additions & 0 deletions app/swagger/[contract_id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createSwaggerSpec } from 'next-swagger-doc'
import ReactSwagger from './react-swagger'
import 'swagger-ui-react/swagger-ui.css'
import {getContractSwagger} from '@/utils/swagger'

const getApiDocs = async (contract_id: string) => {
return createSwaggerSpec({definition: await getContractSwagger(contract_id)})
}

export default async function IndexPage({
params,
}: {
params: Promise<{ contract_id: string }>
})
{
const contract_id = (await params).contract_id.replace("%40", "@");

try {
const spec = await getApiDocs(contract_id)
return (
<section className='container'>
<ReactSwagger spec={spec} />
</section>
)
}
catch (error) {
return (
<div style={{ textAlign: 'center', padding: '50px' }}>
<h1>{'An error occurred'}</h1>
<p>{`The contract '${contract_id}' either does not exist or has an invalid ABI.`}</p>
<p>{`${error}`}</p>
</div>
)
}
}
14 changes: 14 additions & 0 deletions app/swagger/[contract_id]/react-swagger.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client'

import SwaggerUI from 'swagger-ui-react'
import 'swagger-ui-react/swagger-ui.css'

type Props = {
spec: Record<string, any>
}

function ReactSwagger({ spec }: Props) {
return <SwaggerUI spec={spec} docExpansion="none" />
}

export default ReactSwagger
46 changes: 3 additions & 43 deletions app/v1/contract/[contract_id]/abi/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { getContractId } from '@/utils/contracts'
import { fixAbi, getContractId } from '@/utils/contracts'
import { AppError, handleError } from '@/utils/errors'
import { getProvider } from '@/utils/providers'
import { Abi } from 'koilib'
import { convert } from '@roamin/koinos-pb-to-proto'
import protobufjs from 'protobufjs'

/**
* @swagger
Expand Down Expand Up @@ -70,49 +68,11 @@ export async function GET(request: Request, { params }: { params: { contract_id:
throw new AppError(`abi not available for contract ${contract_id}`)
}

const abi: Abi = {
let abi: Abi = {
...JSON.parse(response.meta.abi)
}

Object.keys(abi.methods).forEach((name) => {
abi!.methods[name] = {
...abi!.methods[name]
}

//@ts-ignore this is needed to be compatible with "old" abis
if (abi.methods[name]['entry-point']) {
//@ts-ignore this is needed to be compatible with "old" abis
abi.methods[name].entry_point = parseInt(
//@ts-ignore this is needed to be compatible with "old" abis
String(abi.methods[name]['entry-point'])
)
}

//@ts-ignore this is needed to be compatible with "old" abis
if (abi.methods[name]['read-only'] !== undefined) {
//@ts-ignore this is needed to be compatible with "old" abis
abi.methods[name].read_only = abi.methods[name]['read-only']
}
})

if (abi.types) {
const pd = convert(abi?.types)
if (pd.length) {
try {
const root = new protobufjs.Root()
for (const desc of pd) {
const parserResult = protobufjs.parse(desc.definition, {
keepCase: true
})
root.add(parserResult.root)
}
// extract the first nested object
abi.koilib_types = root.toJSON().nested?.['']
} catch (error) {
// ignore the parsing errors
}
}
}
abi = fixAbi(abi)

return Response.json({ contract_id, ...response.meta, abi })
} catch (error) {
Expand Down
43 changes: 43 additions & 0 deletions app/v1/contract/[contract_id]/swagger/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { AppError, handleError } from '@/utils/errors'
import { getContractSwagger } from '@/utils/swagger'

/**
* @swagger
* /v1/contract/{contract_id}/swagger:
* get:
* tags: [Contracts]
* description: Returns a Swagger file for the Contract's methods
* summary: Returns a Swagger file for the Contract, detailing its methods.
* parameters:
* - name: contract_id
* schema:
* type: string
* in: path
* description: Koinos address of the contract, name of the contract (for system contracts) or KAP name
* required: true
* example: 15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL
* responses:
* 200:
* description: Swagger
* content:
* application/json:
* schema:
* type: object
* example:
* openapi: 3.0.0
* info:
* title: "'15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL' REST API"
* version: 1.0.0
* tags:
* - name: Contracts
* description: Includes endpoints for interacting with the '15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL' contract on Koinos
* paths: {}
*/

export async function GET(request: Request, { params }: { params: { contract_id: string } }) {
try {
return Response.json(await getContractSwagger(params.contract_id))
} catch (error) {
return handleError(error as Error)
}
}
12 changes: 0 additions & 12 deletions swagger.specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
"title": "Koinos REST API",
"version": "1.0.0"
},
"components": {
"parameters": {
"X-JSON-RPC-URL": {
"name": "X-JSON-RPC-URL",
"schema": {
"type": "string"
},
"in": "header",
"description": "Override default JSON RPC URL used for querying the blockchain"
}
}
},
"tags": [
{
"name": "Accounts",
Expand Down
Loading

0 comments on commit c9d230f

Please sign in to comment.