forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsoroban.userPosition.test.ts
More file actions
151 lines (118 loc) · 4.53 KB
/
Copy pathsoroban.userPosition.test.ts
File metadata and controls
151 lines (118 loc) · 4.53 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { describe, it, expect } from 'vitest';
import { nativeToScVal, xdr } from '@stellar/stellar-sdk';
import { parseUserPositionFromXdrResult } from './soroban';
const POOL_ID = 'CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4';
const USER_ADDR = 'GBBD47IF6LWK7P7MDEVSCWR7DPUWV3NY3DTQEVFL4NAT4AQH3ZLLFLA5';
/** Build a Soroban Map ScVal from a plain JS object — mirrors what the contract returns. */
function makePositionScVal(fields: Record<string, unknown>): xdr.ScVal {
return nativeToScVal(fields);
}
describe('parseUserPositionFromXdrResult', () => {
it('parses every UserPosition field from canonical contract names', () => {
const scVal = makePositionScVal({
amount: 50_000_000n, // 5.0000000 XLM
locked_at: 1_700_000_000n,
credits: 10_000_000n, // 1.0000000
is_locked: true,
unlockable_at: 1_700_086_400n,
boost_allocation: 2,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos).not.toBeNull();
expect(pos!.user).toBe(USER_ADDR);
expect(pos!.poolId).toBe(POOL_ID);
expect(pos!.amount).toBe('5.0000000');
expect(pos!.lockedAt).toBe(1_700_000_000);
expect(pos!.credits).toBe('1.0000000');
expect(pos!.isLocked).toBe(true);
expect(pos!.unlockableAt).toBe(1_700_086_400);
expect(pos!.boostAllocation).toBe(2);
});
it('derives unlockableAt from lockedAt + min_lock_period when unlockable_at is absent', () => {
const LOCKED_AT = 1_700_000_000n;
const MIN_LOCK = 86_400n; // 1 day in seconds
const scVal = makePositionScVal({
amount: 10_000_000n,
locked_at: LOCKED_AT,
credits: 0n,
is_locked: true,
min_lock_period: MIN_LOCK,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos).not.toBeNull();
expect(pos!.unlockableAt).toBe(Number(LOCKED_AT) + Number(MIN_LOCK));
});
it('accepts the unlock_at alias for unlockableAt', () => {
const scVal = makePositionScVal({
amount: 10_000_000n,
locked_at: 1_700_000_000n,
credits: 0n,
is_locked: true,
unlock_at: 1_700_100_000n,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.unlockableAt).toBe(1_700_100_000);
});
it('defaults unlockableAt to 0 when neither unlockable_at nor min_lock_period are present', () => {
const scVal = makePositionScVal({
amount: 10_000_000n,
locked_at: 1_700_000_000n,
credits: 0n,
is_locked: false,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.unlockableAt).toBe(0);
});
it('accepts locked_amount alias for amount field', () => {
const scVal = makePositionScVal({
locked_amount: 20_000_000n, // 2.0000000 XLM
locked_at: 1_700_000_000n,
credits: 0n,
is_locked: true,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.amount).toBe('2.0000000');
});
it('accepts accrued_credits alias for credits field', () => {
const scVal = makePositionScVal({
amount: 10_000_000n,
locked_at: 1_700_000_000n,
accrued_credits: 5_000_000n, // 0.5000000
is_locked: false,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.credits).toBe('0.5000000');
});
it('returns null for a void/null ScVal (wallet has no position)', () => {
const scVal = xdr.ScVal.scvVoid();
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos).toBeNull();
});
it('returns null for a non-map ScVal (unexpected type)', () => {
const scVal = nativeToScVal('unexpected-string');
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos).toBeNull();
});
it('omits boostAllocation when neither boost_allocation nor boost are present', () => {
const scVal = makePositionScVal({
amount: 10_000_000n,
locked_at: 1_700_000_000n,
credits: 0n,
is_locked: false,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.boostAllocation).toBeUndefined();
});
it('converts zero-amount fields to 0.0000000', () => {
const scVal = makePositionScVal({
amount: 0n,
locked_at: 0n,
credits: 0n,
is_locked: false,
});
const pos = parseUserPositionFromXdrResult(scVal, POOL_ID, USER_ADDR);
expect(pos!.amount).toBe('0.0000000');
expect(pos!.credits).toBe('0.0000000');
expect(pos!.lockedAt).toBe(0);
});
});