forked from crypto-org-chain/chain-jslib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c98f96
commit d19ee16
Showing
25 changed files
with
233 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,6 @@ coverage | |
|
||
# Generated docs file | ||
docs/dist/ | ||
|
||
# Yarn | ||
yarn.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from 'chai'; | ||
import { fuzzyDescribe } from '../test/mocha-fuzzy/suite'; | ||
|
||
import { CroNetwork, CroSDK } from '../core/cro'; | ||
|
||
const cro = CroSDK({ network: CroNetwork.Testnet }); | ||
|
||
describe('CroClient', function () { | ||
describe('connect', function () { | ||
fuzzyDescribe('should throw Error when the provided argument is not url', function (fuzzy) { | ||
const testRunner = fuzzy(fuzzy.StringArg('test')); | ||
testRunner( | ||
async function (args0) { | ||
if (args0.valid) { | ||
return; | ||
} | ||
try { | ||
await cro.CroClient.connect(args0.value); | ||
} catch (e) { | ||
expect(e.toString()).to.be.contain( | ||
'ArgumentError: Expected `endpoint` to be of type `string` but received type', | ||
); | ||
} | ||
}, | ||
{ invalidArgsOnly: true }, | ||
); | ||
}); | ||
|
||
context('When input is a string', function () { | ||
it('should throw Error when the provided string is not a url', async function () { | ||
try { | ||
await cro.CroClient.connect('invalid'); | ||
} catch (e) { | ||
expect(e.toString()).to.be.equals( | ||
'ArgumentError: Expected string `endpoint` to be an url, got `invalid`', | ||
); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { Tendermint34Client } from '@cosmjs/tendermint-rpc'; | ||
import { | ||
StargateClient, | ||
QueryClient, | ||
AuthExtension, | ||
BankExtension, | ||
DistributionExtension, | ||
StakingExtension, | ||
setupAuthExtension, | ||
setupBankExtension, | ||
setupDistributionExtension, | ||
setupStakingExtension, | ||
} from '@cosmjs/stargate'; | ||
import { Account } from '@cosmjs/stargate/build/accounts'; | ||
import { Coin } from '@cosmjs/stargate/build/codec/cosmos/base/v1beta1/coin'; | ||
import { SearchTxFilter, SearchTxQuery } from '@cosmjs/stargate/build/search'; | ||
import { Block, BroadcastTxResponse, IndexedTx, SequenceResponse } from '@cosmjs/stargate/build/stargateclient'; | ||
import ow from 'ow'; | ||
import { InitConfigurations } from '../core/cro'; | ||
import { owUrl } from './ow.types'; | ||
|
||
export interface ICroClient { | ||
query(): (QueryClient & AuthExtension & BankExtension & DistributionExtension & StakingExtension) | undefined; | ||
getChainId(): Promise<string>; | ||
getHeight(): Promise<number>; | ||
getAccount(searchAddress: string): Promise<Account | null>; | ||
getAccountVerified(searchAddress: string): Promise<Account | null>; | ||
getSequence(address: string): Promise<SequenceResponse>; | ||
getBlock(height?: number): Promise<Block>; | ||
getCroBalance(address: string): Promise<Coin>; | ||
getTx(id: string): Promise<IndexedTx | null>; | ||
searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise<readonly IndexedTx[]>; | ||
disconnect(): void; | ||
broadcastTx(tx: Uint8Array): Promise<BroadcastTxResponse>; | ||
} | ||
|
||
export const croClient = function (config: InitConfigurations) { | ||
return class CroClient implements ICroClient { | ||
tmClient: Tendermint34Client; | ||
|
||
baseDenom: string; | ||
|
||
readonly txClient: StargateClient; | ||
|
||
readonly queryClient: | ||
| (QueryClient & AuthExtension & BankExtension & DistributionExtension & StakingExtension) | ||
| undefined; | ||
|
||
private constructor(tmClient: Tendermint34Client, txClient: StargateClient) { | ||
this.txClient = txClient; | ||
this.tmClient = tmClient; | ||
this.queryClient = QueryClient.withExtensions( | ||
tmClient, | ||
setupAuthExtension, | ||
setupBankExtension, | ||
setupStakingExtension, | ||
setupDistributionExtension, | ||
); | ||
|
||
this.baseDenom = config.network.coin.baseDenom; | ||
} | ||
|
||
public static async connect(endpoint: string = config.network.rpcUrl): Promise<CroClient> { | ||
ow(endpoint, 'endpoint', owUrl); | ||
const tmClient = await Tendermint34Client.connect(endpoint); | ||
const txClient = await StargateClient.connect(endpoint); | ||
return new CroClient(tmClient, txClient); | ||
} | ||
|
||
public disconnect(): void { | ||
if (this.tmClient) this.tmClient.disconnect(); | ||
if (this.txClient) this.txClient.disconnect(); | ||
} | ||
|
||
public query(): | ||
| (QueryClient & AuthExtension & BankExtension & DistributionExtension & StakingExtension) | ||
| undefined { | ||
return this.queryClient; | ||
} | ||
|
||
public async getChainId(): Promise<string> { | ||
return this.txClient.getChainId(); | ||
} | ||
|
||
public async getHeight(): Promise<number> { | ||
return this.txClient.getHeight(); | ||
} | ||
|
||
public async getAccount(searchAddress: string): Promise<Account | null> { | ||
return this.txClient.getAccount(searchAddress); | ||
} | ||
|
||
public async getAccountVerified(searchAddress: string): Promise<Account | null> { | ||
return this.txClient.getAccountVerified(searchAddress); | ||
} | ||
|
||
public async getSequence(address: string): Promise<SequenceResponse> { | ||
return this.txClient.getSequence(address); | ||
} | ||
|
||
public async getBlock(height?: number): Promise<Block> { | ||
return this.txClient.getBlock(height); | ||
} | ||
|
||
public async getCroBalance(address: string): Promise<Coin> { | ||
return this.txClient.getBalance(address, this.baseDenom); | ||
} | ||
|
||
public async getTx(id: string): Promise<IndexedTx | null> { | ||
return this.txClient.getTx(id); | ||
} | ||
|
||
public async searchTx(query: SearchTxQuery, filter?: SearchTxFilter): Promise<readonly IndexedTx[]> { | ||
return this.txClient.searchTx(query, filter); | ||
} | ||
|
||
public async broadcastTx(tx: Uint8Array): Promise<BroadcastTxResponse> { | ||
return this.txClient.broadcastTx(tx); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import ow from 'ow'; | ||
|
||
export const owUrl = ow.string.validate((val) => ({ | ||
validator: val.startsWith('http://') || val.startsWith('https://'), | ||
message: (label) => `Expected ${label} to be an url, got \`${val}\``, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ export type Network = { | |
coinType: number; | ||
account: number; | ||
}; | ||
rpcUrl: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ export const owNetwork = () => | |
coinType: ow.number, | ||
account: ow.number, | ||
}), | ||
rpcUrl: ow.string, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ const cro = CroSDK({ | |
coinType: 1, | ||
account: 0, | ||
}, | ||
rpcUrl: '', | ||
}, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ const cro = CroSDK({ | |
coinType: 1, | ||
account: 0, | ||
}, | ||
rpcUrl: '', | ||
}, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ const cro = CroSDK({ | |
coinType: 1, | ||
account: 0, | ||
}, | ||
rpcUrl: '', | ||
}, | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ const cro = CroSDK({ | |
coinType: 1, | ||
account: 0, | ||
}, | ||
rpcUrl: '', | ||
}, | ||
}); | ||
|
||
|
Oops, something went wrong.