Skip to content

Commit

Permalink
wip 4
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Oct 27, 2023
1 parent dade228 commit f924756
Show file tree
Hide file tree
Showing 16 changed files with 181 additions and 178 deletions.
2 changes: 1 addition & 1 deletion agent/agent-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ export abstract class Agent {
options: {
label: Label,
initMsg: Message,
initFee?: ICoin[]|'auto',
initFee?: unknown,
initFunds?: ICoin[],
initMemo?: string,
}
Expand Down
87 changes: 59 additions & 28 deletions agent/agent-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,30 @@ export class ContractTemplate extends CompiledCode {
}

export class ContractInstance extends ContractTemplate {
/** Part of label. */
name?: Name
/** Part of label. */
prefix?: Name
/** Part of label. */
suffix?: Name
/** Full label of the instance. Unique for a given Chain. */
label?: Label
label?: Label
/** Address of this contract instance. Unique per chain. */
address?: Address
address?: Address
/** Contents of init message. */
initMsg?: Into<Message>
/** Address of agent that performed the init tx. */
initBy?: Address
initBy?: Address
/** Native tokens to send to the new contract. */
initFunds?: ICoin[]
/** Fee to use for init. */
initFee?: unknown
/** Instantiation memo. */
initMemo?: string
/** TXID of transaction that performed the init. */
initTx?: TxHash
initTx?: TxHash
/** Contents of init message. */
initMsg?: Message
initGas?: unknown

constructor (properties: Partial<ContractInstance> = {}) {
super(properties as Partial<ContractTemplate>)
Expand All @@ -339,9 +353,10 @@ export class ContractInstance extends ContractTemplate {
toInstanceReceipt () {
return {
...this.toUploadReceipt(),
initBy: this.initBy,
initMsg: this.initMsg,
initBy: this.initBy,
initTx: this.initTx,
initGas: this.initGas,
address: this.address,
label: this.label,
}
Expand Down Expand Up @@ -384,23 +399,6 @@ export class Deployment extends ValueObject {

contracts: Map<string, ContractInstance> = new Map()

toReceipt () {
const templates: Record<string, ContractTemplate> = {}
const contracts: Record<string, ContractInstance> = {}
for (const [name, template] of this.templates.entries()) {
templates[name] = template
}
for (const [name, contract] of this.contracts.entries()) {
contracts[name] = contract
}
return {
name: this.name,
mode: this.mode,
templates,
contracts
}
}

static fromReceipt (receipt: DeploymentState) {
const name = receipt.name
const deployment = new this({ name })
Expand All @@ -417,8 +415,30 @@ export class Deployment extends ValueObject {

constructor (properties: Partial<Deployment> = {}) {
super()
properties = { name: timestamp(), ...properties }
this.overrideAll(properties, [ 'name', 'mode', 'store', 'templates', 'contracts' ])
this.overrideAll({ name: timestamp(), ...properties } as Partial<this>, [
'name',
'mode',
'store',
'templates',
'contracts'
])
}

toReceipt () {
const templates: Record<string, ContractTemplate> = {}
const contracts: Record<string, ContractInstance> = {}
for (const [name, template] of this.templates.entries()) {
templates[name] = template
}
for (const [name, contract] of this.contracts.entries()) {
contracts[name] = contract
}
return {
name: this.name,
mode: this.mode,
templates,
contracts
}
}

template (options?: Partial<ContractTemplate>): ContractTemplate {
Expand All @@ -433,7 +453,9 @@ export class Deployment extends ValueObject {
return contract
}

async build (options: { builder: Builder }): Promise<Record<CodeHash, CompiledCode>> {
async build (options: {
builder?: Builder
}): Promise<Record<CodeHash, CompiledCode>> {
const building: Array<Promise<CompiledCode & { codeHash: CodeHash }>> = []
for (const [name, contract] of this.templates.entries()) {
building.push(contract.compile(options.builder))
Expand All @@ -445,7 +467,11 @@ export class Deployment extends ValueObject {
return built
}

async upload (options: { agent: Agent }): Promise<Record<CodeId, ContractTemplate>> {
async upload (options: {
agent?: Agent,
builder?: Builder,
uploadStore?: UploadStore,
}): Promise<Record<CodeId, ContractTemplate>> {
const uploading: Array<Promise<ContractTemplate & { codeId: CodeId }>> = []
for (const [name, contract] of this.templates.entries()) {
uploading.push(contract.upload(options.agent, {}))
Expand All @@ -457,7 +483,12 @@ export class Deployment extends ValueObject {
return uploaded
}

async deploy (options: { agent: Agent }): Promise<Record<Address, ContractInstance>> {
async deploy (options: {
agent?: Agent,
builder?: Builder,
uploadStore?: UploadStore,
deployStore?: DeployStore,
}): Promise<Record<Address, ContractInstance>> {
const deploying: Array<Promise<ContractInstance & { address: Address }>> = []
for (const [name, contract] of this.contracts.entries()) {
deploying.push(contract.instantiate(options.agent, {}))
Expand Down
6 changes: 5 additions & 1 deletion connect/connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export default new Suite([
export async function testConnectChains () {
for (const platform of ['secretjs', 'secretcli']) {
for (const mode of ['mainnet', 'testnet', 'devnet', 'mocknet']) {
const agent = connect({ platform, mode, mnemonic: '...' })
const agent = connect({
platform,
mode,
mnemonic: '...'
})
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions connect/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

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

import { Config } from '@hackbg/conf'
import type { Environment } from '@hackbg/conf'

export * from '@hackbg/conf'

export * from '@fadroma/agent'

export { Scrt, CW }
Expand Down
29 changes: 16 additions & 13 deletions connect/cw/cw-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
} from '@fadroma/agent'

import { CosmWasmClient, SigningCosmWasmClient, serializeSignDoc } from '@hackbg/cosmjs-esm'
import type { logs, OfflineSigner as Signer, Block } from '@hackbg/cosmjs-esm'
import type { logs, OfflineSigner as Signer, Block, StdFee } from '@hackbg/cosmjs-esm'

import { ripemd160 } from "@noble/hashes/ripemd160"
import { sha256 } from "@noble/hashes/sha256"
Expand Down Expand Up @@ -105,7 +105,7 @@ class CWChain extends Chain {
class CWAgent extends Agent {

constructor (options: Partial<CWAgent> = {}) {
super(options)
super(options as Partial<Agent>)

// When not using an external signer, these must be
// either defined in a subclass or passed to the constructor.
Expand Down Expand Up @@ -223,35 +223,38 @@ class CWAgent extends Agent {
options: {
label: Label,
initMsg: Message,
initFee?: ICoin[]|'auto',
initFee?: StdFee|'auto',
initFunds?: ICoin[],
initMemo?: string,
codeHash?: CodeHash
}
): Promise<Partial<ContractInstance>> {
const { api } = await this.ready
const { initFee, initFunds, initMemo } = options

const result = await api.instantiate(
this.address!,
Number(codeId),
options.initMsg,
options.label,
options.initFee,
options.initFee || 'auto',
{
funds: initFunds,
admin: this.address,
memo: options.initMemo
}
)

return {
codeId,
codeHash,
label: options.label,
initMsg: options.initMsg,
chainId: assertChain(this).id,
address: result.contractAddress,
initTx: result.transactionHash,
initGas: result.gasUsed,
initBy: this.address,
codeHash: options.codeHash,
label: options.label,
initMsg: options.initMsg,
chainId: assertChain(this).id,
address: result.contractAddress,
initTx: result.transactionHash,
initGas: result.gasUsed,
initBy: this.address,
initFee,
initFunds,
initMemo
Expand Down Expand Up @@ -341,7 +344,7 @@ function setMnemonic (agent: CWAgent, mnemonic: string) {
}
}

function encodeSecp256k1Signature (pubkey: Uint8Array, signature: Uint8Array): {
export function encodeSecp256k1Signature (pubkey: Uint8Array, signature: Uint8Array): {
pub_key: { type: string, value: string },
signature: string
} {
Expand Down
9 changes: 5 additions & 4 deletions connect/cw/cw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function testCWSigner () {

export async function testCWChain () {
// Throws because devnet instance is not passed:
assert.throws(()=>new CW.Base.Chain({ mode: CW.Base.Chain.Mode.Devnet }).ready)
assert.throws(()=>new CW.Chain({ mode: CW.Chain.Mode.Devnet }).ready)

const devnet = await new Devnet({ platform: 'okp4_5.0' }).create()
const chain = await (devnet.getChain() as CW.OKP4.Chain).ready
Expand All @@ -46,8 +46,9 @@ export async function testCWChain () {
}

export async function testCWOKP4 () {
new CW.OKP4.Cognitarium()
new CW.OKP4.Objectarium()
new CW.OKP4.LawStone()
const agent = new CW.Agent()
new CW.OKP4.Cognitarium({}, agent)
new CW.OKP4.Objectarium({}, agent)
new CW.OKP4.LawStone({}, agent)
CW.OKP4.testnet()
}
2 changes: 1 addition & 1 deletion connect/cw/cw.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as Base from './cw-base'
export * from './cw-base'
export * as OKP4 from './okp4/okp4'
2 changes: 1 addition & 1 deletion connect/cw/okp4/okp4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class OKP4Chain extends Chain {
const contract = new $C(
{ address, codeHash, codeId: String(codeId) } as Partial<C>
)
contract.meta.chainId = chainId
contract.chainId = chainId
if (map) {
(contracts as Map<Address, C>).set(address, contract)
} else {
Expand Down
9 changes: 9 additions & 0 deletions connect/ensuite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
coverage:
exclude:
- "**/*.dist.*"
- "**/*.test.*"
- "*.dist.*"
- "coverage/**/*"
- "*/coverage/**/*"
- "scrt/secretjs-esm"
- "cw/cosmjs-esm"
4 changes: 2 additions & 2 deletions connect/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"scripts": {
"check": "tsc --noEmit",
"ubik": "pnpm check && ubik",
"test": "ensuite connect.test.ts",
"cov": "ensuite-cov -r text -r lcov -- connect.test.ts"
"test": "time ensuite connect.test.ts",
"cov": "time ensuite-cov -r text -r lcov -- connect.test.ts"
},
"dependencies": {
"@fadroma/agent": "workspace:2.0.0-rc.1",
Expand Down
16 changes: 7 additions & 9 deletions connect/scrt/scrt-chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class ScrtAgent extends Agent {
const { encryptionUtils } = this
const apiOptions = { chainId, url, wallet, walletAddress, encryptionUtils }
this.api = await this.chain.getApi(apiOptions)
console.log({thisChain:this.chain, thisApi:this.api})
// Optional: override api.encryptionUtils (e.g. with the ones from Keplr).
if (encryptionUtils) Object.assign(this.api, { encryptionUtils })
// If fees are not specified, get default fees from API.
Expand Down Expand Up @@ -317,7 +318,7 @@ class ScrtAgent extends Agent {
}
}

get account () {
get account (): ReturnType<SecretNetworkClient['query']['auth']['account']> {
return this.ready.then(({ api, address })=>{
return api.query.auth.account({ address })
})
Expand Down Expand Up @@ -372,12 +373,10 @@ class ScrtAgent extends Agent {
}

async getNonce (): Promise<{ accountNumber: number, sequence: number }> {
const { api } = await this.ready
if (!this.address) throw new Error("No address")
const failed = ()=>{
const { api, address } = await this.ready
const result = await api.query.auth.account({ address: this.address }) ?? (() => {
throw new Error(`Cannot find account "${this.address}", make sure it has a balance.`)
}
const result = await api.query.auth.account({ address: this.address }) ?? failed()
})
const { account_number, sequence } = result.account as any
return { accountNumber: Number(account_number), sequence: Number(sequence) }
}
Expand Down Expand Up @@ -409,10 +408,9 @@ class ScrtAgent extends Agent {

/** Upload a WASM binary. */
protected async doUpload (data: Uint8Array): Promise<Partial<ContractTemplate>> {
const { api } = await this.ready
if (!this.address) throw new Error.Missing.Address()
const { api, address } = await this.ready

const request = { sender: this.address, wasm_byte_code: data, source: "", builder: "" }
const request = { sender: address, wasm_byte_code: data, source: "", builder: "" }
const gasLimit = Number(this.fees.upload?.amount[0].amount) || undefined
const result = await api.tx.compute.storeCode(request, { gasLimit }).catch(error=>error)
const { code, message, details = [], rawLog } = result
Expand Down
Loading

0 comments on commit f924756

Please sign in to comment.