Skip to content

Commit

Permalink
refactor!: drop compiler stamp
Browse files Browse the repository at this point in the history
BREAKING CHANGE: ContractCompilerHttp stamp removed
Use Compiler class instead.
  • Loading branch information
davidyuk committed Jun 13, 2022
1 parent 7ac1243 commit ddf1363
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 52 deletions.
3 changes: 1 addition & 2 deletions src/ae/aepp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
8 changes: 8 additions & 0 deletions src/ae/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
*/
Expand Down
3 changes: 1 addition & 2 deletions src/ae/universal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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)
3 changes: 1 addition & 2 deletions src/ae/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -44,4 +43,4 @@ import stampit from '@stamp/it'
address: keypair.publicKey,
})
*/
export default Ae.compose(WalletRpc, ContractCompilerHttp)
export default Ae.compose(WalletRpc)
8 changes: 4 additions & 4 deletions src/contract/aci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'>
Expand All @@ -184,8 +184,8 @@ export default async function getContractInstance ({
validateBytecode?: boolean
}): Promise<ContractInstance> {
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()

Expand Down
56 changes: 18 additions & 38 deletions src/contract/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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[]) => {
Expand All @@ -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
}
})
6 changes: 3 additions & 3 deletions src/contract/ga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<typeof buildTx>[2]
Expand Down Expand Up @@ -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<EncodedData<'tx'>> {
const wrapInEmptySignedTx = (
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit ddf1363

Please sign in to comment.