Skip to content

Commit

Permalink
Wasm message compatibility for Bombay/Columbus-5 (#108)
Browse files Browse the repository at this point in the history
* feat: wasm compatibility for columbus-5/bombay
* feat(client): 30s default timeout parameter for axios
* build: bump up version to 1.8.9
  • Loading branch information
hanjukim authored Jul 29, 2021
1 parent dd3e71c commit ab9d7cd
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@terra-money/terra.js",
"version": "1.8.8",
"version": "1.8.9",
"description": "The JavaScript SDK for Terra",
"license": "MIT",
"author": "Terraform Labs, PTE.",
Expand Down
1 change: 1 addition & 0 deletions src/client/lcd/APIRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export class APIRequester {
headers: {
Accept: 'application/json',
},
timeout: 30000,
});
}

Expand Down
8 changes: 8 additions & 0 deletions src/client/lcd/LCDClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Wallet } from './Wallet';
import { Numeric } from '../../core/numeric';
import { Coins } from '../../core/Coins';
import { Key } from '../../key';
import { setContractEncoding } from '../../util/contract';

export interface LCDClientConfig {
/**
Expand Down Expand Up @@ -114,6 +115,13 @@ export class LCDClient {
...config,
};

// TODO: Deprcate this after columbus-5/bombay
// Make wasm module compatible with bombay network. Because it is a global flag,
// it doesn't support columbus-4/tequila and columbus-5/bombay at the same time
setContractEncoding(
!/^(?:columbus-5|bombay|localterra)/.test(config.chainID)
);

this.apiRequester = new APIRequester(this.config.URL);

// instantiate APIs
Expand Down
3 changes: 2 additions & 1 deletion src/client/lcd/api/WasmAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseAPI } from './BaseAPI';
import { AccAddress } from '../../../core/bech32';
import { b64ToDict } from '../../../util/contract';

export interface CodeInfo {
code_hash: string;
Expand Down Expand Up @@ -52,7 +53,7 @@ export class WasmAPI extends BaseAPI {
code_id: Number.parseInt(d.code_id),
address: d.address,
owner: d.owner,
init_msg: JSON.parse(Buffer.from(d.init_msg, 'base64').toString()),
init_msg: b64ToDict(d.init_msg),
migratable: d.migratable,
}));
}
Expand Down
21 changes: 20 additions & 1 deletion src/util/contract.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { BlockTxBroadcastResult, isTxError } from '../client/lcd/api/TxAPI';
import { TxInfo } from '../core/TxInfo';

let useEncoding = true;

/**
* columbus-5/bombay doesn't encode msgs into base64 string.
* This function is for making main branch to be compatible with the change.
* Will be deprecated after bombay branch is merged into main branch
*/
export function setContractEncoding(flag: boolean) {
useEncoding = flag;
}

/**
* Serializes a JavaScript object to a Base64-encoded string. If the data passed is
* already a string, it will not be serialized and just return as-is.
Expand All @@ -9,6 +20,10 @@ import { TxInfo } from '../core/TxInfo';
* @returns base64-encoded string
*/
export function dictToB64(data: any): string {
if (!useEncoding) {
return data;
}

// if data is just a plain string, it was not valid Base64-encoded JSON so it could not be parsed
if (typeof data === 'string') {
return data;
Expand All @@ -24,7 +39,11 @@ export function dictToB64(data: any): string {
* @param data string
* @returns converted object
*/
export function b64ToDict(data: string): any {
export function b64ToDict(data: any): any {
if (!useEncoding) {
return data;
}

try {
return JSON.parse(Buffer.from(data, 'base64').toString());
} catch {
Expand Down

0 comments on commit ab9d7cd

Please sign in to comment.