diff --git a/packages/agent/chain.md b/packages/agent/chain.md index 3fa66da0bf..cf72dce85b 100644 --- a/packages/agent/chain.md +++ b/packages/agent/chain.md @@ -186,41 +186,43 @@ Get info about the block with a specific hash. ## 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.
const result: Record<string, unknown> = await connection.fetchCodeInfo()+Fetch info about a single code ID.
const result: unknown = await connection.fetchCodeInfo(id: string)+Fetch info about multiple code IDs.
const result: Record<string, unknown> = await connection.fetchCodeInfo(ids: Iterable<string>)## 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.
const result: Record<string, Contract> = await connection.fetchCodeInstances(id: string)-Fetch all contracts that match one or more code IDs +Fetch all instances of a code ID, with custom client class.
const result: Record<string, InstanceType> = await connection.fetchCodeInstances( $C: C, id: string, )-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs.
const result: Record<string, Record> = await connection.fetchCodeInstances(ids: Iterable<string>)-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs, with custom client class.
const result: Record<string, Record> = await connection.fetchCodeInstances( $C: C, ids: Iterable<string>, )-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs, with multiple custom client classes.
const result: Record<string, > = await connection.fetchCodeInstances(ids: ???)@@ -233,30 +235,6 @@ Fetch all contracts that match one or more code IDs const result: Record<string, unknown> = await connection.fetchContractInfo(addresses: string) -## method [*connection.getCodeHashOfAddress*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code hash of a given address. -
-const result: string = await connection.getCodeHashOfAddress(contract: string | { - address, -}) -- -## method [*connection.getCodeHashOfCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code hash of a given code id. -
-const result: string = await connection.getCodeHashOfCodeId(contract: string | { - codeId, -}) -- -## method [*connection.getCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code id of a given address. -
-const result: string = await connection.getCodeId(contract: string | { - address, -}) -- ## 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.
diff --git a/packages/agent/chain.ts b/packages/agent/chain.ts index 308387acce..22f24012db 100644 --- a/packages/agent/chain.ts +++ b/packages/agent/chain.ts @@ -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## 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.> + /** Fetch info about a single code ID. */ fetchCodeInfo (id: Deploy.CodeId): Promise + /** Fetch info about multiple code IDs. */ fetchCodeInfo (ids: Iterable ): Promise > async fetchCodeInfo (...args: unknown[]): Promise { if (args.length === 0) { @@ -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 { + //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 > - - /** Get the code id of a given address. */ - getCodeId (contract: Address|{ address: Address }): Promise { - 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 - - /** Get the code hash of a given code id. */ - getCodeHashOfCodeId (contract: Deploy.CodeId|{ codeId: Deploy.CodeId }): Promise { - 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 ///////////////////////////////////////////////////////////////////////////////////////////////// - fetchContractInfo (address: Address): Promise - fetchContractInfo (addresses: Address[]): Promise > + fetchContractInfo (address: Address): + Promise + fetchContractInfo (addresses: Address[]): + Promise > async fetchContractInfo (...args: unknown[]): Promise { throw new Error("unimplemented!") return {} + //getCodeHashOfAddress (contract: Address|{ address: Address }): Promise { + //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 { + //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 - /** Get the code hash of a given address. */ - getCodeHashOfAddress (contract: Address|{ address: Address }): Promise { - 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 /** 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 ( options: Address|{ address: Address }, $C: C = Contract as C, ): InstanceType { @@ -428,17 +425,21 @@ export abstract class Connection extends Endpoint { ///////////////////////////////////////////////////////////////////////////////////////////////// + /** Fetch all instances of a code ID. */ fetchCodeInstances (id: Deploy.CodeId): Promise > + /** Fetch all instances of a code ID, with custom client class. */ fetchCodeInstances ($C: C, id: Deploy.CodeId): Promise >> + /** Fetch all instances of multple code IDs. */ fetchCodeInstances (ids: Iterable ): Promise >> + /** Fetch all instances of multple code IDs, with custom client class. */ fetchCodeInstances ($C: C, ids: Iterable ): Promise >>> + /** Fetch all instances of multple code IDs, with multiple custom client classes. */ fetchCodeInstances (ids: { [id: Deploy.CodeId]: typeof Contract }): Promise }>> - /** Fetch all contracts that match one or more code IDs */ async fetchCodeInstances (...args: unknown[]): Promise { let $C = Contract if (typeof args[0] === 'function') { diff --git a/packages/agent/stub.md b/packages/agent/stub.md index 6b02e669dc..861a002aec 100644 --- a/packages/agent/stub.md +++ b/packages/agent/stub.md @@ -169,41 +169,43 @@ Get info about the block with a specific hash.
const result: Record<string, unknown> = await stubConnection.fetchCodeInfo()+Fetch info about a single code ID.
const result: unknown = await stubConnection.fetchCodeInfo(id: string)+Fetch info about multiple code IDs.
const result: Record<string, unknown> = await stubConnection.fetchCodeInfo(ids: Iterable<string>)## 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.
const result: Record<string, Contract> = await stubConnection.fetchCodeInstances(id: string)-Fetch all contracts that match one or more code IDs +Fetch all instances of a code ID, with custom client class.
const result: Record<string, InstanceType> = await stubConnection.fetchCodeInstances( $C: C, id: string, )-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs.
const result: Record<string, Record> = await stubConnection.fetchCodeInstances(ids: Iterable<string>)-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs, with custom client class.
const result: Record<string, Record> = await stubConnection.fetchCodeInstances( $C: C, ids: Iterable<string>, )-Fetch all contracts that match one or more code IDs +Fetch all instances of multple code IDs, with multiple custom client classes.
const result: Record<string, > = await stubConnection.fetchCodeInstances(ids: ???)@@ -216,30 +218,6 @@ Fetch all contracts that match one or more code IDs const result: Record<string, unknown> = await stubConnection.fetchContractInfo(addresses: string) -## method [*stubConnection.getCodeHashOfAddress*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code hash of a given address. -
-const result: string = await stubConnection.getCodeHashOfAddress(contract: string | { - address, -}) -- -## method [*stubConnection.getCodeHashOfCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code hash of a given code id. -
-const result: string = await stubConnection.getCodeHashOfCodeId(contract: string | { - codeId, -}) -- -## method [*stubConnection.getCodeId*](https://github.com/hackbg/fadroma/tree/v2/packages/agent/chain.ts) -Get the code id of a given address. -
-const result: string = await stubConnection.getCodeId(contract: string | { - address, -}) -- ## 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.
diff --git a/packages/agent/stub.ts b/packages/agent/stub.ts index d025108db9..89503a11e9 100644 --- a/packages/agent/stub.ts +++ b/packages/agent/stub.ts @@ -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{ - 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 { - 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 { const code = this.backend.uploads.get(id)