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

chore: return spendable and actual balance in rpc api getErc20Balance #308

Merged
merged 22 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
7dbedb0
fix: allow multiple consecutive pending transactions in estimateFee a…
khanti42 Jul 11, 2024
132b4a4
Merge branch 'main' into fix/sf-656
khanti42 Jul 12, 2024
5ea1806
fix: remove try catch block. Just check if nonce provided
khanti42 Jul 12, 2024
d8ad095
test: estimateFee.test.ts should rely only on estimateFeeBulk
khanti42 Jul 12, 2024
7269c0b
Merge branch 'main' into fix/sf-656
khanti42 Jul 25, 2024
1cbda3d
chore: lint + prettier
khanti42 Jul 25, 2024
e6f2e5f
refactor: function account nounce on block naming
khanti42 Jul 29, 2024
a8106ed
refactor: removed gasConsumed and gasPrice in estamiteFee
khanti42 Jul 29, 2024
02d329c
fix: nonce undefined
khanti42 Jul 30, 2024
973fceb
revert: yarn.lock and snap.manifest.json from main
khanti42 Jul 30, 2024
70207c4
Merge branch 'main' into fix/sf-656
khanti42 Jul 30, 2024
d8bd712
fix: use BlockIdentifierEnum
khanti42 Jul 30, 2024
21e6d5b
fix: update packages/starknet-snap/src/utils/starknetUtils.ts
khanti42 Jul 30, 2024
d5d64d7
feat: sets blockIdentifier on getProvider no need for getAccountNonce…
khanti42 Jul 30, 2024
9dac4f5
chore: lint + prettier
khanti42 Jul 30, 2024
3e4aa15
fix: pending/latest balance
khanti42 Jul 31, 2024
7607371
feat: support spendable balance in case of unconfirmed amount
khanti42 Jul 31, 2024
b0f0d6a
revert: wallet-ui changes
khanti42 Jul 31, 2024
41930bf
test: update getErc20TokenBalance.test.ts
khanti42 Jul 31, 2024
10af5b0
feat: let consumer app decide the logic to use to display balance
khanti42 Aug 2, 2024
897d803
chore: lint + prettier
khanti42 Aug 2, 2024
ba7e84d
Merge branch 'main' into feat/sf-670-backup
khanti42 Aug 2, 2024
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
31 changes: 15 additions & 16 deletions packages/starknet-snap/src/getErc20TokenBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import { BlockIdentifierEnum } from './utils/constants';
import { logger } from './utils/logger';
import { toJson } from './utils/serializer';
import { getNetworkFromChainId } from './utils/snapUtils';
import {
getBalance,
isAccountDeployed,
validateAndParseAddress,
} from './utils/starknetUtils';
import { getBalance, validateAndParseAddress } from './utils/starknetUtils';

/**
*
Expand Down Expand Up @@ -53,23 +49,26 @@ export async function getErc20TokenBalance(params: ApiParams) {
`getErc20Balance:\nerc20Address: ${erc20Address}\nuserAddress: ${userAddress}`,
);

// For deployed accounts, use the PENDING block to show balance updates as soon as possible,
// facilitating concurrent transactions without delays.
// For non-deployed accounts, use the LATEST block to avoid displaying non-zero balances
// from pending transactions, because confirmed non-zero balance is required for deployment.
const blockIdentifier = (await isAccountDeployed(network, userAddress))
? BlockIdentifierEnum.Pending
: BlockIdentifierEnum.Latest;
const balance = await getBalance(
const balanceLatest = await getBalance(
userAddress,
erc20Address,
network,
blockIdentifier,
BlockIdentifierEnum.Latest,
);
const balancePending = await getBalance(
userAddress,
erc20Address,
network,
BlockIdentifierEnum.Pending,
);

logger.log(`getErc20Balance:\nresp: ${toJson(balance)}`);
const resp = {
balancePending,
balanceLatest,
khanti42 marked this conversation as resolved.
Show resolved Hide resolved
};
logger.log(`getErc20Balance:\nresp: ${toJson(resp)}`);

return balance;
return resp;
} catch (error) {
logger.error(`Problem found:`, error);
throw error;
Expand Down
15 changes: 12 additions & 3 deletions packages/starknet-snap/test/src/getErc20TokenBalance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ describe('Test function: getErc20TokenBalance', function () {
};
apiParams.requestParams = requestObject;
const result = await getErc20TokenBalance(apiParams);
expect(result).to.be.eq('0x64a');
expect(result).to.be.eql({
balanceLatest: hexAmount,
balancePending: hexAmount,
});
});

it('should get ERC-20 token balance with BlockIdentifier pending if the account is deployed', async function () {
khanti42 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -69,7 +72,10 @@ describe('Test function: getErc20TokenBalance', function () {
};
apiParams.requestParams = requestObject;
const result = await getErc20TokenBalance(apiParams);
expect(result).to.be.eq('0x64a');
expect(result).to.be.eql({
balanceLatest: hexAmount,
balancePending: hexAmount,
});
expect(stub).to.have.been.calledWith(
requestObject.userAddress,
requestObject.tokenAddress,
Expand All @@ -93,7 +99,10 @@ describe('Test function: getErc20TokenBalance', function () {
};
apiParams.requestParams = requestObject;
const result = await getErc20TokenBalance(apiParams);
expect(result).to.be.eq('0x64a');
expect(result).to.be.eql({
balanceLatest: hexAmount,
balancePending: hexAmount,
});
expect(stub).to.have.been.calledWith(
requestObject.userAddress,
requestObject.tokenAddress,
Expand Down
2 changes: 1 addition & 1 deletion packages/wallet-ui/src/services/useStarkNetSnap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export const useStarkNetSnap = () => {
},
},
});
return response;
return response.balanceLatest;
} catch (err) {
//eslint-disable-next-line no-console
console.error(err);
Expand Down
Loading