Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/get balance #125

Merged
merged 16 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/soft-flies-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effectai/sdk": patch
---

update getBalance action to return eos, efx and usdt balance.
17 changes: 10 additions & 7 deletions docs/pages/docs/token/get-balance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

## Description

This function is used to get the balance of EFX of an Effect Network account on the EOS blockchain.
There is a difference between the balance of EFX of an account on the blockchain and the balance of EFX deposited in an Effect Account.
This function returns the balance of EFX on the blockchain.
Note the use of `balance.toString()` to convert the balance to a string.
This function is used to return the blance of EFX, USDT, and EOS of an account on the EOS blockchain.
Namely the balance is what is available in the wallet of the user.

There is a difference between the balance of EFX of an account that is available in the wallet of the user and the balance of EFX that is available in an Effect Network Virtual Account.
To retrieve the EFX balance of a user in their Virtual Account, use the [getVaccount](/docs/vaccount/get-accounts.mdx)


## Usage
Expand All @@ -17,7 +18,9 @@ Note the use of `balance.toString()` to convert the balance to a string.
## Output

```
"100.0000 EFX"
378332.1630 EFX
26.7212 USDT
31.3322 EOS
```
## Parameters

Expand All @@ -34,10 +37,10 @@ Note that the account name is a Name object that is created using the `Name.from

## Returns

**Type:** Asset
**Type:** `{ efxBalance: Asset; usdtBalance: Asset; eosBalance: Asset; }`

**Description:**
Returns the balance of the account in the form of an Asset object.
Return an object with three properties: `efxBalance`, `usdtBalance`, and `eosBalance`, each of which is an Asset object.
The asset object has properties that represent the amount and symbol of the balance of the user.
Note that the Asset object has a `toString` method that can be used to convert the balance to a string.

Expand Down
12 changes: 8 additions & 4 deletions docs/snippets/token/get-balance.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createClient, getBalance, jungle4 } from "@effectai/sdk";
import { createClient, eos, getBalance, jungle4 } from "@effectai/sdk";

const client = await createClient({ network: jungle4 });
const actor = "forcedev1234";
const client = await createClient({ network: eos });
const actor = "cryptonode42";
const balance = await getBalance({ client, actor });
console.log(balance.toString());
console.log(
balance.efxBalance.toString(),
balance.usdtBalance.toString(),
balance.eosBalance.toString(),
);
14 changes: 9 additions & 5 deletions src/actions/token/getBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ import { eos, jungle4 } from "../../exports";

describe("getBalance", async () => {
test("getBalance() should retrieve balance from user on mainnet", async () => {
const client = await createClient({ network: jungle4 });
const actor = "forcedev1234";
const client = await createClient({ network: eos });
const actor = "cryptonode42";
const balance = await getBalance({ client, actor });
expect(balance).toBeDefined();
expect(balance.toString()).toBeDefined();
expect(balance.toString()).toContain("EFX");
expect(balance.efxBalance).toBeDefined();
expect(balance.usdtBalance).toBeDefined();
expect(balance.eosBalance).toBeDefined();
expect(balance.efxBalance).toBeGreaterThan(0);
expect(balance.usdtBalance).toBeGreaterThan(0);
expect(balance.eosBalance).toBeGreaterThan(0);
});

test("getBalance() should throw Error retrieving balance from unknown user.", async () => {
const client = await createClient({ network: eos });
const client = await createClient({ network: jungle4 });
const actor = "cryptonode99";
expect(async () => await getBalance({ client, actor })).toThrowError();
});
Expand Down
28 changes: 23 additions & 5 deletions src/actions/token/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,36 @@ export type GetBalanceArgs = {
export const getBalance = async ({
client,
actor,
}: GetBalanceArgs): Promise<Asset> => {
}: GetBalanceArgs): Promise<{
efxBalance: Asset;
usdtBalance: Asset;
eosBalance: Asset;
}> => {
const { network, provider } = client;
const { contracts } = network.config.efx;

const [balance] = await provider.v1.chain.get_currency_balance(
const [efxBalance] = await provider.v1.chain.get_currency_balance(
contracts.token,
actor,
);

if (!balance) {
throw new Error("No balance found");
const [usdtBalance] = await provider.v1.chain.get_currency_balance(
contracts.usdt,
actor,
);

const [eosBalance] = await provider.v1.chain.get_currency_balance(
contracts.eostoken,
actor,
);

if (!efxBalance && !usdtBalance && !eosBalance) {
throw new Error("No efxBalance found");
}

return balance;
return {
efxBalance,
usdtBalance,
eosBalance,
};
};
62 changes: 0 additions & 62 deletions src/actions/token/token.test.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/constants/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const jungle4: Network = {
contracts: {
tasks: "effecttasks2",
token: "efxtoken1112",
usdt: "tethertether",
eostoken: "eosio.token",
stake: "efxstake1111",
feepool: "efxfeepool11",
proposals: "efxproposals",
Expand All @@ -39,6 +41,8 @@ export const eos: Network = {
contracts: {
tasks: "force.efx",
token: "effecttokens",
usdt: "tethertether",
eostoken: "eosio.token",
stake: "efxstakepool",
feepool: "feepool.efx",
proposals: "daoproposals",
Expand Down
2 changes: 2 additions & 0 deletions src/types/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface EfxConfig {
contracts: {
tasks: string;
token: string;
usdt: string;
eostoken: string;
stake: string;
feepool: string;
proposals: string;
Expand Down