Skip to content

Commit

Permalink
Merge pull request #385 from Takadenoshi/fix/remove-pact-lang-api
Browse files Browse the repository at this point in the history
fix: replace pact-lang-api with @kadena/client | Kadena
  • Loading branch information
kvhnuke authored Jan 23, 2024
2 parents 009cdfb + 4c1516f commit 9ff4f2f
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 628 deletions.
4 changes: 3 additions & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
"private": true,
"scripts": {
"zip": "cd dist; zip -r release.zip *;",
"prebuild": "yarn kadena:prebuild",
"build:chrome": "cross-env BROWSER='chrome' vue-cli-service build && yarn build:rollup",
"build:firefox": "cross-env BROWSER='firefox' vue-cli-service build && yarn build:rollup && node configs/get-system-info.js",
"lint": "vue-cli-service lint --fix",
"build:rollup": "cross-env minify=on rollup --config configs/rollup.config.contentscript.mjs && cross-env minify=on rollup --config configs/rollup.config.inject.mjs",
"inspectWebpack": "vue-cli-service inspect > webpack.log",
"kadena:prebuild": "pactjs contract-generate --contract=coin --api https://api.chainweb.com/chainweb/0.0/mainnet01/chain/1/pact",
"test": "ts-mocha --require ./configs/testNullCompiler.js --paths -p configs/tsconfig.test.json ./**/*.mocha.ts",
"watch": "rimraf dist && concurrently 'npm:watch-*(!firefox)'",
"watch:firefox": "concurrently 'npm:watch-*(!chrome)'",
Expand Down Expand Up @@ -56,7 +58,6 @@
"memoize-one": "^6.0.0",
"moment": "^2.29.4",
"nanoevents": "^7.0.1",
"pact-lang-api": "^4.3.6",
"pinia": "^2.1.6",
"qrcode.vue": "^3.4.1",
"switch-ts": "^1.1.1",
Expand All @@ -73,6 +74,7 @@
"zxcvbn": "^4.4.2"
},
"devDependencies": {
"@kadena/pactjs-cli": "^1.7.0",
"@polkadot/api": "^10.9.1",
"@polkadot/extension-inject": "^0.46.5",
"@polkadot/keyring": "^12.5.1",
Expand Down
54 changes: 30 additions & 24 deletions packages/extension/src/providers/kadena/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { ProviderAPIInterface } from "@/types/provider";
import { KadenaNetworkOptions } from "../types/kadena-network";
import {
ICommand,
IUnsignedCommand,
ICommandResult,
ITransactionDescriptor,
createClient,
Pact,
ChainId,
} from "@kadena/client";
import { toBase } from "@enkryptcom/utils";
import DomainState from "@/libs/domain-state";
Expand Down Expand Up @@ -47,12 +50,16 @@ class API implements ProviderAPIInterface {
});
}

async getTransactionStatus(hash: string): Promise<KadenaRawInfo | null> {
const Pact = require("pact-lang-api");
const cmd = { requestKeys: [hash] };
async getTransactionStatus(requestKey: string): Promise<KadenaRawInfo> {
const chainId = await this.getChainId();
const transactions = await Pact.fetch.poll(cmd, this.getApiHost(chainId));
return transactions[hash];
const networkId = this.networkId;
const { pollStatus } = createClient(this.getApiHost(chainId));
const responses = await pollStatus({
requestKey,
networkId,
chainId: chainId as ChainId,
});
return responses[requestKey];
}

async getBalanceByChainId(address: string, chainId: string): Promise<string> {
Expand All @@ -61,8 +68,14 @@ class API implements ProviderAPIInterface {
chainId
);

if (balance.result.error) {
return toBase("0", this.decimals);
if (balance.result.status === "failure") {
const error = balance.result.error as { message: string | undefined };
const message = error.message ?? "Unknown error retrieving balances";
// expected error when account does not exist on a chain (balance == 0)
if (message.includes("row not found")) {
return toBase("0", this.decimals);
}
throw new Error(message);
}

const balanceValue = parseFloat(balance.result.data.toString()).toFixed(
Expand All @@ -78,22 +91,13 @@ class API implements ProviderAPIInterface {
}

async getBalanceAPI(account: string, chainId: string) {
const Pact = require("pact-lang-api");
const cmd = {
networkId: this.networkId,
pactCode: `(coin.get-balance "${account}")`,
envData: {},
meta: {
creationTime: Math.round(new Date().getTime() / 1000),
ttl: 600,
gasLimit: 600,
chainId: chainId,
gasPrice: 0.0000001,
sender: "",
},
};

return Pact.fetch.local(cmd, this.getApiHost(chainId));
const transaction = Pact.builder
.execution(Pact.modules.coin["get-balance"](account))
.setMeta({ chainId: chainId as ChainId })
.setNetworkId(this.networkId)
.createTransaction();

return this.dirtyRead(transaction);
}

async sendLocalTransaction(
Expand All @@ -120,7 +124,9 @@ class API implements ProviderAPIInterface {
return client.listen(transactionDescriptor);
}

async dirtyRead(signedTranscation: ICommand): Promise<ICommandResult> {
async dirtyRead(
signedTranscation: ICommand | IUnsignedCommand
): Promise<ICommandResult> {
const chainId = await this.getChainId();
const client = createClient(this.getApiHost(chainId));
return client.dirtyRead(signedTranscation);
Expand Down
9 changes: 2 additions & 7 deletions packages/extension/src/types/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
TokenTypeTo,
StatusOptionsResponse,
} from "@enkryptcom/swap";
import { ICommandResult } from "@kadena/client";

interface BTCInOuts {
address: string;
Expand Down Expand Up @@ -62,13 +63,7 @@ interface SubstrateRawInfo {
asset_type: string;
}

interface KadenaRawInfo {
gas: number;
result: { status: string; data: string };
reqKey: string;
logs: string;
txId: number;
}
type KadenaRawInfo = ICommandResult;

enum ActivityStatus {
pending = "pending",
Expand Down
2 changes: 1 addition & 1 deletion packages/extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"sourceMap": true,
"types": ["mocha", "chrome"],
"types": ["mocha", "chrome", ".kadena/pactjs-generated"],
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"],
Expand Down
Loading

0 comments on commit 9ff4f2f

Please sign in to comment.