Skip to content

Commit

Permalink
wip 39
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 4, 2023
1 parent 2d2982f commit 66980a4
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 153 deletions.
14 changes: 5 additions & 9 deletions agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,12 @@ export abstract class Agent {
}

/** Create a new, authenticated Agent. */
async authenticate (options?: {
name?: Name,
address?: Address,
mnemonic?: string
authenticate (options?: {
name?: Name,
address?: Address,
mnemonic?: string,
signer?: unknown
}): Promise<this> {
if (!options?.mnemonic && options?.name && this.devnet) {
await this.devnet.start()
const account = await this.devnet.getGenesisAccount(options.name)
options = { ...options, ...account }
}
return new (this.constructor as any)({
...this,
...options
Expand Down
8 changes: 4 additions & 4 deletions agent/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ export class Deployment extends Map<Name, DeploymentUnit> {
Partial<CompiledCode> &
Partial<UploadedCode>
): ContractTemplate {
const source = new SourceCode(properties)
const source = new SourceCode(properties)
const compiled = new CompiledCode(properties)
const uploaded = new UploadedCode(properties)
const unit = new ContractTemplate({
const unit = new ContractTemplate({
deployment: this, name, source, compiled, uploaded
})
this.set(name, unit)
Expand All @@ -222,10 +222,10 @@ export class Deployment extends Map<Name, DeploymentUnit> {
Partial<UploadedCode> &
Partial<ContractInstance>
): ContractInstance {
const source = new SourceCode(properties)
const source = new SourceCode(properties)
const compiled = new CompiledCode(properties)
const uploaded = new UploadedCode(properties)
const unit = new ContractInstance({
const unit = new ContractInstance({
deployment: this, name, source, compiled, uploaded, ...properties
})
this.set(name, unit)
Expand Down
9 changes: 9 additions & 0 deletions connect/cw/cw-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,17 @@ class CWAgent extends Agent {
return this.getBlockInfo().then(({ header: { height } })=>height)
}

get balance () {
if (this.address) {
return this.getBalance('uknow', this.address)
} else {
throw new Error('not authenticated, use getBalance')
}
}

/** Query native token balance. */
async getBalance (denom?: string, address?: Address): Promise<string> {
this.log.debug('Querying balance of', bold(address), 'in', bold(denom))
denom ??= this.defaultDenom
address ??= this.address
if (!address) {
Expand Down
13 changes: 5 additions & 8 deletions connect/cw/cw.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { throws, rejects, deepEqual } from 'node:assert'
import { throws, rejects, deepEqual, equal } from 'node:assert'

import * as Devnets from '../../ops/devnets'
import * as CW from '.'
Expand All @@ -22,19 +22,16 @@ export async function testCWDevnet () {
platform: 'okp4_5.0',
genesisAccounts: {
Alice: 123456789,
Bob: 987654321,
Bob: 987654321,
}
})
const [alice, bob] = await Promise.all([
devnet.authenticate('Alice'),
devnet.authenticate('Bob'),
])
deepEqual(await alice.balance, {
uknow: 123456789
})
deepEqual(await bob.balance, {
uknow: 987654321
})
await alice.height
equal(await alice.balance, '122456789')
equal(await bob.balance, '987654321')
}

export async function testCWOKP4 () {
Expand Down
2 changes: 1 addition & 1 deletion connect/scrt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"@fadroma/agent": "workspace:2.0.0-rc.1",
"@hackbg/conf": "workspace:3.1.5",
"@hackbg/secretjs-esm": "workspace:1.9.3-patch.28",
"@hackbg/secretjs-esm": "workspace:1.9.3-patch.29",
"@noble/secp256k1": "^2.0.0",
"@noble/ed25519": "^2.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion ensuite
5 changes: 5 additions & 0 deletions fadroma.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
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 { Suite } from '@hackbg/ensuite'

import { ProjectCommands } from './fadroma'

new ProjectCommands()

export default new Suite([
['agent', () => import('./agent/agent.test')],
['build', () => import('./ops/build.test')],
Expand Down
2 changes: 1 addition & 1 deletion fadroma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class ProjectCommands extends CommandContext {
async (path?: string) => exportDeployment(
this.project.root, await this.getProject().getDeployment(), path))
.addCommand('reset', 'stop and erase running devnets',
(...ids: ChainId[]) => Devnets.Container.resetAll(
(...ids: ChainId[]) => Devnets.Container.deleteMany(
this.project.root, ids))
}
}
Expand Down
28 changes: 12 additions & 16 deletions ops/devnets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ export default new Suite([
['genesis', testDevnetGenesis],
])

export async function testDevnetDocs () {
//@ts-ignore
await import('./Devnet.spec.ts.md')
}

export async function testDevnetPlatform (platform: Devnets.Platform) {
let devnet: any

ok(devnet = Devnets.Container.fromEnvironment(), "construct devnet")

ok(devnet = new Devnets.Container({ platform }), "construct devnet")

ok(await devnet.start(), "starting the devnet works")
ok(await devnet.assertPresence() || true, "devnet start automatically created container")
ok(await devnet.pause(), "pausing the devnet works")
Expand Down Expand Up @@ -64,12 +63,14 @@ export async function testDevnetGenesis () {

// FIXME: these should be called automatically by authenticate
await devnet.create()
await devnet.create()
await devnet.start()
await devnet.start()

const agent1 = await devnet.authenticate('User1')
await agent1.instantiate('7', { label: 'test-7', initMsg: {} })
const agent2 = await devnet.authenticate('User2')
await agent2.instantiate('8', { label: 'test-8', initMsg: {} })
//await agent1.instantiate('7', { label: 'test-7', initMsg: {} })
//const agent2 = await devnet.authenticate('User2')
//await agent2.instantiate('8', { label: 'test-8', initMsg: {} })
}

export async function testDevnetChainId () {
Expand All @@ -83,31 +84,26 @@ export async function testDevnetChainId () {
"construct must work with no options"
)
ok(
devnet = new Devnets.Container({ platform: 'scrt_1.9' }),
"can construct when passing platform and chainId"
devnet = new Devnets.Container('scrt_1.9'),
"can construct when passing platform as string"
)

ok(
typeof devnet.chainId === 'string',
"chain id must be auto populated when not passed"
)

// TODO: can't delete before creating
ok(
await devnet.save(),
"can save"
)

ok(
await devnet.delete(),
"can delete"
)

ok(
devnet = new Devnets.Container({ platform: 'scrt_1.9' }),
devnet = new Devnets.Container('scrt_1.9'),
"after deletion, can construct new devnet with same chainId"
)

// TODO: devnet with same chainid points to same resource
ok(
await devnet.save(),
Expand Down
4 changes: 0 additions & 4 deletions ops/devnets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,6 @@ export const platforms: Record<Platform, PlatformInfo> = {
* running in a container managed by @hackbg/dock. */
class ContainerDevnet extends Devnet {

static resetAll (cwd: string|Path, ids: ChainId[]) {
return ContainerDevnet.deleteMany($(cwd).in('state'), ids)
}

/** Delete multiple devnets. */
static deleteMany (path: string|Path, ids?: ChainId[]): Promise<ContainerDevnet[]> {
const state = $(path).as(OpaqueDirectory)
Expand Down
9 changes: 6 additions & 3 deletions ops/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@ export async function testProjectCommands () {
const scriptProject =
await Projects.ScriptProject.create({
name: 'test-script-project',
root: `${tmpDir()}/test-script-project`
root: `${tmpDir()}/test-script-project`,
interactive: false
})
const crateProject =
await Projects.CrateProject.create({
name: 'test-crate-project',
root: `${tmpDir()}/test-crate-project`
root: `${tmpDir()}/test-crate-project`,
interactive: false
})
const workspaceProject =
await Projects.WorkspaceProject.create({
name: 'test-workspace-project',
root: `${tmpDir()}/test-workspace-project`
root: `${tmpDir()}/test-workspace-project`,
interactive: false
})

for (const project of [scriptProject, crateProject, workspaceProject]) {
Expand Down
15 changes: 9 additions & 6 deletions ops/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ export class ProjectRoot {
name?: string,
root?: string|Path|Promise<string|Path>|undefined,
}> = {}) {
const { tools = new Tools.SystemTools(), interactive = tools.interactive } =
properties
const { name = await Promise.resolve(interactive ? this.askName() : undefined) } =
properties
const tools =
properties.tools || new Tools.SystemTools()
const interactive =
properties.interactive || tools.interactive
const name =
properties.name || await Promise.resolve(interactive ? this.askName() : undefined)
if (!name) {
throw new Error('missing project name')
}
let { root = await Promise.resolve(interactive ? this.askRoot(name) : undefined) } =
properties
let root =
properties.root || await Promise.resolve(interactive ? this.askRoot(name) : undefined)
if (!root) {
root = $(tools.cwd, name as string)
}
Expand Down Expand Up @@ -112,6 +114,7 @@ export class Project extends ProjectRoot {
properties.interactive ??= properties.tools.interactive
const project = await super.create(properties) as Project
const name = await Promise.resolve(properties.interactive ? this.askName() : undefined)
throw new Error('bang')
if (!name) {
throw new Error("missing project name")
}
Expand Down
Loading

0 comments on commit 66980a4

Please sign in to comment.