Skip to content

Commit

Permalink
refactor([email protected],[email protected]): base chain info on default export
Browse files Browse the repository at this point in the history
  • Loading branch information
egasimus committed Nov 29, 2023
1 parent 69889eb commit 7fe7c91
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 103 deletions.
13 changes: 4 additions & 9 deletions cw/cw-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
} from '@fadroma/agent'
import { CosmWasmClient, SigningCosmWasmClient, serializeSignDoc } from '@hackbg/cosmjs-esm'
import type { Block, StdFee } from '@hackbg/cosmjs-esm'
import type * as Identity from './cw-identity'
import type { CWMnemonicIdentity, CWSignerIdentity } from './cw-identity'

const assertApi =
({ api }: { api?: CWConnection["api"] }): NonNullable<CWConnection["api"]> => {
Expand All @@ -16,7 +16,7 @@ const assertApi =
}

/** Generic agent for CosmWasm-enabled chains. */
class CWConnection extends Connection {
export class CWConnection extends Connection {
/** The bech32 prefix for the account's address */
bech32Prefix?: string
/** The coin type in the HD derivation path */
Expand All @@ -26,7 +26,7 @@ class CWConnection extends Connection {
/** API connects asynchronously, so API handle is a promise of either variant. */
declare api: Promise<CosmWasmClient|SigningCosmWasmClient>
/** A supported method of authentication. */
declare identity: Identity.MnemonicIdentity|Identity.SignerIdentity
declare identity: CWMnemonicIdentity|CWSignerIdentity

constructor (properties: Partial<CWConnection>) {
super(properties)
Expand Down Expand Up @@ -250,7 +250,7 @@ class CWConnection extends Connection {
}

/** Transaction batch for CosmWasm-enabled chains. */
class CWBatch extends Batch<CWConnection> {
export class CWBatch extends Batch<CWConnection> {

upload (
code: Parameters<Batch<Connection>["upload"]>[0],
Expand All @@ -276,8 +276,3 @@ class CWBatch extends Batch<CWConnection> {
async submit () {}

}

export {
CWConnection as Connection,
CWBatch as Batch
}
16 changes: 8 additions & 8 deletions cw/cw-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import { sha256 } from "@noble/hashes/sha256"
import { secp256k1 } from "@noble/curves/secp256k1"
import { numberToBytesBE } from "@noble/curves/abstract/utils"

class CWIdentity extends Identity {}
export class CWMnemonicIdentity extends Identity {
declare signer: OfflineSigner

class CWMnemonicIdentity extends CWIdentity {
bech32Prefix: string
coinType: number
hdAccountIndex: number

pubkey: Uint8Array
signer: OfflineSigner

constructor ({
bech32Prefix,
coinType,
Expand Down Expand Up @@ -98,7 +99,7 @@ class CWMnemonicIdentity extends CWIdentity {
}
}

class CWSignerIdentity extends Identity {
export class CWSignerIdentity extends Identity {
signer: OfflineSigner
constructor ({ signer, ...properties }: Partial<Identity & { signer: OfflineSigner }>) {
super(properties)
Expand Down Expand Up @@ -132,8 +133,7 @@ export function encodeSecp256k1Signature (pubkey: Uint8Array, signature: Uint8Ar
}
}

export {
CWIdentity as Identity,
CWMnemonicIdentity as MnemonicIdentity,
CWSignerIdentity as SignerIdentity,
export default {
Mnemonic: CWMnemonicIdentity,
Signer: CWSignerIdentity
}
13 changes: 11 additions & 2 deletions cw/cw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
**/

import { CWConnection, CWBatch } from './cw-connection'
import CWIdentity from './cw-identity'
import OKP4 from './okp4/okp4'
export * from './cw-base'
export * from './cw-connection'
export * from './cw-identity'
export * as OKP4 from './okp4/okp4'
export { Connection as default } from './cw-connection'
export default class FadromaCW {
static Connection = CWConnection
static Identity = CWIdentity
static Batch = CWBatch
static Chain = {
'OKP4': OKP4
}
}
40 changes: 20 additions & 20 deletions cw/okp4/okp4.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Error, Config } from '../cw-base'
import { Connection, Batch } from '../cw-connection'
import { MnemonicIdentity } from '../cw-identity'
import { CWConnection, CWBatch } from '../cw-connection'
import CWIdentity, { CWMnemonicIdentity } from '../cw-identity'

import { Objectarium, objectariumCodeIds } from './okp4-objectarium'
import { Cognitarium, cognitariumCodeIds } from './okp4-cognitarium'
Expand All @@ -11,17 +11,7 @@ import type { Uint128, Address, ChainId, CodeId } from '@fadroma/agent'
import { Contract, Token } from '@fadroma/agent'
import type { CosmWasmClient } from '@hackbg/cosmjs-esm'

/** Connect to OKP4 in testnet mode. */
export function testnet (options: Partial<OKP4Connection> = {}): OKP4Connection {
return new OKP4Connection({
chainId: 'okp4-nemeton-1',
url: 'https://okp4-testnet-rpc.polkachu.com/',
//'https://okp4-testnet-api.polkachu.com/'
...options||{}
})
}

class OKP4MnemonicIdentity extends MnemonicIdentity {
export class OKP4MnemonicIdentity extends CWMnemonicIdentity {
constructor (properties?: { mnemonic: string }) {
super({
coinType: 118,
Expand All @@ -33,7 +23,7 @@ class OKP4MnemonicIdentity extends MnemonicIdentity {
}

/** Connection for OKP4. */
class OKP4Connection extends Connection {
export class OKP4Connection extends CWConnection {
/** Default denomination of gas token. */
static gasToken = new Token.Native('uknow')
/** Transaction fees for this agent. */
Expand All @@ -50,7 +40,7 @@ class OKP4Connection extends Connection {
bech32Prefix: 'okp4',
hdAccountIndex: 0,
...options
} as Partial<Connection>)
} as Partial<CWConnection>)
}

/** Get clients for all Cognitarium instances, keyed by address. */
Expand Down Expand Up @@ -121,11 +111,21 @@ class OKP4Connection extends Connection {
//}
}

export {
OKP4MnemonicIdentity as MnemonicIdentity,
OKP4Connection as Connection
}

export * from './okp4-cognitarium'
export * from './okp4-objectarium'
export * from './okp4-law-stone'

export default class FadromaOKP4 {
static Connection = OKP4Connection
static Identity = { ...CWIdentity, Mnemonic: OKP4MnemonicIdentity }
static Batch = CWBatch
/** Connect to OKP4 in testnet mode. */
static testnet = (options: Partial<OKP4Connection> = {}): OKP4Connection => {
return new OKP4Connection({
chainId: 'okp4-nemeton-1',
url: 'https://okp4-testnet-rpc.polkachu.com/',
//'https://okp4-testnet-api.polkachu.com/'
...options||{}
})
}
}
2 changes: 1 addition & 1 deletion scrt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fadroma/scrt",
"version": "11.0.0-rc.22",
"version": "11.0.0-rc.23",
"license": "AGPL-3.0-only",
"type": "module",
"main": "scrt.ts",
Expand Down
55 changes: 5 additions & 50 deletions scrt/scrt-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { Tx, ReadonlySigner, SecretNetworkClient, Wallet } from '@hackbg/secretjs-esm'
import type { CreateClientOptions, EncryptionUtils, TxResponse } from '@hackbg/secretjs-esm'
import { Error, console } from './scrt-base'
import { Identity } from './scrt-identity'
import { ScrtIdentity } from './scrt-identity'
import faucets from './scrt-faucets'
//import * as Mocknet from './scrt-mocknet'
import type {
Uint128, Contract, Message, Name, Address, TxHash, ChainId, CodeId, CodeHash, Label,
Expand All @@ -17,54 +18,16 @@ import {

const { MsgStoreCode, MsgExecuteContract, MsgInstantiateContract } = Tx

const pickRandom = <T>(set: Set<T>): T => [...set][Math.floor(Math.random()*set.size)]

/** Connect to the Secret Network Mainnet. */
export function mainnet (options: Partial<ScrtConnection> = {}): ScrtConnection {
return new ScrtConnection({
chainId: 'secret-4', url: pickRandom(mainnets), ...options||{}
})
}

/** See https://docs.scrt.network/secret-network-documentation/development/resources-api-contract-addresses/connecting-to-the-network/mainnet-secret-4#api-endpoints */
export const mainnets = new Set([
'https://lcd.mainnet.secretsaturn.net',
'https://lcd.secret.express',
'https://rpc.ankr.com/http/scrt_cosmos',
'https://1rpc.io/scrt-lcd',
'https://lcd-secret.whispernode.com',
'https://secret-api.lavenderfive.com',
])

/** Connect to the Secret Network Testnet. */
export function testnet (options: Partial<ScrtConnection> = {}): ScrtConnection {
return new ScrtConnection({
chainId: 'pulsar-3', url: pickRandom(testnets), ...options||{}
})
}

export const testnets = new Set([
'https://api.pulsar.scrttestnet.com',
'https://api.pulsar3.scrttestnet.com/'
])

export const faucets: Record<ChainId, Set<string>> = {
'secret-4': new Set([
`https://faucet.secretsaturn.net/`
]),
'pulsar-3': new Set([
`https://faucet.pulsar.scrttestnet.com/`
])
}
export type { TxResponse }

/** Represents a Secret Network API endpoint. */
class ScrtConnection extends Connection {
export class ScrtConnection extends Connection {
/** Smallest unit of native token. */
static gasToken = new Token.Native('uscrt')
/** Underlying API client. */
declare api: SecretNetworkClient
/** Supports multiple authentication methods. */
declare identity: Identity
declare identity: ScrtIdentity
/** Set permissive fees by default. */
fees = {
upload: ScrtConnection.gasToken.fee(10000000),
Expand Down Expand Up @@ -410,14 +373,6 @@ function removeTrailingSlash (url: string) {
return url
}

export {
ScrtConnection as Connection
}

export type {
TxResponse
}

export class ScrtBatch extends Batch<ScrtConnection> {
/** Messages to encrypt. */
messages: Array<
Expand Down
16 changes: 16 additions & 0 deletions scrt/scrt-faucets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** Fadroma. Copyright (C) 2023 Hack.bg. License: GNU AGPLv3 or custom.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. **/

// Private module, don't reexport!

import type { ChainId } from '@fadroma/agent'

export default {
'secret-4': new Set([
`https://faucet.secretsaturn.net/`
]),
'pulsar-3': new Set([
`https://faucet.pulsar.scrttestnet.com/`
])
} as Record<ChainId, Set<string>>
16 changes: 7 additions & 9 deletions scrt/scrt-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import { SecretNetworkClient, Wallet } from '@hackbg/secretjs-esm'
import type { EncryptionUtils } from '@hackbg/secretjs-esm'
import { Error } from './scrt-base'

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

static fromKeplr = () => {
throw new Error('unimplemented')
}
}

class ScrtSignerIdentity extends ScrtIdentity {
export class ScrtSignerIdentity extends ScrtIdentity {
encryptionUtils?: EncryptionUtils
constructor ({ encryptionUtils, ...properties }: Partial<ScrtSignerIdentity>) {
super(properties)
Expand All @@ -20,7 +24,7 @@ class ScrtSignerIdentity extends ScrtIdentity {
}
}

class ScrtMnemonicIdentity extends ScrtIdentity {
export class ScrtMnemonicIdentity extends ScrtIdentity {
wallet: Wallet
constructor ({
mnemonic = bip39.generateMnemonic(bip39EN),
Expand All @@ -43,9 +47,3 @@ class ScrtMnemonicIdentity extends ScrtIdentity {
})
}
}

export {
ScrtIdentity as Identity,
ScrtSignerIdentity as SignerIdentity,
ScrtMnemonicIdentity as MnemonicIdentity,
}
6 changes: 3 additions & 3 deletions scrt/scrt-mocknet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Console, bold, Error, Stub, base16, sha256, into, bech32, randomBech32,
ContractInstance, brailleDump, Token, bip39, bip39EN
} from '@fadroma/agent'
import { MnemonicIdentity } from './scrt-identity'
import { ScrtMnemonicIdentity } from './scrt-identity'
import { Wallet } from '@hackbg/secretjs-esm'
import * as secp256k1 from '@noble/secp256k1'
import * as ed25519 from '@noble/ed25519'
Expand Down Expand Up @@ -347,7 +347,7 @@ class ScrtMocknetBackend extends Stub.Backend {
}

getIdentity (name: string): Promise<Identity> {
return Promise.resolve(new MnemonicIdentity({ name, ...this.accounts.get(name) }))
return Promise.resolve(new ScrtMnemonicIdentity({ name, ...this.accounts.get(name) }))
}

async connect (
Expand All @@ -361,7 +361,7 @@ class ScrtMocknetBackend extends Stub.Backend {
url: this.url,
alive: this.alive,
backend: this,
identity: new MnemonicIdentity(parameter)
identity: new ScrtMnemonicIdentity(parameter)
})
}

Expand Down
Loading

0 comments on commit 7fe7c91

Please sign in to comment.