Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
: NodeEngine
}
@Route('')
export class ContainerController extends Controller {
@SuccessResponse('204', 'No Content')
@Post('loadSmartContract')
public async loadSmartContract (@Body() { contractAddress, executable }: LoadContractArgs): Promise {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await engine.load({ contractAddress, executable })
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadContractArgs): Promise {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await engine.unload({ contractAddress })
}
@SuccessResponse('200', 'Ok')
@Post('execute/{contractAddress}/{method}')
public async execute (contractAddress: string, method: string, @Body() methodArguments: any): Promise<{ data: SmartContractResponse } | { error: string }> {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
return engine.execute({ contractAddress, method, methodArgs: methodArguments })
this.setStatus(204)
await engine.load({ contractAddress, executable })
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadContractArgs): Promise {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await engine.unload({ contractAddress })
}
@SuccessResponse('200', 'Ok')
@Post('execute/{contractAddress}/{method}')
public async execute (contractAddress: string, method: string, @Body() methodArguments: any): Promise<{ data: SmartContractResponse } | { error: string }> {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
return engine.execute({ contractAddress, method, methodArgs: methodArguments })
.catch(e => {
if (e instanceof ContractNotLoaded) {
this.setStatus(404)
} else {
this.setStatus(500)
}
return { error: e.message }
})
}
} from '../docker-api'
interface LoadSmartContractRequest {
contractAddress: string
dockerImageRepository: string
}
interface UnloadSmartContractRequest {
contractAddress: string
}
type SmartContractResponse = any
@Route('')
export class ContainerController extends Controller {
@SuccessResponse('204', 'No Content')
@Post('loadSmartContract')
public async loadSmartContract (@Body() { contractAddress, dockerImageRepository }: LoadSmartContractRequest): Promise {
const { CONTAINER_LOWER_PORT_BOUND, CONTAINER_UPPER_PORT_BOUND } = process.env
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await loadContainer({
contractAddress,
dockerImageRepository,
lowerPortBound: Number(CONTAINER_LOWER_PORT_BOUND),
upperPortBound: Number(CONTAINER_UPPER_PORT_BOUND)
})
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadSmartContractRequest): Promise {
contractAddress,
dockerImageRepository,
lowerPortBound: Number(CONTAINER_LOWER_PORT_BOUND),
upperPortBound: Number(CONTAINER_UPPER_PORT_BOUND)
})
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadSmartContractRequest): Promise {
this.setStatus(204)
contractAddress = contractAddress.toLowerCase()
await unloadContainer(contractAddress)
}
@SuccessResponse('201', 'Created')
@Post('execute/{contractAddress}/{method}')
public async execute (contractAddress: string, method: string, @Body() methodArguments: any): Promise<{ data: SmartContractResponse } | { error: string }> {
const { RUNTIME } = process.env
contractAddress = contractAddress.toLowerCase()
let contractEndpoint: string
const containerNotFoundError = { error: 'Container not initialized. Call /loadContainer and try again' }
if (RUNTIME !== 'docker') {
const associatedPort = await findContainerPort(contractAddress)
if (associatedPort === 0) {
this.setStatus(400)
return containerNotFoundError
}
contractEndpoint = `http://localhost:${associatedPort}`
} else {
export class ContainerController extends Controller {
@SuccessResponse('204', 'No Content')
@Post('loadSmartContract')
public async loadSmartContract (@Body() { contractAddress, dockerImageRepository }: LoadSmartContractRequest): Promise {
const { CONTAINER_LOWER_PORT_BOUND, CONTAINER_UPPER_PORT_BOUND } = process.env
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await loadContainer({
contractAddress,
dockerImageRepository,
lowerPortBound: Number(CONTAINER_LOWER_PORT_BOUND),
upperPortBound: Number(CONTAINER_UPPER_PORT_BOUND)
})
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadSmartContractRequest): Promise {
this.setStatus(204)
contractAddress = contractAddress.toLowerCase()
await unloadContainer(contractAddress)
}
@SuccessResponse('201', 'Created')
@Post('execute/{contractAddress}/{method}')
public async execute (contractAddress: string, method: string, @Body() methodArguments: any): Promise<{ data: SmartContractResponse } | { error: string }> {
const { RUNTIME } = process.env
contractAddress = contractAddress.toLowerCase()
let contractEndpoint: string
const containerNotFoundError = { error: 'Container not initialized. Call /loadContainer and try again' }
if (RUNTIME !== 'docker') {
import { Post, Route, Body, SuccessResponse, Controller } from 'tsoa'
import DockerEngine from '../docker'
import NodeEngine from '../node_js'
import { LoadContractArgs, ExecutionEngine, UnloadContractArgs, SmartContractResponse, ExecutionEngines } from '../ExecutionEngine'
import { ContractNotLoaded } from '../errors'
import { getConfig, ExecutionEngineConfig } from '../config'
function getEngine (config: ExecutionEngineConfig): ExecutionEngine {
return config.executionEngine === ExecutionEngines.docker
? DockerEngine
: NodeEngine
}
@Route('')
export class ContainerController extends Controller {
@SuccessResponse('204', 'No Content')
@Post('loadSmartContract')
public async loadSmartContract (@Body() { contractAddress, executable }: LoadContractArgs): Promise {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)
await engine.load({ contractAddress, executable })
}
@SuccessResponse('204', 'No Content')
@Post('unloadSmartContract')
public async unloadSmartContract (@Body() { contractAddress }: UnloadContractArgs): Promise {
const engine = getEngine(getConfig())
contractAddress = contractAddress.toLowerCase()
this.setStatus(204)