gagliardetto / solana-go

Go SDK library and RPC client for the Solana Blockchain
Apache License 2.0
901 stars 260 forks source link

Do you know how hard I tried to get the TransactionDetails? #150

Open yangyile1990 opened 1 year ago

yangyile1990 commented 1 year ago

func (s Solana) GetLog(blockNum int64, mapToken map[string]anychain.Token) ([]anychain.ContractTokenTran, cerror.CError) { latestBlockNum, erc := s.GetBlockNum() if erc != nil { return nil, erc }

if false {
    block, err := s.client2.GetBlockWithConfig(context.TODO(), uint64(blockNum), client.GetBlockConfig{
        Commitment:         rpc2.CommitmentFinalized,
        TransactionDetails: rpc2.GetBlockConfigTransactionDetailsFull,
        Rewards:            nil,
    })
    if err != nil {
        return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, err.Error())
    }
    fmt.Println(utjson.NeatString(block))
} else {
    out, err := s.client.GetBlockWithOpts(context.TODO(), uint64(blockNum), &rpc.GetBlockOpts{
        Encoding:   solana.EncodingBase64,
        Commitment: rpc.CommitmentFinalized,
        // Get only signatures:
        TransactionDetails: rpc.TransactionDetailsFull,
        // Exclude rewards:
        //Rewards: &includeRewards,
    })
    if err != nil {
        return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, err.Error())
    }
    //fmt.Println(utjson.NeatString(out))

    var res = make([]*anychain.ContractTokenTran, 0, len(out.Transactions))
    for i, x := range out.Transactions {
        transaction, err := x.GetTransaction()
        if err != nil {
            panic(err)
        }
        fmt.Println(utjson.NeatString(transaction))
        if i > 3 {
            break
        }
        res = append(res, &anychain.ContractTokenTran{
            Chain:             SOLANA,
            TxId:              transaction.Signatures[0].String(),
            TransferTimestamp: out.BlockTime.Time().UnixMilli(),
            BlockNum:          int64(*out.BlockHeight),
            Confirmations:     latestBlockNum - int64(*out.BlockHeight),
            Result:            true,
            Remark:            "SUCCESS",
            FeeSymbol:         SOL,
            FeeSymbolPrice:    "0.1",
            FeeAmountCoin:     "0.1",
            FeeAmountUsdt:     "0.1",
            Transfers:         []*anychain.CallbackTransfer{},
        })
        list, err := transaction.AccountMetaList()
        if err != nil {
            return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, err.Error())
        }
        fmt.Println(utjson.NeatString(list))

        //instruction, err := vote.DecodeInstruction(list, transaction.Message.Instructions[0].Data)
        //if err != nil {
        //  return nil, cerror.NewError(cerror.CODE_THIRD_REQ_ERROR, err.Error())
        //}
        //fmt.Println(utjson.NeatString(instruction))

        //dataBts, err := base58.Decode(transaction.Message.Instructions[0].Data)
        //if err != nil {
        //  t.Fatal(err)
        //}
    }
    out.Transactions = nil
    fmt.Println(utjson.NeatString(out))

    return res, nil
}
return nil, nil

}

How to parse the (transaction.Message.Instructions[0].Data)?

gagliardetto commented 1 year ago

Each Solana program (aka "contract" as they are called in Ethereum) has its own data format. It might be borsh, it might be something completely original.

You need to know the layout of the data that you are trying to parse, and then write a parser for it.