@aeternity/aepp-sdk
Version:
SDK for the æternity blockchain
125 lines (122 loc) • 4.12 kB
JavaScript
import { RestError, userAgentPolicyName, setClientRequestIdPolicyName } from '@azure/core-rest-pipeline';
import { Compiler as CompilerApi } from '../../apis/compiler/index.js';
import { genErrorFormatterPolicy, genVersionCheckPolicy } from '../../utils/autorest.js';
import CompilerBase from './Base.js';
import { CompilerError, NotImplementedError } from '../../utils/errors.js';
/**
* Contract Compiler over HTTP
*
* This class include api call's related to contract compiler functionality.
* @category contract
* @example CompilerHttp('COMPILER_URL')
*/
export default class CompilerHttp extends CompilerBase {
/**
* @param compilerUrl - Url for compiler API
* @param options - Options
* @param options.ignoreVersion - Print warning instead of throwing exception if compiler version
* is not supported, use with caution
*/
constructor(compilerUrl, {
ignoreVersion = false
} = {}) {
super();
let version;
const getVersion = async opts => {
if (version != null) return version;
version = (await this.api.apiVersion(opts)).apiVersion;
return version;
};
this.api = new CompilerApi(compilerUrl, {
allowInsecureConnection: true,
additionalPolicies: [genVersionCheckPolicy('compiler', getVersion, '8.0.0', '9.0.0', ignoreVersion), genErrorFormatterPolicy(body => {
let message = '';
if ('reason' in body) {
message += ` ${body.reason}${body.parameter != null ? ` in ${body.parameter}` : ''
// TODO: revising after improving documentation https://github.com/aeternity/aesophia_http/issues/78
}${body.info != null ? ` (${JSON.stringify(body.info)})` : ''}`;
}
if (Array.isArray(body)) {
message += `\n${body.map(e => `${e.type}:${e.pos.line}:${e.pos.col}: ${e.message}${e.context != null ? ` (${e.context})` : ''}`).join('\n')}`;
}
return message;
})]
});
this.api.pipeline.removePolicy({
name: userAgentPolicyName
});
this.api.pipeline.removePolicy({
name: setClientRequestIdPolicyName
});
}
async compileBySourceCode(sourceCode, fileSystem) {
try {
const cmpOut = await this.api.compileContract({
code: sourceCode,
options: {
fileSystem
}
});
const warnings = cmpOut.warnings.map(({
type,
...warning
}) => warning);
const res = {
...cmpOut,
warnings
};
// TODO: should be fixed when the compiledAci interface gets updated
return res;
} catch (error) {
if (error instanceof RestError && error.statusCode === 400) {
throw new CompilerError(error.message);
}
throw error;
}
}
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async compile(path) {
throw new NotImplementedError('File system access, use CompilerHttpNode instead');
}
async generateAciBySourceCode(sourceCode, fileSystem) {
try {
return await this.api.generateACI({
code: sourceCode,
options: {
fileSystem
}
});
} catch (error) {
if (error instanceof RestError && error.statusCode === 400) {
throw new CompilerError(error.message);
}
throw error;
}
}
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async generateAci(path) {
throw new NotImplementedError('File system access, use CompilerHttpNode instead');
}
async validateBySourceCode(bytecode, sourceCode, fileSystem) {
try {
await this.api.validateByteCode({
bytecode,
source: sourceCode,
options: {
fileSystem
}
});
return true;
} catch {
return false;
}
}
// eslint-disable-next-line class-methods-use-this, @typescript-eslint/no-unused-vars
async validate(bytecode, path) {
throw new NotImplementedError('File system access, use CompilerHttpNode instead');
}
async version() {
return (await this.api.version()).version;
}
}
//# sourceMappingURL=Http.js.map