Skip to content
Open
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
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

## Unreleased

### Breaking Changes
* `XdrLargeInt` has been renamed to just `Int` ([#809](https://github.com/stellar/js-stellar-base/pull/809)).
* `ScInt` and `scValToBigInt` have been removed to simplify the API surface. Please migrate in the following way ([#809](https://github.com/stellar/js-stellar-base/pull/809)):
```typescript
// before:
new ScInt(value);
new ScInt(value, { type: 'i128' });
// after:
Int.fromValue(value);
new Int('i128', value);

// before:
scValToBigInt(scv);
// after:
Int.fromScVal(scv).toBigInt();
```

### Added
* `XdrLargeInt` (renamed to `Int`) has new features ([#809](https://github.com/stellar/js-stellar-base/pull/809)):
- `fromValue(x: number|string|BigInt)` will create an `Int` of an appopriate size for the given value
- `fromScVal(x: xdr.ScVal)` will create an `Int` from an `ScVal`
- `.toU32()` will return an `ScVal` of the type `scvU32`
- `.toI32()` will return an `ScVal` of the type `scvI32`
- the constructor now accepts `i32` and `u32` as the first `type` parameter


## [`v14.0.0-rc.2`](https://github.com/stellar/js-stellar-base/compare/v13.1.0...v14.0.0-rc.2)

Expand Down
54 changes: 1 addition & 53 deletions src/numbers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,4 @@ export { Uint128 } from './uint128';
export { Uint256 } from './uint256';
export { Int128 } from './int128';
export { Int256 } from './int256';
export { ScInt } from './sc_int';
export { XdrLargeInt };

/**
* Transforms an opaque {@link xdr.ScVal} into a native bigint, if possible.
*
* If you then want to use this in the abstractions provided by this module,
* you can pass it to the constructor of {@link XdrLargeInt}.
*
* @example
* let scv = contract.call("add", x, y); // assume it returns an xdr.ScVal
* let bigi = scValToBigInt(scv);
*
* new ScInt(bigi); // if you don't care about types, and
* new XdrLargeInt('i128', bigi); // if you do
*
* @param {xdr.ScVal} scv - the raw XDR value to parse into an integer
* @returns {bigint} the native value of this input value
*
* @throws {TypeError} if the `scv` input value doesn't represent an integer
*/
export function scValToBigInt(scv) {
const scIntType = XdrLargeInt.getType(scv.switch().name);

switch (scv.switch().name) {
case 'scvU32':
case 'scvI32':
return BigInt(scv.value());

case 'scvU64':
case 'scvI64':
return new XdrLargeInt(scIntType, scv.value()).toBigInt();

case 'scvU128':
case 'scvI128':
return new XdrLargeInt(scIntType, [
scv.value().lo(),
scv.value().hi()
]).toBigInt();

case 'scvU256':
case 'scvI256':
return new XdrLargeInt(scIntType, [
scv.value().loLo(),
scv.value().loHi(),
scv.value().hiLo(),
scv.value().hiHi()
]).toBigInt();

default:
throw TypeError(`expected integer type, got ${scv.switch()}`);
}
}
export { XdrLargeInt as Int };
112 changes: 0 additions & 112 deletions src/numbers/sc_int.js

This file was deleted.

Loading