Skip to content

Commit

Permalink
Add getBalance for Kadena network
Browse files Browse the repository at this point in the history
  • Loading branch information
Peixer committed Sep 8, 2023
1 parent 9f92e32 commit f037935
Show file tree
Hide file tree
Showing 5 changed files with 704 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"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 Down
42 changes: 37 additions & 5 deletions packages/extension/src/providers/kadena/libs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,26 @@ class API implements ProviderAPIInterface {
node: string;
decimals: number;
name: string;
NETWORK_ID = "testnet04";
CHAIN_ID = "1";
API_HOST = `https://api.testnet.chainweb.com/chainweb/0.0/${this.NETWORK_ID}/chain/${this.CHAIN_ID}/pact`;
KEY_PAIR = {
publicKey: "",
secretKey: "",
};

constructor(node: string) {
this.node = node;
this.decimals = 18;
this.decimals = 7;
this.name = "Kadena";
}

public get api() {
return this;
}

init(): Promise<void> {
throw new Error("Method not implemented.");
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async init(): Promise<void> {}

getTransactionStatus(
hash: string
Expand All @@ -31,7 +37,33 @@ class API implements ProviderAPIInterface {
}

async getBalance(address: string): Promise<string> {
return "0";
const balance = await this.getBalanceAPI(address);
const balanceToString = balance.result.error
? "0".padEnd(7, "0")
: balance.result.data
.toString()
.replace(".", "")
.padEnd(7 + balance.result.data, "0");
return balanceToString;
}

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

return await Pact.fetch.local(cmd, this.API_HOST);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/extension/src/providers/kadena/networks/kadena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const kadenaOptions: KadenaNetworkOptions = {
currencyName: "KDA",
currencyNameLong: "Kadena",
icon: require("./icons/kadena-kda-logo.svg"),
decimals: 18,
decimals: 7,
prefix: 0,
node: "https://us-e1.chainweb.com/",
coingeckoID: "kadena",
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"@/*": ["./src/*"],
"@action/*": ["./src/ui/action/*"]
},
"lib": ["esnext", "dom", "dom.iterable", "scripthost"]
"lib": ["esnext", "dom", "dom.iterable", "scripthost"],
"allowJs": true
},
"include": ["src/**/*.ts", "src/**/*.vue", "tests/**/*.ts"],
"exclude": ["node_modules"]
Expand Down
Loading

0 comments on commit f037935

Please sign in to comment.