Skip to content

Commit

Permalink
wip: refactor: fixing type checks, pt.3
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 16, 2024
1 parent 66a6819 commit 131f0e1
Show file tree
Hide file tree
Showing 24 changed files with 63 additions and 69 deletions.
3 changes: 2 additions & 1 deletion examples/example.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Deployment } from '@fadroma/deploy'
import type { DeploymentState } from '@fadroma/deploy'

export default function main (state) {
export default function main (state: DeploymentState) {

return new Deployment(state)
.addContract("cw-null", {
Expand Down
18 changes: 0 additions & 18 deletions fadroma.test.ts

This file was deleted.

11 changes: 11 additions & 0 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,15 @@ export default new Suite([
['compute', ()=>import('./test/agent-compute.test')],
['token', ()=>import('./test/agent-token.test')],
//['stub', ()=>import('./agent-stub.test')]
['stores', () => import('./test/stores.test')],
['scrt', () => import('./packages/scrt/scrt.test')],
['cw', () => import('./packages/cw/cw.test')],
//['oci', () =>
//@ts-ignore
//import('./oci/oci.test')],

// When running sequentially, these should go last, as they are the slowest.
['devnet', () => import('./packages/devnet/devnet.test')],
['compile', () => import('./packages/compile/compile.test')],
['create', () => import('./packages/create/create.test')],
])
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"clean": "rm -rf .ubik *.dist.*",
"cloc": "cloc --verbose=2 --fullpath --not-match-d=node_modules --not-match-f=pnpm-lock.yaml --exclude-dir=.github,.husky,ensuite,cosmjs-esm,secretjs-esm,coverage,state .",
"cov": "time ensuite-cov -r text -r lcov -- index.test.ts",
"cov:fast": "ensuite-cov fadroma.test.ts --parallel",
"cov:fast": "ensuite-cov index.test.ts --parallel",
"doc": "typedoc --disableGit --sourceLinkTemplate 'https://github.com/hackbg/fadroma/tree/v2/{path}#L{line}' --json docs.json && ./docs.ts",
"docs:dev": "ensuite-dev",
"docs:render": "ensuite-render",
Expand All @@ -38,7 +38,7 @@
"prepare": "husky install",
"repl": "./fadroma.cli.cjs repl",
"test": "time ensuite index.test.ts",
"test:fast": "ensuite fadroma.test.ts --parallel",
"test:fast": "ensuite index.test.ts --parallel",

"release": "time sh -c 'pnpm clean && pnpm i && pnpm check && pnpm cov all && ubik release --otp 123123'",
"release:fast": "time sh -c 'pnpm clean && pnpm i && pnpm check && ubik release --otp 123123'",
Expand Down
2 changes: 1 addition & 1 deletion packages/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ export class ContractInstance extends DeploymentUnit {
compiler = this.compiler,
rebuild = false,
...initOptions
}: Parameters<this["upload"]>[0] & Parameters<Agent["instantiate"]>[1] & {
}: Parameters<this["upload"]>[0] & Parameters<Agent["instantiate"]>[0] & {
deployer?: Address|{ instantiate: Agent["instantiate"] }
redeploy?: boolean
} = {}): Promise<ContractInstance & {
Expand Down
3 changes: 2 additions & 1 deletion packages/deploy/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noEmitOnError": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}
2 changes: 1 addition & 1 deletion packages/devnet/devnet-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function initContainer (
devnet.container.engine = devnet.container.image.engine = new OCI.Connection()
}

const defineGetter = (name, get) => Object.defineProperty(devnet, name, {
const defineGetter = (name: string, get: () => any) => Object.defineProperty(devnet, name, {
enumerable: true, configurable: true, get
})
defineGetter('created', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/devnet/platforms/devnet-axelar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { APIMode } from '../devnet-base'
import type * as Platform from '../devnet-platform'
import * as OCI from '@fadroma/oci'
import * as CW from '@fadroma/cw'
import { Connection, Identity, Token } from '@fadroma/agent'
import { Connection, Identity, Token } from '@hackbg/fadroma'

export type Version = `0.34.3`

Expand Down
2 changes: 1 addition & 1 deletion packages/devnet/platforms/devnet-scrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function version (platformVersion: Version, baseImage: string, baseSha256
return {
platformName,
platformVersion,
Connection: Scrt.Connection as { new (...args: unknown[]): Connection },
Connection: Scrt.ScrtConnection as { new (...args: unknown[]): Connection },
Identity: Scrt.MnemonicIdentity as { new (...args: unknown[]): Identity },
gasToken: new Token.Native('uscrt'),
nodePortMode: 'http' as APIMode,
Expand Down
3 changes: 2 additions & 1 deletion packages/devnet/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noEmitOnError": true,
"skipLibCheck": true
"skipLibCheck": true,
"strict": true
}
}
1 change: 1 addition & 0 deletions packages/oci/oci-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ class OCIConsole extends Console {
export {
OCIConsole as Console,
OCIError as Error,
bold
}
4 changes: 2 additions & 2 deletions packages/oci/oci.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Suite } from '@hackbg/ensuite'
import * as OCI from './oci'
import { Core } from '@hackbg/fadroma'
import { randomBase16 } from '@hackbg/fadroma'
import * as assert from 'node:assert'
import { resolve, dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
Expand Down Expand Up @@ -28,7 +28,7 @@ export async function testContainerEngine () {

console.log('Build...')
await image.build()
const container = image.container(`test-hello-${Core.randomBase16()}`)
const container = image.container(`test-hello-${randomBase16()}`)
assert.ok(container instanceof OCI.Container)
assert.equal(container.image, image)
assert.equal(await container.exists(), false)
Expand Down
18 changes: 8 additions & 10 deletions packages/oci/oci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,24 @@ class OCIConnection extends Connection {
}

class OCISigningConnection extends SigningConnection {
protected override async sendImpl (_) {
override async sendImpl (_: never): Promise<never> {
throw new Error('send: not applicable')
}

protected override async uploadImpl (_) {
override async uploadImpl (_: never): Promise<never> {
throw new Error('upload (load/import image): not implemented')
return {}
}

protected override async instantiateImpl (_) {
override async instantiateImpl (_: never): Promise<never> {
throw new Error('instantiate (create container): not implemented')
}

protected override async executeImpl <T> () {
override async executeImpl <T> (_: never): Promise<never> {
throw new Error('execute (run in container): not implemented')
return {} as T
}
}

class OCIImage extends Deploy.ContractTemplate {
class OCIImage extends ContractTemplate {

constructor (properties: Partial<OCIImage> = {}) {
super(properties)
Expand Down Expand Up @@ -190,13 +188,13 @@ class OCIImage extends Deploy.ContractTemplate {
try {
await this.assertExists()
this.log.imageExists()
} catch (e1) {
} catch (e1: any) {
if (e1.statusCode !== 404) return reject(e1)
// if image doesn't exist locally, try pulling it
try {
this.log.notCachedPulling()
await this.pull()
} catch (e2) {
} catch (e2: any) {
this.log.error(e2)
if (!this.dockerfile) {
const NO_FILE = `Unavailable and no Dockerfile provided; can't proceed.`
Expand Down Expand Up @@ -307,7 +305,7 @@ class OCIImage extends Deploy.ContractTemplate {
}

/** Interface to a Docker container. */
class OCIContainer extends Deploy.ContractInstance {
class OCIContainer extends ContractInstance {

constructor (properties: Partial<OCIContainer> = {}) {
super(properties)
Expand Down
8 changes: 4 additions & 4 deletions packages/scrt/scrt-chain.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SecretNetworkClient } from '@hackbg/secretjs-esm'
import { Chain, Connection, Token } from '@hackbg/fadroma'
import { Chain, Connection, Token, base16 } from '@hackbg/fadroma'
import type { ChainId } from '@hackbg/fadroma'
import { ScrtAgent } from './scrt-identity'
import { ScrtBlock } from './scrt-tx'
Expand Down Expand Up @@ -81,8 +81,8 @@ export class ScrtConnection extends Connection {
block: { header, data, evidence, last_commit } = {}
} = await this.api.query.tendermint.getLatestBlock({})
return new ScrtBlock({
hash: base16.encode(hash),
height: Number(header.height)
hash: hash ? base16.encode(hash) : undefined,
height: Number(header?.height)
})
}
}
Expand Down Expand Up @@ -116,7 +116,7 @@ export class ScrtConnection extends Connection {
return await ScrtCompute.fetchContractInfo(this, ...args)
}

override async queryImpl <T> (parameters): Promise<T> {
override async queryImpl <T> (parameters: Parameters<Connection["queryImpl"]>[0]): Promise<T> {
return await ScrtCompute.query(this, parameters) as T
}
}
7 changes: 4 additions & 3 deletions packages/scrt/scrt-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export async function fetchCodeInfo (
const result: Record<CodeId, UploadedCode> = {}
await withIntoError(api.query.compute.codes({})).then(({code_infos})=>{
for (const { code_id, code_hash, creator } of code_infos||[]) {
if (!args.codeIds || args.codeIds.includes(code_id)) {
if (!args?.codeIds || args.codeIds.includes(code_id!)) {
result[code_id!] = new UploadedCode({
chainId,
codeId: code_id,
Expand All @@ -41,14 +41,15 @@ export async function fetchCodeInstances (
}
const result: Record<CodeId, Record<Address, Contract>> = {}
for (const [codeId, Contract] of Object.entries(args.codeIds)) {
let codeHash
let codeHash: string
const instances = {}
await withIntoError(api.query.compute.codeHashByCodeId({ code_id: codeId }))
.then(({code_hash})=>codeHash = code_hash)
await withIntoError(api.query.compute.contractsByCodeId({ code_id: codeId }))
.then(({contract_infos})=>{
for (const { contract_address, contract_info: { label, creator } } of contract_infos!) {
result[contract_address] = new Contract({
result[codeId] ??= {}
result[codeId][contract_address!] = new Contract({
chain,
codeId,
codeHash,
Expand Down
4 changes: 3 additions & 1 deletion packages/scrt/scrt-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
} from '@hackbg/fadroma'
import { MsgStoreCode, MsgInstantiateContract, MsgExecuteContract } from '@hackbg/secretjs-esm'

export class ScrtBlock extends Block {}
export class ScrtBlock extends Block {
hash: string
}

export class ScrtBatch extends Batch {
/** Messages to encrypt. */
Expand Down
1 change: 1 addition & 0 deletions packages/scrt/scrt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export {
} from './scrt-base'
export {
ScrtChain as Chain,
ScrtConnection as Connection,
} from './scrt-chain'
export {
ScrtBatch as Batch,
Expand Down
4 changes: 1 addition & 3 deletions packages/stub/StubTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/

import { Block } from '../src/Block'
import { Batch } from '../src/Batch'
import { Transaction } from '../src/Transaction'
import { Block, Batch, Transaction } from '@hackbg/fadroma'
import type { StubChain } from './StubChain'
import type { StubAgent } from './StubIdentity'

Expand Down
1 change: 1 addition & 0 deletions test/agent-chain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Backend,
Identity,
Batch,
Contract,
} from '@hackbg/fadroma'
import { fixture } from '@fadroma/fixtures'
import * as Stub from '@fadroma/stub'
Expand Down
14 changes: 7 additions & 7 deletions test/agent-compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import assert, { equal, deepEqual, rejects, throws } from 'node:assert'
import * as Stub from '../stub/stub'
import * as Stub from '@fadroma/stub'
import {
Contract,
} from '../src/compute/Contract'
import {
SourceCode,
RustSourceCode,
} from '../src/compute/Source'
import {
CompiledCode as BaseCompiledCode,
} from '../src/compute/Compile'
import {
LocalCompiledCode as CompiledCode,
} from '../src/compute/Compile.node'
import {
UploadedCode,
UploadStore
} from '../src/Upload'
} from '../src/compute/Upload'
import {
CompiledCode as BaseCompiledCode,
ContractCode,
ContractInstance,
UploadedCode,
} from '../src/Compute'
import {
Deployment,
DeployStore
} from '@fadroma/deploy'
Expand All @@ -43,7 +43,7 @@ export async function testDeploymentUnits () {
equal(
await contract.deploy(), contract)
assert(
contract.connect(new Stub.Connection()) instanceof Contract)
contract.connect(new Stub.Connection({})) instanceof Contract)
rejects(
()=>new ContractInstance({
uploaded: { codeId: 123 } as any,
Expand Down
9 changes: 2 additions & 7 deletions test/agent-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,23 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import assert from 'node:assert'
import { Console, Error, assign, into, intoArray, intoRecord } from '../agent-core'

import { Console, Error, assign, into, intoArray, intoRecord } from '@hackbg/fadroma'
import { Suite } from '@hackbg/ensuite'

export default new Suite([
['collections', testCollections],
['assign', testAssign],
])

export async function testCollections () {
assert.equal(await into(1), 1)

assert.equal(await into(Promise.resolve(1)), 1)

assert.equal(await into(()=>1), 1)

assert.equal(await into(async ()=>1), 1)

assert.deepEqual(
await intoArray([1, ()=>1, Promise.resolve(1), async () => 1]),
[1, 1, 1, 1]
)

assert.deepEqual(await intoRecord({
ready: 1,
getter: () => 2,
Expand Down
2 changes: 1 addition & 1 deletion test/agent-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import assert from 'node:assert'
import {
Token, Fungible, NonFungible, Native, Custom, Coin, Fee, Pair, Amount, Swap
} from '../agent-token'
} from '../src/dlt/Token'

class MyFungible extends Fungible {
get id () {
Expand Down
7 changes: 4 additions & 3 deletions stores.test.ts → test/stores.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { TestProjectDeployment } from './fixtures/fixtures'
import { JSONFileUploadStore } from './fadroma'
import { Stub, Identity } from '@hackbg/fadroma'
import { TestProjectDeployment } from '@fadroma/fixtures'
import { JSONFileUploadStore } from '../fadroma'
import { Identity } from '@hackbg/fadroma'
import * as Stub from '@fadroma/stub'
import { withTmpDir } from '@hackbg/file'
export default async function testJSONFileStores () {
await withTmpDir(async dir=>{
Expand Down
2 changes: 1 addition & 1 deletion toolbox

0 comments on commit 131f0e1

Please sign in to comment.