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

feat: add torii tokens functions to dojo sdk #339

Merged
merged 1 commit into from
Nov 26, 2024
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
25 changes: 25 additions & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,30 @@ export async function init<T extends SchemaType>(
throw error;
}
},

/**
* @param {(string)[]} contract_addresses
* @returns {Promise<Tokens>}
*/
getTokens: async (
contract_addresses: string[]
): Promise<torii.Tokens> => {
return await client.getTokens(contract_addresses);
},
Comment on lines +160 to +168
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance documentation and error handling for getTokens

The implementation looks good but could benefit from some improvements:

  1. JSDoc needs a proper description and complete param/return documentation
  2. Consider adding input validation for contract_addresses
  3. Add error handling wrapper similar to sendMessage method

Consider updating the implementation like this:

        /**
+        * Retrieves token data for specified contract addresses.
         * @param {string[]} contract_addresses - Array of contract addresses to fetch token data for
+        * @returns {Promise<torii.Tokens>} Promise resolving to token data for the specified contracts
+        * @throws {Error} If the token data fetch fails
         */
        getTokens: async (
            contract_addresses: string[]
        ): Promise<torii.Tokens> => {
+           if (!contract_addresses?.length) {
+               throw new Error("contract_addresses array cannot be empty");
+           }
+           try {
                return await client.getTokens(contract_addresses);
+           } catch (error) {
+               console.error("Failed to fetch tokens:", error);
+               throw error;
+           }
        },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @param {(string)[]} contract_addresses
* @returns {Promise<Tokens>}
*/
getTokens: async (
contract_addresses: string[]
): Promise<torii.Tokens> => {
return await client.getTokens(contract_addresses);
},
/**
* Retrieves token data for specified contract addresses.
* @param {string[]} contract_addresses - Array of contract addresses to fetch token data for
* @returns {Promise<torii.Tokens>} Promise resolving to token data for the specified contracts
* @throws {Error} If the token data fetch fails
*/
getTokens: async (
contract_addresses: string[]
): Promise<torii.Tokens> => {
if (!contract_addresses?.length) {
throw new Error("contract_addresses array cannot be empty");
}
try {
return await client.getTokens(contract_addresses);
} catch (error) {
console.error("Failed to fetch tokens:", error);
throw error;
}
},


/**
* @param {(string)[]} account_addresses
* @param {(string)[]} contract_addresses
* @returns {Promise<TokenBalances>}
*/
getTokenBalances: async (
account_addresses: string[],
contract_addresses: string[]
): Promise<torii.TokenBalances> => {
return await client.getTokenBalances(
account_addresses,
contract_addresses
);
},
Comment on lines +170 to +183
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance documentation and error handling for getTokenBalances

Similar to getTokens, this implementation needs improvements in documentation and error handling:

  1. JSDoc needs a proper description and complete param/return documentation
  2. Consider adding input validation for both arrays
  3. Add error handling wrapper

Consider updating the implementation like this:

        /**
+        * Retrieves token balances for specified accounts and contracts.
         * @param {string[]} account_addresses - Array of account addresses to fetch balances for
         * @param {string[]} contract_addresses - Array of contract addresses to fetch balances from
+        * @returns {Promise<torii.TokenBalances>} Promise resolving to token balances for the specified accounts and contracts
+        * @throws {Error} If the balance fetch fails
         */
        getTokenBalances: async (
            account_addresses: string[],
            contract_addresses: string[]
        ): Promise<torii.TokenBalances> => {
+           if (!account_addresses?.length || !contract_addresses?.length) {
+               throw new Error("Both account_addresses and contract_addresses arrays must not be empty");
+           }
+           try {
                return await client.getTokenBalances(
                    account_addresses,
                    contract_addresses
                );
+           } catch (error) {
+               console.error("Failed to fetch token balances:", error);
+               throw error;
+           }
        },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/**
* @param {(string)[]} account_addresses
* @param {(string)[]} contract_addresses
* @returns {Promise<TokenBalances>}
*/
getTokenBalances: async (
account_addresses: string[],
contract_addresses: string[]
): Promise<torii.TokenBalances> => {
return await client.getTokenBalances(
account_addresses,
contract_addresses
);
},
/**
* Retrieves token balances for specified accounts and contracts.
* @param {string[]} account_addresses - Array of account addresses to fetch balances for
* @param {string[]} contract_addresses - Array of contract addresses to fetch balances from
* @returns {Promise<torii.TokenBalances>} Promise resolving to token balances for the specified accounts and contracts
* @throws {Error} If the balance fetch fails
*/
getTokenBalances: async (
account_addresses: string[],
contract_addresses: string[]
): Promise<torii.TokenBalances> => {
if (!account_addresses?.length || !contract_addresses?.length) {
throw new Error("Both account_addresses and contract_addresses arrays must not be empty");
}
try {
return await client.getTokenBalances(
account_addresses,
contract_addresses
);
} catch (error) {
console.error("Failed to fetch token balances:", error);
throw error;
}
},

};
}
14 changes: 14 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,20 @@ export interface SDK<T extends SchemaType> {
domain?: StarknetDomain
) => TypedData;
sendMessage: (data: TypedData, account: Account) => Promise<void>;
/**
* @param {string[]} contract_addresses
* @returns {Promise<torii.Tokens>}
*/
getTokens(contract_addresses: string[]): Promise<torii.Tokens>;
/**
* @param {string[]} account_addresses
* @param {string[]} contract_addresses
* @returns {Promise<torii.TokenBalances>}
*/
getTokenBalances(
account_addresses: string[],
contract_addresses: string[]
): Promise<torii.TokenBalances>;
}

/**
Expand Down