From 7fe7c9188e7f8f8aabdd3648c29bf14c6f354f61 Mon Sep 17 00:00:00 2001 From: Adam Avramov Date: Wed, 29 Nov 2023 15:26:55 +0200 Subject: [PATCH] refactor(cw@1-rc.9,scrt@11-rc.23): base chain info on default export --- cw/cw-connection.ts | 13 +++------- cw/cw-identity.ts | 16 ++++++------ cw/cw.ts | 13 ++++++++-- cw/okp4/okp4.ts | 40 +++++++++++++++--------------- scrt/package.json | 2 +- scrt/scrt-connection.ts | 55 ++++------------------------------------- scrt/scrt-faucets.ts | 16 ++++++++++++ scrt/scrt-identity.ts | 16 ++++++------ scrt/scrt-mocknet.ts | 6 ++--- scrt/scrt.ts | 52 +++++++++++++++++++++++++++++++++++++- scrt/tsconfig.json | 1 + 11 files changed, 127 insertions(+), 103 deletions(-) create mode 100644 scrt/scrt-faucets.ts diff --git a/cw/cw-connection.ts b/cw/cw-connection.ts index 5c75b25c68f..e1a4b1ba453 100644 --- a/cw/cw-connection.ts +++ b/cw/cw-connection.ts @@ -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 => { @@ -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 */ @@ -26,7 +26,7 @@ class CWConnection extends Connection { /** API connects asynchronously, so API handle is a promise of either variant. */ declare api: Promise /** A supported method of authentication. */ - declare identity: Identity.MnemonicIdentity|Identity.SignerIdentity + declare identity: CWMnemonicIdentity|CWSignerIdentity constructor (properties: Partial) { super(properties) @@ -250,7 +250,7 @@ class CWConnection extends Connection { } /** Transaction batch for CosmWasm-enabled chains. */ -class CWBatch extends Batch { +export class CWBatch extends Batch { upload ( code: Parameters["upload"]>[0], @@ -276,8 +276,3 @@ class CWBatch extends Batch { async submit () {} } - -export { - CWConnection as Connection, - CWBatch as Batch -} diff --git a/cw/cw-identity.ts b/cw/cw-identity.ts index b087dd7e964..2faef1c027f 100644 --- a/cw/cw-identity.ts +++ b/cw/cw-identity.ts @@ -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, @@ -98,7 +99,7 @@ class CWMnemonicIdentity extends CWIdentity { } } -class CWSignerIdentity extends Identity { +export class CWSignerIdentity extends Identity { signer: OfflineSigner constructor ({ signer, ...properties }: Partial) { super(properties) @@ -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 } diff --git a/cw/cw.ts b/cw/cw.ts index 1e646ff3c4d..c8d9bdab821 100644 --- a/cw/cw.ts +++ b/cw/cw.ts @@ -15,9 +15,18 @@ You should have received a copy of the GNU Affero General Public License along with this program. If not, see . **/ - +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 + } +} diff --git a/cw/okp4/okp4.ts b/cw/okp4/okp4.ts index e3f02ea32fa..a873ad50630 100644 --- a/cw/okp4/okp4.ts +++ b/cw/okp4/okp4.ts @@ -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' @@ -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 { - 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, @@ -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. */ @@ -50,7 +40,7 @@ class OKP4Connection extends Connection { bech32Prefix: 'okp4', hdAccountIndex: 0, ...options - } as Partial) + } as Partial) } /** Get clients for all Cognitarium instances, keyed by address. */ @@ -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 => { + return new OKP4Connection({ + chainId: 'okp4-nemeton-1', + url: 'https://okp4-testnet-rpc.polkachu.com/', + //'https://okp4-testnet-api.polkachu.com/' + ...options||{} + }) + } +} diff --git a/scrt/package.json b/scrt/package.json index 43d273af1d6..b2ccc903e88 100644 --- a/scrt/package.json +++ b/scrt/package.json @@ -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", diff --git a/scrt/scrt-connection.ts b/scrt/scrt-connection.ts index 64731e92a83..39737e504b2 100644 --- a/scrt/scrt-connection.ts +++ b/scrt/scrt-connection.ts @@ -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, @@ -17,54 +18,16 @@ import { const { MsgStoreCode, MsgExecuteContract, MsgInstantiateContract } = Tx -const pickRandom = (set: Set): T => [...set][Math.floor(Math.random()*set.size)] - -/** Connect to the Secret Network Mainnet. */ -export function mainnet (options: Partial = {}): 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 { - 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> = { - '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), @@ -410,14 +373,6 @@ function removeTrailingSlash (url: string) { return url } -export { - ScrtConnection as Connection -} - -export type { - TxResponse -} - export class ScrtBatch extends Batch { /** Messages to encrypt. */ messages: Array< diff --git a/scrt/scrt-faucets.ts b/scrt/scrt-faucets.ts new file mode 100644 index 00000000000..a351b2795d1 --- /dev/null +++ b/scrt/scrt-faucets.ts @@ -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 . **/ + +// 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> diff --git a/scrt/scrt-identity.ts b/scrt/scrt-identity.ts index 0974434bd15..3eb98076aed 100644 --- a/scrt/scrt-identity.ts +++ b/scrt/scrt-identity.ts @@ -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) { super(properties) @@ -20,7 +24,7 @@ class ScrtSignerIdentity extends ScrtIdentity { } } -class ScrtMnemonicIdentity extends ScrtIdentity { +export class ScrtMnemonicIdentity extends ScrtIdentity { wallet: Wallet constructor ({ mnemonic = bip39.generateMnemonic(bip39EN), @@ -43,9 +47,3 @@ class ScrtMnemonicIdentity extends ScrtIdentity { }) } } - -export { - ScrtIdentity as Identity, - ScrtSignerIdentity as SignerIdentity, - ScrtMnemonicIdentity as MnemonicIdentity, -} diff --git a/scrt/scrt-mocknet.ts b/scrt/scrt-mocknet.ts index 649ddb2fa2b..23d64599aaf 100644 --- a/scrt/scrt-mocknet.ts +++ b/scrt/scrt-mocknet.ts @@ -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' @@ -347,7 +347,7 @@ class ScrtMocknetBackend extends Stub.Backend { } getIdentity (name: string): Promise { - return Promise.resolve(new MnemonicIdentity({ name, ...this.accounts.get(name) })) + return Promise.resolve(new ScrtMnemonicIdentity({ name, ...this.accounts.get(name) })) } async connect ( @@ -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) }) } diff --git a/scrt/scrt.ts b/scrt/scrt.ts index 241dfc2d1d3..202315f6f2c 100644 --- a/scrt/scrt.ts +++ b/scrt/scrt.ts @@ -16,6 +16,57 @@ along with this program. If not, see . **/ +import type { ChainId } from '@fadroma/agent' +import { ScrtConnection, ScrtBatch } from './scrt-connection' +import { ScrtIdentity } from './scrt-identity' +import faucets from './scrt-faucets' + +const pickRandom = (set: Set): T => [...set][Math.floor(Math.random()*set.size)] + +export default class FadromaScrt { + + static Connection = ScrtConnection + + static Identity = ScrtIdentity + + static Batch = ScrtBatch + + /** Connect to the Secret Network Mainnet. */ + static mainnet = (options: Partial = {}): ScrtConnection => { + return new ScrtConnection({ + chainId: 'secret-4', url: pickRandom(this.mainnets), ...options||{} + }) + } + + /** See https://docs.scrt.network/secret-network-documentation/development/resources-api-contract-addresses/connecting-to-the-network/mainnet-secret-4#api-endpoints */ + static 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. */ + static testnet = (options: Partial = {}): ScrtConnection => { + return new ScrtConnection({ + chainId: 'pulsar-3', url: pickRandom(this.testnets), ...options||{} + }) + } + + static testnets = new Set([ + 'https://api.pulsar.scrttestnet.com', + 'https://api.pulsar3.scrttestnet.com/' + ]) + + static faucets = faucets + + constructor () { + throw new Error('static class') + } +} + export * from './scrt-base' export * from './scrt-connection' export * from './scrt-identity' @@ -24,4 +75,3 @@ export * as Snip20 from './snip-20' export * as Snip24 from './snip-24' export * as Snip721 from './snip-721' export * as SecretJS from '@hackbg/secretjs-esm' -export { Connection as default, ScrtBatch as Batch } from './scrt-connection' diff --git a/scrt/tsconfig.json b/scrt/tsconfig.json index fb6a1dad45c..2db6923367b 100644 --- a/scrt/tsconfig.json +++ b/scrt/tsconfig.json @@ -4,6 +4,7 @@ "scrt-base.ts", "scrt-connection.ts", "scrt-identity.ts", + "scrt-faucets.ts", "scrt-mocknet.ts", "snip-20.ts", "snip-21.ts",