-
Notifications
You must be signed in to change notification settings - Fork 18
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(rgbpp-sdk-service): support generate_rgbpp_transfer_all_txs
API
#278
Conversation
…e, also correct snake/camel case types
b3a1da6
to
8482241
Compare
Why does a key with 0x-hex-string appear? |
May I ask why the |
@@ -0,0 +1,28 @@ | |||
import isPlainObject from 'lodash/isPlainObject.js'; | |||
|
|||
export function ensureSafeJson<Input extends object, Output = Input>(json: Input): Output { |
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.
We should avoid BigInt data in the parameter instead of converting BigInt to hex-string in the JSON stringify function.
It is recommended not to introduce unnecessary complexity, as it may lead to potential bugs.
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.
https://github.com/search?q=repo%3Ackb-cell%2Frgbpp-sdk%20%3A%20bigint%3B&type=code shows that bigint
has been used in packages/ckb/src/types
.
So, maybe this ensureSafeJson
function is necessary in the current stage?
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.
Yes. However, not all types will be converted to JSON strings. If we add the ensureSafeJson
function, we should add tests to ensure it works fine.
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.
Sure, I can add some tests for it. Bold question: Is it possible to refactor all usages of the native bigint
to use BI
and hex strings instead? Would this be considered a breaking change, or is it acceptable to refactor it partially?
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.
Only affects JSON stringify for BigInt.
So I don't think it is necessary to refactor.
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.
Updated at 27bb01e; added tests for the util functions, and the test.yml
workflow now includes the tests for rgbpp-sdk-service
. Another change is that I replaced jest
with vitest
because jest
was buggy when bundling/compiling the source code in ESM mode.
apps/service/src/utils/case.ts
Outdated
export type SnakeCased<T> = SnakeCasedPropertiesDeep<T>; | ||
export type CamelCased<T> = CamelCasedPropertiesDeep<T>; | ||
|
||
export const toSnakeCase = <T>(obj: T): SnakeCased<T> | null => { |
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.
Maybe we can use this package https://www.npmjs.com/package/camelcase-keys to convert data to snake case with deep mode and it is more simple.
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.
I think you mean: https://github.com/bendrucker/snakecase-keys
This is actually a good idea, not because the type the package provides, but because it provides a exclude
option for excluding strings from being transformed. If everything goes well, this package should help resolve the 0xe1e
to 0_xe_1e
issue in another way. I'll give it a try.
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.
Sorry, https://github.com/bendrucker/snakecase-keys is right.
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.
Updated at 27bb01e; both toSnakeCase()
and toCamelCase()
have been reimplemented using snakecase-keys
and camelcase-keys
, for the exclude
feature they provide.
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.
LGTM
…add tests for the utils
Changes
generate_rgbpp_transfer_all_txs
in the rgbpp-sdk-service for batch transferring of RGB++ xUDT assets, previously implemented at feat(rgbpp): support for batch transferring of RGBPP XUDT assets #270toSnakeCase()
andtoCamelCase()
to provide clearer return types"module": "NodeNext"
related import path issuessrc/*
path shortcut does not work.js
test.yml
workflowFound issues
Case transformation issue
The
toSnakeCase()
util transforms all object keys to snake_case, e.g.snakeCase
tosnake_case
. But when transforming theTransactionGroupSummary
object, an issue was found that if the object key is a hex string, it will also be transformed as snake_case, e.g.0xe1e
to0_xe_1e
. This is unexpected.In the current implementation of the
generate_rgbpp_transfer_all_txs
API, I wrapped the result with an utilensureSafeJson()
, which detects if the object key starts with0_x
, then all underscore marks in the key will be removed, reverting it to a regular hex string, e.g.0_xe_1e
to0xe1e
.BigInt to String issue
The requests in the
rgbpp-sdk-service
returns JSON as response. An JSON-related issue was found that it doesn't serialize BIgInt values. When an object that contains such type of value is passed toJSON.stringify()
, the function throws an error:Currently, I just transform all BIgInt values to hex strings: