Skip to content

Commit 55ea8e5

Browse files
committed
Rename XdrLargeInt -> Int
1 parent 41f0c70 commit 55ea8e5

6 files changed

Lines changed: 66 additions & 66 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
new ScInt(value);
1010
new ScInt(value, { type: 'i128' });
1111
// after:
12-
XdrLargeInt.fromValue(value);
13-
new XdrLargeInt('i128', value);
12+
Int.fromValue(value);
13+
new Int('i128', value);
1414

1515
// before:
1616
scValToBigInt(scv);
1717
// after:
18-
XdrLargeInt.fromScVal(scv).toBigInt();
18+
Int.fromScVal(scv).toBigInt();
1919
```
2020

2121
### Added
22-
* `XdrLargeInt` has new features ([]()):
23-
- `fromValue(x: number|string|BigInt)` will create an `XdrLargeInt` of an appopriate size for the given value
24-
- `fromScVal(x: xdr.ScVal)` will create an `XdrLargeInt` from an `ScVal`
22+
* `XdrLargeInt` (renamed to `Int`) has new features ([]()):
23+
- `fromValue(x: number|string|BigInt)` will create an `Int` of an appopriate size for the given value
24+
- `fromScVal(x: xdr.ScVal)` will create an `Int` from an `ScVal`
2525
- `.toU32()` will return an `ScVal` of the type `scvU32`
2626
- `.toI32()` will return an `ScVal` of the type `scvI32`
2727
- the constructor now accepts `i32` and `u32` as the first `type` parameter

src/numbers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export { Uint128 } from './uint128';
44
export { Uint256 } from './uint256';
55
export { Int128 } from './int128';
66
export { Int256 } from './int256';
7-
export { XdrLargeInt };
7+
export { XdrLargeInt as Int };

src/scval.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import xdr from './xdr';
33
import { Keypair } from './keypair';
44
import { Address } from './address';
55
import { Contract } from './contract';
6-
import { XdrLargeInt } from './numbers';
6+
import { Int } from './numbers';
77

88
/**
99
* Attempts to convert native types into smart contract values
@@ -21,7 +21,7 @@ import { XdrLargeInt } from './numbers';
2121
* - boolean -> scvBool
2222
*
2323
* - number/bigint -> the smallest possible XDR integer type that will fit the
24-
* input value (if you want a specific type, use {@link XdrLargeInt})
24+
* input value (if you want a specific type, use {@link Int})
2525
*
2626
* - {@link Address} or {@link Contract} -> scvAddress (for contracts and
2727
* public keys)
@@ -46,7 +46,7 @@ import { XdrLargeInt } from './numbers';
4646
* types for `val`:
4747
*
4848
* - when `val` is an integer-like type (i.e. number|bigint), this will be
49-
* forwarded to {@link XdrLargeInt}.
49+
* forwarded to {@link Int}.
5050
*
5151
* - when `val` is an array type, this is forwarded to the recursion
5252
*
@@ -244,10 +244,10 @@ export function nativeToScVal(val, opts = {}) {
244244
}
245245

246246
if ((opts?.type ?? '') !== '') {
247-
return new XdrLargeInt(opts.type, val).toScVal();
247+
return new Int(opts.type, val).toScVal();
248248
}
249249

250-
return XdrLargeInt.fromValue(val).toScVal();
250+
return Int.fromValue(val).toScVal();
251251

252252
case 'string': {
253253
const optType = opts?.type ?? 'string';
@@ -268,8 +268,8 @@ export function nativeToScVal(val, opts = {}) {
268268
return xdr.ScVal.scvI32(parseInt(val, 10));
269269

270270
default:
271-
if (XdrLargeInt.isType(optType)) {
272-
return new XdrLargeInt(optType, val).toScVal();
271+
if (Int.isType(optType)) {
272+
return new Int(optType, val).toScVal();
273273
}
274274

275275
throw new TypeError(
@@ -335,7 +335,7 @@ export function scValToNative(scv) {
335335
case xdr.ScValType.scvI128().value:
336336
case xdr.ScValType.scvU256().value:
337337
case xdr.ScValType.scvI256().value:
338-
return XdrLargeInt.fromScVal(scv).toBigInt();
338+
return Int.fromScVal(scv).toBigInt();
339339

340340
case xdr.ScValType.scvVec().value:
341341
return (scv.vec() ?? []).map(scValToNative);

test/unit/int_test.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const I128 = StellarBase.Int128;
22
const U128 = StellarBase.Uint128;
33
const I256 = StellarBase.Int256;
44
const U256 = StellarBase.Uint256;
5-
const { xdr, XdrLargeInt } = StellarBase; // shorthand
5+
const { xdr, Int } = StellarBase; // shorthand
66

77
describe('creating large integers', function () {
88
describe('picks the right types', function () {
@@ -14,7 +14,7 @@ describe('creating large integers', function () {
1414
}).forEach(([type, values]) => {
1515
values.forEach((value) => {
1616
it(`picks ${type} for ${value}`, function () {
17-
const bi = XdrLargeInt.fromValue(value);
17+
const bi = Int.fromValue(value);
1818
expect(bi.type).to.equal(type);
1919
expect(bi.toBigInt()).to.equal(BigInt(value));
2020
});
@@ -25,7 +25,7 @@ describe('creating large integers', function () {
2525
it('has correct utility methods', function () {
2626
const v =
2727
123456789123456789123456789123456789123456789123456789123456789123456789n;
28-
const i = XdrLargeInt.fromValue(v);
28+
const i = Int.fromValue(v);
2929
expect(i.valueOf()).to.be.eql(new U256(v));
3030
expect(i.toString()).to.equal(v.toString());
3131
expect(i.toJSON()).to.be.eql({ value: v.toString(), type: 'u256' });
@@ -35,14 +35,14 @@ describe('creating large integers', function () {
3535
const sentinel = 800000085n;
3636

3737
it('handles u64', function () {
38-
let b = XdrLargeInt.fromValue(sentinel);
38+
let b = Int.fromValue(sentinel);
3939
expect(b.toBigInt()).to.equal(sentinel);
4040
expect(b.toNumber()).to.equal(Number(sentinel));
4141
let u64 = b.toU64().u64();
4242
expect(u64.low).to.equal(Number(sentinel));
4343
expect(u64.high).to.equal(0);
4444

45-
b = XdrLargeInt.fromValue(-sentinel);
45+
b = Int.fromValue(-sentinel);
4646
expect(b.toBigInt()).to.equal(-sentinel);
4747
expect(b.toNumber()).to.equal(Number(-sentinel));
4848
u64 = b.toU64().u64();
@@ -51,7 +51,7 @@ describe('creating large integers', function () {
5151
});
5252

5353
it('handles i64', function () {
54-
let b = XdrLargeInt.fromValue(sentinel);
54+
let b = Int.fromValue(sentinel);
5555
expect(b.toBigInt()).to.equal(sentinel);
5656
expect(b.toNumber()).to.equal(Number(sentinel));
5757
let i64 = b.toI64().i64();
@@ -60,14 +60,14 @@ describe('creating large integers', function () {
6060
});
6161

6262
it(`upscales u64 to 128`, function () {
63-
const b = XdrLargeInt.fromValue(sentinel);
63+
const b = Int.fromValue(sentinel);
6464
const i128 = b.toI128().i128();
6565
expect(i128.lo().toBigInt()).to.equal(sentinel);
6666
expect(i128.hi().toBigInt()).to.equal(0n);
6767
});
6868

6969
it(`upscales i64 to 128`, function () {
70-
const b = XdrLargeInt.fromValue(-sentinel);
70+
const b = Int.fromValue(-sentinel);
7171
const i128 = b.toI128().i128();
7272
const hi = i128.hi().toBigInt();
7373
const lo = i128.lo().toBigInt();
@@ -77,7 +77,7 @@ describe('creating large integers', function () {
7777
});
7878

7979
it(`upscales i64 to 256`, function () {
80-
const b = XdrLargeInt.fromValue(sentinel);
80+
const b = Int.fromValue(sentinel);
8181
const i = b.toI256().i256();
8282

8383
const [hiHi, hiLo, loHi, loLo] = [
@@ -100,7 +100,7 @@ describe('creating large integers', function () {
100100
});
101101

102102
it(`upscales i64 to 256`, function () {
103-
const b = XdrLargeInt.fromValue(-sentinel);
103+
const b = Int.fromValue(-sentinel);
104104
const i = b.toI256().i256();
105105

106106
const [hiHi, hiLo, loHi, loLo] = [
@@ -127,7 +127,7 @@ describe('creating large integers', function () {
127127
const sentinel = 800000000000000000000085n; // 80 bits long
128128

129129
it('handles inputs', function () {
130-
let b = XdrLargeInt.fromValue(sentinel);
130+
let b = Int.fromValue(sentinel);
131131
expect(b.toBigInt()).to.equal(sentinel);
132132
expect(() => b.toNumber()).to.throw(/not in range/i);
133133
expect(() => b.toU64()).to.throw(/too large/i);
@@ -143,7 +143,7 @@ describe('creating large integers', function () {
143143
]).toBigInt()
144144
).to.equal(sentinel);
145145

146-
b = XdrLargeInt.fromValue(-sentinel);
146+
b = Int.fromValue(-sentinel);
147147
u128 = b.toU128().u128();
148148
expect(
149149
new U128([
@@ -154,7 +154,7 @@ describe('creating large integers', function () {
154154
]).toBigInt()
155155
).to.equal(BigInt.asUintN(128, -sentinel));
156156

157-
b = XdrLargeInt.fromValue(sentinel);
157+
b = Int.fromValue(sentinel);
158158
let i128 = b.toI128().i128();
159159
expect(
160160
new I128([
@@ -165,7 +165,7 @@ describe('creating large integers', function () {
165165
]).toBigInt()
166166
).to.equal(sentinel);
167167

168-
b = XdrLargeInt.fromValue(-sentinel);
168+
b = Int.fromValue(-sentinel);
169169
i128 = b.toI128().i128();
170170
expect(
171171
new I128([
@@ -178,7 +178,7 @@ describe('creating large integers', function () {
178178
});
179179

180180
it('upscales to 256 bits', function () {
181-
let b = XdrLargeInt.fromValue(-sentinel);
181+
let b = Int.fromValue(-sentinel);
182182
let i256 = b.toI256().i256();
183183
let u256 = b.toU256().u256();
184184

@@ -212,7 +212,7 @@ describe('creating large integers', function () {
212212

213213
describe('conversion to/from ScVals', function () {
214214
const v = 80000085n;
215-
const i = XdrLargeInt.fromValue(v);
215+
const i = Int.fromValue(v);
216216

217217
[
218218
[i.toI64(), 'i64'],
@@ -226,9 +226,9 @@ describe('creating large integers', function () {
226226
expect(scv.switch().name).to.equal(`scv${type.toUpperCase()}`);
227227
expect(typeof scv.toXDR('base64')).to.equal('string');
228228

229-
const bigi = StellarBase.XdrLargeInt.fromScVal(scv).toBigInt();
229+
const bigi = StellarBase.Int.fromScVal(scv).toBigInt();
230230
expect(bigi).to.equal(v);
231-
expect(new StellarBase.XdrLargeInt(type, bigi).toJSON()).to.eql({
231+
expect(new StellarBase.Int(type, bigi).toJSON()).to.eql({
232232
...i.toJSON(),
233233
type
234234
});
@@ -239,44 +239,44 @@ describe('creating large integers', function () {
239239
const i32 = new xdr.ScVal.scvI32(Number(v));
240240
const u32 = new xdr.ScVal.scvU32(Number(v));
241241

242-
expect(XdrLargeInt.fromScVal(i32).toBigInt()).to.equal(v);
243-
expect(XdrLargeInt.fromScVal(u32).toBigInt()).to.equal(v);
242+
expect(Int.fromScVal(i32).toBigInt()).to.equal(v);
243+
expect(Int.fromScVal(u32).toBigInt()).to.equal(v);
244244
});
245245

246246
it('throws for non-integers', function () {
247247
expect(() =>
248-
XdrLargeInt.fromScVal(new xdr.ScVal.scvString('hello'))
248+
Int.fromScVal(new xdr.ScVal.scvString('hello'))
249249
).to.throw(/integer/i);
250250
});
251251
});
252252

253253
describe('error handling', function () {
254254
['u64', 'u128', 'u256'].forEach((type) => {
255255
it(`throws when signed parts and {type: '${type}'}`, function () {
256-
expect(() => new StellarBase.XdrLargeInt(type, -2)).to.throw(
256+
expect(() => new StellarBase.Int(type, -2)).to.throw(
257257
/positive/i
258258
);
259259
});
260260
});
261261

262262
it('throws when too big', function () {
263-
expect(() => XdrLargeInt.fromValue(1n << 400n)).to.throw(/expected/i);
263+
expect(() => Int.fromValue(1n << 400n)).to.throw(/expected/i);
264264
});
265265

266266
it('throws when big interpreted as small', function () {
267267
let big;
268268

269-
big = XdrLargeInt.fromValue(1n << 64n);
269+
big = Int.fromValue(1n << 64n);
270270
expect(() => big.toNumber()).to.throw(/not in range/i);
271271

272-
big = XdrLargeInt.fromValue(Number.MAX_SAFE_INTEGER + 1);
272+
big = Int.fromValue(Number.MAX_SAFE_INTEGER + 1);
273273
expect(() => big.toNumber()).to.throw(/not in range/i);
274274

275-
big = new XdrLargeInt('i128', 1);
275+
big = new Int('i128', 1);
276276
expect(() => big.toU64()).to.throw(/too large/i);
277277
expect(() => big.toI64()).to.throw(/too large/i);
278278

279-
big = new XdrLargeInt('i256', 1);
279+
big = new Int('i256', 1);
280280
expect(() => big.toU64()).to.throw(/too large/i);
281281
expect(() => big.toI64()).to.throw(/too large/i);
282282
expect(() => big.toI128()).to.throw(/too large/i);
@@ -294,7 +294,7 @@ describe('creating raw large XDR integers', function () {
294294
].forEach(([type, count], idx) => {
295295
it(`works for ${type}`, function () {
296296
const input = new Array(count).fill(1n);
297-
const xdrI = new StellarBase.XdrLargeInt(type, input);
297+
const xdrI = new StellarBase.Int(type, input);
298298

299299
let expected = input.reduce((accum, v, i) => {
300300
return (accum << 32n) | v;

test/unit/scval_test.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const {
22
xdr,
33
Address,
44
Keypair,
5-
XdrLargeInt,
5+
Int,
66
scValToNative,
77
nativeToScVal,
88
scValToBigInt
@@ -16,10 +16,10 @@ describe('parsing and building ScVals', function () {
1616
i32: xdr.ScVal.scvI32(1),
1717
u64: BigInt(2 ** 33),
1818
i64: BigInt(-1 * 2 ** 33),
19-
u128: XdrLargeInt.fromValue(1).toU128(),
20-
i128: XdrLargeInt.fromValue(1).toI128(),
21-
u256: XdrLargeInt.fromValue(1).toU256(),
22-
i256: XdrLargeInt.fromValue(1).toI256(),
19+
u128: Int.fromValue(1).toU128(),
20+
i128: Int.fromValue(1).toI128(),
21+
u256: Int.fromValue(1).toU256(),
22+
i256: Int.fromValue(1).toI256(),
2323
map: {
2424
arbitrary: 1n,
2525
nested: 'values',
@@ -31,8 +31,8 @@ describe('parsing and building ScVals', function () {
3131
const targetScv = xdr.ScVal.scvMap(
3232
[
3333
['bool', xdr.ScVal.scvBool(true)],
34-
['i128', new XdrLargeInt('i128', 1).toScVal()],
35-
['i256', new XdrLargeInt('i256', 1).toScVal()],
34+
['i128', new Int('i128', 1).toScVal()],
35+
['i256', new Int('i256', 1).toScVal()],
3636
['i32', xdr.ScVal.scvI32(1)],
3737
['i64', xdr.ScVal.scvI64(new xdr.Int64(-1 * 2 ** 33))],
3838
[
@@ -52,8 +52,8 @@ describe('parsing and building ScVals', function () {
5252
})
5353
])
5454
],
55-
['u128', new XdrLargeInt('u128', 1).toScVal()],
56-
['u256', new XdrLargeInt('u256', 1).toScVal()],
55+
['u128', new Int('u128', 1).toScVal()],
56+
['u256', new Int('u256', 1).toScVal()],
5757
['u32', xdr.ScVal.scvU32(1)],
5858
['u64', xdr.ScVal.scvU64(new xdr.Uint64(2 ** 33))],
5959
[
@@ -98,14 +98,14 @@ describe('parsing and building ScVals', function () {
9898
[xdr.ScVal.scvVoid(), null],
9999
[xdr.ScVal.scvBool(true), true],
100100
[xdr.ScVal.scvBool(false), false],
101-
[XdrLargeInt.fromValue(1).toU32(), 1],
102-
[XdrLargeInt.fromValue(1).toI32(), 1],
103-
[XdrLargeInt.fromValue(11).toU64(), 11n],
104-
[XdrLargeInt.fromValue(11).toI64(), 11n],
105-
[XdrLargeInt.fromValue(22).toU128(), 22n],
106-
[XdrLargeInt.fromValue(22).toI128(), 22n],
107-
[XdrLargeInt.fromValue(33).toU256(), 33n],
108-
[XdrLargeInt.fromValue(33).toI256(), 33n],
101+
[Int.fromValue(1).toU32(), 1],
102+
[Int.fromValue(1).toI32(), 1],
103+
[Int.fromValue(11).toU64(), 11n],
104+
[Int.fromValue(11).toI64(), 11n],
105+
[Int.fromValue(22).toU128(), 22n],
106+
[Int.fromValue(22).toI128(), 22n],
107+
[Int.fromValue(33).toU256(), 33n],
108+
[Int.fromValue(33).toI256(), 33n],
109109
[xdr.ScVal.scvTimepoint(new xdr.Uint64(44n)), 44n],
110110
[xdr.ScVal.scvDuration(new xdr.Uint64(55n)), 55n],
111111
[xdr.ScVal.scvBytes(Buffer.alloc(32, 123)), Buffer.from('{'.repeat(32))],
@@ -125,7 +125,7 @@ describe('parsing and building ScVals', function () {
125125
[
126126
xdr.ScVal.scvMap(
127127
[
128-
[XdrLargeInt.fromValue(0).toI256(), xdr.ScVal.scvBool(true)],
128+
[Int.fromValue(0).toI256(), xdr.ScVal.scvBool(true)],
129129
[xdr.ScVal.scvBool(false), xdr.ScVal.scvString('second')],
130130
[
131131
xdr.ScVal.scvU32(2),
@@ -238,8 +238,8 @@ describe('parsing and building ScVals', function () {
238238
it('lets strings be large integer ScVals', function () {
239239
['i64', 'i128', 'i256', 'u64', 'u128', 'u256'].forEach((type) => {
240240
const scv = nativeToScVal('12345', { type });
241-
expect(XdrLargeInt.getType(scv.switch().name)).to.equal(type);
242-
expect(XdrLargeInt.fromScVal(scv).toBigInt()).to.equal(BigInt(12345));
241+
expect(Int.getType(scv.switch().name)).to.equal(type);
242+
expect(Int.fromScVal(scv).toBigInt()).to.equal(BigInt(12345));
243243
});
244244

245245
expect(() => nativeToScVal('not a number', { type: 'i128' })).to.throw();

0 commit comments

Comments
 (0)