package helpers import ( "encoding/binary" "ripple/commands" "ripple/state" "ripple/types" ) func buildCommitOrInit(paymentID [32]byte, amount uint64, cleanupRate uint32, fee uint64) []byte { buf := make([]byte, 52) copy(buf[:32], paymentID[:]) binary.BigEndian.PutUint64(buf[32:40], amount) binary.BigEndian.PutUint32(buf[40:44], cleanupRate) binary.BigEndian.PutUint64(buf[44:52], fee) return buf } func BuildInitPayment(paymentID [32]byte, amount uint64, cleanupRate uint32, fee uint64) types.Instruction { return types.Instruction{ Command: commands.LOCKSTEP_INIT_PAYMENT, Arguments: buildCommitOrInit(paymentID, amount, cleanupRate, fee), } } func BuildCommitPayment(paymentID [32]byte, amount uint64, cleanupRate uint32, fee uint64) types.Instruction { return types.Instruction{ Command: commands.LOCKSTEP_COMMIT_PAYMENT, Arguments: buildCommitOrInit(paymentID, amount, cleanupRate, fee), } } func BuildSealPayment(paymentID [32]byte, p state.Payment) types.Instruction { buf := make([]byte, 40) copy(buf[:32], paymentID[:]) binary.BigEndian.PutUint64(buf[32:40], p.PenaltyTicker()) return types.Instruction{ Command: commands.LOCKSTEP_SEAL_PAYMENT, Arguments: buf, } } func buildCancelOrFinalize(paymentID [32]byte, preimage [32]byte, amount uint64) []byte { buf := make([]byte, 72) copy(buf[:32], paymentID[:]) copy(buf[32:64], preimage[:]) binary.BigEndian.PutUint64(buf[64:72], amount) return buf } func buildCancelPaymentInstr(paymentID [32]byte, preimage [32]byte, amount uint64) types.Instruction { return types.Instruction{ Command: commands.LOCKSTEP_CANCEL_PAYMENT, Arguments: buildCancelOrFinalize(paymentID, preimage, amount), } } func buildFinalizePaymentInstr(paymentID [32]byte, preimage [32]byte, amount uint64) types.Instruction { return types.Instruction{ Command: commands.LOCKSTEP_FINALIZE_PAYMENT, Arguments: buildCancelOrFinalize(paymentID, preimage, amount), } } func BuildFinalizePayment(paymentID [32]byte, p state.Payment) types.Instruction { penaltyTicker := p.PenaltyTicker() fee := p.CalculateFeeIn(penaltyTicker) var penalty uint64 if penaltyTicker > p.CommitPenalty { penalty = penaltyTicker - p.CommitPenalty } return buildFinalizePaymentInstr(paymentID, p.Preimage, p.Amount-penalty+fee) } func BuildCancelOutgoing(paymentID [32]byte, p state.Payment) types.Instruction { penalty := p.PenaltyTicker() fee := p.CalculateFeeOut(penalty) return buildCancelPaymentInstr(paymentID, p.Preimage, penalty+fee) } func BuildCancelIncoming(paymentID [32]byte, p state.Payment) types.Instruction { penalty := p.PenaltyTicker() fee := p.CalculateFeeIn(penalty) return buildCancelPaymentInstr(paymentID, p.Preimage, penalty+fee) } func BuildCancelAuto(id [32]byte, p state.Payment) (bool, types.UserIdentifier, types.Instruction) { switch { case p.Incoming != (types.UserIdentifier{}): return true, p.Incoming, BuildCancelIncoming(id, p) case p.Outgoing != (types.UserIdentifier{}): return true, p.Outgoing, BuildCancelOutgoing(id, p) default: return false, types.UserIdentifier{}, types.Instruction{} } } func BuildCleanupPayment(paymentID [32]byte) types.Instruction { return types.Instruction{ Command: commands.LOCKSTEP_INIT_PAYMENT, Arguments: paymentID[:], } }