diff --git a/docs/sdk.thirdwebsdk.getbalance.md b/docs/sdk.thirdwebsdk.getbalance.md new file mode 100644 index 000000000..1108fb727 --- /dev/null +++ b/docs/sdk.thirdwebsdk.getbalance.md @@ -0,0 +1,32 @@ + + +[Home](./index.md) > [@thirdweb-dev/sdk](./sdk.md) > [ThirdwebSDK](./sdk.thirdwebsdk.md) > [getBalance](./sdk.thirdwebsdk.getbalance.md) + +## ThirdwebSDK.getBalance() method + +Get the native balance of a given address (wallet or contract) + +Signature: + +```typescript +getBalance(address: string): Promise; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| address | string | the address to check the balance for | + +Returns: + +Promise<[CurrencyValue](./sdk.currencyvalue.md)> + +## Example + + +```javascript +const balance = await sdk.getBalance("0x..."); +console.log(balance.displayValue); +``` + diff --git a/docs/sdk.thirdwebsdk.md b/docs/sdk.thirdwebsdk.md index 256121816..3d833620d 100644 --- a/docs/sdk.thirdwebsdk.md +++ b/docs/sdk.thirdwebsdk.md @@ -34,6 +34,7 @@ export declare class ThirdwebSDK extends RPCConnectionHandler | --- | --- | --- | | [fromPrivateKey(privateKey, network, options, storage)](./sdk.thirdwebsdk.fromprivatekey.md) | static | (BETA) Get an instance of the thirdweb SDK based on a private key. | | [fromSigner(signer, network, options, storage)](./sdk.thirdwebsdk.fromsigner.md) | static | (BETA) 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) | | (BETA) Get an instance of a Custom ThirdwebContract | | [getContractFromAbi(address, abi)](./sdk.thirdwebsdk.getcontractfromabi.md) | | (BETA) 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 | diff --git a/etc/sdk.api.md b/etc/sdk.api.md index 5de2cd6df..0e78ab136 100644 --- a/etc/sdk.api.md +++ b/etc/sdk.api.md @@ -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; // @internal (undocumented) getBuiltInContract(address: string, contractType: TContractType): ContractForContractType; // @beta diff --git a/src/core/sdk.ts b/src/core/sdk.ts index 8ef722400..45e991359 100644 --- a/src/core/sdk.ts +++ b/src/core/sdk.ts @@ -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 @@ -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 { + return fetchCurrencyValue( + this.getProvider(), + NATIVE_TOKEN_ADDRESS, + await this.getProvider().getBalance(address), + ); + } + /** * @internal */ diff --git a/test/split.test.ts b/test/split.test.ts index 78f5e08e6..cf8040667 100644 --- a/test/split.test.ts +++ b/test/split.test.ts @@ -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"); @@ -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();