implement new Solana account parsing

Change-Id: I2f59dae3950b2f95e8c0578c4f56c460f7166d2e
This commit is contained in:
Hendrik Hofstadt 2021-04-15 11:36:29 +02:00
parent f1de6086f1
commit c7c94441a5
1 changed files with 40 additions and 71 deletions

View File

@ -191,94 +191,63 @@ func (s *SolanaWatcher) Run(ctx context.Context) error {
} }
type ( type (
TransferOutProposal struct { MessagePublicationAccount struct {
Amount *big.Int VaaVersion uint8
ToChainID vaa.ChainID VaaTime time.Time
SourceAddress vaa.Address VaaSignatureAccount vaa.Address
ForeignAddress vaa.Address SubmissionTime time.Time
Asset vaa.AssetMeta Nonce uint32
Nonce uint32 EmitterChain vaa.ChainID
VAA [1001]byte EmitterAddress vaa.Address
VaaTime time.Time Payload []byte
LockupTime time.Time
PokeCounter uint8
SignatureAccount solana.PublicKey
} }
) )
func ParseTransferOutProposal(data []byte) (*TransferOutProposal, error) { func ParseTransferOutProposal(data []byte) (*MessagePublicationAccount, error) {
prop := &TransferOutProposal{} prop := &MessagePublicationAccount{}
r := bytes.NewBuffer(data) r := bytes.NewBuffer(data)
var amountBytes [32]byte // Skip initialized bool
if n, err := r.Read(amountBytes[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read amount: %w", err)
}
// Reverse (little endian -> big endian)
for i := 0; i < len(amountBytes)/2; i++ {
amountBytes[i], amountBytes[len(amountBytes)-i-1] = amountBytes[len(amountBytes)-i-1], amountBytes[i]
}
prop.Amount = new(big.Int).SetBytes(amountBytes[:])
if err := binary.Read(r, binary.LittleEndian, &prop.ToChainID); err != nil {
return nil, fmt.Errorf("failed to read to chain id: %w", err)
}
if n, err := r.Read(prop.SourceAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read source address: %w", err)
}
if n, err := r.Read(prop.ForeignAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read source address: %w", err)
}
assetMeta := vaa.AssetMeta{}
if n, err := r.Read(assetMeta.Address[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read asset meta address: %w", err)
}
if err := binary.Read(r, binary.LittleEndian, &assetMeta.Chain); err != nil {
return nil, fmt.Errorf("failed to read asset meta chain: %w", err)
}
if err := binary.Read(r, binary.LittleEndian, &assetMeta.Decimals); err != nil {
return nil, fmt.Errorf("failed to read asset meta decimals: %w", err)
}
prop.Asset = assetMeta
// Skip alignment byte
r.Next(1) r.Next(1)
if err := binary.Read(r, binary.LittleEndian, &prop.Nonce); err != nil { if err := binary.Read(r, binary.LittleEndian, &prop.VaaVersion); err != nil {
return nil, fmt.Errorf("failed to read nonce: %w", err) return nil, fmt.Errorf("failed to read to vaa version: %w", err)
} }
if n, err := r.Read(prop.VAA[:]); err != nil || n != 1001 {
return nil, fmt.Errorf("failed to read vaa: %w", err)
}
// Skip alignment bytes
r.Next(3)
var vaaTime uint32 var vaaTime uint32
if err := binary.Read(r, binary.LittleEndian, &vaaTime); err != nil { if err := binary.Read(r, binary.LittleEndian, &vaaTime); err != nil {
return nil, fmt.Errorf("failed to read vaa time: %w", err) return nil, fmt.Errorf("failed to read vaa time: %w", err)
} }
prop.VaaTime = time.Unix(int64(vaaTime), 0) prop.VaaTime = time.Unix(int64(vaaTime), 0)
var lockupTime uint32 if n, err := r.Read(prop.VaaSignatureAccount[:]); err != nil || n != 32 {
if err := binary.Read(r, binary.LittleEndian, &lockupTime); err != nil {
return nil, fmt.Errorf("failed to read lockup time: %w", err)
}
prop.LockupTime = time.Unix(int64(lockupTime), 0)
if err := binary.Read(r, binary.LittleEndian, &prop.PokeCounter); err != nil {
return nil, fmt.Errorf("failed to read poke counter: %w", err)
}
if n, err := r.Read(prop.SignatureAccount[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read signature account: %w", err) return nil, fmt.Errorf("failed to read signature account: %w", err)
} }
var submissionTime uint32
if err := binary.Read(r, binary.LittleEndian, &submissionTime); err != nil {
return nil, fmt.Errorf("failed to read lockup time: %w", err)
}
prop.SubmissionTime = time.Unix(int64(submissionTime), 0)
if err := binary.Read(r, binary.LittleEndian, &prop.Nonce); err != nil {
return nil, fmt.Errorf("failed to read nonce: %w", err)
}
if err := binary.Read(r, binary.LittleEndian, &prop.EmitterChain); err != nil {
return nil, fmt.Errorf("failed to read emitter chain: %w", err)
}
if n, err := r.Read(prop.EmitterAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read emitter address: %w", err)
}
payload := make([]byte, 1000)
n, err := r.Read(payload)
if err != nil || n == 0 {
return nil, fmt.Errorf("failed to read vaa: %w", err)
}
prop.Payload = payload[:n]
return prop, nil return prop, nil
} }