Skip to content

Commit

Permalink
Merge branch 'master' into fix/e-mode-calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
freeelancer committed Mar 31, 2023
2 parents dd4d7aa + 269a637 commit ad6614c
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 68 deletions.
76 changes: 50 additions & 26 deletions examples/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Long from "long";
import { QueryAllTransactionRequest } from "../lib/codec";
import { PageRequest } from "../lib/codec/cosmos/base/query/v1beta1/pagination";
import { CarbonSDK, CarbonTx, GenericUtils } from "./_sdk";
import "./_setup";

Expand All @@ -14,35 +15,51 @@ import "./_setup";
// GRPC Queries

// query market stats
const marketStats = await sdk.query.marketstats.MarketStats({});
const marketStats = await sdk.query.marketstats.MarketStats({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("marketStats", marketStats);

// query msg type gas costs
const msgGasCosts = await sdk.query.fee.MsgGasCostAll({});
const msgGasCosts = await sdk.query.fee.MsgGasCostAll({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("msg gas costs", msgGasCosts);

// query txn min gas price
const minGasPrices = await sdk.query.fee.MinGasPriceAll({});
const minGasPrices = await sdk.query.fee.MinGasPriceAll({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("min gas prices", minGasPrices);

// query all tokens
const tokens = await sdk.query.coin.TokenAll({
pagination: {
reverse: false,
limit: new Long(100),
offset: new Long(0),
key: new Uint8Array(),
countTotal: true,
},
pagination: PageRequest.fromPartial({
limit: new Long(20),
}),
});
console.log("tokens", tokens);

// query all token mappings
const mappings = await sdk.query.coin.WrapperMappings({});
const mappings = await sdk.query.coin.WrapperMappings({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("mappings", mappings);

// query all markets
const markets = await sdk.query.market.MarketAll({});
const markets = await sdk.query.market.MarketAll({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("markets", markets);

// query all orders;
Expand All @@ -51,49 +68,56 @@ import "./_setup";
market: "",
orderType: "",
orderStatus: "",
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("orders", orders);

// query all profiles with pagination
const profiles = await sdk.query.profile.ProfileAll({
username: "",
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("profiles", profiles);

// query 10 latest blocks
const blocksResponse = await sdk.query.misc.BlockAll({
pagination: {
page: new Long(1),
pageSize: new Long(10),
},
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("latest block", blocksResponse.blocks[0]);

// query 10 latest transactions
const txnsResponse = await sdk.query.misc.TransactionAll(
QueryAllTransactionRequest.fromPartial({
pagination: {
page: new Long(1),
pageSize: new Long(10),
},
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
})
);
console.log("latest txn", txnsResponse.transactions[0]);

// query all transaction MessageTypes
const messageTypeReponse = await sdk.query.misc.MessageTypeAll({});
const messageTypeReponse = await sdk.query.misc.MessageTypeAll({
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
});
console.log("message types", messageTypeReponse.messageTypes);

const firstMessageType = messageTypeReponse.messageTypes[0].messageType;
const firstMessageType = messageTypeReponse.messageTypes[0]?.messageType;

// filter transactions with MessageType
const filteredTxns = await sdk.query.misc.TransactionAll(
QueryAllTransactionRequest.fromPartial({
msgTypeFilters: [firstMessageType],
pagination: {
page: new Long(1),
pageSize: new Long(10),
},
pagination: PageRequest.fromPartial({
limit: new Long(5),
}),
})
);
console.log("filtered txns by messageType:", firstMessageType, filteredTxns.transactions[0]);
Expand Down
10 changes: 6 additions & 4 deletions examples/query_cdp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as BIP39 from "bip39";
import Long from "long";
import { CarbonSDK } from "./_sdk";
import { PageRequest } from "../lib/codec/cosmos/base/query/v1beta1/pagination";
import "./_setup";

(async () => {
Expand Down Expand Up @@ -47,10 +48,11 @@ import "./_setup";
console.log("accountStablecoin", accountStablecoin);

const positionsAll = await sdk.query.cdp.PositionsAll({
pagination: {
page: Long.fromNumber(1),
pageSize: Long.fromNumber(200),
}
minHealthFactor: "",
maxHealthFactor: "",
pagination: PageRequest.fromPartial({
limit: new Long(10),
}),
});
console.log("positionsAll", positionsAll); // shift healthFactor by 18 decimals

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": "carbon-js-sdk",
"version": "0.3.59",
"version": "0.4.5",
"description": "TypeScript SDK for Carbon blockchain",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
10 changes: 8 additions & 2 deletions src/clients/TokenClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,17 @@ class TokenClient {
}

public tokenForId(id: string): Token | undefined {
return Object.values(this.tokens).find((token) => token.id === id);
let tokensList = Object.values(this.tokens);
if (TokenClient.isPoolToken(id)) {
tokensList = Object.values(this.poolTokens);
} else if (TokenClient.isCdpToken(id)) {
tokensList = Object.values(this.cdpTokens);
}
return tokensList.find((token) => token.id === id);
}

public tokenForDenom(denom: string): Token | undefined {
return this.tokens[denom];
return this.poolTokens[denom] ?? this.cdpTokens[denom] ?? this.tokens[denom];
}

public async getFeeInfo(denom: string): Promise<FeeQuote> {
Expand Down
20 changes: 10 additions & 10 deletions src/codec/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import { MsgSoftwareUpgrade, MsgSoftwareUpgradeResponse, MsgCancelUpgrade, MsgCa
import { SoftwareUpgradeProposal, CancelSoftwareUpgradeProposal } from "./cosmos/upgrade/v1beta1/upgrade";
import { MsgGrantAllowance, MsgGrantAllowanceResponse, MsgRevokeAllowance, MsgRevokeAllowanceResponse } from "./cosmos/feegrant/v1beta1/tx";
import { MsgSubmitEvidence, MsgSubmitEvidenceResponse } from "./cosmos/evidence/v1beta1/tx";
import { MsgSend, MsgSendResponse } from "./cosmos/nft/v1beta1/tx";
import { MsgSend as MsgSendNft, MsgSendResponse as MsgSendNftResponse } from "./cosmos/nft/v1beta1/tx";
import { MsgCreateGroup, MsgCreateGroupResponse, MsgUpdateGroupMembers, MsgUpdateGroupMembersResponse, MsgUpdateGroupAdmin, MsgUpdateGroupAdminResponse, MsgUpdateGroupMetadata, MsgUpdateGroupMetadataResponse, MsgCreateGroupPolicy, MsgCreateGroupPolicyResponse, MsgUpdateGroupPolicyAdmin, MsgCreateGroupWithPolicy, MsgCreateGroupWithPolicyResponse, MsgUpdateGroupPolicyAdminResponse, MsgUpdateGroupPolicyDecisionPolicy, MsgUpdateGroupPolicyDecisionPolicyResponse, MsgUpdateGroupPolicyMetadata, MsgUpdateGroupPolicyMetadataResponse, MsgSubmitProposal, MsgSubmitProposalResponse, MsgWithdrawProposal, MsgWithdrawProposalResponse, MsgVote, MsgVoteResponse, MsgExec, MsgExecResponse, MsgLeaveGroup, MsgLeaveGroupResponse } from "./cosmos/group/v1/tx";
import { MsgSend as MsgBankSend, MsgSendResponse as MsgBankSendResponse, MsgMultiSend, MsgMultiSendResponse } from "./cosmos/bank/v1beta1/tx";
import { MsgSend, MsgSendResponse, MsgMultiSend, MsgMultiSendResponse } from "./cosmos/bank/v1beta1/tx";
import { MsgSetWithdrawAddress, MsgSetWithdrawAddressResponse, MsgWithdrawDelegatorReward, MsgWithdrawDelegatorRewardResponse, MsgWithdrawValidatorCommission, MsgWithdrawValidatorCommissionResponse, MsgFundCommunityPool, MsgFundCommunityPoolResponse } from "./cosmos/distribution/v1beta1/tx";
import { CommunityPoolSpendProposal } from "./cosmos/distribution/v1beta1/distribution";
import { MsgVerifyInvariant, MsgVerifyInvariantResponse } from "./cosmos/crisis/v1beta1/tx";
Expand Down Expand Up @@ -215,8 +215,8 @@ registry.register("/cosmos.feegrant.v1beta1.MsgRevokeAllowanceResponse", MsgRevo
registry.register("/cosmos.evidence.v1beta1.MsgSubmitEvidence", MsgSubmitEvidence);
registry.register("/cosmos.evidence.v1beta1.MsgSubmitEvidenceResponse", MsgSubmitEvidenceResponse);

registry.register("/cosmos.nft.v1beta1.MsgSend", MsgSend);
registry.register("/cosmos.nft.v1beta1.MsgSendResponse", MsgSendResponse);
registry.register("/cosmos.nft.v1beta1.MsgSend", MsgSendNft);
registry.register("/cosmos.nft.v1beta1.MsgSendResponse", MsgSendNftResponse);

registry.register("/cosmos.group.v1.MsgCreateGroup", MsgCreateGroup);
registry.register("/cosmos.group.v1.MsgCreateGroupResponse", MsgCreateGroupResponse);
Expand Down Expand Up @@ -247,8 +247,8 @@ registry.register("/cosmos.group.v1.MsgExecResponse", MsgExecResponse);
registry.register("/cosmos.group.v1.MsgLeaveGroup", MsgLeaveGroup);
registry.register("/cosmos.group.v1.MsgLeaveGroupResponse", MsgLeaveGroupResponse);

registry.register("/cosmos.bank.v1beta1.MsgSend", MsgBankSend);
registry.register("/cosmos.bank.v1beta1.MsgSendResponse", MsgBankSendResponse);
registry.register("/cosmos.bank.v1beta1.MsgSend", MsgSend);
registry.register("/cosmos.bank.v1beta1.MsgSendResponse", MsgSendResponse);
registry.register("/cosmos.bank.v1beta1.MsgMultiSend", MsgMultiSend);
registry.register("/cosmos.bank.v1beta1.MsgMultiSendResponse", MsgMultiSendResponse);

Expand Down Expand Up @@ -670,8 +670,8 @@ export const TxTypes = {
"MsgRevokeAllowanceResponse": "/cosmos.feegrant.v1beta1.MsgRevokeAllowanceResponse",
"MsgSubmitEvidence": "/cosmos.evidence.v1beta1.MsgSubmitEvidence",
"MsgSubmitEvidenceResponse": "/cosmos.evidence.v1beta1.MsgSubmitEvidenceResponse",
"MsgSend": "/cosmos.nft.v1beta1.MsgSend",
"MsgSendResponse": "/cosmos.nft.v1beta1.MsgSendResponse",
"MsgSendNft": "/cosmos.nft.v1beta1.MsgSend",
"MsgSendNftResponse": "/cosmos.nft.v1beta1.MsgSendResponse",
"MsgCreateGroup": "/cosmos.group.v1.MsgCreateGroup",
"MsgCreateGroupResponse": "/cosmos.group.v1.MsgCreateGroupResponse",
"MsgUpdateGroupMembers": "/cosmos.group.v1.MsgUpdateGroupMembers",
Expand Down Expand Up @@ -700,8 +700,8 @@ export const TxTypes = {
"MsgExecResponse": "/cosmos.group.v1.MsgExecResponse",
"MsgLeaveGroup": "/cosmos.group.v1.MsgLeaveGroup",
"MsgLeaveGroupResponse": "/cosmos.group.v1.MsgLeaveGroupResponse",
"MsgBankSend": "/cosmos.bank.v1beta1.MsgSend",
"MsgBankSendResponse": "/cosmos.bank.v1beta1.MsgSendResponse",
"MsgSend": "/cosmos.bank.v1beta1.MsgSend",
"MsgSendResponse": "/cosmos.bank.v1beta1.MsgSendResponse",
"MsgMultiSend": "/cosmos.bank.v1beta1.MsgMultiSend",
"MsgMultiSendResponse": "/cosmos.bank.v1beta1.MsgMultiSendResponse",
"MsgSetWithdrawAddress": "/cosmos.distribution.v1beta1.MsgSetWithdrawAddress",
Expand Down
53 changes: 51 additions & 2 deletions src/modules/ibc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { MsgTransfer } from "@carbon-sdk/codec/ibc/applications/transfer/v1/tx";
import { CarbonTx, GenericUtils } from "@carbon-sdk/util";
import { CarbonTx } from "@carbon-sdk/util";
import BigNumber from "bignumber.js";
import Long from "long";
import BaseModule from "./base";

export class IBCModule extends BaseModule {
/** @deprecated please use sendIbcTransferUpdated instead */
public async sendIBCTransfer(params: IBCModule.SendIBCTransferParams, msgOpts?: CarbonTx.SignTxOpts) {
const wallet = this.getWallet();

Expand Down Expand Up @@ -36,12 +38,45 @@ export class IBCModule extends BaseModule {
typeUrl: CarbonTx.Types.MsgTransfer,
value,
},
msgOpts
msgOpts,
);
}

public async sendIbcTransferV2(params: IBCModule.SendIBCTransferV2Params, msgOpts?: CarbonTx.SignTxOpts) {
const wallet = this.getWallet();

const value = MsgTransfer.fromPartial({
sourcePort: params.sourcePort,
sourceChannel: params.sourceChannel,
token: {
denom: params.denom,
amount: params.amount.toString(10),
},
sender: params.sender ?? wallet.bech32Address,
receiver: params.receiver,
...params.timeoutHeight && ({
timeoutHeight: {
revisionHeight: new Long(params.timeoutHeight.revisionHeight.toNumber()),
revisionNumber: new Long(params.timeoutHeight.revisionNumber.toNumber()),
},
}),
...params.timeoutTimestamp && ({
timeoutTimestamp: params.timeoutTimestamp.toNumber(),
}),
});

return await wallet.sendTx(
{
typeUrl: CarbonTx.Types.MsgTransfer,
value,
},
msgOpts,
);
}
}

export namespace IBCModule {
/** @deprecated sendIBCTransfer function is deprecated, please use sendIbcTransferUpdated instead */
export interface SendIBCTransferParams {
sender?: string;
receiver: string;
Expand All @@ -53,4 +88,18 @@ export namespace IBCModule {
revisionNumber?: number;
timeoutTimestamp?: number;
}

export interface SendIBCTransferV2Params {
sender?: string;
receiver: string;
amount: BigNumber;
denom: string;
sourceChannel: string;
sourcePort: string;
timeoutHeight?: {
revisionNumber: BigNumber;
revisionHeight: BigNumber;
};
timeoutTimestamp?: BigNumber;
}
}
4 changes: 3 additions & 1 deletion src/websocket/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,21 @@ export class WSConnector {
* options: subscribe, unsubscribe, get_recent_trades, get_candlesticks, get_open_orders,
* get_account_trades, get_market_stats, get_leverages, get_open_positions, get_closed_positions
* @param {any} params - parameters based on the specified method
* @param {any} extras - additional args to be sent in the request
*
* @returns {Promise<WSResult<T>>} - a Promise resolving to the response from the endpoint
*
* @see WSConnector documentation for usage example
*/
public async request<T = unknown>(method: string, params: any): Promise<WSResult<T> | undefined> {
public async request<T = unknown>(method: string, params: any, extras: any = {}): Promise<WSResult<T> | undefined> {
const requestId = `r${++this.requestIdCounter}`;

this.sendMessage(
JSON.stringify({
id: requestId,
method,
params,
...extras,
})
);

Expand Down
Loading

0 comments on commit ad6614c

Please sign in to comment.