Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions xrpl/rpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1455,3 +1455,122 @@ func (c *countingReadCloser) Close() error {
c.closeFunc()
return nil
}

func TestClient_calculateConfidentialMPTFees(t *testing.T) {
confidentialTypes := []string{
transaction.ConfidentialMPTSendTx.String(),
transaction.ConfidentialMPTConvertTx.String(),
transaction.ConfidentialMPTConvertBackTx.String(),
transaction.ConfidentialMPTMergeInboxTx.String(),
transaction.ConfidentialMPTClawbackTx.String(),
}

for _, txType := range confidentialTypes {
t.Run(txType, func(t *testing.T) {
client := setupConfidentialFeeTestClient(t, 2)
tx := transaction.FlatTransaction{"TransactionType": txType}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "100", tx["Fee"])
})
}

t.Run("ordinary transaction", func(t *testing.T) {
client := setupConfidentialFeeTestClient(t, 2)
tx := transaction.FlatTransaction{"TransactionType": transaction.PaymentTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "10", tx["Fee"])
})

t.Run("multisigned confidential transaction", func(t *testing.T) {
client := setupConfidentialFeeTestClient(t, 2)
tx := transaction.FlatTransaction{"TransactionType": transaction.ConfidentialMPTSendTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 2))
require.Equal(t, "120", tx["Fee"])
})

t.Run("confidential transaction max fee", func(t *testing.T) {
client := setupConfidentialFeeTestClient(t, 0.00005)
tx := transaction.FlatTransaction{"TransactionType": transaction.ConfidentialMPTSendTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "50", tx["Fee"])
})

batchTests := []struct {
name string
innerTypes []string
expected string
}{
{
name: "batch with confidential inner transaction",
innerTypes: []string{transaction.ConfidentialMPTSendTx.String()},
expected: "120",
},
{
name: "batch with confidential and ordinary inner transactions",
innerTypes: []string{
transaction.ConfidentialMPTSendTx.String(),
transaction.PaymentTx.String(),
},
expected: "130",
},
}

for _, test := range batchTests {
t.Run(test.name, func(t *testing.T) {
rawTransactions := make([]map[string]any, 0, len(test.innerTypes))
for _, txType := range test.innerTypes {
rawTransactions = append(rawTransactions, map[string]any{
"RawTransaction": map[string]any{"TransactionType": txType},
})
}
tx := transaction.FlatTransaction{
"TransactionType": transaction.BatchTx.String(),
"RawTransactions": rawTransactions,
}
client := setupConfidentialFeeTestClient(t, 2)

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, test.expected, tx["Fee"])
})
}
}

func TestClient_AutofillPreservesExplicitConfidentialMPTFee(t *testing.T) {
client := setupConfidentialFeeTestClient(t, 2)
tx := transaction.FlatTransaction{
"Account": "rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH",
"TransactionType": transaction.ConfidentialMPTSendTx.String(),
"Sequence": uint32(1),
"Fee": "777",
"LastLedgerSequence": uint32(100),
}

require.NoError(t, client.Autofill(&tx))
require.Equal(t, "777", tx["Fee"])
}

func setupConfidentialFeeTestClient(t *testing.T, maxFeeXRP float32) *Client {
mc := &testutil.JSONRPCMockClient{}
mc.DoFunc = testutil.MockResponse(`{
"result": {
"info": {
"validated_ledger": {"base_fee_xrp": 0.00001},
"load_factor": 1
}
}
}`, http.StatusOK, mc)

cfg, err := NewClientConfig(
"http://testnode/",
WithHTTPClient(mc),
WithFeeCushion(1),
WithMaxFeeXRP(maxFeeXRP),
)
require.NoError(t, err)

return NewClient(cfg)
}
4 changes: 4 additions & 0 deletions xrpl/rpc/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
)

