From 1bf2d9b287d7d83bbd01198172619052f23e2414 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Fri, 31 Jan 2020 17:50:10 +0100 Subject: [PATCH] Extract RestClient, TxsResponse, AminoTx into @cosmwasm/sdk --- packages/bcp/package.json | 2 +- packages/bcp/src/cosmwasmconnection.ts | 2 +- packages/bcp/src/decode.ts | 2 +- packages/bcp/src/encode.ts | 3 +- packages/bcp/src/types.ts | 2 - packages/bcp/types/decode.d.ts | 2 +- packages/bcp/types/encode.d.ts | 3 +- packages/bcp/types/types.d.ts | 3 -- packages/sdk/.eslintignore | 1 + packages/sdk/.gitignore | 3 ++ .../sdk/jasmine-spec-reporter.config.json | 12 +++++ packages/sdk/jasmine-testrunner.js | 26 +++++++++ packages/sdk/karma.conf.js | 54 +++++++++++++++++++ packages/sdk/nonces/README.txt | 1 + packages/sdk/package.json | 46 ++++++++++++++++ packages/sdk/src/index.ts | 2 + packages/{bcp => sdk}/src/restclient.ts | 7 ++- packages/sdk/src/types.ts | 3 ++ packages/sdk/tsconfig.json | 12 +++++ packages/sdk/tslint.json | 3 ++ packages/sdk/typedoc.js | 14 +++++ packages/sdk/types/index.d.ts | 2 + packages/{bcp => sdk}/types/restclient.d.ts | 7 ++- packages/sdk/types/types.d.ts | 4 ++ packages/sdk/webpack.web.config.js | 19 +++++++ 25 files changed, 216 insertions(+), 19 deletions(-) create mode 120000 packages/sdk/.eslintignore create mode 100644 packages/sdk/.gitignore create mode 100644 packages/sdk/jasmine-spec-reporter.config.json create mode 100755 packages/sdk/jasmine-testrunner.js create mode 100644 packages/sdk/karma.conf.js create mode 100644 packages/sdk/nonces/README.txt create mode 100644 packages/sdk/package.json create mode 100644 packages/sdk/src/index.ts rename packages/{bcp => sdk}/src/restclient.ts (93%) create mode 100644 packages/sdk/src/types.ts create mode 100644 packages/sdk/tsconfig.json create mode 100644 packages/sdk/tslint.json create mode 100644 packages/sdk/typedoc.js create mode 100644 packages/sdk/types/index.d.ts rename packages/{bcp => sdk}/types/restclient.d.ts (87%) create mode 100644 packages/sdk/types/types.d.ts create mode 100644 packages/sdk/webpack.web.config.js diff --git a/packages/bcp/package.json b/packages/bcp/package.json index a21e6deb..ae98612d 100644 --- a/packages/bcp/package.json +++ b/packages/bcp/package.json @@ -38,12 +38,12 @@ "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" }, "dependencies": { + "@cosmwasm/sdk": "^0.0.1", "@iov/bcp": "^2.0.0-alpha.7", "@iov/crypto": "^2.0.0-alpha.7", "@iov/encoding": "^2.0.0-alpha.7", "@iov/stream": "^2.0.0-alpha.7", "@tendermint/amino-js": "^0.7.0-alpha.1", - "axios": "^0.19.0", "fast-deep-equal": "^3.1.1", "readonly-date": "^1.0.0", "xstream": "^11.11.0" diff --git a/packages/bcp/src/cosmwasmconnection.ts b/packages/bcp/src/cosmwasmconnection.ts index 839e21d2..05d0c20a 100644 --- a/packages/bcp/src/cosmwasmconnection.ts +++ b/packages/bcp/src/cosmwasmconnection.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ +import { RestClient, TxsResponse } from "@cosmwasm/sdk"; import { Account, AccountQuery, @@ -37,7 +38,6 @@ import { Stream } from "xstream"; import { CosmosBech32Prefix, decodeCosmosPubkey, pubkeyToAddress } from "./address"; import { Caip5 } from "./caip5"; import { decodeAmount, parseTxsResponse } from "./decode"; -import { RestClient, TxsResponse } from "./restclient"; import { accountToNonce, TokenInfos } from "./types"; interface ChainData { diff --git a/packages/bcp/src/decode.ts b/packages/bcp/src/decode.ts index 4e2f57f3..72eaf9cf 100644 --- a/packages/bcp/src/decode.ts +++ b/packages/bcp/src/decode.ts @@ -1,3 +1,4 @@ +import { TxsResponse } from "@cosmwasm/sdk"; import { Address, Algorithm, @@ -18,7 +19,6 @@ import { import { Encoding } from "@iov/encoding"; import amino from "@tendermint/amino-js"; -import { TxsResponse } from "./restclient"; import { coinToAmount, isAminoStdTx, TokenInfos } from "./types"; const { fromBase64 } = Encoding; diff --git a/packages/bcp/src/encode.ts b/packages/bcp/src/encode.ts index 42f2624f..28843930 100644 --- a/packages/bcp/src/encode.ts +++ b/packages/bcp/src/encode.ts @@ -1,4 +1,5 @@ /* eslint-disable @typescript-eslint/camelcase */ +import { AminoTx } from "@cosmwasm/sdk"; import { Algorithm, Amount, @@ -13,7 +14,7 @@ import { Secp256k1 } from "@iov/crypto"; import { Encoding } from "@iov/encoding"; import amino from "@tendermint/amino-js"; -import { AminoTx, amountToCoin, TokenInfos } from "./types"; +import { amountToCoin, TokenInfos } from "./types"; const { toBase64 } = Encoding; diff --git a/packages/bcp/src/types.ts b/packages/bcp/src/types.ts index e929d61b..8a77aa71 100644 --- a/packages/bcp/src/types.ts +++ b/packages/bcp/src/types.ts @@ -1,8 +1,6 @@ import { Amount, Nonce, Token } from "@iov/bcp"; import amino from "@tendermint/amino-js"; -export type AminoTx = amino.Tx & { readonly value: amino.StdTx }; - export function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx { const { memo, msg, fee, signatures } = txValue as amino.StdTx; return ( diff --git a/packages/bcp/types/decode.d.ts b/packages/bcp/types/decode.d.ts index 8b7332c7..7c4e96f8 100644 --- a/packages/bcp/types/decode.d.ts +++ b/packages/bcp/types/decode.d.ts @@ -1,3 +1,4 @@ +import { TxsResponse } from "@cosmwasm/sdk"; import { Amount, ChainId, @@ -12,7 +13,6 @@ import { UnsignedTransaction, } from "@iov/bcp"; import amino from "@tendermint/amino-js"; -import { TxsResponse } from "./restclient"; import { TokenInfos } from "./types"; export declare function decodePubkey(pubkey: amino.PubKey): PubkeyBundle; export declare function decodeSignature(signature: string): SignatureBytes; diff --git a/packages/bcp/types/encode.d.ts b/packages/bcp/types/encode.d.ts index 2a0788df..1068443a 100644 --- a/packages/bcp/types/encode.d.ts +++ b/packages/bcp/types/encode.d.ts @@ -1,6 +1,7 @@ +import { AminoTx } from "@cosmwasm/sdk"; import { Amount, Fee, FullSignature, PubkeyBundle, SignedTransaction, UnsignedTransaction } from "@iov/bcp"; import amino from "@tendermint/amino-js"; -import { AminoTx, TokenInfos } from "./types"; +import { TokenInfos } from "./types"; export declare function encodePubkey(pubkey: PubkeyBundle): amino.PubKey; export declare function encodeAmount(amount: Amount, tokens: TokenInfos): amino.Coin; export declare function encodeFee(fee: Fee, tokens: TokenInfos): amino.StdFee; diff --git a/packages/bcp/types/types.d.ts b/packages/bcp/types/types.d.ts index 259be466..49106266 100644 --- a/packages/bcp/types/types.d.ts +++ b/packages/bcp/types/types.d.ts @@ -1,8 +1,5 @@ import { Amount, Nonce, Token } from "@iov/bcp"; import amino from "@tendermint/amino-js"; -export declare type AminoTx = amino.Tx & { - readonly value: amino.StdTx; -}; export declare function isAminoStdTx(txValue: amino.TxValue): txValue is amino.StdTx; export interface TokenInfo extends Token { readonly denom: string; diff --git a/packages/sdk/.eslintignore b/packages/sdk/.eslintignore new file mode 120000 index 00000000..86039baf --- /dev/null +++ b/packages/sdk/.eslintignore @@ -0,0 +1 @@ +../../.eslintignore \ No newline at end of file diff --git a/packages/sdk/.gitignore b/packages/sdk/.gitignore new file mode 100644 index 00000000..68bf3735 --- /dev/null +++ b/packages/sdk/.gitignore @@ -0,0 +1,3 @@ +build/ +dist/ +docs/ diff --git a/packages/sdk/jasmine-spec-reporter.config.json b/packages/sdk/jasmine-spec-reporter.config.json new file mode 100644 index 00000000..3a997556 --- /dev/null +++ b/packages/sdk/jasmine-spec-reporter.config.json @@ -0,0 +1,12 @@ +{ + "suite": { + "displayNumber": true + }, + "spec": { + "displayDuration": true + }, + "summary": { + "displayPending": false, + "displayStacktrace": true + } +} diff --git a/packages/sdk/jasmine-testrunner.js b/packages/sdk/jasmine-testrunner.js new file mode 100755 index 00000000..55b2df29 --- /dev/null +++ b/packages/sdk/jasmine-testrunner.js @@ -0,0 +1,26 @@ +#!/usr/bin/env node + +require("source-map-support").install(); +const defaultSpecReporterConfig = require("./jasmine-spec-reporter.config.json"); + +// setup Jasmine +const Jasmine = require("jasmine"); +const jasmine = new Jasmine(); +jasmine.loadConfig({ + spec_dir: "build", + spec_files: ["**/*.spec.js"], + helpers: [], + random: false, + seed: null, + stopSpecOnExpectationFailure: false, +}); +jasmine.jasmine.DEFAULT_TIMEOUT_INTERVAL = 15 * 1000; + +// setup reporter +const { SpecReporter } = require("jasmine-spec-reporter"); +const reporter = new SpecReporter({ ...defaultSpecReporterConfig }); + +// initialize and execute +jasmine.env.clearReporters(); +jasmine.addReporter(reporter); +jasmine.execute(); diff --git a/packages/sdk/karma.conf.js b/packages/sdk/karma.conf.js new file mode 100644 index 00000000..ff71fce8 --- /dev/null +++ b/packages/sdk/karma.conf.js @@ -0,0 +1,54 @@ +module.exports = function(config) { + config.set({ + // base path that will be used to resolve all patterns (eg. files, exclude) + basePath: ".", + + // frameworks to use + // available frameworks: https://npmjs.org/browse/keyword/karma-adapter + frameworks: ["jasmine"], + + // list of files / patterns to load in the browser + files: ["dist/web/tests.js"], + + client: { + jasmine: { + random: false, + timeoutInterval: 15000, + }, + }, + + // test results reporter to use + // possible values: 'dots', 'progress' + // available reporters: https://npmjs.org/browse/keyword/karma-reporter + reporters: ["progress", "kjhtml"], + + // web server port + port: 9876, + + // enable / disable colors in the output (reporters and logs) + colors: true, + + // level of logging + // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG + logLevel: config.LOG_INFO, + + // enable / disable watching file and executing tests whenever any file changes + autoWatch: false, + + // start these browsers + // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher + browsers: ["Firefox"], + + browserNoActivityTimeout: 90000, + + // Keep brower open for debugging. This is overridden by yarn scripts + singleRun: false, + + customLaunchers: { + ChromeHeadlessInsecure: { + base: "ChromeHeadless", + flags: ["--disable-web-security"], + }, + }, + }); +}; diff --git a/packages/sdk/nonces/README.txt b/packages/sdk/nonces/README.txt new file mode 100644 index 00000000..092fe732 --- /dev/null +++ b/packages/sdk/nonces/README.txt @@ -0,0 +1 @@ +Directory used to trigger lerna package updates for all packages diff --git a/packages/sdk/package.json b/packages/sdk/package.json new file mode 100644 index 00000000..d69de11a --- /dev/null +++ b/packages/sdk/package.json @@ -0,0 +1,46 @@ +{ + "name": "@cosmwasm/sdk", + "version": "0.0.1", + "description": "CosmWasm SDK", + "author": "Ethan Frey ", + "license": "Apache-2.0", + "main": "build/index.js", + "types": "types/index.d.ts", + "files": [ + "build/", + "types/", + "*.md", + "!*.spec.*", + "!**/testdata/" + ], + "repository": { + "type": "git", + "url": "https://github.com/confio/cosm-js/tree/master/packages/sdk" + }, + "publishConfig": { + "access": "public" + }, + "scripts": { + "docs": "shx rm -rf docs && typedoc --options typedoc.js", + "format": "prettier --write --loglevel warn \"./src/**/*.ts\"", + "lint": "eslint --max-warnings 0 \"**/*.{js,ts}\" && tslint -t verbose --project .", + "lint-fix": "eslint --max-warnings 0 \"**/*.{js,ts}\" --fix", + "move-types": "shx rm -rf ./types/* && shx mv build/types/* ./types && rm -rf ./types/testdata && shx rm -f ./types/*.spec.d.ts", + "format-types": "prettier --write --loglevel warn \"./types/**/*.d.ts\"", + "build": "shx rm -rf ./build && tsc && yarn move-types && yarn format-types", + "build-or-skip": "[ -n \"$SKIP_BUILD\" ] || yarn build", + "test-node": "node jasmine-testrunner.js", + "test-edge": "yarn pack-web && karma start --single-run --browsers Edge", + "test-firefox": "yarn pack-web && karma start --single-run --browsers Firefox", + "test-chrome": "yarn pack-web && karma start --single-run --browsers ChromeHeadlessInsecure", + "test-safari": "yarn pack-web && karma start --single-run --browsers Safari", + "test": "yarn build-or-skip && yarn test-node", + "pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js" + }, + "dependencies": { + "@tendermint/amino-js": "^0.7.0-alpha.1", + "axios": "^0.19.0" + }, + "devDependencies": { + } +} diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts new file mode 100644 index 00000000..c5976e02 --- /dev/null +++ b/packages/sdk/src/index.ts @@ -0,0 +1,2 @@ +export { RestClient, TxsResponse } from "./restclient"; +export { AminoTx } from "./types"; diff --git a/packages/bcp/src/restclient.ts b/packages/sdk/src/restclient.ts similarity index 93% rename from packages/bcp/src/restclient.ts rename to packages/sdk/src/restclient.ts index 8ef49e48..3bd74f70 100644 --- a/packages/bcp/src/restclient.ts +++ b/packages/sdk/src/restclient.ts @@ -1,4 +1,3 @@ -import { Address, PostableBytes, TransactionId } from "@iov/bcp"; import amino, { unmarshalTx } from "@tendermint/amino-js"; import axios, { AxiosInstance } from "axios"; @@ -132,7 +131,7 @@ export class RestClient { return responseData as BlocksResponse; } - public async authAccounts(address: Address, height?: string): Promise { + public async authAccounts(address: string, height?: string): Promise { const path = height === undefined ? `/auth/accounts/${address}` : `/auth/accounts/${address}?tx.height=${height}`; const responseData = await this.get(path); @@ -150,7 +149,7 @@ export class RestClient { return responseData as SearchTxsResponse; } - public async txsById(id: TransactionId): Promise { + public async txsById(id: string): Promise { const responseData = await this.get(`/txs/${id}`); if (!(responseData as any).tx) { throw new Error("Unexpected response data format"); @@ -158,7 +157,7 @@ export class RestClient { return responseData as TxsResponse; } - public async postTx(tx: PostableBytes): Promise { + public async postTx(tx: Uint8Array): Promise { const unmarshalled = unmarshalTx(tx, true); const params = { tx: unmarshalled.value, diff --git a/packages/sdk/src/types.ts b/packages/sdk/src/types.ts new file mode 100644 index 00000000..7b5d6599 --- /dev/null +++ b/packages/sdk/src/types.ts @@ -0,0 +1,3 @@ +import amino from "@tendermint/amino-js"; + +export type AminoTx = amino.Tx & { readonly value: amino.StdTx }; diff --git a/packages/sdk/tsconfig.json b/packages/sdk/tsconfig.json new file mode 100644 index 00000000..167e8c02 --- /dev/null +++ b/packages/sdk/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "build", + "declarationDir": "build/types", + "rootDir": "src" + }, + "include": [ + "src/**/*" + ] +} diff --git a/packages/sdk/tslint.json b/packages/sdk/tslint.json new file mode 100644 index 00000000..0946f209 --- /dev/null +++ b/packages/sdk/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "../../tslint.json" +} diff --git a/packages/sdk/typedoc.js b/packages/sdk/typedoc.js new file mode 100644 index 00000000..e2387c7d --- /dev/null +++ b/packages/sdk/typedoc.js @@ -0,0 +1,14 @@ +const packageJson = require("./package.json"); + +module.exports = { + src: ["./src"], + out: "docs", + exclude: "**/*.spec.ts", + target: "es6", + name: `${packageJson.name} Documentation`, + readme: "README.md", + mode: "file", + excludeExternals: true, + excludeNotExported: true, + excludePrivate: true, +}; diff --git a/packages/sdk/types/index.d.ts b/packages/sdk/types/index.d.ts new file mode 100644 index 00000000..c5976e02 --- /dev/null +++ b/packages/sdk/types/index.d.ts @@ -0,0 +1,2 @@ +export { RestClient, TxsResponse } from "./restclient"; +export { AminoTx } from "./types"; diff --git a/packages/bcp/types/restclient.d.ts b/packages/sdk/types/restclient.d.ts similarity index 87% rename from packages/bcp/types/restclient.d.ts rename to packages/sdk/types/restclient.d.ts index df1d9251..ace2f82b 100644 --- a/packages/bcp/types/restclient.d.ts +++ b/packages/sdk/types/restclient.d.ts @@ -1,4 +1,3 @@ -import { Address, PostableBytes, TransactionId } from "@iov/bcp"; import amino from "@tendermint/amino-js"; import { AminoTx } from "./types"; interface NodeInfo { @@ -69,9 +68,9 @@ export declare class RestClient { nodeInfo(): Promise; blocksLatest(): Promise; blocks(height: number): Promise; - authAccounts(address: Address, height?: string): Promise; + authAccounts(address: string, height?: string): Promise; txs(query: string): Promise; - txsById(id: TransactionId): Promise; - postTx(tx: PostableBytes): Promise; + txsById(id: string): Promise; + postTx(tx: Uint8Array): Promise; } export {}; diff --git a/packages/sdk/types/types.d.ts b/packages/sdk/types/types.d.ts new file mode 100644 index 00000000..e0c11766 --- /dev/null +++ b/packages/sdk/types/types.d.ts @@ -0,0 +1,4 @@ +import amino from "@tendermint/amino-js"; +export declare type AminoTx = amino.Tx & { + readonly value: amino.StdTx; +}; diff --git a/packages/sdk/webpack.web.config.js b/packages/sdk/webpack.web.config.js new file mode 100644 index 00000000..a26d8e41 --- /dev/null +++ b/packages/sdk/webpack.web.config.js @@ -0,0 +1,19 @@ +const glob = require("glob"); +const path = require("path"); +const webpack = require("webpack"); + +const target = "web"; +const distdir = path.join(__dirname, "dist", "web"); + +module.exports = [ + { + // bundle used for Karma tests + target: target, + entry: glob.sync("./build/**/*.spec.js"), + output: { + path: distdir, + filename: "tests.js", + }, + plugins: [new webpack.EnvironmentPlugin(["COSMOS_ENABLED"])], + }, +];