Skip to content

Commit

Permalink
wip: refactor: fixin' type checks (pt.2)
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 15, 2024
1 parent 44982b5 commit 3a1905d
Show file tree
Hide file tree
Showing 16 changed files with 303 additions and 154 deletions.
43 changes: 22 additions & 21 deletions fadroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
export * from './fadroma.browser'

// And more!
import type { ChainId, CodeHash } from '@fadroma/agent'
import { Console, bold, timestamp, Chain, Store } from '@fadroma/agent'
import type { ChainId, CodeHash, Connection, Agent } from '@fadroma/agent'
import { Console, bold, timestamp, Chain, UploadedCode, UploadStore, Compiler } from '@fadroma/agent'
import { Deployment, DeployStore } from '@fadroma/deploy'
import { getProject, ProjectPrompter } from '@fadroma/create'
import Commands from '@hackbg/cmds'
import { FileFormat } from '@hackbg/file'
Expand Down Expand Up @@ -95,7 +96,7 @@ export default function main (...args: any) {
})))
.addCommand(
{name: 'select', info: `activate another deployment`, args: ''},
async (name?: string): Promise<Deploy.Deployment|undefined> => selectDeployment(
async (name?: string): Promise<Deployment|undefined> => selectDeployment(
getProject().root,
name
))
Expand All @@ -115,19 +116,19 @@ export default function main (...args: any) {

type Project = { // FIXME
root: any
getDeployment(): Promise<Deploy.Deployment>
getDeployment(): Promise<Deployment>
logStatus(): unknown
}

export function getConnection (): Chain.Connection {
export function getConnection (): Connection {
throw new Error('not implemented')
}

export function getAgent (): Chain.Agent {
export function getAgent (): Agent {
throw new Error('not implemented')
}

export function getCompiler (pkg = "@fadroma/compile"): Promise<Program.Compiler> {
export function getCompiler (pkg = "@fadroma/compile"): Promise<Compiler> {
return import(pkg).catch(e=>{
console.error(
"Failed to import @fadroma/compile.\n ",
Expand All @@ -144,19 +145,19 @@ export function getCompiler (pkg = "@fadroma/compile"): Promise<Program.Compiler
})
}

export function getUploadStore (path?: string|Path): Store.UploadStore {
export function getUploadStore (path?: string|Path): UploadStore {
if (path) {
return new JSONFileUploadStore(path)
} else {
return new Store.UploadStore()
return new UploadStore()
}
}

export function getDeployStore (path?: string): Store.DeployStore {
export function getDeployStore (path?: string): DeployStore {
if (path) {
return new JSONFileDeployStore(path)
} else {
return new Store.DeployStore()
return new DeployStore()
}
}

Expand Down Expand Up @@ -194,7 +195,7 @@ export async function runRepl (context?: { project?: Project, script?: string, a

export async function selectDeployment (
cwd: string|Path, name?: string, store: string|Store.DeployStore = getDeployStore()

Check failure on line 197 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find namespace 'Store'.

Check failure on line 197 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find namespace 'Store'.
): Promise<Deploy.Deployment> {
): Promise<Deployment> {
if (typeof store === 'string') {
store = getDeployStore(store)
}
Expand All @@ -209,11 +210,11 @@ export async function selectDeployment (
if (!state) {
throw new Error(`no deployment ${name} in store`)
}
return Deploy.Deployment.fromSnapshot(state)
return Deployment.fromSnapshot(state)
}

export function exportDeployment (
cwd: string|Path, deployment?: Deploy.Deployment, path?: string|Path
cwd: string|Path, deployment?: Deployment, path?: string|Path
) {
if (!deployment) {
throw new Error("deployment not found")
Expand All @@ -236,7 +237,7 @@ export function exportDeployment (
}

/** Directory containing upload receipts, e.g. `state/$CHAIN/upload`. */
export class JSONFileUploadStore extends Store.UploadStore {
export class JSONFileUploadStore extends UploadStore {
dir: SyncFS.Directory

constructor (dir: string|Path) {
Expand All @@ -248,7 +249,7 @@ export class JSONFileUploadStore extends Store.UploadStore {
return `${this.dir?.short??'-'}`
}

get (codeHash: CodeHash|{ codeHash: CodeHash }): Deploy.UploadedCode|undefined {
get (codeHash: CodeHash|{ codeHash: CodeHash }): UploadedCode|undefined {
if (typeof codeHash === 'object') {
codeHash = codeHash.codeHash
}
Expand All @@ -273,7 +274,7 @@ export class JSONFileUploadStore extends Store.UploadStore {

set (
codeHash: CodeHash|{ codeHash: CodeHash },
value: Partial<Deploy.UploadedCode>
value: Partial<UploadedCode>
): this {
if (typeof codeHash === 'object') {
codeHash = codeHash.codeHash
Expand All @@ -289,7 +290,7 @@ export class JSONFileUploadStore extends Store.UploadStore {
}

/** Directory containing deploy receipts, e.g. `state/$CHAIN/deploy`. */
export class JSONFileDeployStore extends Store.DeployStore {
export class JSONFileDeployStore extends DeployStore {
/** Root directory of deploy store. */
dir: SyncFS.Directory
/** Name of symlink pointing to active deployment, without extension. */
Expand All @@ -304,7 +305,7 @@ export class JSONFileDeployStore extends Store.DeployStore {
return `${this.dir?.short??'-'}`
}

get (name: Deploy.Name): Deploy.DeploymentState|undefined {
get (name: Name): DeploymentState|undefined {

Check failure on line 308 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'Name'.

Check failure on line 308 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'DeploymentState'.

Check failure on line 308 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'Name'.

Check failure on line 308 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'DeploymentState'.
const receipt = this.dir.file(`${name}.json`).setFormat(FileFormat.JSON)
if (receipt.exists()) {
const state = receipt.load()
Expand All @@ -319,8 +320,8 @@ export class JSONFileDeployStore extends Store.DeployStore {
return super.get(name)
}

set (name: Deploy.Name, state: Partial<Deploy.Deployment>|Deploy.DeploymentState): this {
if (state instanceof Deploy.Deployment) state = state.serialize()
set (name: Name, state: Partial<Deployment>|DeploymentState): this {

Check failure on line 323 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'Name'.

Check failure on line 323 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'DeploymentState'.

Check failure on line 323 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'Name'.

Check failure on line 323 in fadroma.ts

View workflow job for this annotation

GitHub Actions / PNPM CI

Cannot find name 'DeploymentState'.
if (state instanceof Deployment) state = state.serialize()
const receipt = new SyncFS.File(this.dir, `${name}.json`).setFormat(FileFormat.JSON)
this.log('writing', receipt.shortPath)
receipt.save(state)
Expand Down
7 changes: 6 additions & 1 deletion fixtures/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"name": "@fadroma/fixtures",
"main": "fixtures.ts"
"main": "fixtures.ts",
"dependencies": {
"@hackbg/file": "workspace:*",
"@fadroma/agent": "workspace:*",
"@fadroma/deploy": "workspace:*"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@fadroma/cw": "workspace:*",
"@fadroma/scrt": "workspace:*",
"@fadroma/create": "workspace:*",
"@fadroma/deploy": "workspace:*",

"@hackbg/cmds": "workspace:*",
"@hackbg/conf": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class Agent extends Logged {
this.identity = properties.identity
this.fees = properties.fees
}
/** The connection that will broadcast the transactions. */
/** The chain on which this agent operates. */
chain: Chain
/** The identity that will sign the transactions. */
identity: Identity
Expand Down
65 changes: 29 additions & 36 deletions packages/agent/test/agent-compute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,27 @@ import assert, { equal, deepEqual, rejects, throws } from 'node:assert'
import * as Stub from '../stub/stub'
import {
Contract,
} from '../src/compute/Contract'
import {
SourceCode,
RustSourceCode,
} from '../src/compute/Source'
import {
LocalCompiledCode as CompiledCode,
} from '../src/compute/Compile.node'
import {
UploadStore
} from '../src/Upload'
import {
CompiledCode as BaseCompiledCode,
Deployment,
ContractCode,
ContractInstance,
UploadedCode,
} from '../src/Compute'
import {
LocalCompiledCode as CompiledCode,
} from '../src/Compute.node'
import {
UploadStore,
Deployment,
DeployStore
} from '../src/Store'
} from '@fadroma/deploy'

import { Suite } from '@hackbg/ensuite'
export default new Suite([
Expand Down Expand Up @@ -212,12 +218,9 @@ export async function testCodeUnits () {
()=>new ContractCode({ compiler: new Stub.Compiler() }).compile())

const source1 = new SourceCode()
assert(
source1[Symbol.toStringTag])
assert(
!source1.canFetch)
assert(
!source1.canCompile)
assert(source1[Symbol.toStringTag])
assert(!source1.status().canFetch)
assert(!source1.status().canCompile)
deepEqual(source1.serialize(), {
sourceOrigin: undefined,
sourceRef: undefined,
Expand All @@ -226,19 +229,14 @@ export async function testCodeUnits () {
})

source1.sourceOrigin = 'foo'
assert(
source1.canFetch)
assert(
source1.canCompile)
assert(source1.status().canFetch)
assert(source1.status().canCompile)

source1.sourceOrigin = undefined
source1.sourcePath = 'foo'
assert(
!source1.canFetch)
assert(
source1.canCompile)
rejects(
()=>new ContractCode({ source: source1 }).compile())
assert(!source1.status().canFetch)
assert(source1.status().canCompile)
rejects(()=>new ContractCode({ source: source1 }).compile())

assert(
await new ContractCode({ source: source1, compiler: new Stub.Compiler() })
Expand All @@ -249,9 +247,9 @@ export async function testCodeUnits () {
assert(
rustSource1[Symbol.toStringTag])
assert(
!rustSource1.canFetch)
!rustSource1.status().canFetch)
assert(
!rustSource1.canCompile)
!rustSource1.status().canCompile)
deepEqual(rustSource1.serialize(), {
sourceOrigin: undefined,
sourceRef: undefined,
Expand All @@ -264,24 +262,19 @@ export async function testCodeUnits () {
})

rustSource1.sourceOrigin = 'foo'
assert(
rustSource1.canFetch)
assert(
!rustSource1.canCompile)
assert(rustSource1.status().canFetch)
assert(!rustSource1.status().canCompile)

rustSource1.sourceOrigin = undefined
rustSource1.sourcePath = 'foo'
assert(
!rustSource1.canFetch)
assert(
!rustSource1.canCompile)
assert(!rustSource1.status().canFetch)
assert(!rustSource1.status().canCompile)

rustSource1.cargoToml = 'foo'
assert(
rustSource1.canCompile)
assert(rustSource1.status().canCompile)

rustSource1.canFetch
rustSource1.canCompileInfo
rustSource1.status().canFetch
rustSource1.status().canCompileInfo

const compiled1 = new CompiledCode()
deepEqual(compiled1.serialize(), {
Expand Down
7 changes: 4 additions & 3 deletions packages/compile/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import assert, { deepEqual, throws } from 'node:assert'
import { dirname } from 'node:path'
import { DotGit } from '@hackbg/repo'
import { Program, Deploy } from '@fadroma/agent'
import { Compiler } from '@fadroma/agent'
import { Deployment } from '@fadroma/deploy'
import * as OCI from '@fadroma/oci'
import { getCompiler, RawLocalRustCompiler, ContainerizedLocalRustCompiler } from './compile'
import { packageRoot } from './package'

const sourcePath = dirname(packageRoot)

class TestBuildDeployment extends Deploy.Deployment {
class TestBuildDeployment extends Deployment {

a = this.contract('null-a', {
language: 'rust', sourcePath, cargoToml: 'examples/contracts/cw-null/Cargo.toml'
Expand Down Expand Up @@ -60,7 +61,7 @@ export async function testBuild () {
const compiler = getCompiler({ useContainer })
assert(compiler)
compiler[Symbol.toStringTag as unknown as keyof typeof compiler]
assert(compiler instanceof Program.Compiler)
assert(compiler instanceof Compiler)

const deployment = new TestBuildDeployment()
await deployment.build({ compiler })
Expand Down
1 change: 1 addition & 0 deletions packages/create/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. **/
import type { ChainId } from '@fadroma/agent'
import { Error, bold, timestamp, Bip39, Bip39EN } from '@fadroma/agent'
import type { Deployment } from '@fadroma/deploy'

import type { Path } from '@hackbg/file'
import { SyncFS, FileFormat } from '@hackbg/file'
Expand Down
9 changes: 4 additions & 5 deletions packages/create/tools.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Core, Deploy, Store } from '@fadroma/agent'
import { bold, colors, UploadedCode } from '@fadroma/agent'
import { DeployStore } from '@fadroma/deploy'
import { SyncFS, FileFormat } from '@hackbg/file'
import type { Path } from '@hackbg/file'

Expand All @@ -14,8 +15,6 @@ import type { Project } from './create'

export const NOT_INSTALLED = 'not installed'

const { bold, colors } = Core

export class SystemTools {
constructor (readonly verbose: boolean = true) {}

Expand Down Expand Up @@ -371,7 +370,7 @@ export class Prompter {

export class ProjectPrompter extends Prompter {

deployment (store: Store.DeployStore & { root?: Path }): Promise<string|undefined> {
deployment (store: DeployStore & { root?: Path }): Promise<string|undefined> {
const label = store.root
? `Select a deployment from ${store.root.short}:`
: `Select a deployment:`
Expand Down Expand Up @@ -425,7 +424,7 @@ export class ProjectPrompter extends Prompter {
choice('One workspace plus scripts.', Infinity),
])

contractCrates = (name: string): Promise<Record<string, Partial<Deploy.UploadedCode>>> =>
contractCrates = (name: string): Promise<Record<string, Partial<UploadedCode>>> =>
this.untilDone({}, state => {
const message = [
`Project ${name} contains ${Object.keys(state).length} contract(s):\n`,
Expand Down
Loading

0 comments on commit 3a1905d

Please sign in to comment.