aptos-labs / aptos-go-sdk

Aptos Go SDK
https://aptos.dev/sdks/go-sdk
Apache License 2.0
11 stars 16 forks source link

[Feature Request][Go SDK]Create struct tag from string #76

Open dkatzan opened 4 months ago

dkatzan commented 4 months ago

🚀 Feature Request Description

Add support for creating a struct tag from a string

Motivation

There are a few helper functions to create a few basic struct tag, such as NewStringTag, NewOptionTag, and NewObjectTag

I would like to have a new function, that can parse a string: NewStructTagFromString that can parse (recursively) and build a struct tag

the motivation is to be able to easily build transfer transactions from the name of a coin

e.g. here is a snippet, of how I implemented a basic function to do so:

    coinTypeStrParts := strings.Split(coinTypeStr, "::")
    accountAddress := aptosSdk.AccountAddress{}
    err := accountAddress.ParseStringRelaxed(coinTypeStrParts[0])
    if err != nil {
        return aptosSdk.TypeTag{}, err
    }
    typedArgument := aptosSdk.TypeTag{
        Value: &aptosSdk.StructTag{
            Address: accountAddress,
            Module:  coinTypeStrParts[1],
            Name:    coinTypeStrParts[2],
        },
    }
    return typedArgument, nil

but this function will fail to build a proper struct tag in a recursive case, e.g. for LP coins like: 0x31a6675cbe84365bf2b0cbce617ece6c47023ef70826533bde5203d32171dc3c::swap::LPToken<0x1::aptos_coin::AptosCoin, 0xf22bede237a07e121b56d91a491eb7bcdfd1f5907926a9e58338f964a01b17fa::asset::USDC>

This function, does exist in the python SDK: https://github.com/aptos-labs/aptos-python-sdk/blob/e5e1ecb0a6bea419a24098f17582798cdfc7be56/aptos_sdk/type_tag.py#L311

and it is parsing the string and building the struct recursively from the string

gregnazario commented 4 months ago

Yep, it exists in TypeScript and Python, needs to be migrated over