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

Normalize address's for transactions #597

Merged
merged 1 commit into from
Aug 28, 2023
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
7 changes: 3 additions & 4 deletions src/api/hooks/useGetAccountAllTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {gql, useQuery as useGraphqlQuery} from "@apollo/client";
import {normalizeAddress} from "../../utils";

const ACCOUNT_TRANSACTIONS_COUNT_QUERY = gql`
query AccountTransactionsCount($address: String) {
Expand All @@ -18,7 +19,7 @@ export function useGetAccountAllTransactionCount(
): number | undefined {
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const addr64Hash = "0x" + address.substring(2).padStart(64, "0");
const addr64Hash = normalizeAddress(address);

const {loading, error, data} = useGraphqlQuery(
ACCOUNT_TRANSACTIONS_COUNT_QUERY,
Expand Down Expand Up @@ -50,9 +51,7 @@ export function useGetAccountAllTransactionVersions(
limit: number,
offset?: number,
): number[] {
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const addr64Hash = "0x" + address.substring(2).padStart(64, "0");
const addr64Hash = normalizeAddress(address);

const {loading, error, data} = useGraphqlQuery(ACCOUNT_TRANSACTIONS_QUERY, {
variables: {address: addr64Hash, limit: limit, offset: offset},
Expand Down
9 changes: 3 additions & 6 deletions src/api/hooks/useGetAccountTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import {
} from "aptos";
import {useGlobalState} from "../../global-config/GlobalConfig";
import {useQuery} from "@tanstack/react-query";
import {normalizeAddress} from "../../utils";

export function useGetAccountTokensCount(address: string) {
const [state] = useGlobalState();
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const addr64Hash = "0x" + address.substring(2).padStart(64, "0");
const addr64Hash = normalizeAddress(address);
return useQuery(
["account_tokens_count", {addr64Hash}, state.network_value],
async () => {
Expand All @@ -30,9 +29,7 @@ export function useGetAccountTokens(
offset?: number,
) {
const [state] = useGlobalState();
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const addr64Hash = "0x" + address.substring(2).padStart(64, "0");
const addr64Hash = normalizeAddress(address);
return useQuery(
["account_tokens", {addr64Hash, limit, offset}, state.network_value],
async () => {
Expand Down
8 changes: 3 additions & 5 deletions src/api/hooks/useGetDelegatedStakeOperationActivities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ApolloError, gql, useQuery as useGraphqlQuery} from "@apollo/client";
import {Types} from "aptos";
import {normalizeAddress} from "../../utils";

export interface DelegatedStakingActivity {
amount: number;
Expand Down Expand Up @@ -40,11 +41,8 @@ export function useGetDelegatedStakeOperationActivities(
loading: boolean;
error: ApolloError | undefined;
} {
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const delegatorAddress64Hash =
"0x" + delegatorAddress.substring(2).padStart(64, "0");
const poolAddress64Hash = "0x" + poolAddress.substring(2).padStart(64, "0");
const delegatorAddress64Hash = normalizeAddress(delegatorAddress);
const poolAddress64Hash = normalizeAddress(poolAddress);

const {loading, error, data} = useGraphqlQuery(
DELEGATED_STAKING_ACTIVITY_QUERY,
Expand Down
5 changes: 2 additions & 3 deletions src/api/hooks/useGetNumberOfDelegators.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Types} from "aptos";
import {gql, useQuery as useGraphqlQuery} from "@apollo/client";
import {normalizeAddress} from "../../utils";

const NUMBER_OF_DELEGATORS_QUERY = gql`
query numberOfDelegatorsQuery($poolAddress: String) {
Expand All @@ -16,9 +17,7 @@ const NUMBER_OF_DELEGATORS_QUERY = gql`
`;

export function useGetNumberOfDelegators(poolAddress: Types.Address) {
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const poolAddress64Hash = "0x" + poolAddress.substring(2).padStart(64, "0");
const poolAddress64Hash = normalizeAddress(poolAddress);

const {loading, error, data} = useGraphqlQuery(NUMBER_OF_DELEGATORS_QUERY, {
variables: {
Expand Down
5 changes: 2 additions & 3 deletions src/pages/Account/Tabs/CoinsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {gql, useQuery} from "@apollo/client";
import EmptyTabContent from "../../../components/IndividualPageContent/EmptyTabContent";
import {Types} from "aptos";
import {CoinsTable} from "../Components/CoinsTable";
import {normalizeAddress} from "../../../utils";

const COINS_QUERY = gql`
query CoinsData($owner_address: String, $limit: Int, $offset: Int) {
Expand All @@ -28,9 +29,7 @@ type TokenTabsProps = {
};

export default function CoinsTab({address}: TokenTabsProps) {
// whenever talking to the indexer, the address needs to fill in leading 0s
// for example: 0x123 => 0x000...000123 (61 0s before 123)
const addr64Hash = "0x" + address.substring(2).padStart(64, "0");
const addr64Hash = normalizeAddress(address);

const {loading, error, data} = useQuery(COINS_QUERY, {
variables: {
Expand Down
3 changes: 2 additions & 1 deletion src/pages/Transaction/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Types} from "aptos";
import {normalizeAddress} from "../../utils";

export type TransactionCounterparty = {
address: string;
Expand Down Expand Up @@ -105,7 +106,7 @@ function getBalanceMap(transaction: Types.Transaction) {
},
event: Types.Event,
) => {
const addr = event.guid.account_address;
Copy link

Choose a reason for hiding this comment

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

So this is the only functional change, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

correct

const addr = normalizeAddress(event.guid.account_address);

if (
event.type === "0x1::coin::DepositEvent" ||
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,9 @@ function assertType(val: any, types: string[] | string, message?: string) {
export function getStableID(): string {
return Statsig.initializeCalled() ? Statsig.getStableID() : "not_initialized";
}

// address' coming back from the node trim leading zeroes
// for example: 0x123 => 0x000...000123 (61 0s before 123)
export function normalizeAddress(address: string): string {
return "0x" + address.substring(2).padStart(64, "0");
}
Loading