-
Notifications
You must be signed in to change notification settings - Fork 52
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
Conversation
WalkthroughThis pull request introduces two new asynchronous methods to the SDK's initialization function: Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
packages/sdk/src/types.ts (1)
310-323
: LGTM! Consider enhancing the JSDoc documentation.The new token-related methods are well-structured and properly typed. However, the JSDoc documentation could be more descriptive to help users better understand these methods.
Consider expanding the JSDoc documentation as follows:
/** + * Retrieves token data for the specified contract addresses. + * * @param {string[]} contract_addresses - Array of contract addresses to fetch token data from + * @throws {Error} If the token data cannot be retrieved * @returns {Promise<torii.Tokens>} */ getTokens(contract_addresses: string[]): Promise<torii.Tokens>; /** + * Retrieves token balances for the specified accounts and contracts. + * * @param {string[]} account_addresses - Array of account addresses to check balances for * @param {string[]} contract_addresses - Array of contract addresses to check balances from + * @throws {Error} If the token balances cannot be retrieved * @returns {Promise<torii.TokenBalances>} */
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
packages/sdk/src/index.ts
(1 hunks)packages/sdk/src/types.ts
(1 hunks)
🔇 Additional comments (1)
packages/sdk/src/index.ts (1)
159-183
: Verify SDK interface type definitions
The implementation looks well-integrated with the existing code. However, we should verify that these new methods are properly defined in the SDK interface.
✅ Verification successful
Let me gather more information to verify the SDK interface definitions.
Let me try one more search with different patterns to locate the interface definitions.
SDK interface is properly defined
The verification is complete. The getTokens
and getTokenBalances
methods are correctly defined in the SDK interface in packages/sdk/src/types.ts
(lines 441-454). The interface includes proper TypeScript types and matches the implementation in index.ts
. The methods are also properly importing the required types from @dojoengine/torii-client
.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check if the new methods are properly defined in the SDK interface
# Expected: Find type definitions for getTokens and getTokenBalances in the SDK interface
# Search for SDK interface definition and its token-related methods
ast-grep --pattern 'interface SDK {
$$$
getTokens($$$): $$$
getTokenBalances($$$): $$$
$$$
}'
# Verify torii.Tokens and torii.TokenBalances types are properly imported
rg -A 2 'import.*torii-client'
Length of output: 5835
Script:
#!/bin/bash
# Search for SDK interface definition in types.ts
ast-grep --pattern 'interface SDK {
$$$
}'
# Also check for any type definitions related to tokens
rg -A 5 'type.*Token' packages/sdk/src/types.ts
Length of output: 96
Script:
#!/bin/bash
# Search for any interface definitions in types.ts
cat packages/sdk/src/types.ts
# Also check the index.ts file since it contains the implementation
cat packages/sdk/src/index.ts
Length of output: 17832
/** | ||
* @param {(string)[]} contract_addresses | ||
* @returns {Promise<Tokens>} | ||
*/ | ||
getTokens: async ( | ||
contract_addresses: string[] | ||
): Promise<torii.Tokens> => { | ||
return await client.getTokens(contract_addresses); | ||
}, |
There was a problem hiding this comment.
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:
- JSDoc needs a proper description and complete param/return documentation
- Consider adding input validation for contract_addresses
- 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.
/** | |
* @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 | ||
); | ||
}, |
There was a problem hiding this comment.
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:
- JSDoc needs a proper description and complete param/return documentation
- Consider adding input validation for both arrays
- 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.
/** | |
* @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; | |
} | |
}, |
Closes #
Introduced changes
Checklist
Summary by CodeRabbit
getTokens
: Fetches token data for specified contract addresses.getTokenBalances
: Retrieves token balances for specified accounts and contracts.These enhancements improve the SDK's capability to manage and access token information efficiently.