Skip to content

Commit

Permalink
Release 4.1.0 (#11)
Browse files Browse the repository at this point in the history
* Issue 7: Erroneous "?" (#8)

* return empty string if no query parameters are provided

* Adding Binance integration. (#10)

* Fixed bug & added Binance support.
  • Loading branch information
cheeseandcereal authored and Wingman4l7 committed Nov 14, 2019
1 parent 7e36ff6 commit 327b2d6
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 9 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog

## 4.1.0

- **Feature:**

- Add support for new Binance interchain endpoints with client functions:
- `createBinanceInterchain`
- `updateBinanceInterchain`
- `signBinanceTransaction`

- **Bug Fix**
- Remove erroneous `?` from query string when no query string parameters are provided
- Fix typing bug with `getSmartContractObject`

## 4.0.0

Because Dragonchain has a breaking change to replace its indexing solution, this is also a breaking SDK change for queries and custom indexing.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dragonchain-sdk",
"version": "4.0.0",
"version": "4.1.0",
"description": "Dragonchain SDK for Node.JS and the Browser",
"license": "Apache-2.0",
"homepage": "https://github.com/dragonchain/dragonchain-sdk-javascript#readme",
Expand Down
32 changes: 29 additions & 3 deletions src/interfaces/DragonchainClientInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export interface PublicBlockchainTransactionResponse {
signed: string;
}

export type SupportedInterchains = 'ethereum' | 'bitcoin';
export type SupportedInterchains = 'ethereum' | 'bitcoin' | 'binance';

/**
* @example
Expand Down Expand Up @@ -255,7 +255,7 @@ export interface EthereumInterchainNetwork {
* "blockchain": "bitcoin",
* "name": "myBTCTestNetwork",
* "rpc_address": "http://1.2.3.4:18332",
* "tesnet": true,
* "testnet": true,
* "address": "n1bUzF6LRENLPaiJRFTcnGLMLsbZSquft1"
* }
* ```
Expand All @@ -269,6 +269,32 @@ export interface BitcoinInterchainNetwork {
address: string;
}

/**
* @example
* ```json
*
* {
* "version": "1",
* "blockchain": "binance",
* "name": "myBNBTestNetwork",
* "testnet": True,
* "node_url": "http://1.2.3.4",
* "rpc_port": 26657,
* "api_port": 11699,
* "address": "tbnb1zesqcktldshz7tat9u74duc037frzwvdq83wan"
* }
* ```
*/
export interface BinanceInterchainNetwork {
version: string;
blockchain: 'binance';
name: string;
node_url: string;
rpc_port: number;
api_port: number;
address: string;
}

/**
* Array of interchains will be all of one type
* @example
Expand All @@ -287,7 +313,7 @@ export interface BitcoinInterchainNetwork {
* ```
*/
export interface InterchainNetworkList {
interchains: BitcoinInterchainNetwork[] | EthereumInterchainNetwork[];
interchains: BitcoinInterchainNetwork[] | EthereumInterchainNetwork[] | BinanceInterchainNetwork[];
}

/**
Expand Down
57 changes: 55 additions & 2 deletions src/services/dragonchain-client/DragonchainClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,11 @@ describe('DragonchainClient', () => {
await client.getSmartContractLogs({ smartContractId: 'test', tail: 100, since: 'a-date' });
assert.calledWith(fetch, `fakeUrl/v1/contract/test/logs?tail=100&since=a-date`, expectedFetchOptions);
});

it('calls #fetch() with correct params and missing erroneous ?', async () => {
await client.getSmartContractLogs({ smartContractId: 'test' });
assert.calledWith(fetch, `fakeUrl/v1/contract/test/logs`, expectedFetchOptions);
});
});

describe('.getInterchainNetwork', () => {
Expand Down Expand Up @@ -420,6 +425,23 @@ describe('DragonchainClient', () => {
});
});

describe('.createBinanceInterchain', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
version: '1',
name: 'banana',
testnet: true,
private_key: 'abcd',
node_url: 'some IP',
rpc_port: 1234,
api_port: 5678
};
await client.createBinanceInterchain({ name: 'banana', testnet: true, privateKey: 'abcd', nodeURL: 'some IP', rpcPort: 1234, apiPort: 5678 });
const obj = { ...expectedFetchOptions, body: JSON.stringify(fakeBody) };
assert.calledWith(fetch, 'fakeUrl/v1/interchains/binance', obj);
});
});

describe('.signBitcoinTransaction', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
Expand Down Expand Up @@ -452,6 +474,21 @@ describe('DragonchainClient', () => {
});
});

describe('.signBinanceTransaction', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
version: '1',
amount: 123,
to_address: 'receiver addy',
symbol: 'TOKEN',
memo: 'for bananas'
};
await client.signBinanceTransaction({ name: 'banana', amount: 123, toAddress: 'receiver addy', symbol: 'TOKEN', memo: 'for bananas' });
const obj = { ...expectedFetchOptions, body: JSON.stringify(fakeBody) };
assert.calledWith(fetch, 'fakeUrl/v1/interchains/binance/banana/transaction', obj);
});
});

describe('.setDefaultInterchainNetwork', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
Expand Down Expand Up @@ -541,14 +578,30 @@ describe('DragonchainClient', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
version: '1',
private_key: 'private key',
private_key: 'abcd',
rpc_address: 'some rpc',
chain_id: 12
};
await client.updateEthereumInterchain({ name: 'banana', privateKey: 'private key', rpcAddress: 'some rpc', chainId: 12 });
await client.updateEthereumInterchain({ name: 'banana', privateKey: 'abcd', rpcAddress: 'some rpc', chainId: 12 });
const obj = { ...expectedFetchOptions, body: JSON.stringify(fakeBody) };
assert.calledWith(fetch, 'fakeUrl/v1/interchains/ethereum/banana', obj);
});
});

describe('.updateBinanceInterchain', () => {
it('calls #fetch() with correct params', async () => {
const fakeBody: any = {
version: '1',
testnet: true,
private_key: 'abcd',
node_url: 'some IP',
rpc_port: 1234,
api_port: 5678
};
await client.updateBinanceInterchain({ name: 'banana', testnet: true, privateKey: 'abcd', nodeURL: 'some IP', rpcPort: 1234, apiPort: 5678 });
const obj = { ...expectedFetchOptions, body: JSON.stringify(fakeBody) };
assert.calledWith(fetch, 'fakeUrl/v1/interchains/binance/banana', obj);
});
});
});
});
127 changes: 124 additions & 3 deletions src/services/dragonchain-client/DragonchainClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
DeleteAPIKeyResponse,
EthereumInterchainNetwork,
BitcoinInterchainNetwork,
BinanceInterchainNetwork,
SupportedInterchains,
InterchainNetworkList,
CustomTextFieldOptions,
Expand Down Expand Up @@ -748,8 +749,7 @@ export class DragonchainClient {
if (!process.env.SMART_CONTRACT_ID) throw new FailureByDesign('PARAM_ERROR', 'Parameter `smartContractId` is required when not running within a smart contract');
options.smartContractId = process.env.SMART_CONTRACT_ID;
}
const response = (await this.get(`/v1/get/${options.smartContractId}/${options.key}`, false)) as unknown;
return response as string;
return (await this.get(`/v1/get/${options.smartContractId}/${options.key}`, false)) as Response<string>;
};

/**
Expand Down Expand Up @@ -1076,6 +1076,127 @@ export class DragonchainClient {
return (await this.post(`/v1/interchains/ethereum/${options.name}/transaction`, body)) as Response<PublicBlockchainTransactionResponse>;
};

/**
* Create (or overwrite) a binance wallet/network for interchain use
*/

public createBinanceInterchain = async (options: {
/**
* The name of the network to update
*/
name: string;
/**
* Whether or not this is a testnet wallet/address. Defaults to True.
*/
testnet?: boolean;
/**
* The base64 or hex encoded private key (or mnemonic) to use. Will automatically generate a random one if not provided
*/
privateKey?: string;
/**
* The endpoint of the binance node to use (i.e. http://my.node.address)
*/
nodeURL?: string;
/**
* The port being used to hit the RPC endpoints (i.e. 27147)
*/
rpcPort?: number;
/**
* The port being used to hit the API endpoints (i.e. 1169)
*/
apiPort?: number;
}) => {
if (!options.name) throw new FailureByDesign('PARAM_ERROR', 'Parameter `name` is required');
if (options.rpcPort && !Number.isInteger(options.rpcPort)) throw new FailureByDesign('PARAM_ERROR', 'Parameter `rpcPort` must be an integer');
if (options.apiPort && !Number.isInteger(options.apiPort)) throw new FailureByDesign('PARAM_ERROR', 'Parameter `apiPort` must be an integer');
const body: any = { version: '1', name: options.name };
if (typeof options.testnet === 'boolean') body.testnet = options.testnet;
if (options.privateKey) body.private_key = options.privateKey;
if (options.nodeURL) body.node_url = options.nodeURL;
if (options.rpcPort) body.rpc_port = options.rpcPort;
if (options.rpcPort) body.api_port = options.apiPort;
return (await this.post(`/v1/interchains/binance`, body)) as Response<BinanceInterchainNetwork>;
};

/**
* Update an existing binance wallet/network for interchain use
*/
public updateBinanceInterchain = async (options: {
/**
* The name of the network to update
*/
name: string;
/**
* Whether or not this is a testnet wallet/address. Defaults to True.
*/
testnet?: boolean;
/**
* The base64 or hex encoded private key to use. Will automatically generate a random one if not provided
*/
privateKey?: string;
/**
* The endpoint of the binance node to use (i.e. http://my.node.address)
*/
nodeURL?: string;
/**
* The port being used to hit the RPC endpoints (i.e. 27147)
*/
rpcPort?: number;
/**
* The port being used to hit the API endpoints (i.e. 1169)
*/
apiPort?: number;
}) => {
if (!options.name) throw new FailureByDesign('PARAM_ERROR', 'Parameter `name` is required');
if (options.rpcPort && !Number.isInteger(options.rpcPort)) throw new FailureByDesign('PARAM_ERROR', 'Parameter `rpcPort` must be an integer');
if (options.apiPort && !Number.isInteger(options.apiPort)) throw new FailureByDesign('PARAM_ERROR', 'Parameter `apiPort` must be an integer');
const body: any = { version: '1' };
if (typeof options.testnet === 'boolean') body.testnet = options.testnet;
if (options.privateKey) body.private_key = options.privateKey;
if (options.nodeURL) body.node_url = options.nodeURL;
if (options.rpcPort) body.rpc_port = options.rpcPort;
if (options.apiPort) body.api_port = options.apiPort;
return (await this.patch(`/v1/interchains/binance/${options.name}`, body)) as Response<BinanceInterchainNetwork>;
};

/**
* Create and sign a binance transaction using your chain's interchain network
*/
public signBinanceTransaction = async (options: {
/**
* The name of the binance network to use for signing
*/
name: string;
/**
* the amount of token to send with this transaction
*/
amount: number;
/**
* The (hex-encoded) address to send the transaction to
*/
toAddress: string;
/**
* the exchange symbol for the token (defaults to BNB)
*/
symbol?: string;
/**
* string of data to publish in the transaction (defaults to "")
*/
memo?: string;
}) => {
if (!options.name) throw new FailureByDesign('PARAM_ERROR', 'Parameter `name` is required');
if (!options.amount) throw new FailureByDesign('PARAM_ERROR', 'Parameter `amount` is required');
if (!options.toAddress) throw new FailureByDesign('PARAM_ERROR', 'Parameter `toAddress` is required');
const body: any = {
version: '1',
amount: options.amount,
to_address: options.toAddress
};
if (options.symbol) body.symbol = options.symbol;
if (options.memo) body.memo = options.memo;
return (await this.post(`/v1/interchains/binance/${options.name}/transaction`, body)) as Response<PublicBlockchainTransactionResponse>;
};

/**
* Get a configured interchain network/wallet from the chain
*/
Expand Down Expand Up @@ -1347,7 +1468,7 @@ export class DragonchainClient {
/**
* @hidden
*/
private generateQueryString = (queryObject: Map<string, string | number>) => `?${UrlSearchParams(queryObject)}`;
private generateQueryString = (queryObject: Map<string, string | number>) => (Object.keys(queryObject).length > 0 ? `?${UrlSearchParams(queryObject)}` : '');

/**
* @hidden
Expand Down

0 comments on commit 327b2d6

Please sign in to comment.