forked from cerc-io/registry-sdk
Update transaction response parsing (#4)
Part of https://www.notion.so/Create-laconic-registry-SDK-d3a636d4aba44f7cbba3bd99b7146811 - Handle errors in the tx response - Move response parsing to client class - Run previously skipped naming tests Co-authored-by: neeraj <neeraj.rtly@gmail.com> Reviewed-on: deep-stack/registry-sdk#4 Co-authored-by: Prathamesh Musale <prathamesh@noreply.git.vdb.to> Co-committed-by: Prathamesh Musale <prathamesh@noreply.git.vdb.to>
This commit is contained in:
parent
8d3c35b22e
commit
7fe256e714
@ -6,7 +6,7 @@ import { DENOM } from './constants';
|
|||||||
|
|
||||||
const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml');
|
const WATCHER_YML_PATH = path.join(__dirname, './testing/data/watcher.yml');
|
||||||
|
|
||||||
const BOND_AMOUNT = '10000';
|
const BOND_AMOUNT = '1000000000';
|
||||||
const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
const { chainId, restEndpoint, gqlEndpoint, privateKey, fee } = getConfig();
|
||||||
|
|
||||||
jest.setTimeout(90 * 1000);
|
jest.setTimeout(90 * 1000);
|
||||||
@ -99,7 +99,7 @@ const bondTests = () => {
|
|||||||
|
|
||||||
bondId1 = await registry.getNextBondId(privateKey);
|
bondId1 = await registry.getNextBondId(privateKey);
|
||||||
expect(bondId1).toBeDefined();
|
expect(bondId1).toBeDefined();
|
||||||
await registry.createBond({ denom: DENOM, amount: '1000000000' }, privateKey, fee);
|
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
|
||||||
|
|
||||||
// Create a new record.
|
// Create a new record.
|
||||||
let watcher = await publishNewWatcherVersion(bondId1);
|
let watcher = await publishNewWatcherVersion(bondId1);
|
||||||
@ -124,7 +124,7 @@ const bondTests = () => {
|
|||||||
|
|
||||||
bondId1 = await registry.getNextBondId(privateKey);
|
bondId1 = await registry.getNextBondId(privateKey);
|
||||||
expect(bondId1).toBeDefined();
|
expect(bondId1).toBeDefined();
|
||||||
await registry.createBond({ denom: DENOM, amount: '1000000000' }, privateKey, fee);
|
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
|
||||||
|
|
||||||
// Create a new record version.
|
// Create a new record version.
|
||||||
let watcher = await publishNewWatcherVersion(bondId1);
|
let watcher = await publishNewWatcherVersion(bondId1);
|
||||||
@ -141,7 +141,7 @@ const bondTests = () => {
|
|||||||
// Create another bond.
|
// Create another bond.
|
||||||
bondId2 = await registry.getNextBondId(privateKey);
|
bondId2 = await registry.getNextBondId(privateKey);
|
||||||
expect(bondId2).toBeDefined();
|
expect(bondId2).toBeDefined();
|
||||||
await registry.createBond({ denom: DENOM, amount: '1000000000' }, privateKey, fee);
|
await registry.createBond({ denom: DENOM, amount: BOND_AMOUNT }, privateKey, fee);
|
||||||
const [bond] = await registry.getBondsByIds([bondId2]);
|
const [bond] = await registry.getBondsByIds([bondId2]);
|
||||||
expect(bond.id).toBe(bondId2);
|
expect(bond.id).toBe(bondId2);
|
||||||
|
|
||||||
|
109
src/index.ts
109
src/index.ts
@ -42,8 +42,6 @@ import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
|||||||
|
|
||||||
export const DEFAULT_CHAIN_ID = 'laconic_9000-1';
|
export const DEFAULT_CHAIN_ID = 'laconic_9000-1';
|
||||||
|
|
||||||
const DEFAULT_WRITE_ERROR = 'Unable to write to laconicd.';
|
|
||||||
|
|
||||||
// Parse Tx response from cosmos-sdk.
|
// Parse Tx response from cosmos-sdk.
|
||||||
export const parseTxResponse = (result: any, parseResponse?: (data: string) => any) => {
|
export const parseTxResponse = (result: any, parseResponse?: (data: string) => any) => {
|
||||||
const { txhash: hash, height, ...txResponse } = result;
|
const { txhash: hash, height, ...txResponse } = result;
|
||||||
@ -96,18 +94,6 @@ export class Registry {
|
|||||||
_chain: Chain;
|
_chain: Chain;
|
||||||
_client: RegistryClient;
|
_client: RegistryClient;
|
||||||
|
|
||||||
static processWriteError (error: string) {
|
|
||||||
// error string a stacktrace containing the message.
|
|
||||||
// https://gist.github.com/nikugogoi/de55d390574ded3466abad8bffd81952#file-txresponse-js-L7
|
|
||||||
const errorMessage = NAMESERVICE_ERRORS.find(message => error.includes(message));
|
|
||||||
|
|
||||||
if (!errorMessage) {
|
|
||||||
console.error(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
return errorMessage || DEFAULT_WRITE_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor (gqlUrl: string, restUrl = '', chainId: string = DEFAULT_CHAIN_ID) {
|
constructor (gqlUrl: string, restUrl = '', chainId: string = DEFAULT_CHAIN_ID) {
|
||||||
this._endpoints = {
|
this._endpoints = {
|
||||||
rest: restUrl,
|
rest: restUrl,
|
||||||
@ -179,12 +165,10 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.setRecord({ privateKey, record, bondId },
|
return laconicClient.setRecord({ privateKey, record, bondId },
|
||||||
account.address,
|
account.address,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -251,14 +235,12 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.createBond(
|
return laconicClient.createBond(
|
||||||
account.address,
|
account.address,
|
||||||
denom,
|
denom,
|
||||||
amount,
|
amount,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -269,15 +251,13 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.refillBond(
|
return laconicClient.refillBond(
|
||||||
account.address,
|
account.address,
|
||||||
denom,
|
denom,
|
||||||
amount,
|
amount,
|
||||||
id,
|
id,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -288,15 +268,13 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.withdrawBond(
|
return laconicClient.withdrawBond(
|
||||||
account.address,
|
account.address,
|
||||||
denom,
|
denom,
|
||||||
amount,
|
amount,
|
||||||
id,
|
id,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -307,13 +285,11 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.cancelBond(
|
return laconicClient.cancelBond(
|
||||||
account.address,
|
account.address,
|
||||||
id,
|
id,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -324,14 +300,12 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.associateBond(
|
return laconicClient.associateBond(
|
||||||
account.address,
|
account.address,
|
||||||
recordId,
|
recordId,
|
||||||
bondId,
|
bondId,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -342,13 +316,11 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.dissociateBond(
|
return laconicClient.dissociateBond(
|
||||||
account.address,
|
account.address,
|
||||||
recordId,
|
recordId,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -359,13 +331,11 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.dissociateRecords(
|
return laconicClient.dissociateRecords(
|
||||||
account.address,
|
account.address,
|
||||||
bondId,
|
bondId,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -376,14 +346,12 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.reassociateRecords(
|
return laconicClient.reassociateRecords(
|
||||||
account.address,
|
account.address,
|
||||||
oldBondId,
|
oldBondId,
|
||||||
newBondId,
|
newBondId,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -393,15 +361,13 @@ export class Registry {
|
|||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
const response: DeliverTxResponse = await laconicClient.reserveAuthority(
|
|
||||||
|
return laconicClient.reserveAuthority(
|
||||||
account.address,
|
account.address,
|
||||||
name,
|
name,
|
||||||
owner || account.address,
|
owner || account.address,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Parse error response
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -411,14 +377,13 @@ export class Registry {
|
|||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
const response: DeliverTxResponse = await laconicClient.setAuthorityBond(
|
|
||||||
|
return laconicClient.setAuthorityBond(
|
||||||
account.address,
|
account.address,
|
||||||
bondId,
|
bondId,
|
||||||
name,
|
name,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -428,14 +393,13 @@ export class Registry {
|
|||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
const response: DeliverTxResponse = await laconicClient.commitBid(
|
|
||||||
|
return laconicClient.commitBid(
|
||||||
account.address,
|
account.address,
|
||||||
auctionId,
|
auctionId,
|
||||||
commitHash,
|
commitHash,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -445,14 +409,12 @@ export class Registry {
|
|||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
const response: DeliverTxResponse = await laconicClient.revealBid(
|
return laconicClient.revealBid(
|
||||||
account.address,
|
account.address,
|
||||||
auctionId,
|
auctionId,
|
||||||
reveal,
|
reveal,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -477,15 +439,12 @@ export class Registry {
|
|||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
|
|
||||||
const response: DeliverTxResponse = await laconicClient.setName(
|
return laconicClient.setName(
|
||||||
account.address,
|
account.address,
|
||||||
lrn,
|
lrn,
|
||||||
cid,
|
cid,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Parse error response
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -502,43 +461,11 @@ export class Registry {
|
|||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
const account = new Account(Buffer.from(privateKey, 'hex'));
|
||||||
await account.init();
|
await account.init();
|
||||||
const laconicClient = await this.getLaconicClient(account);
|
const laconicClient = await this.getLaconicClient(account);
|
||||||
const response: DeliverTxResponse = await laconicClient.deleteName(
|
return laconicClient.deleteName(
|
||||||
account.address,
|
account.address,
|
||||||
lrn,
|
lrn,
|
||||||
fee
|
fee
|
||||||
);
|
);
|
||||||
|
|
||||||
// TODO: Parse error response form delete name
|
|
||||||
return laconicClient.registry.decode(response.msgResponses[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Submit a generic Tx to the chain.
|
|
||||||
*/
|
|
||||||
async _submitTx (message: any, privateKey: string, sender: Sender) {
|
|
||||||
// Check private key.
|
|
||||||
if (!isKeyValid(privateKey)) {
|
|
||||||
throw new Error('Registry privateKey should be a hex string.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that the account exists on-chain.
|
|
||||||
const account = new Account(Buffer.from(privateKey, 'hex'));
|
|
||||||
|
|
||||||
// Generate signed Tx.
|
|
||||||
const transaction = createTransaction(message, account, sender, this._chain);
|
|
||||||
|
|
||||||
const tx = generatePostBodyBroadcast(transaction, BroadcastMode.Block);
|
|
||||||
|
|
||||||
// Submit Tx to chain.
|
|
||||||
const { tx_response: response } = await this._client.submit(tx);
|
|
||||||
|
|
||||||
if (response.code !== 0) {
|
|
||||||
// Throw error when transaction is not successful.
|
|
||||||
// https://docs.starport.com/guide/nameservice/05-play.html#buy-name-transaction-details
|
|
||||||
throw new Error(Registry.processWriteError(response.raw_log));
|
|
||||||
}
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -13,10 +13,15 @@ import { MsgCancelBondEncodeObject, MsgCreateBondEncodeObject, MsgRefillBondEnco
|
|||||||
import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
import { Coin } from './proto2/cosmos/base/v1beta1/coin';
|
||||||
import { MsgAssociateBondEncodeObject, MsgDeleteNameAuthorityEncodeObject, MsgDissociateBondEncodeObject, MsgDissociateRecordsEncodeObject, MsgReassociateRecordsEncodeObject, MsgReserveAuthorityEncodeObject, MsgSetAuthorityBondEncodeObject, MsgSetNameEncodeObject, MsgSetRecordEncodeObject, registryTypes, typeUrlMsgAssociateBond, typeUrlMsgDeleteNameAuthority, typeUrlMsgDissociateBond, typeUrlMsgDissociateRecords, typeUrlMsgReassociateRecords, typeUrlMsgReserveAuthority, typeUrlMsgSetAuthorityBond, typeUrlMsgSetName, typeUrlMsgSetRecord } from './types/cerc/registry/message';
|
import { MsgAssociateBondEncodeObject, MsgDeleteNameAuthorityEncodeObject, MsgDissociateBondEncodeObject, MsgDissociateRecordsEncodeObject, MsgReassociateRecordsEncodeObject, MsgReserveAuthorityEncodeObject, MsgSetAuthorityBondEncodeObject, MsgSetNameEncodeObject, MsgSetRecordEncodeObject, registryTypes, typeUrlMsgAssociateBond, typeUrlMsgDeleteNameAuthority, typeUrlMsgDissociateBond, typeUrlMsgDissociateRecords, typeUrlMsgReassociateRecords, typeUrlMsgReserveAuthority, typeUrlMsgSetAuthorityBond, typeUrlMsgSetName, typeUrlMsgSetRecord } from './types/cerc/registry/message';
|
||||||
import { MsgCommitBidEncodeObject, MsgRevealBidEncodeObject, auctionTypes, typeUrlMsgCommitBid, typeUrlMsgRevealBid } from './types/cerc/auction/message';
|
import { MsgCommitBidEncodeObject, MsgRevealBidEncodeObject, auctionTypes, typeUrlMsgCommitBid, typeUrlMsgRevealBid } from './types/cerc/auction/message';
|
||||||
import { Payload } from './proto2/cerc/registry/v1/tx';
|
import { MsgAssociateBondResponse, MsgDeleteNameAuthorityResponse, MsgDissociateBondResponse, MsgDissociateRecordsResponse, MsgReassociateRecordsResponse, MsgReserveAuthorityResponse, MsgSetAuthorityBondResponse, MsgSetNameResponse, MsgSetRecordResponse, Payload } from './proto2/cerc/registry/v1/tx';
|
||||||
import { Record, Signature } from './proto2/cerc/registry/v1/registry';
|
import { Record, Signature } from './proto2/cerc/registry/v1/registry';
|
||||||
import { Account } from './account';
|
import { Account } from './account';
|
||||||
import { Util } from './util';
|
import { Util } from './util';
|
||||||
|
import { NAMESERVICE_ERRORS } from './messages/registry';
|
||||||
|
import { MsgCommitBidResponse, MsgRevealBidResponse } from './proto2/cerc/auction/v1/tx';
|
||||||
|
import { MsgCancelBondResponse, MsgCreateBondResponse, MsgRefillBondResponse, MsgWithdrawBondResponse } from './proto2/cerc/bond/v1/tx';
|
||||||
|
|
||||||
|
const DEFAULT_WRITE_ERROR = 'Unable to write to laconicd.';
|
||||||
|
|
||||||
export const laconicDefaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
|
export const laconicDefaultRegistryTypes: ReadonlyArray<[string, GeneratedType]> = [
|
||||||
...defaultRegistryTypes,
|
...defaultRegistryTypes,
|
||||||
@ -48,7 +53,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
amount: string,
|
amount: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgCreateBondEncodeObject = {
|
const createMsg: MsgCreateBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgCreateBond,
|
typeUrl: typeUrlMsgCreateBond,
|
||||||
value: {
|
value: {
|
||||||
@ -62,7 +67,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgCreateBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async refillBond (
|
public async refillBond (
|
||||||
@ -72,7 +78,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
id: string,
|
id: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgRefillBondEncodeObject = {
|
const createMsg: MsgRefillBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgRefillBond,
|
typeUrl: typeUrlMsgRefillBond,
|
||||||
value: {
|
value: {
|
||||||
@ -87,7 +93,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgRefillBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async withdrawBond (
|
public async withdrawBond (
|
||||||
@ -97,7 +104,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
id: string,
|
id: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgWithdrawBondEncodeObject = {
|
const createMsg: MsgWithdrawBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgWithdrawBond,
|
typeUrl: typeUrlMsgWithdrawBond,
|
||||||
value: {
|
value: {
|
||||||
@ -112,7 +119,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgWithdrawBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async cancelBond (
|
public async cancelBond (
|
||||||
@ -120,7 +128,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
id: string,
|
id: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgCancelBondEncodeObject = {
|
const createMsg: MsgCancelBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgCancelBond,
|
typeUrl: typeUrlMsgCancelBond,
|
||||||
value: {
|
value: {
|
||||||
@ -129,7 +137,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgCancelBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async associateBond (
|
public async associateBond (
|
||||||
@ -138,7 +147,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
bondId: string,
|
bondId: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgAssociateBondEncodeObject = {
|
const createMsg: MsgAssociateBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgAssociateBond,
|
typeUrl: typeUrlMsgAssociateBond,
|
||||||
value: {
|
value: {
|
||||||
@ -148,7 +157,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgAssociateBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async dissociateBond (
|
public async dissociateBond (
|
||||||
@ -156,7 +166,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
recordId: string,
|
recordId: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgDissociateBondEncodeObject = {
|
const createMsg: MsgDissociateBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgDissociateBond,
|
typeUrl: typeUrlMsgDissociateBond,
|
||||||
value: {
|
value: {
|
||||||
@ -165,7 +175,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgDissociateBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async dissociateRecords (
|
public async dissociateRecords (
|
||||||
@ -173,7 +184,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
bondId: string,
|
bondId: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgDissociateRecordsEncodeObject = {
|
const createMsg: MsgDissociateRecordsEncodeObject = {
|
||||||
typeUrl: typeUrlMsgDissociateRecords,
|
typeUrl: typeUrlMsgDissociateRecords,
|
||||||
value: {
|
value: {
|
||||||
@ -182,7 +193,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgDissociateRecordsResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async reassociateRecords (
|
public async reassociateRecords (
|
||||||
@ -191,7 +203,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
newBondId: string,
|
newBondId: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgReassociateRecordsEncodeObject = {
|
const createMsg: MsgReassociateRecordsEncodeObject = {
|
||||||
typeUrl: typeUrlMsgReassociateRecords,
|
typeUrl: typeUrlMsgReassociateRecords,
|
||||||
value: {
|
value: {
|
||||||
@ -201,7 +213,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgReassociateRecordsResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async reserveAuthority (
|
public async reserveAuthority (
|
||||||
@ -210,7 +223,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
owner: string,
|
owner: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgReserveAuthorityEncodeObject = {
|
const createMsg: MsgReserveAuthorityEncodeObject = {
|
||||||
typeUrl: typeUrlMsgReserveAuthority,
|
typeUrl: typeUrlMsgReserveAuthority,
|
||||||
value: {
|
value: {
|
||||||
@ -220,7 +233,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgReserveAuthorityResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async commitBid (
|
public async commitBid (
|
||||||
@ -229,7 +243,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
commitHash: string,
|
commitHash: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgCommitBidEncodeObject = {
|
const createMsg: MsgCommitBidEncodeObject = {
|
||||||
typeUrl: typeUrlMsgCommitBid,
|
typeUrl: typeUrlMsgCommitBid,
|
||||||
value: {
|
value: {
|
||||||
@ -239,7 +253,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgCommitBidResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async revealBid (
|
public async revealBid (
|
||||||
@ -248,7 +263,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
reveal: string,
|
reveal: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgRevealBidEncodeObject = {
|
const createMsg: MsgRevealBidEncodeObject = {
|
||||||
typeUrl: typeUrlMsgRevealBid,
|
typeUrl: typeUrlMsgRevealBid,
|
||||||
value: {
|
value: {
|
||||||
@ -258,7 +273,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgRevealBidResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setRecord (
|
public async setRecord (
|
||||||
@ -266,7 +282,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
signer: string,
|
signer: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const registryRecord = Record.fromPartial({ attributes: Buffer.from(JSON.stringify(params.record), 'binary') });
|
const registryRecord = Record.fromPartial({ attributes: Buffer.from(JSON.stringify(params.record), 'binary') });
|
||||||
|
|
||||||
// Sign record.
|
// Sign record.
|
||||||
@ -288,7 +304,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgSetRecordResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setAuthorityBond (
|
public async setAuthorityBond (
|
||||||
@ -297,7 +314,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
name: string,
|
name: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgSetAuthorityBondEncodeObject = {
|
const createMsg: MsgSetAuthorityBondEncodeObject = {
|
||||||
typeUrl: typeUrlMsgSetAuthorityBond,
|
typeUrl: typeUrlMsgSetAuthorityBond,
|
||||||
value: {
|
value: {
|
||||||
@ -307,7 +324,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgSetAuthorityBondResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setName (
|
public async setName (
|
||||||
@ -316,7 +334,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
cid: string,
|
cid: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgSetNameEncodeObject = {
|
const createMsg: MsgSetNameEncodeObject = {
|
||||||
typeUrl: typeUrlMsgSetName,
|
typeUrl: typeUrlMsgSetName,
|
||||||
value: {
|
value: {
|
||||||
@ -326,7 +344,8 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgSetNameResponse>(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteName (
|
public async deleteName (
|
||||||
@ -334,7 +353,7 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
lrn: string,
|
lrn: string,
|
||||||
fee: StdFee | 'auto' | number,
|
fee: StdFee | 'auto' | number,
|
||||||
memo = ''
|
memo = ''
|
||||||
): Promise<DeliverTxResponse> {
|
) {
|
||||||
const createMsg: MsgDeleteNameAuthorityEncodeObject = {
|
const createMsg: MsgDeleteNameAuthorityEncodeObject = {
|
||||||
typeUrl: typeUrlMsgDeleteNameAuthority,
|
typeUrl: typeUrlMsgDeleteNameAuthority,
|
||||||
value: {
|
value: {
|
||||||
@ -343,6 +362,28 @@ export class LaconicClient extends SigningStargateClient {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.signAndBroadcast(signer, [createMsg], fee, memo);
|
const response = await this.signAndBroadcast(signer, [createMsg], fee, memo);
|
||||||
|
return this.parseResponse<MsgDeleteNameAuthorityResponse>(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
parseResponse<T> (response: DeliverTxResponse): T {
|
||||||
|
if (response.code !== 0) {
|
||||||
|
// Throw error when transaction is not successful.
|
||||||
|
throw new Error(this.processWriteError(response.rawLog || 'No raw log in response'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.registry.decode(response.msgResponses[0]) as T;
|
||||||
|
}
|
||||||
|
|
||||||
|
processWriteError (error: string) {
|
||||||
|
// error string a stacktrace containing the message.
|
||||||
|
// https://gist.github.com/nikugogoi/de55d390574ded3466abad8bffd81952#file-txresponse-js-L7
|
||||||
|
const errorMessage = NAMESERVICE_ERRORS.find(message => error.includes(message));
|
||||||
|
|
||||||
|
if (!errorMessage) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
return errorMessage || DEFAULT_WRITE_ERROR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,8 +75,7 @@ const namingTests = () => {
|
|||||||
expect(Number(record.height)).toBe(0);
|
expect(Number(record.height)).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Implement parse error response
|
test('Reserve already reserved authority', async () => {
|
||||||
xtest('Reserve already reserved authority', async () => {
|
|
||||||
await expect(registry.reserveAuthority({ name: authorityName }, privateKey, fee))
|
await expect(registry.reserveAuthority({ name: authorityName }, privateKey, fee))
|
||||||
.rejects.toThrow('Name already reserved.');
|
.rejects.toThrow('Name already reserved.');
|
||||||
});
|
});
|
||||||
@ -115,8 +114,7 @@ const namingTests = () => {
|
|||||||
expect(Number(record.height)).toBeGreaterThan(0);
|
expect(Number(record.height)).toBeGreaterThan(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Parse error response from set name
|
test('Set name for unbonded authority', async () => {
|
||||||
xtest('Set name for unbonded authority', async () => {
|
|
||||||
assert(watcherId);
|
assert(watcherId);
|
||||||
await expect(registry.setName({ lrn, cid: watcherId }, privateKey, fee))
|
await expect(registry.setName({ lrn, cid: watcherId }, privateKey, fee))
|
||||||
.rejects.toThrow('Authority bond not found.');
|
.rejects.toThrow('Authority bond not found.');
|
||||||
@ -269,14 +267,12 @@ const namingTests = () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Parse error response form set name
|
test('Set name without reserving authority', async () => {
|
||||||
xtest('Set name without reserving authority', async () => {
|
|
||||||
await expect(registry.setName({ lrn: 'lrn://not-reserved/app/test', cid: watcherId }, privateKey, fee))
|
await expect(registry.setName({ lrn: 'lrn://not-reserved/app/test', cid: watcherId }, privateKey, fee))
|
||||||
.rejects.toThrow('Name authority not found.');
|
.rejects.toThrow('Name authority not found.');
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Parse error response form set name
|
test('Set name for non-owned authority', async () => {
|
||||||
xtest('Set name for non-owned authority', async () => {
|
|
||||||
await registry.sendCoins({ denom: DENOM, amount: '1000000000', destinationAddress: otherAccount.address }, privateKey, fee);
|
await registry.sendCoins({ denom: DENOM, amount: '1000000000', destinationAddress: otherAccount.address }, privateKey, fee);
|
||||||
|
|
||||||
// Other account reserves an authority.
|
// Other account reserves an authority.
|
||||||
@ -286,8 +282,7 @@ const namingTests = () => {
|
|||||||
await expect(registry.setName({ lrn: `lrn://${otherAuthorityName}/app/test`, cid: watcherId }, privateKey, fee)).rejects.toThrow('Access denied.');
|
await expect(registry.setName({ lrn: `lrn://${otherAuthorityName}/app/test`, cid: watcherId }, privateKey, fee)).rejects.toThrow('Access denied.');
|
||||||
});
|
});
|
||||||
|
|
||||||
// TODO: Parse error response form set name
|
test('Delete name for non-owned authority.', async () => {
|
||||||
xtest('Delete name for non-owned authority.', async () => {
|
|
||||||
const otherBondId = await registry.getNextBondId(otherPrivateKey);
|
const otherBondId = await registry.getNextBondId(otherPrivateKey);
|
||||||
await registry.createBond({ denom: DENOM, amount: '1000000' }, otherPrivateKey, fee);
|
await registry.createBond({ denom: DENOM, amount: '1000000' }, otherPrivateKey, fee);
|
||||||
await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherPrivateKey, fee);
|
await registry.setAuthorityBond({ name: otherAuthorityName, bondId: otherBondId }, otherPrivateKey, fee);
|
||||||
|
Loading…
Reference in New Issue
Block a user