const (
confidentialMPTFeeMultiplier = 10

// RestrictedNetworks is the threshold above which sidechains are expected to have network IDs.
// Sidechains are expected to have network IDs above this.
// Networks with ID above this restricted number are expected specify an accurate NetworkID field
Expand Down Expand Up @@ -411,6 +413,8 @@ func (c *Client) calculateFeePerTransactionType(tx *transaction.FlatTransaction,
return err
}
baseFee = reserveFee
case "ConfidentialMPTSend", "ConfidentialMPTConvert", "ConfidentialMPTConvertBack", "ConfidentialMPTMergeInbox", "ConfidentialMPTClawback":
baseFee = baseFeeUint * confidentialMPTFeeMultiplier
case "Batch":
rawTxFees, err := c.calculateBatchFees(tx)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions xrpl/websocket/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import (
)

const (
confidentialMPTFeeMultiplier = 10

// DefaultFeeCushion is the default cushion factor for fee calculations.
DefaultFeeCushion float32 = 1.2
// DefaultMaxFeeXRP is the default maximum fee in XRP.
Expand Down Expand Up @@ -711,6 +713,8 @@ func (c *Client) calculateFeePerTransactionType(tx *transaction.FlatTransaction,
return err
}
baseFee = reserveFee
case "ConfidentialMPTSend", "ConfidentialMPTConvert", "ConfidentialMPTConvertBack", "ConfidentialMPTMergeInbox", "ConfidentialMPTClawback":
baseFee = baseFeeUint * confidentialMPTFeeMultiplier
case "Batch":
rawTxFees, err := c.calculateBatchFees(tx)
if err != nil {
Expand Down
127 changes: 127 additions & 0 deletions xrpl/websocket/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,133 @@ func TestClient_calculateFeePerTransactionType(t *testing.T) {
}
}

func TestClient_calculateConfidentialMPTFees(t *testing.T) {
confidentialTypes := []string{
transaction.ConfidentialMPTSendTx.String(),
transaction.ConfidentialMPTConvertTx.String(),
transaction.ConfidentialMPTConvertBackTx.String(),
transaction.ConfidentialMPTMergeInboxTx.String(),
transaction.ConfidentialMPTClawbackTx.String(),
}

for _, txType := range confidentialTypes {
t.Run(txType, func(t *testing.T) {
client, cleanup := setupConfidentialFeeTestClient(t, 1, DefaultMaxFeeXRP)
defer cleanup()
tx := transaction.FlatTransaction{"TransactionType": txType}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "100", tx["Fee"])
})
}

t.Run("ordinary transaction", func(t *testing.T) {
client, cleanup := setupConfidentialFeeTestClient(t, 1, DefaultMaxFeeXRP)
defer cleanup()
tx := transaction.FlatTransaction{"TransactionType": transaction.PaymentTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "10", tx["Fee"])
})

t.Run("multisigned confidential transaction", func(t *testing.T) {
client, cleanup := setupConfidentialFeeTestClient(t, 1, DefaultMaxFeeXRP)
defer cleanup()
tx := transaction.FlatTransaction{"TransactionType": transaction.ConfidentialMPTSendTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 2))
require.Equal(t, "120", tx["Fee"])
})

t.Run("confidential transaction max fee", func(t *testing.T) {
client, cleanup := setupConfidentialFeeTestClient(t, 1, 0.00005)
defer cleanup()
tx := transaction.FlatTransaction{"TransactionType": transaction.ConfidentialMPTSendTx.String()}

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, "50", tx["Fee"])
})

batchTests := []struct {
name string
innerTypes []string
expected string
}{
{
name: "batch with confidential inner transaction",
innerTypes: []string{transaction.ConfidentialMPTSendTx.String()},
expected: "120",
},
{
name: "batch with confidential and ordinary inner transactions",
innerTypes: []string{
transaction.ConfidentialMPTSendTx.String(),
transaction.PaymentTx.String(),
},
expected: "130",
},
}

for _, test := range batchTests {
t.Run(test.name, func(t *testing.T) {
rawTransactions := make([]map[string]any, 0, len(test.innerTypes))
for _, txType := range test.innerTypes {
rawTransactions = append(rawTransactions, map[string]any{
"RawTransaction": map[string]any{"TransactionType": txType},
})
}
tx := transaction.FlatTransaction{
"TransactionType": transaction.BatchTx.String(),
"RawTransactions": rawTransactions,
}
client, cleanup := setupConfidentialFeeTestClient(t, len(test.innerTypes)+1, DefaultMaxFeeXRP)
defer cleanup()

require.NoError(t, client.calculateFeePerTransactionType(&tx, 0))
require.Equal(t, test.expected, tx["Fee"])
})
}
}

func TestClient_AutofillPreservesExplicitConfidentialMPTFee(t *testing.T) {
client := NewClient(*NewClientConfig())
tx := transaction.FlatTransaction{
"Account": "rN7n7otQDd6FczFgLdSqtcsAUxDkw6fzRH",
"TransactionType": transaction.ConfidentialMPTSendTx.String(),
"Sequence": uint32(1),
"Fee": "777",
"LastLedgerSequence": uint32(100),
}

require.NoError(t, client.Autofill(&tx))
require.Equal(t, "777", tx["Fee"])
}

func setupConfidentialFeeTestClient(t *testing.T, requestCount int, maxFeeXRP float32) (*Client, func()) {
client, cleanup := setupTestClient(t, confidentialFeeServerMessages(requestCount))
client.cfg.feeCushion = 1
client.cfg.maxFeeXRP = maxFeeXRP
return client, cleanup
}

func confidentialFeeServerMessages(count int) []map[string]any {
messages := make([]map[string]any, 0, count)
for i := range count {
messages = append(messages, map[string]any{
"id": i + 1,
"result": map[string]any{
"info": map[string]any{
"validated_ledger": map[string]any{
"base_fee_xrp": float32(0.00001),
},
"load_factor": float32(1),
},
},
})
}
return messages
}

func TestClient_setLastLedgerSequence(t *testing.T) {
tests := []struct {
name string
Expand Down
Loading