Extract RestClient, TxsResponse, AminoTx into @cosmwasm/sdk

This commit is contained in:
Simon Warta 2020-01-31 17:50:10 +01:00
parent 7b25a2e11c
commit 1bf2d9b287
25 changed files with 216 additions and 19 deletions

View File

@ -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"

View File

@ -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 {

View File

@ -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;

View File

@ -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;

View File

@ -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 (

View File

@ -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;

View File

@ -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;

View File

@ -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;

1
packages/sdk/.eslintignore Symbolic link
View File

@ -0,0 +1 @@
../../.eslintignore

3
packages/sdk/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build/
dist/
docs/

View File

@ -0,0 +1,12 @@
{
"suite": {
"displayNumber": true
},
"spec": {
"displayDuration": true
},
"summary": {
"displayPending": false,
"displayStacktrace": true
}
}

View File

@ -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();

View File

@ -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"],
},
},
});
};

View File

@ -0,0 +1 @@
Directory used to trigger lerna package updates for all packages

46
packages/sdk/package.json Normal file
View File

@ -0,0 +1,46 @@
{
"name": "@cosmwasm/sdk",
"version": "0.0.1",
"description": "CosmWasm SDK",
"author": "Ethan Frey <ethanfrey@users.noreply.github.com>",
"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": {
}
}

View File

@ -0,0 +1,2 @@
export { RestClient, TxsResponse } from "./restclient";
export { AminoTx } from "./types";

View File

@ -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<AuthAccountsResponse> {
public async authAccounts(address: string, height?: string): Promise<AuthAccountsResponse> {
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<TxsResponse> {
public async txsById(id: string): Promise<TxsResponse> {
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<PostTxsResponse> {
public async postTx(tx: Uint8Array): Promise<PostTxsResponse> {
const unmarshalled = unmarshalTx(tx, true);
const params = {
tx: unmarshalled.value,

View File

@ -0,0 +1,3 @@
import amino from "@tendermint/amino-js";
export type AminoTx = amino.Tx & { readonly value: amino.StdTx };

View File

@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "build",
"declarationDir": "build/types",
"rootDir": "src"
},
"include": [
"src/**/*"
]
}

3
packages/sdk/tslint.json Normal file
View File

@ -0,0 +1,3 @@
{
"extends": "../../tslint.json"
}

14
packages/sdk/typedoc.js Normal file
View File

@ -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,
};

2
packages/sdk/types/index.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
export { RestClient, TxsResponse } from "./restclient";
export { AminoTx } from "./types";

View File

@ -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<NodeInfoResponse>;
blocksLatest(): Promise<BlocksResponse>;
blocks(height: number): Promise<BlocksResponse>;
authAccounts(address: Address, height?: string): Promise<AuthAccountsResponse>;
authAccounts(address: string, height?: string): Promise<AuthAccountsResponse>;
txs(query: string): Promise<SearchTxsResponse>;
txsById(id: TransactionId): Promise<TxsResponse>;
postTx(tx: PostableBytes): Promise<PostTxsResponse>;
txsById(id: string): Promise<TxsResponse>;
postTx(tx: Uint8Array): Promise<PostTxsResponse>;
}
export {};

4
packages/sdk/types/types.d.ts vendored Normal file
View File

@ -0,0 +1,4 @@
import amino from "@tendermint/amino-js";
export declare type AminoTx = amino.Tx & {
readonly value: amino.StdTx;
};

View File

@ -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"])],
},
];