blocto / solana-go-sdk

Solana Golang SDK
https://blocto.github.io/solana-go-sdk/
MIT License
349 stars 86 forks source link

feat: delete PublicKey struct MarshalJSON func #138

Closed huangzhiran closed 11 months ago

yihau commented 11 months ago

sorry for the late response. could you please add some tests for this one? thank you!

huangzhiran commented 11 months ago

@yihau i found my impl is wrong when add test. if change impl to

func (p *PublicKey) UnmarshalJSON(b []byte) error {
    uj := ""
    if err := json.Unmarshal(b, &uj); err != nil {
        return err
    }
    np := PublicKeyFromString(uj)
    p = &np
    return nil
}

it still not work.

i cannot find a way to support the UnmarshalJSON() func, which json.Unmarshal() will use it . so it would be better to just del the (p *PublicKey) MarshalJSON() func . so that json.Marshal() & json.Unmarshal() can work thanks

yihau commented 11 months ago

Could you provide a reproducible code snippet so that we can try to figure it out together?

huangzhiran commented 11 months ago
package main

import (
    "encoding/json"

    "github.com/blocto/solana-go-sdk/program/system"
    "github.com/blocto/solana-go-sdk/types"
)

var feePayer, _ = types.AccountFromBytes([]byte{}) // fill your private key here (u8 array)

func main() {

    ins := []types.Instruction{
        system.Transfer(system.TransferParam{
            From:   feePayer.PublicKey,
            To:     feePayer.PublicKey,
            Amount: 1,
        }),
    }
    j, err := json.Marshal(ins)
    if err != nil {
        panic(err)
    }
    newIns := []types.Instruction{}
    if err := json.Unmarshal(j, &newIns); err != nil {
        panic(err)
    }
}

the err is

panic: json: cannot unmarshal string into Go struct field Instruction.ProgramID of type common.PublicKey

@yihau hi, thanks for the response. the err slow above, which cause by https://github.com/blocto/solana-go-sdk/blob/main/common/public_key.go#L69 there a self-defined MarshalJSON and missing the UnmarshalJSON func. I cannot find a way to add the UnmarshalJSON func, so could we just del the exist MarshalJSON func ? thanks!

yihau commented 11 months ago

thank you for the reporting. #143 should fix it. let me know if you still can't make it.