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 Server.getSACBalance for fetching built-in token balance entries #1046

Merged
merged 13 commits into from
Sep 19, 2024

Conversation

Shaptic
Copy link
Contributor

@Shaptic Shaptic commented Sep 13, 2024

What

This introduces a new method to the server:

function getSACBalance(
  contractId: string,
  token: Asset,
  networkPassphrase?: string
): Promise<Api.BalanceResponse>

which returns the following schema:

export interface ContractBalanceResponse {
  latestLedger: number;
  // present only on success, otherwise request malformed or no balance
  balance?: {
    amount: string;
    authorized: boolean;
    clawback: boolean;

    lastModifiedLedgerSeq?: number;
    liveUntilLedgerSeq?: number;
  };
}

Why?

It allows developers to fetch how much of a particular SAC is stored within a contract. For example, you may have a liquidity pool contract and want to see its balance of USDC without actually invoking a function. You can achieve that by calling

const balanceEntry = await new rpc.Server("the rpc url").getSACBalance(
  "C...", // the LP contract
  new Asset("USDC", "G..." /* the issuer */),
  Networks.TESTNET, // or whatever network the contracts are built on
);

See #1003.

Copy link

github-actions bot commented Sep 13, 2024

Size Change: +32.6 kB (+0.27%)

Total Size: 12.2 MB

Filename Size Change
dist/stellar-sdk.js 6.84 MB +21.1 kB (+0.31%)
dist/stellar-sdk.min.js 5.38 MB +11.5 kB (+0.21%)

compressed-size-action

src/rpc/api.ts Outdated Show resolved Hide resolved
src/rpc/server.ts Outdated Show resolved Hide resolved
@aristidesstaffieri
Copy link

@Shaptic Do you think there would ever be confusion about being able to use this for custom tokens also?
I see that having to pass in an Asset implies that it has to be a SAC but maybe calling it getSACBalance or something would make it extra clear?

src/rpc/server.ts Outdated Show resolved Hide resolved
@sreuland
Copy link

@Shaptic Do you think there would ever be confusion about being able to use this for custom tokens also? I see that having to pass in an Asset implies that it has to be a SAC but maybe calling it getSACBalance or something would make it extra clear?

👍 , ContractBalance could allow wider interpretation, since this is implicit to only built-in token contracts of network assets, docs use the SAC terminology in description of type of built-in token contract , for consistency here on fn and response could rename - /ContractBalance/SACBalance/

@Shaptic
Copy link
Contributor Author

Shaptic commented Sep 16, 2024

Great call on that @aristidesstaffieri and @sreuland; done in 8bc0e2d!

Copy link

@aristidesstaffieri aristidesstaffieri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

src/rpc/server.ts Outdated Show resolved Hide resolved
@Shaptic Shaptic changed the title Add Server.getContractBalance for fetching contract balance entries Add Server.getSACBalance for fetching contract balance entries Sep 16, 2024
@Shaptic Shaptic changed the title Add Server.getSACBalance for fetching contract balance entries Add Server.getSACBalance for fetching built-in token balance entries Sep 16, 2024
Copy link

@sreuland sreuland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great work!

@Shaptic Shaptic merged commit 10e12cf into master Sep 19, 2024
10 checks passed
@Shaptic Shaptic deleted the get-balance branch September 19, 2024 20:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants