From ddf136371451691615cab3108a92e5b135e82d79 Mon Sep 17 00:00:00 2001 From: Denis Davidyuk Date: Mon, 13 Jun 2022 12:24:30 +0400 Subject: [PATCH] refactor!: drop compiler stamp BREAKING CHANGE: ContractCompilerHttp stamp removed Use Compiler class instead. --- src/ae/aepp.ts | 3 +-- src/ae/index.ts | 8 ++++++ src/ae/universal.ts | 3 +-- src/ae/wallet.ts | 3 +-- src/contract/aci.ts | 8 +++--- src/contract/compiler.ts | 56 +++++++++++++--------------------------- src/contract/ga.ts | 6 ++--- src/index.ts | 2 +- 8 files changed, 37 insertions(+), 52 deletions(-) diff --git a/src/ae/aepp.ts b/src/ae/aepp.ts index 3e5405bc07..d6299f76ad 100644 --- a/src/ae/aepp.ts +++ b/src/ae/aepp.ts @@ -23,7 +23,6 @@ */ import Ae from '.' -import ContractCompilerHttp from '../contract/compiler' import AeppRpc from '../utils/aepp-wallet-communication/rpc/aepp-rpc' import asyncInit from '../utils/async-init' // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -41,4 +40,4 @@ import stampit from '@stamp/it' * @param {Object} [options={}] - Initializer object * @return {Object} Aepp instance */ -export default Ae.compose(asyncInit, ContractCompilerHttp, AeppRpc) +export default Ae.compose(asyncInit, AeppRpc) diff --git a/src/ae/index.ts b/src/ae/index.ts index c807d4c27e..b6a0a1f34f 100644 --- a/src/ae/index.ts +++ b/src/ae/index.ts @@ -29,6 +29,7 @@ import * as spendMethods from './spend' import * as oracleMethods from './oracle' import * as contractMethods from './contract' import * as contractGaMethods from '../contract/ga' +import Compiler from '../contract/compiler' import NodePool from '../node-pool' import AccountResolver from '../account/resolver' import { AE_AMOUNT_FORMATS } from '../utils/amount-formatter' @@ -37,7 +38,14 @@ import { AMOUNT } from '../tx/builder/schema' const { _buildTx, ...otherTxMethods } = txMethods export default stampit(NodePool, AccountResolver, { + init ({ compilerUrl, ignoreVersion }) { + if (compilerUrl == null) return + this.setCompilerUrl(compilerUrl, { ignoreVersion }) + }, methods: { + setCompilerUrl (compilerUrl: string, { ignoreVersion = false } = {}): void { + this.compilerApi = new Compiler(compilerUrl, { ignoreVersion }) + }, /** * Remove all listeners for RPC */ diff --git a/src/ae/universal.ts b/src/ae/universal.ts index 8030c17622..8cc45be32a 100644 --- a/src/ae/universal.ts +++ b/src/ae/universal.ts @@ -24,7 +24,6 @@ import Ae from '.' import AccountMultiple from '../account/multiple' -import ContractCompilerHttp from '../contract/compiler' // eslint-disable-next-line @typescript-eslint/no-unused-vars import stampit from '@stamp/it' @@ -39,4 +38,4 @@ import stampit from '@stamp/it' * @param {Object} [options={}] - Initializer object * @return {Object} Universal instance */ -export default Ae.compose(AccountMultiple, ContractCompilerHttp) +export default Ae.compose(AccountMultiple) diff --git a/src/ae/wallet.ts b/src/ae/wallet.ts index d11e7f5f97..2bdc7e7b2b 100644 --- a/src/ae/wallet.ts +++ b/src/ae/wallet.ts @@ -23,7 +23,6 @@ */ import Ae from '.' -import ContractCompilerHttp from '../contract/compiler' import WalletRpc from '../utils/aepp-wallet-communication/rpc/wallet-rpc' // eslint-disable-next-line @typescript-eslint/no-unused-vars import stampit from '@stamp/it' @@ -44,4 +43,4 @@ import stampit from '@stamp/it' address: keypair.publicKey, }) */ -export default Ae.compose(WalletRpc, ContractCompilerHttp) +export default Ae.compose(WalletRpc) diff --git a/src/contract/aci.ts b/src/contract/aci.ts index de341c0ea4..d11e43beca 100644 --- a/src/contract/aci.ts +++ b/src/contract/aci.ts @@ -46,7 +46,7 @@ import { } from '../utils/errors' import { hash } from '../utils/crypto' import { Aci as BaseAci } from '../apis/compiler' -import { OnCompiler } from './compiler' +import Compiler from './compiler' import Node from '../node' import { getAccount, getContract, getContractByteCode, getKeyBlock, resolveName, txDryRun @@ -174,7 +174,7 @@ export default async function getContractInstance ({ ...otherOptions }: { onAccount: _AccountBase & { send: any, buildTx: any, Ae: any } - onCompiler: OnCompiler + onCompiler: Compiler onNode: Node source?: string bytecode?: EncodedData<'cb'> @@ -184,8 +184,8 @@ export default async function getContractInstance ({ validateBytecode?: boolean }): Promise { if (_aci == null && source != null) { - // @ts-expect-error TODO should be fixed when the compiledAci interface gets updated - _aci = await onCompiler.generateACI({ code: source, options: { fileSystem } }) + // TODO: should be fixed when the compiledAci interface gets updated + _aci = await onCompiler.generateACI({ code: source, options: { fileSystem } }) as Aci } if (_aci == null) throw new MissingContractDefError() diff --git a/src/contract/compiler.ts b/src/contract/compiler.ts index fbe2db8fe5..45fda38e93 100644 --- a/src/contract/compiler.ts +++ b/src/contract/compiler.ts @@ -16,14 +16,13 @@ */ /** - * ContractCompilerHttp module + * Compiler module * * @module @aeternity/aepp-sdk/es/contract/compiler - * @export ContractCompilerHttp - * @example import { ContractCompilerHttp } from '@aeternity/aepp-sdk' + * @export Compiler + * @example import { Compiler } from '@aeternity/aepp-sdk' */ -import stampit from '@stamp/it' import { Compiler as CompilerApi, ErrorModel, CompilerError } from '../apis/compiler' import { genErrorFormatterPolicy, genVersionCheckPolicy } from '../utils/autorest' @@ -32,21 +31,18 @@ type GeneralCompilerError = ErrorModel & { parameter?: string } -export type OnCompiler = _ContractCompilerHttp['compilerApi'] - -export class _ContractCompilerHttp { - compilerApi: CompilerApi - - // TODO: replace with constructor after dropping account stamps - init ( - { compilerUrl, ignoreVersion }: { compilerUrl?: string, ignoreVersion?: boolean } - ): void { - if (compilerUrl == null) return - this.setCompilerUrl(compilerUrl, { ignoreVersion }) - } - - setCompilerUrl (compilerUrl: string, { ignoreVersion = false } = {}): void { - this.compilerApi = new CompilerApi(compilerUrl, { +/** + * Contract Compiler + * + * This class include api call's related to contract compiler functionality. + * @alias module:@aeternity/aepp-sdk/es/contract/compiler + * @param options - Initializer object + * @returns Contract compiler instance + * @example Compiler('COMPILER_URL') + */ +export default class Compiler extends CompilerApi { + constructor (compilerUrl: string, { ignoreVersion }: { ignoreVersion?: boolean } = {}) { + super(compilerUrl, { allowInsecureConnection: true, additionalPolicies: [ genErrorFormatterPolicy((body: GeneralCompilerError | CompilerError[]) => { @@ -67,27 +63,11 @@ export class _ContractCompilerHttp { }) ] }) - if (!ignoreVersion) { - const versionPromise = this.compilerApi.aPIVersion().then(({ apiVersion }) => apiVersion) - this.compilerApi.pipeline.addPolicy( + if (ignoreVersion !== true) { + const versionPromise = this.aPIVersion().then(({ apiVersion }) => apiVersion) + this.pipeline.addPolicy( genVersionCheckPolicy('compiler', '/api-version', versionPromise, '6.1.0', '7.0.0') ) } } } - -/** - * Contract Compiler Stamp - * - * This stamp include api call's related to contract compiler functionality. - * @alias module:@aeternity/aepp-sdk/es/contract/compiler - * @param options - Initializer object - * @returns Contract compiler instance - * @example ContractCompilerHttp({ compilerUrl: 'COMPILER_URL' }) - */ -export default stampit <_ContractCompilerHttp>({ - init: _ContractCompilerHttp.prototype.init, - methods: { - setCompilerUrl: _ContractCompilerHttp.prototype.setCompilerUrl - } -}) diff --git a/src/contract/ga.ts b/src/contract/ga.ts index ec47aff5f8..42bc1abcc2 100644 --- a/src/contract/ga.ts +++ b/src/contract/ga.ts @@ -29,7 +29,7 @@ import { _AccountBase } from '../account/base' import { getContractInstance } from '../ae/contract' import Node from '../node' import { getAccount } from '../chain' -import { OnCompiler } from './compiler' +import Compiler from './compiler' /** * Check if account is GA @@ -59,7 +59,7 @@ export async function createGeneralizedAccount ( { onAccount, onCompiler, onNode, ...options }: { onAccount: _AccountBase & { send: any, buildTx: any, Ae: any } - onCompiler: OnCompiler + onCompiler: Compiler onNode: Node } & Parameters<_AccountBase['address']>[0] & Parameters[2] @@ -114,7 +114,7 @@ export async function createMetaTx ( }, authFnName: string, { onAccount, onCompiler, onNode, ...options }: - { onAccount: any, onCompiler: OnCompiler, onNode: Node } + { onAccount: any, onCompiler: Compiler, onNode: Node } & Parameters<_AccountBase['address']>[0] ): Promise> { const wrapInEmptySignedTx = ( diff --git a/src/index.ts b/src/index.ts index 1f4114e2ba..a722f10da8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -38,7 +38,7 @@ export { default as verifyTransaction } from './tx/validator' export { default as AccountBase } from './account/base' export { default as AccountMultiple } from './account/multiple' export { default as MemoryAccount } from './account/memory' -export { default as ContractCompilerHttp } from './contract/compiler' +export { default as Compiler } from './contract/compiler' export { default as RpcAepp } from './ae/aepp' export { default as RpcWallet } from './ae/wallet' export { default as Channel } from './channel'