minor features and improvements#13
Conversation
|
|
||
| type PublicKey [PublicKeyLength]byte | ||
|
|
||
| func (pk *PublicKey) UnmarshalJSON(b []byte) error { |
There was a problem hiding this comment.
Would you add test for this ?
There was a problem hiding this comment.
i'll handle this a bit later
| } | ||
| } | ||
|
|
||
| func (a *Account) UnmarshalText(b []byte) error { |
There was a problem hiding this comment.
I more prefer AccountFromHex. WDYT
There was a problem hiding this comment.
UnmarshalText is an interface from the standard library, to support some parsing libraries.
There was a problem hiding this comment.
Oh! Ok. Would you add some test for it?
| // MintAuthorityOption uint32 | ||
| MintAuthority *common.PublicKey | ||
| Supply uint64 | ||
| Decimals uint8 | ||
| IsInitialized bool | ||
| // FreezeAuthorityOption uint32 | ||
| FreezeAuthority *common.PublicKey |
There was a problem hiding this comment.
you can remove the // MintAuthorityOption uint32 and // FreezeAuthorityOption uint32.
| const ( | ||
| mintMintAuthorityOptionOffset = 0 | ||
| mintMintAuthorityOffset = mintMintAuthorityOptionOffset + 4 | ||
| mintSupplyOffset = mintMintAuthorityOffset + 32 | ||
| mintDecimalsOffset = mintSupplyOffset + 8 | ||
| mintIsInitializedOffset = mintDecimalsOffset + 1 | ||
| mintFreezeAuthorityOptionOffset = mintIsInitializedOffset + 1 | ||
| mintFreezeAuthorityOffset = mintFreezeAuthorityOptionOffset + 4 | ||
| ) | ||
|
|
||
| func isSome(option []byte) bool { | ||
| return bytes.Equal(option, Some) | ||
| } | ||
|
|
||
| func MintAccountFromData(data []byte) (*MintAccount, error) { | ||
| if len(data) != MintAccountSize { | ||
| return nil, fmt.Errorf("mint account data length mismatch") | ||
| } | ||
|
|
||
| var mint MintAccount | ||
|
|
||
| mintAuthorityOption := data[0:4] | ||
| if isSome(mintAuthorityOption) { | ||
| key := common.PublicKeyFromBytes(data[mintMintAuthorityOffset : mintSupplyOffset+32]) | ||
| mint.MintAuthority = &key | ||
| } | ||
|
|
||
| mint.Supply = binary.LittleEndian.Uint64(data[mintSupplyOffset : mintSupplyOffset+8]) | ||
| mint.Decimals = uint8(data[mintDecimalsOffset]) | ||
| mint.IsInitialized = data[mintIsInitializedOffset] == 1 | ||
|
|
||
| if isSome(data[mintFreezeAuthorityOptionOffset:mintFreezeAuthorityOptionOffset+4]) { | ||
| key := common.PublicKeyFromBytes(data[mintFreezeAuthorityOffset : mintFreezeAuthorityOffset+32]) | ||
| mint.FreezeAuthority = &key | ||
| } | ||
|
|
||
| return &mint, nil |
There was a problem hiding this comment.
I guess maybe you want to make code more readable but I would like to use literal number like I decode token account and would you add a test for this?
There was a problem hiding this comment.
how important is this to you to use literal number? using literal offsets is pretty unreadable to be honest.
There was a problem hiding this comment.
I want to use literal number for some reasons.
- if someday I want to use get accountInfo to fetch data and using offset. the literal number is more intuitive to me. I don't need to recalculate it if I miss in the bytes array.
- I don't think there are someone will read this code, I want to make them as clear as possible.
There was a problem hiding this comment.
if someday I want to use get accountInfo to fetch data and using offset.
if that's the case, wouldn't you want to export the offset constants rather than using literals?
1a94545 to
55b476d
Compare
No description provided.