Skip to content

Commit

Permalink
wip 26
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Oct 31, 2023
1 parent 2912fff commit 40c76f6
Show file tree
Hide file tree
Showing 27 changed files with 658 additions and 871 deletions.
3 changes: 2 additions & 1 deletion agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './chain'
export * from './client'
export * from './code'
export * from './deploy'
export * from './devnet'
export * from './store'
export * from './token'
export * as Stub from './stub'
export * from './token'
6 changes: 3 additions & 3 deletions agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,13 @@ export abstract class Agent {
abstract get height ():
Promise<number>

abstract getContractCodeId (contract: Address):
abstract getCodeId (contract: Address):
Promise<CodeId>

abstract getContractCodeHash (contract: Address):
abstract getCodeHashOfAddress (contract: Address):
Promise<CodeHash>

abstract getCodeHash (codeId: CodeId):
abstract getCodeHashOfCodeId (codeId: CodeId):
Promise<CodeHash>

abstract doQuery (contract: { address: Address }, message: Message):
Expand Down
15 changes: 4 additions & 11 deletions agent/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ assign.allow('UploadedCode', [
const console = new Console()

export abstract class Compiler {
static variants: Record<string, Class<Compiler, any>> = {}

log = new Console(this.constructor.name)

/** Whether to enable build caching.
Expand All @@ -44,17 +42,13 @@ export abstract class Compiler {
/** Up to the implementation.
* `@hackbg/fadroma` implements dockerized and non-dockerized
* variants on top of the `build.impl.mjs` script. */
abstract build (
source: string|Partial<SourceCode>|Partial<CompiledCode>,
...args: any[]
): Promise<CompiledCode>
abstract build (source: string|Partial<SourceCode>, ...args: unknown[]):
Promise<CompiledCode>

/** Default implementation of buildMany is parallel.
* Compiler implementations override this, though. */
abstract buildMany (
sources: (string|Partial<CompiledCode>)[],
...args: unknown[]
): Promise<CompiledCode[]>
abstract buildMany (sources: (string|Partial<CompiledCode>)[], ...args: unknown[]):
Promise<CompiledCode[]>
}

export class ContractCode {
Expand Down Expand Up @@ -122,7 +116,6 @@ export class ContractCode {
} = {}): Promise<UploadedCode & {
codeId: CodeId
}> {
console.log({ uploaded: this.uploaded })
if (this.uploaded?.isValid() && !reupload && !rebuild) {
return this.uploaded
}
Expand Down
1 change: 1 addition & 0 deletions agent/devnet.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'node:assert'
import * as Stub from './stub'
import { Mode } from './chain'

export default async function testDevnet () {
const devnet = {
accounts: [],
Expand Down
6 changes: 3 additions & 3 deletions agent/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class StubAgent extends Agent {
return this.getBlockInfo().then(({height})=>height)
}

async getContractCodeId (txhash: TxHash) {
async getCodeId (address: Address) {
return 'stub-code-id'
}

async getContractCodeHash (address: Address) {
async getCodeHashOfAddress (address: Address) {
return 'stub-code-hash'
}

async getCodeHash (id: CodeId) {
async getCodeHashOfCodeId (id: CodeId) {
return 'stub-code-hash'
}

Expand Down
23 changes: 4 additions & 19 deletions connect/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,17 @@
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 connect, { ConnectConfig, ConnectError, ConnectConsole } from './connect'
import { ConnectConfig } from './connect'
import * as assert from 'node:assert'

import { Suite } from '@hackbg/ensuite'
export default new Suite([
['config', testConnectConfig],
['errors', testConnectErrors],
['console', testConnectConsole],
['scrt', () => import('./scrt/scrt.test')],
['cw', () => import('./cw/cw.test')]
['config', testConnectConfig],
['scrt', () => import('./scrt/scrt.test')],
['cw', () => import('./cw/cw.test')]
])

export async function testConnectConfig () {
const config = new ConnectConfig()
config.getChain()
config.getChain(null as any)
assert.throws(()=>config.getChain('NoSuchChain' as any))
config.authenticate()
config.listChains()
}

export async function testConnectErrors () {
new ConnectError.NoChainSelected()
new ConnectError.UnknownChainSelected('', {})
}

export async function testConnectConsole () {
new ConnectConsole('testing console').selectedChain()
}
135 changes: 33 additions & 102 deletions connect/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

import { Console, Error, Chain, ChainMode, ChainId, bold } from '@fadroma/agent'
import type { Agent } from '@fadroma/agent'
import { Console, Error, Agent, Mode, ChainId, bold } from '@fadroma/agent'
import * as Scrt from '@fadroma/scrt'
import * as CW from '@fadroma/cw'

Expand All @@ -29,35 +28,23 @@ export * from '@fadroma/agent'

export { Scrt, CW }

export default function connect <A extends Agent> (
config: Partial<ConnectConfig> = new ConnectConfig()
): A {
return new ConnectConfig(config).authenticate()
}

/** @returns Agent configured as per environment and options */
export function authenticate (options: Partial<ConnectConfig> = {}): Agent {
return new ConnectConfig(options).authenticate()
}

export type ConnectMode =
|`Scrt${'Mocknet'|'Devnet'|'Testnet'|'Mainnet'}`
|`OKP4${'Devnet'|'Testnet'}`

export const connectModes = {

// Support for Secret Network
ScrtMainnet: Scrt.mainnet,
ScrtTestnet: Scrt.testnet,
ScrtDevnet: (...args: Parameters<typeof Scrt.Chain.devnet>): Scrt.Chain => {
throw new Error('Devnets are only available via @hackbg/fadroma')
ScrtDevnet: (...args: Parameters<typeof Scrt.Agent.devnet>): Scrt.Agent => {
throw new Error('Devnets are only available through @hackbg/fadroma')
},
ScrtMocknet: Scrt.Chain.mocknet,
ScrtMocknet: Scrt.mocknet,

// Support for OKP4
// Support for OKP4:
OKP4Testnet: CW.OKP4.testnet,
OKP4Devnet: (...args: Parameters<typeof CW.OKP4.Chain.devnet>): CW.OKP4.Chain => {
throw new Error('Devnets are only available via @hackbg/fadroma')
OKP4Devnet: (...args: Parameters<typeof CW.OKP4.Agent.devnet>): CW.OKP4.Chain => {
throw new Error('Devnets are only available through @hackbg/fadroma')
},

// TODO: Support for custom chain
Expand Down Expand Up @@ -89,23 +76,27 @@ export class ConnectConfig extends Config {
return chainIds[this.chain as keyof typeof chainIds]
}

protected getChainMode () {
const chainModes: Record<ConnectMode, ChainMode> = {
ScrtDevnet: ChainMode.Devnet,
ScrtTestnet: ChainMode.Testnet,
ScrtMainnet: ChainMode.Mainnet,
ScrtMocknet: ChainMode.Mocknet,
OKP4Devnet: ChainMode.Devnet,
OKP4Testnet: ChainMode.Testnet,
protected getMode () {
const chainModes: Record<ConnectMode, Mode> = {
ScrtDevnet: Mode.Devnet,
ScrtTestnet: Mode.Testnet,
ScrtMainnet: Mode.Mainnet,
ScrtMocknet: Mode.Mocknet,
OKP4Devnet: Mode.Devnet,
OKP4Testnet: Mode.Testnet,
}
if (!this.chain) {
throw new Error('no chain selected')
}
if (!this.chain) throw new ConnectError.NoChainSelected(chainModes)
const result = chainModes[this.chain as keyof typeof chainModes]
if (!result) throw new ConnectError.UnknownChainSelected(this.chain, chainModes)
if (!result) {
throw new Error(`unknown chain '${name}'`)
}
return result
}

/** Logger handle. */
log = new ConnectConsole('@fadroma/connect')
log = new Console('@fadroma/connect')
/** Secret Network configuration. */
scrt: Scrt.Config
/** Secret Network configuration. */
Expand All @@ -117,87 +108,27 @@ export class ConnectConfig extends Config {
/** Override chain id. */
chainId?: ChainId
/** Override chain mode. */
chainMode: ChainMode = this.getString('FADROMA_CHAIN_MODE', () => this.getChainMode())
chainMode: Mode = this.getString('FADROMA_CHAIN_MODE', () => this.getMode())
/** Mnemonic to use for authentication to testnet. */
testnetMnemonic?: string
= this.getString('FADROMA_TESTNET_MNEMONIC', ()=>undefined)
/** Mnemonic to use for authentication. Hidden from logs by default. */
mnemonic?: string
= this.getString('FADROMA_MNEMONIC', ()=>undefined)
/** Create the Chain instance specified by the configuration. */
getChain <C extends Chain> (
chainToGet: ConnectMode|((...args: any[])=>Chain)|undefined = this.chain
): C {
if (!chainToGet) {
chainToGet = this.chain
if (!chainToGet) throw new Error.Missing.Chain()
}
if (typeof chainToGet === 'string') { // allow name to be passed
chainToGet = connectModes[chainToGet as ConnectMode]
}
if (!chainToGet) { // if still unspecified, throw
throw new ConnectError.UnknownChainSelected(this.chain!, connectModes)
}
return chainToGet({ config: this }) as C // create Chain object
}
/** Create the Agent instance identified by the configuration. */
authenticate <A extends Agent> (options: Partial<A> = {}): A {
options.chain ??= this.getChain()
options.name ??= this.agentName
if (this.chainMode === ChainMode.Testnet) {
options.mnemonic ??= this.testnetMnemonic
} else {
options.mnemonic ??= this.mnemonic
}
return options.chain.authenticate(options) as A
}
/** List all known chains. */
listChains () {
this.log.supportedChains()
this.log.selectedChain(this.chain as string)
}
}

export class ConnectConsole extends Console {

label = 'Fadroma Connect'

supportedChains (supportedChains: Record<string, unknown> = connectModes) {
this.br()
this.info('Known chain names:')
for (const chain of Object.keys(supportedChains).sort()) {
this.info(` ${bold(chain)}`)
this.log.br()
this.log.info('Known chain names:')
for (const chain of Object.keys(connectModes).sort()) {
this.log.info(` ${bold(chain)}`)
}
}

selectedChain (chain?: string) {
this.br()
if (chain) {
this.info('Selected chain:')
this.info(` ${bold(chain)}`)
this.log.br()
if (this.chain) {
this.log.info('Selected chain:')
this.log.info(` ${bold(this.chain)}`)
} else {
this.info('No selected chain. Set FADROMA_CHAIN in .env or shell environment.')
this.log.info('No selected chain. Set FADROMA_CHAIN in .env or shell environment.')
}
this.br()
this.log.br()
}

}

export class ConnectError extends Error {

static SelectChainHint =
`Try setting the FADROMA_CHAIN env var to one of the supported values.`

static UnknownChainSelected = this.define('UnknownChainSelected',
(name: string, chains?: Record<string, unknown>)=>{
//chains && log.supportedChains(chains)
return `Unknown chain "${name}". ${this.SelectChainHint}`
})

static NoChainSelected = this.define('NoChainSelected',
(chains?: Record<string, unknown>)=>{
//chains && log.supportedChains(chains)
return `No chain selected. ${this.SelectChainHint}`
})

}
Loading

0 comments on commit 40c76f6

Please sign in to comment.