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 e15fd09
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 101 deletions.
24 changes: 0 additions & 24 deletions packages/agent/chain.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,30 +233,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
83 changes: 39 additions & 44 deletions packages/agent/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,68 +352,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 Down
24 changes: 0 additions & 24 deletions packages/agent/stub.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,30 +216,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 e15fd09

Please sign in to comment.