forked from StellarSend/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.test.ts
More file actions
110 lines (98 loc) · 3.58 KB
/
Copy pathstellar.test.ts
File metadata and controls
110 lines (98 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { TransactionBuilder, Keypair, Networks } from '@stellar/stellar-sdk'
import {
buildBatchPaymentTransaction,
getNetworkPassphrase,
MAX_BATCH_RECIPIENTS,
} from './stellar'
const PUBLIC_KEY = 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5'
const XLM_ASSET = { code: 'XLM', issuer: null, name: 'XLM', decimals: 7 }
const PER_OPERATION_FEE = '100'
// Horizon.Server only talks to the network; stub it so estimateFee() and
// loadAccount() are deterministic and never hit real Horizon endpoints.
const { feeStats, loadAccount } = vi.hoisted(() => ({
feeStats: vi.fn(),
loadAccount: vi.fn(),
}))
vi.mock('@stellar/stellar-sdk', async (importOriginal) => {
const actual = await importOriginal<typeof import('@stellar/stellar-sdk')>()
class FakeHorizonServer {
feeStats() {
return feeStats()
}
loadAccount() {
return loadAccount()
}
}
return {
...actual,
Horizon: { ...actual.Horizon, Server: FakeHorizonServer },
}
})
const mockSourceAccount = {
accountId: () => PUBLIC_KEY,
sequenceNumber: () => 1,
incrementSequenceNumber: () => {},
}
function makeRecipients(count: number) {
return Array.from({ length: count }, () => ({
destinationAddress: Keypair.random().publicKey(),
amount: '1.0000000',
}))
}
describe('buildBatchPaymentTransaction', () => {
beforeEach(() => {
feeStats.mockReset()
loadAccount.mockReset()
feeStats.mockResolvedValue({ fee_charged: { p70: PER_OPERATION_FEE } })
loadAccount.mockResolvedValue(mockSourceAccount)
})
it('charges a total fee that scales linearly with the number of recipients', async () => {
// estimateFee() returns a per-operation fee. TransactionBuilder.build()
// multiplies it by the operation count internally, so for an N-recipient
// batch the on-chain fee field must be estimateFee x N — never x N x N.
for (const count of [1, 10, 100]) {
const xdr = await buildBatchPaymentTransaction({
sourcePublicKey: PUBLIC_KEY,
asset: XLM_ASSET,
recipients: makeRecipients(count),
network: 'testnet',
})
const tx = TransactionBuilder.fromXDR(xdr, getNetworkPassphrase('testnet'))
expect(Number(tx.fee)).toBe(Number(PER_OPERATION_FEE) * count)
}
})
it('does not double-count the recipient multiplier on top of the SDK fee ramp', async () => {
// Regression: pre-multiplying the per-operation fee *and* letting the SDK
// multiply it again by operations.length produced estimateFee x N x N.
const count = 10
const xdr = await buildBatchPaymentTransaction({
sourcePublicKey: PUBLIC_KEY,
asset: XLM_ASSET,
recipients: makeRecipients(count),
network: 'testnet',
})
const tx = TransactionBuilder.fromXDR(xdr, Networks.TESTNET)
expect(Number(tx.fee)).not.toBe(Number(PER_OPERATION_FEE) * count * count)
})
it('rejects a batch with no recipients', async () => {
await expect(
buildBatchPaymentTransaction({
sourcePublicKey: PUBLIC_KEY,
asset: XLM_ASSET,
recipients: [],
network: 'testnet',
}),
).rejects.toThrow('A batch payment needs at least one recipient')
})
it(`rejects a batch larger than ${MAX_BATCH_RECIPIENTS} recipients`, async () => {
await expect(
buildBatchPaymentTransaction({
sourcePublicKey: PUBLIC_KEY,
asset: XLM_ASSET,
recipients: makeRecipients(MAX_BATCH_RECIPIENTS + 1),
network: 'testnet',
}),
).rejects.toThrow(`A batch payment supports at most ${MAX_BATCH_RECIPIENTS} recipients`)
})
})