Skip to content

Commit e2e0aac

Browse files
authored
fix: patch support for timepoint and duration scvals (#819)
* fix: patch support for timepoint and duration scvals
1 parent c18336d commit e2e0aac

File tree

6 files changed

+55
-1
lines changed

6 files changed

+55
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixed
6+
7+
- Add conversions for timepoint and duration scval types ([#819](https://github.com/stellar/js-stellar-base/pull/819))
8+
59
## [`v14.0.2`](https://github.com/stellar/js-stellar-base/compare/v14.0.1...v14.0.2):
610

711
### Fixed

src/numbers/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export function scValToBigInt(scv) {
3535

3636
case 'scvU64':
3737
case 'scvI64':
38+
case 'scvTimepoint':
39+
case 'scvDuration':
3840
return new XdrLargeInt(scIntType, scv.value()).toBigInt();
3941

4042
case 'scvU128':

src/numbers/xdr_large_int.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class XdrLargeInt {
5656
this.int = new Int256(values);
5757
break;
5858
case 'u64':
59+
case 'timepoint':
60+
case 'duration':
5961
this.int = new UnsignedHyper(values);
6062
break;
6163
case 'u128':
@@ -111,6 +113,22 @@ export class XdrLargeInt {
111113
);
112114
}
113115

116+
/** @returns {xdr.ScVal} the integer encoded with `ScValType = Timepoint` */
117+
toTimepoint() {
118+
this._sizeCheck(64);
119+
return xdr.ScVal.scvTimepoint(
120+
new xdr.Uint64(BigInt.asUintN(64, this.toBigInt())) // reiterpret as unsigned
121+
);
122+
}
123+
124+
/** @returns {xdr.ScVal} the integer encoded with `ScValType = Duration` */
125+
toDuration() {
126+
this._sizeCheck(64);
127+
return xdr.ScVal.scvDuration(
128+
new xdr.Uint64(BigInt.asUintN(64, this.toBigInt())) // reiterpret as unsigned
129+
);
130+
}
131+
114132
/**
115133
* @returns {xdr.ScVal} the integer encoded with `ScValType = I128`
116134
* @throws {RangeError} if the value cannot fit in 128 bits
@@ -197,6 +215,10 @@ export class XdrLargeInt {
197215
return this.toU128();
198216
case 'u256':
199217
return this.toU256();
218+
case 'timepoint':
219+
return this.toTimepoint();
220+
case 'duration':
221+
return this.toDuration();
200222
default:
201223
throw TypeError(`invalid type: ${this.type}`);
202224
}
@@ -231,6 +253,8 @@ export class XdrLargeInt {
231253
case 'u64':
232254
case 'u128':
233255
case 'u256':
256+
case 'timepoint':
257+
case 'duration':
234258
return true;
235259
default:
236260
return false;

src/scval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ export function nativeToScVal(val, opts = {}) {
294294
*
295295
* - void -> `null`
296296
* - u32, i32 -> `number`
297-
* - u64, i64, u128, i128, u256, i256 -> `bigint`
297+
* - u64, i64, u128, i128, u256, i256, timepoint, duration -> `bigint`
298298
* - vec -> `Array` of any of the above (via recursion)
299299
* - map -> key-value object of any of the above (via recursion)
300300
* - bool -> `boolean`

test/unit/scint_test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ describe('creating large integers', function () {
5151
expect(u64.high).to.equal(-1);
5252
});
5353

54+
it('handles timepoint', function () {
55+
let b = new StellarBase.ScInt(sentinel);
56+
expect(b.toBigInt()).to.equal(sentinel);
57+
expect(b.toNumber()).to.equal(Number(sentinel));
58+
let u64 = b.toTimepoint().timepoint();
59+
expect(u64.low).to.equal(Number(sentinel));
60+
expect(u64.high).to.equal(0);
61+
});
62+
63+
it('handles duration', function () {
64+
let b = new StellarBase.ScInt(sentinel);
65+
expect(b.toBigInt()).to.equal(sentinel);
66+
expect(b.toNumber()).to.equal(Number(sentinel));
67+
let u64 = b.toDuration().duration();
68+
expect(u64.low).to.equal(Number(sentinel));
69+
expect(u64.high).to.equal(0);
70+
});
71+
5472
it('handles i64', function () {
5573
let b = new StellarBase.ScInt(sentinel);
5674
expect(b.toBigInt()).to.equal(sentinel);

test/unit/scval_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ describe('parsing and building ScVals', function () {
1717
i32: xdr.ScVal.scvI32(1),
1818
u64: 1n,
1919
i64: -1n,
20+
timepoint: new ScInt(1443571200n).toTimepoint(),
21+
duration: new ScInt(1000n).toDuration(),
2022
u128: new ScInt(1).toU128(),
2123
i128: new ScInt(1).toI128(),
2224
u256: new ScInt(1).toU256(),
@@ -32,6 +34,7 @@ describe('parsing and building ScVals', function () {
3234
const targetScv = xdr.ScVal.scvMap(
3335
[
3436
['bool', xdr.ScVal.scvBool(true)],
37+
['duration', new ScInt(1000n, { type: 'duration' }).toScVal()],
3538
['i128', new ScInt(1, { type: 'i128' }).toScVal()],
3639
['i256', new ScInt(1, { type: 'i256' }).toScVal()],
3740
['i32', xdr.ScVal.scvI32(1)],
@@ -53,6 +56,7 @@ describe('parsing and building ScVals', function () {
5356
})
5457
])
5558
],
59+
['timepoint', new ScInt(1443571200n, { type: 'timepoint' }).toScVal()],
5660
['u128', new ScInt(1, { type: 'u128' }).toScVal()],
5761
['u256', new ScInt(1, { type: 'u256' }).toScVal()],
5862
['u32', xdr.ScVal.scvU32(1)],
@@ -156,6 +160,8 @@ describe('parsing and building ScVals', function () {
156160
[1, 'i64', 'scvI64'],
157161
[1, 'i128', 'scvI128'],
158162
[1, 'u256', 'scvU256'],
163+
[2, 'timepoint', 'scvTimepoint'],
164+
[3, 'duration', 'scvDuration'],
159165
['a', 'symbol', 'scvSymbol'],
160166
['a', undefined, 'scvString'],
161167
[Keypair.random(), undefined, 'scvAddress'],

0 commit comments

Comments
 (0)