Skip to content

Commit

Permalink
refactor(agent): remove more get-methods from Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 3, 2024
1 parent 4a4a166 commit a86f6fd
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 115 deletions.
38 changes: 8 additions & 30 deletions packages/agent/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,41 +186,43 @@ Get info about the block with a specific hash.
</pre>

## method [*connection.fetchCodeInfo*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Fetch info about 1, many, or all code IDs (uploaded binaries).
Fetch info about all code IDs uploaded to the chain.
<pre>
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> connection.fetchCodeInfo()
</pre>
Fetch info about a single code ID.
<pre>
<strong>const</strong> result: <em>unknown</em> = <strong>await</strong> connection.fetchCodeInfo(id: string)
</pre>
Fetch info about multiple code IDs.
<pre>
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> connection.fetchCodeInfo(ids: Iterable&lt;string&gt;)
</pre>

## method [*connection.fetchCodeInstances*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Fetch all contracts that match one or more code IDs
Fetch all instances of a code ID.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Contract&gt;</em> = <strong>await</strong> connection.fetchCodeInstances(id: string)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of a code ID, with custom client class.
<pre>
<strong>const</strong> result: <em>Record&lt;string, InstanceType&gt;</em> = <strong>await</strong> connection.fetchCodeInstances(
$C: <em>C</em>,
id: <em>string</em>,
)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Record&gt;</em> = <strong>await</strong> connection.fetchCodeInstances(ids: Iterable&lt;string&gt;)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs, with custom client class.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Record&gt;</em> = <strong>await</strong> connection.fetchCodeInstances(
$C: <em>C</em>,
ids: <em>Iterable&lt;string&gt;</em>,
)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs, with multiple custom client classes.
<pre>
<strong>const</strong> result: <em>Record&lt;string, &gt;</em> = <strong>await</strong> connection.fetchCodeInstances(ids: ???)
</pre>
Expand All @@ -233,30 +235,6 @@ Fetch all contracts that match one or more code IDs
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> connection.fetchContractInfo(addresses: string)
</pre>

## method [*connection.getCodeHashOfAddress*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code hash of a given address.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> connection.getCodeHashOfAddress(contract: string | {
address,
})
</pre>

## method [*connection.getCodeHashOfCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code hash of a given code id.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> connection.getCodeHashOfCodeId(contract: string | {
codeId,
})
</pre>

## method [*connection.getCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code id of a given address.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> connection.getCodeId(contract: string | {
address,
})
</pre>

