punm parser
This commit is contained in:
49
parser.go
49
parser.go
@@ -17,40 +17,45 @@ var actionPrograms = map[solana.PublicKey]actionParser{
|
||||
budgGetProgram: budgetParser,
|
||||
}
|
||||
|
||||
func Parser(rawTx *RawTx) (*Tx, error) {
|
||||
accountList := rawTx.getAccountList()
|
||||
var tx = &Tx{
|
||||
TxHash: (*[64]byte)((rawTx.Transaction.Signatures[0][:])),
|
||||
Signer: rawTx.GetSigner(),
|
||||
Block: rawTx.Slot,
|
||||
BlockIndex: uint64(rawTx.IndexWithinBlock),
|
||||
BlockAt: rawTx.BlockTime,
|
||||
|
||||
BeforeSolBalance: decimal.NewFromUint64(rawTx.Meta.PreBalances[0]).Div(decimal.NewFromInt(1e9)),
|
||||
AfterSOLBalance: decimal.NewFromUint64(rawTx.Meta.PostBalances[0]).Div(decimal.NewFromInt(1e9)),
|
||||
func (tx *Tx) Parser() error {
|
||||
if tx.rawTx == nil {
|
||||
return errors.New("rawTx is nil")
|
||||
}
|
||||
accountList := tx.rawTx.getAccountList()
|
||||
|
||||
tx.TxHash = (*[64]byte)((tx.rawTx.Transaction.Signatures[0][:]))
|
||||
tx.Signer = tx.rawTx.GetSigner()
|
||||
tx.Block = tx.rawTx.Slot
|
||||
tx.BlockIndex = uint64(tx.rawTx.IndexWithinBlock)
|
||||
tx.BlockAt = tx.rawTx.BlockTime
|
||||
|
||||
tx.BeforeSolBalance = decimal.NewFromUint64(tx.rawTx.Meta.PreBalances[0]).Div(decimal.NewFromInt(1e9))
|
||||
tx.AfterSOLBalance = decimal.NewFromUint64(tx.rawTx.Meta.PostBalances[0]).Div(decimal.NewFromInt(1e9))
|
||||
|
||||
tx.Token = make(map[solana.PublicKey]TokenMeta)
|
||||
|
||||
var innersMap = make(map[int]InnerInstructions)
|
||||
for _, inner := range rawTx.Meta.InnerInstructions {
|
||||
for _, inner := range tx.rawTx.Meta.InnerInstructions {
|
||||
innersMap[inner.Index] = inner
|
||||
}
|
||||
for i, instr := range rawTx.Transaction.Message.Instructions {
|
||||
for i, instr := range tx.rawTx.Transaction.Message.Instructions {
|
||||
programAccount := accountList[instr.ProgramIDIndex]
|
||||
if p, exists := swapPrograms[programAccount]; exists {
|
||||
swaps, _, err := p(rawTx, instr, innersMap[i], [2]uint{uint(i), uint(0)})
|
||||
swaps, _, err := p(tx, instr, innersMap[i], [2]uint{uint(i), uint(0)})
|
||||
if err != nil {
|
||||
if errors.Is(err, InstructionIgnoredError) {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
tx.Swaps = append(tx.Swaps, swaps...)
|
||||
} else if p, exists := actionPrograms[programAccount]; exists {
|
||||
_, err := p(rawTx, instr, innersMap[i], [2]uint{uint(i), uint(0)}, tx)
|
||||
_, err := p(tx, instr, innersMap[i], [2]uint{uint(i), uint(0)})
|
||||
if err != nil {
|
||||
if errors.Is(err, InstructionIgnoredError) {
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
ii := i
|
||||
@@ -61,25 +66,25 @@ func Parser(rawTx *RawTx) (*Tx, error) {
|
||||
innerProgramAccount := accountList[innerInstr.ProgramIDIndex]
|
||||
|
||||
if p, exists := swapPrograms[innerProgramAccount]; exists {
|
||||
swaps, offset, err := p(rawTx, innerInstr, innersMap[i], [2]uint{uint(i), uint(j)})
|
||||
swaps, offset, err := p(tx, innerInstr, innersMap[i], [2]uint{uint(i), uint(j)})
|
||||
if err != nil {
|
||||
if errors.Is(err, InstructionIgnoredError) {
|
||||
j = int(offset[1])
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
tx.Swaps = append(tx.Swaps, swaps...)
|
||||
j = int(offset[1])
|
||||
ii = int(offset[0])
|
||||
} else if p, exists := actionPrograms[innerProgramAccount]; exists {
|
||||
offset, err := p(rawTx, innerInstr, innersMap[i], [2]uint{uint(i), uint(j)}, tx)
|
||||
offset, err := p(tx, innerInstr, innersMap[i], [2]uint{uint(i), uint(j)})
|
||||
if err != nil {
|
||||
if errors.Is(err, InstructionIgnoredError) {
|
||||
j = int(offset[1])
|
||||
continue
|
||||
}
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
j = int(offset[1])
|
||||
ii = int(offset[0])
|
||||
@@ -93,5 +98,5 @@ func Parser(rawTx *RawTx) (*Tx, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return tx, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user