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

Add getBalance Method to GnoLandClient Interface #120

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions packages/gno-type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# GNO Types

TypeScript Interface for Gno.land

## Functions

- [getBalance](./actions/get-balance/get-balance.ts)
63 changes: 63 additions & 0 deletions packages/gno-type/actions/get-balance/get-balance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
getBalance,
GetBalanceParameters,
GetBalanceResponse,
} from './get-balance';

const mockGetBalance: getBalance = async (
param: GetBalanceParameters
): Promise<GetBalanceResponse> => {
return {
address: param.address,
coins: [
{
symbol: 'ugnot',
amount: '9786000000',
decimals: 8,
},
{
symbol: 'myToken',
amount: '100',
decimals: 2,
},
],
};
};

describe('getBalance', () => {
it('should return the correct balance', async () => {
const param: GetBalanceParameters = {
address: 'g1test',
};

const expectedResponse: GetBalanceResponse = {
address: 'g1test',
coins: [
{
symbol: 'ugnot',
amount: '9786000000',
decimals: 8,
},
{
symbol: 'myToken',
amount: '100',
decimals: 2,
},
],
};

const response = await mockGetBalance(param);
expect(response).toEqual(expectedResponse);
});

it('sould accept an address with the correct format', async () => {
const param: GetBalanceParameters = {
address: 'g1test',
};

const response = await mockGetBalance(param);
expect(response.address).toEqual(param.address);
});

// TODO: Add more tests
});
32 changes: 32 additions & 0 deletions packages/gno-type/actions/get-balance/get-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export type Address = `g1${string}`;

export interface Coin {
symbol: string;
amount: string;
decimals: number;
}

export interface GetBalanceParameters {
address: Address;
}

export interface GetBalanceResponse {
address: Address;
coins: Coin[];
}

/**
* Get the balance of an address
*
* @param param.address - The address to get the balance of (must start with 'g1')
*
* @returns The balance of the address
*
* @throws Error if the address is not in the correct format
* @throws Error if the address does not exist
* @throws Error if the address has no balance
* @throws Error if the address has an invalid balance
* */
export interface getBalance {
(param: GetBalanceParameters): Promise<GetBalanceResponse>;
}