Skip to content

Commit

Permalink
wip: fix(scrt): update to match changes in agent, pt.1
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 3, 2024
1 parent 20a0b53 commit 36855ec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 60 deletions.
134 changes: 76 additions & 58 deletions packages/scrt/scrt-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,24 @@ export type { TxResponse }

/** Represents a Secret Network API endpoint. */
export class ScrtConnection extends Chain.Connection {

/** Smallest unit of native token. */
static gasToken = new Token.Native('uscrt')

/** Underlying API client. */
declare api: SecretNetworkClient

/** Supports multiple authentication methods. */
declare identity: ScrtIdentity

/** Set permissive fees by default. */
fees = {
upload: ScrtConnection.gasToken.fee(10000000),
init: ScrtConnection.gasToken.fee(10000000),
exec: ScrtConnection.gasToken.fee(1000000),
send: ScrtConnection.gasToken.fee(1000000),
}

constructor (properties?: Partial<ScrtConnection>) {
super(properties as Partial<Chain.Connection>)
this.api ??= new SecretNetworkClient({ url: this.url!, chainId: this.chainId!, })
Expand Down Expand Up @@ -59,19 +64,29 @@ export class ScrtConnection extends Chain.Connection {
}
}

protected async doGetBlockInfo () {
protected async fetchBlockImpl () {
const {
block_id: { hash, part_set_header } = {},
block: { header, data, evidence, last_commit } = {}
} = await this.api.query.tendermint.getLatestBlock({})
}

protected doGetHeight () {
return this.doGetBlockInfo().then((block: any)=>
protected async fetchCodeInfoImpl () {}

protected async fetchCodeInstancesImpl () {}

protected async fetchContractInfoImpl () {}

authenticate (identity: ScrtIdentity): Promise<ScrtAgent> {
return new ScrtAgent({ connection: this, identity })
}

protected fetchHeightImpl () {
return this.fetchBlockInfoImpl().then((block: any)=>
Number(block.block?.header?.height))
}

protected doGetCodes () {
protected fetchCodesImpl () {
const codes: Record<CodeId, Deploy.UploadedCode> = {}
return withIntoError(this.api.query.compute.codes({}))
.then(({code_infos})=>{
Expand All @@ -87,14 +102,14 @@ export class ScrtConnection extends Chain.Connection {
})
}

protected async doGetCodeId (contract_address: Address): Promise<CodeId> {
protected async fetchCodeIdImpl (contract_address: Address): Promise<CodeId> {
return (await withIntoError(this.api.query.compute.contractInfo({
contract_address
})))
.ContractInfo!.code_id!
}

protected async doGetContractsByCodeId (code_id: CodeId): Promise<Iterable<{address: Address}>> {
protected async fetchContractsByCodeIdImpl (code_id: CodeId): Promise<Iterable<{address: Address}>> {
return (await withIntoError(this.api.query.compute.contractsByCodeId({ code_id })))
.contract_infos!
.map(({ contract_address, contract_info: { label, creator } }: any)=>({
Expand All @@ -104,21 +119,21 @@ export class ScrtConnection extends Chain.Connection {
}))
}

protected async doGetCodeHashOfAddress (contract_address: Address): Promise<CodeHash> {
protected async fetchCodeHashOfAddressImpl (contract_address: Address): Promise<CodeHash> {
return (await withIntoError(this.api.query.compute.codeHashByContractAddress({
contract_address
})))
.code_hash!
}

protected async doGetCodeHashOfCodeId (code_id: CodeId): Promise<CodeHash> {
protected async fetchCodeHashOfCodeIdImpl (code_id: CodeId): Promise<CodeHash> {
return (await withIntoError(this.api.query.compute.codeHashByCodeId({
code_id
})))
.code_hash!
}

protected async doGetBalance (denom: string = this.defaultDenom, address: string|undefined = this.address) {
protected async fetchBalanceImpl (denom: string = this.defaultDenom, address: string|undefined = this.address) {
return (await withIntoError(this.api.query.bank.balance({
address,
denom
Expand All @@ -135,7 +150,7 @@ export class ScrtConnection extends Chain.Connection {

/** Query a contract.
* @returns the result of the query */
protected doQuery <U> (contract: { address: Address, codeHash: CodeHash }, message: Message): Promise<U> {
protected queryImpl <U> (contract: { address: Address, codeHash: CodeHash }, message: Message): Promise<U> {
return withIntoError(this.api.query.compute.queryContract({
contract_address: contract.address,
code_hash: contract.codeHash,
Expand All @@ -147,18 +162,61 @@ export class ScrtConnection extends Chain.Connection {
return this.api.query.auth.account({ address: this.address })
}

protected async doSend (
async setMaxGas (): Promise<this> {
const max = ScrtConnection.gasToken.fee((await this.fetchLimits()).gas)
this.fees = { upload: max, init: max, exec: max, send: max }
return this
}

async fetchLimits (): Promise<{ gas: number }> {
const params = { subspace: "baseapp", key: "BlockParams" }
const { param } = await this.api.query.params.params(params)
let { max_bytes, max_gas } = JSON.parse(param?.value??'{}')
this.log.debug(`Fetched default gas limit: ${max_gas} and code size limit: ${max_bytes}`)
if (max_gas < 0) {
max_gas = 10000000
this.log.warn(`Chain returned negative max gas limit. Defaulting to: ${max_gas}`)
}
return { gas: max_gas }
}

async getNonce (): Promise<{ accountNumber: number, sequence: number }> {
const result: any = await this.api.query.auth.account({ address: this.address }) ?? (() => {
throw new Error(`Cannot find account "${this.address}", make sure it has a balance.`)
})
const { account_number, sequence } = result.account
return { accountNumber: Number(account_number), sequence: Number(sequence) }
}

async encrypt (codeHash: CodeHash, msg: Message) {
if (!codeHash) {
throw new Error("can't encrypt message without code hash")
}
const { encryptionUtils } = this.api as any
const encrypted = await encryptionUtils.encrypt(codeHash, msg as object)
return base64.encode(encrypted)
}

batch (): Batch<this> {
return new ScrtBatch({ connection: this }) as unknown as Batch<this>
}

}

export class ScrtAgent extends Chain.Agent {

protected async sendImpl (
recipient: Address,
amounts: Token.ICoin[],
options?: Parameters<Chain.Connection["doSend"]>[2]
options?: Parameters<Chain.Connection["sendImpl"]>[2]
) {
return withIntoError(this.api.tx.bank.send(
{ from_address: this.address!, to_address: recipient, amount: amounts },
{ gasLimit: Number(options?.sendFee?.gas) }
))
}

protected doSendMany (
protected sendManyImpl (
outputs: [Address, Token.ICoin[]][], options?: unknown
): Promise<unknown> {
throw new Error('unimplemented')
Expand All @@ -169,7 +227,7 @@ export class ScrtConnection extends Chain.Connection {
}

/** Upload a WASM binary. */
protected async doUpload (data: Uint8Array): Promise<Partial<Deploy.UploadedCode>> {
protected async uploadImpl (data: Uint8Array): Promise<Partial<Deploy.UploadedCode>> {
const request = { sender: this.address!, wasm_byte_code: data, source: "", builder: "" }
const gasLimit = Number(this.fees.upload?.amount[0].amount) || undefined
const result = await withIntoError(this.api!.tx.compute.storeCode(request, { gasLimit }))
Expand Down Expand Up @@ -208,9 +266,9 @@ export class ScrtConnection extends Chain.Connection {
}
}

protected async doInstantiate (
protected async instantiateImpl (
codeId: CodeId,
options: Parameters<Chain.Connection["doInstantiate"]>[1]
options: Parameters<Chain.Connection["instantiateImpl"]>[1]
): Promise<Partial<Deploy.ContractInstance>> {
if (!this.address) throw new Error("agent has no address")
const parameters = {
Expand Down Expand Up @@ -246,10 +304,10 @@ export class ScrtConnection extends Chain.Connection {
}
}

protected async doExecute (
protected async executeImpl (
contract: { address: Address, codeHash: CodeHash },
message: Message,
options?: Parameters<Chain.Connection["doExecute"]>[2] & {
options?: Parameters<Chain.Connection["executeImpl"]>[2] & {
preSimulate?: boolean
}
): Promise<TxResponse> {
Expand Down Expand Up @@ -288,46 +346,6 @@ export class ScrtConnection extends Chain.Connection {
}
return result as TxResponse
}

async setMaxGas (): Promise<this> {
const max = ScrtConnection.gasToken.fee((await this.fetchLimits()).gas)
this.fees = { upload: max, init: max, exec: max, send: max }
return this
}

async fetchLimits (): Promise<{ gas: number }> {
const params = { subspace: "baseapp", key: "BlockParams" }
const { param } = await this.api.query.params.params(params)
let { max_bytes, max_gas } = JSON.parse(param?.value??'{}')
this.log.debug(`Fetched default gas limit: ${max_gas} and code size limit: ${max_bytes}`)
if (max_gas < 0) {
max_gas = 10000000
this.log.warn(`Chain returned negative max gas limit. Defaulting to: ${max_gas}`)
}
return { gas: max_gas }
}

async getNonce (): Promise<{ accountNumber: number, sequence: number }> {
const result: any = await this.api.query.auth.account({ address: this.address }) ?? (() => {
throw new Error(`Cannot find account "${this.address}", make sure it has a balance.`)
})
const { account_number, sequence } = result.account
return { accountNumber: Number(account_number), sequence: Number(sequence) }
}

async encrypt (codeHash: CodeHash, msg: Message) {
if (!codeHash) {
throw new Error("can't encrypt message without code hash")
}
const { encryptionUtils } = this.api as any
const encrypted = await encryptionUtils.encrypt(codeHash, msg as object)
return base64.encode(encrypted)
}

batch (): Batch<this> {
return new ScrtBatch({ connection: this }) as unknown as Batch<this>
}

}

export class ScrtBlock extends Chain.Block {
Expand Down
4 changes: 2 additions & 2 deletions packages/scrt/scrt-identity.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Chain, Identity } from '@fadroma/agent'
import { Chain } from '@fadroma/agent'
import type { ChainId } from '@fadroma/agent'
import { SecretNetworkClient, Wallet } from '@hackbg/secretjs-esm'
import type { EncryptionUtils } from '@hackbg/secretjs-esm'
import { ScrtError as Error, bold, colors, assign, Bip39, Bip39EN } from './scrt-base'

export abstract class ScrtIdentity extends Identity {
export abstract class ScrtIdentity extends Chain.Identity {
abstract getApi ({chainId, url}: {chainId: ChainId, url: string|URL}): SecretNetworkClient

static fromKeplr = () => {
Expand Down

0 comments on commit 36855ec

Please sign in to comment.