Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.
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
32 changes: 32 additions & 0 deletions docs/sdk.thirdwebsdk.getbalance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@thirdweb-dev/sdk](./sdk.md) &gt; [ThirdwebSDK](./sdk.thirdwebsdk.md) &gt; [getBalance](./sdk.thirdwebsdk.getbalance.md)

## ThirdwebSDK.getBalance() method

Get the native balance of a given address (wallet or contract)

<b>Signature:</b>

```typescript
getBalance(address: string): Promise<CurrencyValue>;
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| address | string | the address to check the balance for |

<b>Returns:</b>

Promise&lt;[CurrencyValue](./sdk.currencyvalue.md)<!-- -->&gt;

## Example


```javascript
const balance = await sdk.getBalance("0x...");
console.log(balance.displayValue);
```

1 change: 1 addition & 0 deletions docs/sdk.thirdwebsdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export declare class ThirdwebSDK extends RPCConnectionHandler
| --- | --- | --- |
| [fromPrivateKey(privateKey, network, options, storage)](./sdk.thirdwebsdk.fromprivatekey.md) | <code>static</code> | <b><i>(BETA)</i></b> Get an instance of the thirdweb SDK based on a private key. |
| [fromSigner(signer, network, options, storage)](./sdk.thirdwebsdk.fromsigner.md) | <code>static</code> | <b><i>(BETA)</i></b> Get an instance of the thirdweb SDK based on an existing ethers signer |
| [getBalance(address)](./sdk.thirdwebsdk.getbalance.md) | | Get the native balance of a given address (wallet or contract) |
| [getContract(address)](./sdk.thirdwebsdk.getcontract.md) | | <b><i>(BETA)</i></b> Get an instance of a Custom ThirdwebContract |
| [getContractFromAbi(address, abi)](./sdk.thirdwebsdk.getcontractfromabi.md) | | <b><i>(BETA)</i></b> Get an instance of a Custom contract from a json ABI |
| [getContractList(walletAddress)](./sdk.thirdwebsdk.getcontractlist.md) | | Return all the contracts deployed by the specified address |
Expand Down
1 change: 1 addition & 0 deletions etc/sdk.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4988,6 +4988,7 @@ export class ThirdwebSDK extends RPCConnectionHandler {
//
// @beta
static fromSigner(signer: Signer, network?: ChainOrRpc, options?: SDKOptions, storage?: IStorage): ThirdwebSDK;
getBalance(address: string): Promise<CurrencyValue>;
// @internal (undocumented)
getBuiltInContract<TContractType extends ContractType = ContractType>(address: string, contractType: TContractType): ContractForContractType<TContractType>;
// @beta
Expand Down
20 changes: 20 additions & 0 deletions src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ import {
ChainOrRpc,
getProviderForNetwork,
getReadOnlyProvider,
NATIVE_TOKEN_ADDRESS,
} from "../constants";
import { UserWallet } from "./wallet/UserWallet";
import { Multiwrap } from "../contracts/multiwrap";
import { WalletAuthenticator } from "./auth/wallet-authenticator";
import { CurrencyValue } from "../types/index";
import { fetchCurrencyValue } from "../common/currency";

/**
* The main entry point for the thirdweb SDK
Expand Down Expand Up @@ -466,6 +469,23 @@ export class ThirdwebSDK extends RPCConnectionHandler {
return contract;
}

/**
* Get the native balance of a given address (wallet or contract)
* @example
* ```javascript
* const balance = await sdk.getBalance("0x...");
* console.log(balance.displayValue);
* ```
* @param address - the address to check the balance for
*/
public async getBalance(address: string): Promise<CurrencyValue> {
return fetchCurrencyValue(
this.getProvider(),
NATIVE_TOKEN_ADDRESS,
await this.getProvider().getBalance(address),
);
}

/**
* @internal
*/
Expand Down
12 changes: 11 additions & 1 deletion test/split.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { assert } from "chai";
import { assert, expect } from "chai";
import { sdk, signers } from "./before-setup";
import { Split, Token } from "../src";
import { ethers } from "ethers";

global.fetch = require("cross-fetch");

Expand Down Expand Up @@ -41,6 +42,15 @@ describe("Splits Contract", async () => {
splitsContract = sdk.getSplit(address);
});

it("should fetch contract balance", async () => {
await adminWallet.sendTransaction({
to: splitsContract.getAddress(),
value: ethers.utils.parseEther("1.2"),
});
const balance = await sdk.getBalance(splitsContract.getAddress());
expect(balance.displayValue).to.eq("1.2");
});

// TODO: Fix bug in the `getAllRecipients` function
it("should return all recipients of splits", async () => {
const recipients = await splitsContract.getAllRecipients();
Expand Down