Skip to content

Commit

Permalink
wip: refactor(scrt): bring implementation up to date with 2.0 api
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed May 6, 2024
1 parent caf50a6 commit f19d87a
Show file tree
Hide file tree
Showing 15 changed files with 991 additions and 788 deletions.
584 changes: 348 additions & 236 deletions packages/agent/chain.ts

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion packages/agent/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export type Decimal256 = string
export interface ICoin { amount: Uint128, denom: string }

/** A gas fee, payable in native tokens. */
export interface IFee { amount: readonly ICoin[], gas: Uint128 }
export interface IFee { gas: Uint128, amount: readonly ICoin[] }

/** A mapping of transaction type to default fee in one or more tokens. */
export type FeeMap<T extends string> = { [key in T]: IFee }

/** A constructable gas fee in native tokens. */
export class Fee implements IFee {
Expand Down
26 changes: 11 additions & 15 deletions packages/cw/cw-bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ import type { Address, Token, Chain } from '@fadroma/agent'
import { Core } from '@fadroma/agent'

type Connection = {
address?: Address,
log: Core.Console
api: CosmWasmClient|Promise<CosmWasmClient>
}

export async function getBalance (
{ api, log, address }: Connection, token: string, queriedAddress: Address|undefined = address
{ api }: Connection,
token: string,
address: Address
) {
api = await Promise.resolve(api)
if (!queriedAddress) {
if (!address) {
throw new Error('getBalance: need address')
}
if (queriedAddress === address) {
log.debug('Querying', Core.bold(token), 'balance')
} else {
log.debug('Querying', Core.bold(token), 'balance of', Core.bold(queriedAddress))
}
const { amount } = await api.getBalance(queriedAddress!, token!)
const { amount } = await api.getBalance(address, token)
return amount
}

Expand All @@ -32,9 +27,10 @@ type SigningConnection = {

export async function send (
{ api, address }: SigningConnection,
recipient: Address,
amounts: Token.ICoin[],
options?: Parameters<Chain.Connection["doSend"]>[2]
{ outputs
, sendFee = 'auto'
, sendMemo
, parallel }: Parameters<Chain.Agent["sendImpl"]>[0]
) {
api = await Promise.resolve(api)
if (!(api?.sendTokens)) {
Expand All @@ -44,7 +40,7 @@ export async function send (
address!,
recipient as string,
amounts,
options?.sendFee || 'auto',
options?.sendMemo
sendFee,
sendMemo
)
}
17 changes: 8 additions & 9 deletions packages/cw/cw-batch.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
import { Core, Chain } from '@fadroma/agent'
import type { CWConnection } from './cw-connection'
import type { CWConnection, CWAgent } from './cw-connection'

/** Transaction batch for CosmWasm-enabled chains. */
export class CWBatch extends Chain.Batch<CWConnection> {
export class CWBatch extends Chain.Batch<CWConnection, CWAgent> {
upload (
code: Parameters<Chain.Batch<Chain.Connection>["upload"]>[0],
options: Parameters<Chain.Batch<Chain.Connection>["upload"]>[1]
code: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["upload"]>[0],
options: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["upload"]>[1]
) {
throw new Core.Error("CWBatch#upload: not implemented")
return this
}
instantiate (
code: Parameters<Chain.Batch<Chain.Connection>["instantiate"]>[0],
options: Parameters<Chain.Batch<Chain.Connection>["instantiate"]>[1]
code: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["instantiate"]>[0],
options: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["instantiate"]>[1]
) {
throw new Core.Error("CWBatch#instantiate: not implemented")
return this
}
execute (
contract: Parameters<Chain.Batch<Chain.Connection>["execute"]>[0],
options: Parameters<Chain.Batch<Chain.Connection>["execute"]>[1]
contract: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["execute"]>[0],
options: Parameters<Chain.Batch<Chain.Connection, Chain.Agent>["execute"]>[1]
) {
throw new Core.Error("CWBatch#execute: not implemented")
return this
}
async submit () {}
}

102 changes: 60 additions & 42 deletions packages/cw/cw-compute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ type Connection = {
api: CosmWasmClient|Promise<CosmWasmClient>
}

export type SigningConnection = {
log: Core.Console,
chainId?: string,
address: string,
fees?: any,
api: SigningCosmWasmClient|Promise<SigningCosmWasmClient>
}

export async function getCodes (
{ chainId, api }: Connection
) {
Expand All @@ -25,31 +33,46 @@ export async function getCodes (
return codes
}

export async function getCodeId ({ api }: Connection, address: Address): Promise<CodeId> {
export async function getCodeId (
{ api }: Connection,
address: Address
): Promise<CodeId> {
api = await Promise.resolve(api)
const { codeId } = await api.getContract(address)
return String(codeId)
}

export async function getContractsByCodeId ({ api }: Connection, id: CodeId) {
export async function getContractsByCodeId (
{ api }: Connection,
id: CodeId
) {
api = await Promise.resolve(api)
const addresses = await api.getContracts(Number(id))
return addresses.map(address=>({address}))
}

export async function getCodeHashOfAddress (connection: Connection, address: Address) {
export async function getCodeHashOfAddress (
connection: Connection,
address: Address
) {
const api = await Promise.resolve(connection.api)
const {codeId} = await api.getContract(address)
return getCodeHashOfCodeId(connection, String(codeId))
}

export async function getCodeHashOfCodeId ({ api }: Connection, codeId: CodeId) {
export async function getCodeHashOfCodeId (
{ api }: Connection,
codeId: CodeId
) {
api = await Promise.resolve(api)
const {checksum} = await api.getCodeDetails(Number(codeId))
return checksum
}

export async function getLabel ({ api }: Connection, address: Address) {
export async function getLabel (
{ api }: Connection,
address: Address
) {
if (!address) {
throw new Error('chain.getLabel: no address')
}
Expand All @@ -58,39 +81,36 @@ export async function getLabel ({ api }: Connection, address: Address) {
return label
}

export async function query <U> (
{ api }: Connection,
contract: Address|{ address: Address },
message: Message
): Promise<U> {
export async function query <T> (
{ api }: Connection,
options: Parameters<Chain.Connection["queryImpl"]>[0]
): Promise<T> {
api = await Promise.resolve(api)
if (typeof contract === 'string') {
contract = { address: contract }
}
if (!contract.address) {
if (!options.address) {
throw new Error('no contract address')
}
return api.queryContractSmart((contract as { address: Address }).address, message) as U
}

export type SigningConnection = {
log: Core.Console,
chainId?: string,
address: string,
fees?: any,
api: SigningCosmWasmClient|Promise<SigningCosmWasmClient>
return await api.queryContractSmart(
options.address,
options.message,
) as T
}

export async function upload (
{ address, chainId, fees, api }: SigningConnection,
data: Uint8Array
options: Parameters<Chain.Agent["uploadImpl"]>[0]
) {
if (!address) {
throw new Error("can't upload contract without sender address")
}
api = await Promise.resolve(api)
if (!(api?.upload)) {
throw new Error("can't upload contract with an unauthenticated agent")
}
const result = await api.upload(
address!, data, fees?.upload || 'auto', "Uploaded by Fadroma"
address!,
options.binary,
fees?.upload || 'auto',
"Uploaded by Fadroma"
)
return {
chainId: chainId,
Expand All @@ -104,23 +124,25 @@ export async function upload (

export async function instantiate (
{ api, address, chainId }: SigningConnection,
codeId: CodeId,
options: Parameters<Chain.Connection["doInstantiate"]>[1]
options: Parameters<Chain.Agent["instantiateImpl"]>[0]
) {
if (!this.address) {
throw new Error("can't instantiate contract without sender address")
}
api = await Promise.resolve(api)
if (!(api?.instantiate)) {
throw new Error("can't instantiate contract without authorizing the agent")
}
const result = await (api as SigningCosmWasmClient).instantiate(
address!,
Number(codeId),
Number(options.codeId),
options.initMsg,
options.label!,
options.initFee as Amino.StdFee || 'auto',
{ admin: address, funds: options.initSend, memo: options.initMemo }
)
return {
codeId,
codeId: options.codeId,
codeHash: options.codeHash,
label: options.label,
initMsg: options.initMsg,
Expand All @@ -135,27 +157,23 @@ export async function instantiate (
}
}

type ExecOptions =
Omit<NonNullable<Parameters<Chain.Connection["execute"]>[2]>, 'execFee'> & {
execFee?: Token.IFee | number | 'auto'
}

export async function execute (
{ api, address, chainId }: SigningConnection,
contract: { address: Address },
message: Message,
{ execSend, execMemo, execFee }: ExecOptions = {}
options: Parameters<Chain.Agent["executeImpl"]>[0]
) {
if (!address) {
throw new Error("can't execute transaction without sender address")
}
api = await Promise.resolve(api)
if (!(api?.execute)) {
throw new Error("can't execute transaction without authorizing the agent")
}
return api.execute(
address!,
contract.address,
message,
execFee!,
execMemo,
execSend
options.address,
options.message,
options.execFee!,
options.execMemo,
options.execSend
)
}
Loading

0 comments on commit f19d87a

Please sign in to comment.