Skip to content

Commit

Permalink
Handle InsufficientFundsForRent
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Oct 17, 2024
1 parent 31e0b32 commit 1b3b902
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions solana-errors/from-json-to-protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package solanaerrors

import (
"bytes"
"encoding/json"
"fmt"

bin "github.com/gagliardetto/binary"
Expand Down Expand Up @@ -89,21 +90,59 @@ func FromJSONToProtobuf(j map[string]interface{}) ([]byte, error) {
return nil, err
}

return buf.Bytes(), nil
case InsufficientFundsForRent:
doer.Do("write transactionErrorType", func() error {
return wr.WriteUint32(uint32(TransactionErrorType_INSUFFICIENT_FUNDS_FOR_RENT), bin.LE)
})
// write the accountIndex
{
// "{\"InsufficientFundsForRent\":{\"account_index\":2}}"
// read accountIndex
object, ok := j[InsufficientFundsForRent].(map[string]any)
if !ok {
return nil, fmt.Errorf("expected an object")
}
accountIndexFloat, ok := object["account_index"].(float64)
if !ok {
return nil, fmt.Errorf("expected a float64")
}
accountIndex := uint8(accountIndexFloat)
doer.Do("write accountIndex", func() error {
return wr.WriteUint8(accountIndex)
})

if err := doer.Err(); err != nil {
return nil, err
}
}

return buf.Bytes(), nil

default:
return nil, fmt.Errorf("unhandled error type: %s", firstKey)
return nil, fmt.Errorf("unhandled error type: %s from %q", firstKey, toJsonString(j))
}
}

func toJsonString(v interface{}) string {
j, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("%v", v)
}
return string(j)
}

func getFirstKey(m map[string]interface{}) string {
for k := range m {
return k
}
return ""
}

const InstructionError = "InstructionError"
const (
InstructionError = "InstructionError"
InsufficientFundsForRent = "InsufficientFundsForRent"
)

type ChainOps struct {
e error
Expand Down

0 comments on commit 1b3b902

Please sign in to comment.