Skip to content

Commit

Permalink
Add the experimental support for the stargate version of cosmos-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Thunnini committed Oct 16, 2020
1 parent 9a8b6cf commit 3885e4d
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 42 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
Expand Up @@ -18,7 +18,7 @@
"author": "chainapsis",
"license": "Apache-2.0",
"dependencies": {
"@chainapsis/cosmosjs": "git+https://github.com/chainapsis/cosmosjs-dist.git#9e94261",
"@chainapsis/cosmosjs": "git+https://github.com/chainapsis/cosmosjs-dist.git#b271dc5",
"@cosmjs/encoding": "^0.23.0",
"@cosmjs/launchpad": "^0.23.0",
"@ensdomains/resolver": "^0.2.4",
Expand Down
5 changes: 3 additions & 2 deletions src/background/tx/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const handleRequestBackgroundTxMsg: (
msg.origin
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await keeper.requestTx(msg.chainId, msg.txBytes, msg.mode!);
await keeper.requestTx(msg.chainId, msg.txBytes, msg.mode!, msg.isRestAPI);
return {};
};
};
Expand All @@ -54,7 +54,8 @@ const handleRequestBackgroundTxWithResultMsg: (
msg.chainId,
msg.txBytes,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
msg.mode!
msg.mode!,
msg.isRestAPI
);
};
};
79 changes: 61 additions & 18 deletions src/background/tx/keeper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,52 +32,77 @@ export class BackgroundTxKeeper {
async requestTx(
chainId: string,
txBytes: string,
mode: "sync" | "async" | "commit"
mode: "sync" | "async" | "commit",
isRestAPI: boolean
) {
const info = await this.chainsKeeper.getChainInfo(chainId);
const instance = Axios.create({
const rpcInstance = Axios.create({
...{
baseURL: info.rpc
},
...info.rpcConfig
});
const restInstance = Axios.create({
...{
baseURL: info.rest
},
...info.restConfig
});

// Do not await.
BackgroundTxKeeper.sendTransaction(chainId, instance, txBytes, mode);
BackgroundTxKeeper.sendTransaction(
chainId,
rpcInstance,
restInstance,
txBytes,
mode,
isRestAPI
);

return;
}

async requestTxWithResult(
chainId: string,
txBytes: string,
mode: "sync" | "async" | "commit"
mode: "sync" | "async" | "commit",
isRestAPI: boolean
): Promise<ResultBroadcastTx | ResultBroadcastTxCommit> {
const info = await this.chainsKeeper.getChainInfo(chainId);
const instance = Axios.create({
const rpcInstance = Axios.create({
...{
baseURL: info.rpc
},
...info.rpcConfig
});
const restInstance = Axios.create({
...{
baseURL: info.rest
},
...info.restConfig
});

return await BackgroundTxKeeper.sendTransaction(
chainId,
instance,
rpcInstance,
restInstance,
txBytes,
mode
mode,
isRestAPI
);
}

private static async sendTransaction(
chainId: string,
instance: AxiosInstance,
rpcInstance: AxiosInstance,
restInstance: AxiosInstance,
txBytes: string,
mode: "sync" | "async" | "commit"
mode: "sync" | "async" | "commit",
isRestAPI: boolean
): Promise<ResultBroadcastTx | ResultBroadcastTxCommit> {
const rpc = new TendermintRPC(
new Context({
rpcInstance: instance
rpcInstance
} as IContext)
);

Expand All @@ -91,10 +116,26 @@ export class BackgroundTxKeeper {
});

try {
if (mode === "commit") {
result = await rpc.broadcastTxCommit(Buffer.from(txBytes, "hex"));
if (!isRestAPI) {
if (mode === "commit") {
result = await rpc.broadcastTxCommit(Buffer.from(txBytes, "hex"));
} else {
result = await rpc.broadcastTx(Buffer.from(txBytes, "hex"), mode);
}
} else {
result = await rpc.broadcastTx(Buffer.from(txBytes, "hex"), mode);
const json = JSON.parse(Buffer.from(txBytes, "hex").toString());
const restResult = await restInstance.post<
ResultBroadcastTx | ResultBroadcastTxCommit
>("/txs", {
tx: json,
mode: mode === "commit" ? "block" : mode
});

if (restResult.status !== 200 && restResult.status !== 202) {
throw new Error(restResult.statusText);
}

result = restResult.data;
}

BackgroundTxKeeper.processTxResultNotification(result);
Expand All @@ -118,11 +159,7 @@ export class BackgroundTxKeeper {
result: ResultBroadcastTx | ResultBroadcastTxCommit
): void {
try {
if (result.mode === "sync" || result.mode === "async") {
if (result.code !== 0) {
throw new Error(result.log);
}
} else if (result.mode === "commit") {
if (result.mode === "commit") {
if (result.checkTx.code !== undefined && result.checkTx.code !== 0) {
throw new Error(result.checkTx.log);
}
Expand All @@ -132,6 +169,12 @@ export class BackgroundTxKeeper {
) {
throw new Error(result.deliverTx.log);
}
} else {
if (result.code != null && result.code !== 0) {
// XXX: Hack of the support of the stargate.
const log = result.log ?? (result as any)["raw_log"];
throw new Error(log);
}
}

browser.notifications.create({
Expand Down
6 changes: 4 additions & 2 deletions src/background/tx/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class RequestBackgroundTxMsg extends Message<{}> {
constructor(
public readonly chainId: string,
public readonly txBytes: string,
public readonly mode: "sync" | "async" | "commit"
public readonly mode: "sync" | "async" | "commit",
public readonly isRestAPI: boolean = false
) {
super();
}
Expand Down Expand Up @@ -68,7 +69,8 @@ export class RequestBackgroundTxWithResultMsg extends Message<
constructor(
public readonly chainId: string,
public readonly txBytes: string,
public readonly mode: "sync" | "async" | "commit"
public readonly mode: "sync" | "async" | "commit",
public readonly isRestAPI: boolean = false
) {
super();
}
Expand Down
21 changes: 18 additions & 3 deletions src/ui/hooks/use-cosmosjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export const useCosmosJS = <R extends Rest = Rest>(
setLoading(false);
let isSubscribed = true;

const isStargate = chainInfo.features
? chainInfo.features.includes("stargate")
: false;

const api = new Api<R>(
{
chainId: chainInfo.chainId,
Expand Down Expand Up @@ -128,7 +132,10 @@ export const useCosmosJS = <R extends Rest = Rest>(
return queryAccount(
context.get("rpcInstance"),
address,
chainInfo.bech32Config.bech32PrefixAccAddr
chainInfo.bech32Config.bech32PrefixAccAddr,
{
isStargate
}
);
},
bech32Config: chainInfo.bech32Config,
Expand All @@ -155,6 +162,8 @@ export const useCosmosJS = <R extends Rest = Rest>(
})();
}

api.isStargate = isStargate;

const _sendMsgs: SendMsgs = async (
msgs: Msg[],
config: TxBuilderConfig,
Expand Down Expand Up @@ -196,12 +205,18 @@ export const useCosmosJS = <R extends Rest = Rest>(
msgs,
config
);
const bz = api.context.get("txEncoder")(api.context, tx);
let bz = api.context.get("txEncoder")(api.context, tx, isStargate);

if (isStargate) {
const json = JSON.parse(Buffer.from(bz).toString());
bz = Buffer.from(JSON.stringify(json.value));
}

const msg = new RequestBackgroundTxMsg(
api.context.get("chainId"),
Buffer.from(bz).toString("hex"),
mode
mode,
isStargate
);
await sendMessage(BACKGROUND_PORT, msg);
}
Expand Down
67 changes: 53 additions & 14 deletions src/ui/popup/stores/account/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,60 @@ export class AccountStore {
this.isAssetFetching = true;

try {
const account = await task(
queryAccount(
Axios.create({
...this.chainInfo.rpcConfig,
...{
baseURL: this.chainInfo.rpc,
cancelToken: this.lastFetchingCancleToken.token
}
}),
this.bech32Address,
this.chainInfo.bech32Config.bech32PrefixAccAddr
)
);
const isStargate = this.chainInfo.features
? this.chainInfo.features.includes("stargate")
: false;

if (!isStargate) {
const account = await task(
queryAccount(
Axios.create({
...this.chainInfo.rpcConfig,
...{
baseURL: this.chainInfo.rpc,
cancelToken: this.lastFetchingCancleToken.token
}
}),
this.bech32Address,
this.chainInfo.bech32Config.bech32PrefixAccAddr,
{ isStargate }
)
);

this.pushAssets(account.getCoins());
} else {
// In stargate, the assets doens't exist on account itself.
// It exists on the bank module. So, we should to fetch the balances from bank module.
const restInstance = Axios.create({
...this.chainInfo.restConfig,
...{
baseURL: this.chainInfo.rest,
cancelToken: this.lastFetchingCancleToken.token
}
});

const result = await task(
restInstance.get<{
result: {
denom: string;
amount: string;
}[];
}>(`/bank/balances/${this.bech32Address}`)
);

if (result.status !== 200) {
throw new Error(result.statusText);
}

const balances: Coin[] = [];
for (const asset of result.data.result) {
const balance = new Coin(asset.denom, asset.amount);
balances.push(balance);
}

this.pushAssets(balances);
}

this.pushAssets(account.getCoins());
this.lastAssetFetchingError = undefined;
// Save the assets to storage.
await task(
Expand Down

0 comments on commit 3885e4d

Please sign in to comment.