## method [*connection.getContract*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get a client handle for a specific smart contract, authenticated as as this agent.
<pre>
Expand Down
93 changes: 47 additions & 46 deletions packages/agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,11 @@ export abstract class Connection extends Endpoint {

/////////////////////////////////////////////////////////////////////////////////////////////////

/** Fetch info about 1, many, or all code IDs (uploaded binaries). */
/** Fetch info about all code IDs uploaded to the chain. */
fetchCodeInfo (): Promise<Record<Deploy.CodeId, unknown>>
/** Fetch info about a single code ID. */
fetchCodeInfo (id: Deploy.CodeId): Promise<unknown>
/** Fetch info about multiple code IDs. */
fetchCodeInfo (ids: Iterable<Deploy.CodeId>): Promise<Record<Deploy.CodeId, unknown>>
async fetchCodeInfo (...args: unknown[]): Promise<unknown> {
if (args.length === 0) {
Expand All @@ -352,68 +354,63 @@ export abstract class Connection extends Endpoint {
} else {
throw new Error('fetchCodeInfo takes 0 or 1 arguments')
}

//[>* Get the code hash of a given code id. <]
//getCodeHashOfCodeId (contract: Deploy.CodeId|{ codeId: Deploy.CodeId }): Promise<Code.CodeHash> {
//const codeId = (typeof contract === 'object') ? contract.codeId : contract
//this.log.debug(`Querying code hash of code id ${bold(codeId)}`)
//return timed(
//this.fetchCodeHashOfCodeIdImpl.bind(this, codeId),
//({ elapsed, result }) => this.log.debug(
//`Queried in ${bold(elapsed)}: code hash of code id ${bold(codeId)} is ${bold(result)}`
//)
//)
//}
}
/** Chain-specific implementation of fetchCodeInfo. */
protected abstract fetchCodeInfoImpl (options: { ids?: Deploy.CodeId[] }|undefined):
Promise<Record<Deploy.CodeId, Deploy.UploadedCode>>

/** Get the code id of a given address. */
getCodeId (contract: Address|{ address: Address }): Promise<Deploy.CodeId> {
const address = (typeof contract === 'string') ? contract : contract.address
this.log.debug(`Querying code ID of ${bold(address)}`)
return timed(
this.fetchCodeIdImpl.bind(this, address),
({ elapsed, result }) => this.log.debug(
`Queried in ${bold(elapsed)}: ${bold(address)} is code id ${bold(result)}`
)
)
}

protected abstract fetchCodeIdImpl (contract: Address):
Promise<Deploy.CodeId>

/** Get the code hash of a given code id. */
getCodeHashOfCodeId (contract: Deploy.CodeId|{ codeId: Deploy.CodeId }): Promise<Code.CodeHash> {
const codeId = (typeof contract === 'object') ? contract.codeId : contract
this.log.debug(`Querying code hash of code id ${bold(codeId)}`)
return timed(
this.fetchCodeHashOfCodeIdImpl.bind(this, codeId),
({ elapsed, result }) => this.log.debug(
`Queried in ${bold(elapsed)}: code hash of code id ${bold(codeId)} is ${bold(result)}`
)
)
}

protected abstract fetchCodeHashOfCodeIdImpl (codeId: Deploy.CodeId):
Promise<Code.CodeHash>

/////////////////////////////////////////////////////////////////////////////////////////////////

fetchContractInfo (address: Address): Promise<unknown>
fetchContractInfo (addresses: Address[]): Promise<Record<Address, unknown>>
fetchContractInfo (address: Address):
Promise<unknown>
fetchContractInfo (addresses: Address[]):
Promise<Record<Address, unknown>>
async fetchContractInfo (...args: unknown[]): Promise<unknown> {
throw new Error("unimplemented!")
return {}
//getCodeHashOfAddress (contract: Address|{ address: Address }): Promise<Code.CodeHash> {
//const address = (typeof contract === 'string') ? contract : contract.address
//this.log.debug(`Querying code hash of address ${bold(address)}`)
//return timed(
//this.fetchCodeHashOfAddressImpl.bind( this, address),
//({ elapsed, result }) => this.log.debug(
//`Queried in ${bold(elapsed)}: code hash of address ${bold(address)} is ${bold(result)}`
//)
//)
//}
/** Get the code id of a given address. */
//getCodeId (contract: Address|{ address: Address }): Promise<Deploy.CodeId> {
//const address = (typeof contract === 'string') ? contract : contract.address
//this.log.debug(`Querying code ID of ${bold(address)}`)
//return timed(
//this.fetchCodeIdImpl.bind(this, address),
//({ elapsed, result }) => this.log.debug(
//`Queried in ${bold(elapsed)}: ${bold(address)} is code id ${bold(result)}`
//)
//)
//}
}
/** Chain-specific implementation of fetchContractInfo. */
protected abstract fetchContractInfoImpl (): Promise<unknown>
/** Get the code hash of a given address. */
getCodeHashOfAddress (contract: Address|{ address: Address }): Promise<Code.CodeHash> {
const address = (typeof contract === 'string') ? contract : contract.address
this.log.debug(`Querying code hash of address ${bold(address)}`)
return timed(
this.fetchCodeHashOfAddressImpl.bind( this, address),
({ elapsed, result }) => this.log.debug(
`Queried in ${bold(elapsed)}: code hash of address ${bold(address)} is ${bold(result)}`
)
)
}
protected abstract fetchCodeHashOfAddressImpl (
contract: Address
): Promise<Code.CodeHash>
/** Get a client handle for a specific smart contract, authenticated as as this agent. */
getContract (
options: Address|{ address: Address }): Contract
getContract (options: Address|{ address: Address }):
Contract
getContract <C extends typeof Contract> (
options: Address|{ address: Address }, $C: C = Contract as C,
): InstanceType<C> {
Expand All @@ -428,17 +425,21 @@ export abstract class Connection extends Endpoint {

/////////////////////////////////////////////////////////////////////////////////////////////////

/** Fetch all instances of a code ID. */
fetchCodeInstances (id: Deploy.CodeId):
Promise<Record<Address, Contract>>
/** Fetch all instances of a code ID, with custom client class. */
fetchCodeInstances <C extends typeof Contract> ($C: C, id: Deploy.CodeId):
Promise<Record<Address, InstanceType<C>>>
/** Fetch all instances of multple code IDs. */
fetchCodeInstances (ids: Iterable<Deploy.CodeId>):
Promise<Record<Deploy.CodeId, Record<Address, Contract>>>
/** Fetch all instances of multple code IDs, with custom client class. */
fetchCodeInstances <C extends typeof Contract> ($C: C, ids: Iterable<Deploy.CodeId>):
Promise<Record<Deploy.CodeId, Record<Address, InstanceType<C>>>>
/** Fetch all instances of multple code IDs, with multiple custom client classes. */
fetchCodeInstances (ids: { [id: Deploy.CodeId]: typeof Contract }):
Promise<Record<Deploy.CodeId, { [id in keyof typeof ids]: InstanceType<typeof ids[id]> }>>
/** Fetch all contracts that match one or more code IDs */
async fetchCodeInstances (...args: unknown[]): Promise<unknown> {
let $C = Contract
if (typeof args[0] === 'function') {
Expand Down
38 changes: 8 additions & 30 deletions packages/agent/stub.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,41 +169,43 @@ Get info about the block with a specific hash.
</pre>

## method [*stubConnection.fetchCodeInfo*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Fetch info about 1, many, or all code IDs (uploaded binaries).
Fetch info about all code IDs uploaded to the chain.
<pre>
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInfo()
</pre>
Fetch info about a single code ID.
<pre>
<strong>const</strong> result: <em>unknown</em> = <strong>await</strong> stubConnection.fetchCodeInfo(id: string)
</pre>
Fetch info about multiple code IDs.
<pre>
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInfo(ids: Iterable&lt;string&gt;)
</pre>

## method [*stubConnection.fetchCodeInstances*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Fetch all contracts that match one or more code IDs
Fetch all instances of a code ID.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Contract&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInstances(id: string)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of a code ID, with custom client class.
<pre>
<strong>const</strong> result: <em>Record&lt;string, InstanceType&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInstances(
$C: <em>C</em>,
id: <em>string</em>,
)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Record&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInstances(ids: Iterable&lt;string&gt;)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs, with custom client class.
<pre>
<strong>const</strong> result: <em>Record&lt;string, Record&gt;</em> = <strong>await</strong> stubConnection.fetchCodeInstances(
$C: <em>C</em>,
ids: <em>Iterable&lt;string&gt;</em>,
)
</pre>
Fetch all contracts that match one or more code IDs
Fetch all instances of multple code IDs, with multiple custom client classes.
<pre>
<strong>const</strong> result: <em>Record&lt;string, &gt;</em> = <strong>await</strong> stubConnection.fetchCodeInstances(ids: ???)
</pre>
Expand All @@ -216,30 +218,6 @@ Fetch all contracts that match one or more code IDs
<strong>const</strong> result: <em>Record&lt;string, unknown&gt;</em> = <strong>await</strong> stubConnection.fetchContractInfo(addresses: string)
</pre>

## method [*stubConnection.getCodeHashOfAddress*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code hash of a given address.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> stubConnection.getCodeHashOfAddress(contract: string | {
address,
})
</pre>

## method [*stubConnection.getCodeHashOfCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code hash of a given code id.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> stubConnection.getCodeHashOfCodeId(contract: string | {
codeId,
})
</pre>

## method [*stubConnection.getCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get the code id of a given address.
<pre>
<strong>const</strong> result: <em>string</em> = <strong>await</strong> stubConnection.getCodeId(contract: string | {
address,
})
</pre>

## method [*stubConnection.getContract*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts)
Get a client handle for a specific smart contract, authenticated as as this agent.
<pre>
Expand Down
19 changes: 10 additions & 9 deletions packages/agent/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,21 @@ export class StubConnection extends Connection {
const balance = (this.backend.balances.get(address!)||{})[token] ?? 0
return Promise.resolve(String(balance))
}
protected async getCodeIdImpl (address: Address): Promise<CodeId> {
const contract = this.backend.instances.get(address)
if (!contract) {
throw new Error(`unknown contract ${address}`)
}
return contract.codeId
}
protected fetchContractsByCodeIdImpl (id: CodeId) {
return Promise.resolve([...this.backend.uploads.get(id)!.instances]
.map(address=>({address})))
}
protected fetchCodeHashOfAddressImpl (address: Address): Promise<CodeHash> {
return this.getCodeId(address)
.then(id=>this.getCodeHashOfCodeId(id))
const contract = this.backend.instances.get(address)
if (!contract) {
throw new Error(`unknown contract ${address}`)
}
const { codeId } = contract
const code = this.backend.uploads.get(codeId)
if (!code) {
throw new Error(`inconsistent state: missing code ${codeId} for address ${address}`)
}
return Promise.resolve(code.codeHash)
}
protected fetchCodeHashOfCodeIdImpl (id: CodeId): Promise<CodeHash> {
const code = this.backend.uploads.get(id)
Expand Down

0 comments on commit a86f6fd

Please sign in to comment.