diff --git a/CHANGELOG.md b/CHANGELOG.md index 34137a92..5c9b8c4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### Fixed * Improve the efficiency and portability of asset type retrieval ([#758](https://github.com/stellar/js-stellar-base/pull/758)). * `nativeToScVal` now correctly sorts maps lexicographically based on the keys to match what the Soroban environment expects ([#759](https://github.com/stellar/js-stellar-base/pull/759)). +* `nativeToScVal` now allows all integer types to come from strings ([#763](https://github.com/stellar/js-stellar-base/pull/763)). ## [`v12.0.1`](https://github.com/stellar/js-stellar-base/compare/v12.0.0...v12.0.1) diff --git a/src/scval.js b/src/scval.js index 42eeee46..67bf0e62 100644 --- a/src/scval.js +++ b/src/scval.js @@ -239,6 +239,12 @@ export function nativeToScVal(val, opts = {}) { case 'address': return new Address(val).toScVal(); + case 'u32': + return xdr.ScVal.scvU32(parseInt(val, 10)); + + case 'i32': + return xdr.ScVal.scvI32(parseInt(val, 10)); + default: if (XdrLargeInt.isType(optType)) { return new XdrLargeInt(optType, val).toScVal(); diff --git a/src/transaction_builder.js b/src/transaction_builder.js index 2437a02c..385c218f 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -226,8 +226,8 @@ export class TransactionBuilder { addOperation(operation) { this.operations.push(operation); return this; - } - + } + /** * Adds an operation to the transaction at a specific index. * diff --git a/test/unit/scval_test.js b/test/unit/scval_test.js index b9ad6451..c62a0e90 100644 --- a/test/unit/scval_test.js +++ b/test/unit/scval_test.js @@ -211,6 +211,16 @@ describe('parsing and building ScVals', function () { expect(() => nativeToScVal([1, 'a', false])).to.throw(/same type/i); }); + it('lets strings be small integer ScVals', function () { + ['i32', 'u32'].forEach((type) => { + const scv = nativeToScVal('12345', { type }); + expect(scv.switch()).to.eql( + type === 'u32' ? xdr.ScValType.scvU32() : xdr.ScValType.scvI32() + ); + expect(scv.value()).to.eql(12345); + }); + }); + it('lets strings be large integer ScVals', function () { ['i64', 'i128', 'i256', 'u64', 'u128', 'u256'].forEach((type) => { const scv = nativeToScVal('12345', { type }); @@ -220,7 +230,6 @@ describe('parsing and building ScVals', function () { expect(() => nativeToScVal('not a number', { type: 'i128' })).to.throw(); expect(() => nativeToScVal('12345', { type: 'notnumeric' })).to.throw(); - expect(() => nativeToScVal('use a Number', { type: 'i32' })).to.throw(); }); it('lets strings be addresses', function () { diff --git a/test/unit/transaction_builder_test.js b/test/unit/transaction_builder_test.js index d2cda555..1ffeed21 100644 --- a/test/unit/transaction_builder_test.js +++ b/test/unit/transaction_builder_test.js @@ -956,51 +956,60 @@ describe('TransactionBuilder', function () { }); it('adds operations at a specific index', function () { - const builder = new StellarBase.TransactionBuilder(source, { - fee: '100', - timebounds: { minTime: 0, maxTime: 0 }, - memo: new StellarBase.Memo(StellarBase.MemoText, 'Testing adding op at index'), - networkPassphrase - }); + const builder = new StellarBase.TransactionBuilder(source, { + fee: '100', + timebounds: { minTime: 0, maxTime: 0 }, + memo: new StellarBase.Memo( + StellarBase.MemoText, + 'Testing adding op at index' + ), + networkPassphrase + }); - builder.addOperationAt(StellarBase.Operation.payment({ + builder.addOperationAt( + StellarBase.Operation.payment({ source: source.accountId(), destination: destination, amount: '1', asset: asset - }), 0); + }), + 0 + ); - builder.addOperationAt(StellarBase.Operation.payment({ + builder.addOperationAt( + StellarBase.Operation.payment({ source: source.accountId(), destination: destination, amount: '2', asset: asset - }), 1); - - const tx = builder.build(); - // Assert operations - expect(tx.operations.length).to.equal(2); - expect(tx.operations[0].source).to.equal(source.accountId()); - expect(parseInt(tx.operations[0].amount)).to.equal(1); - expect(tx.operations[1].source).to.equal(source.accountId()); - expect(parseInt(tx.operations[1].amount)).to.equal(2); - - const clonedTx = StellarBase.TransactionBuilder.cloneFrom(tx) - clonedTx.clearOperationAt(0); - const newOperation = StellarBase.Operation.payment({ - source: source.accountId(), - destination: destination, - amount: '3', - asset: asset - }) - clonedTx.addOperationAt(newOperation, 0); - const newTx = clonedTx.build() - // Assert that the operations are the same, but the first one is updated - expect(newTx.operations.length).to.equal(2); - expect(newTx.operations[0].source).to.equal(source.accountId()); - expect(parseInt(newTx.operations[0].amount)).to.equal(3); - expect(newTx.operations[1].source).to.equal(source.accountId()); - expect(parseInt(newTx.operations[1].amount)).to.equal(2); + }), + 1 + ); + + const tx = builder.build(); + // Assert operations + expect(tx.operations.length).to.equal(2); + expect(tx.operations[0].source).to.equal(source.accountId()); + expect(parseInt(tx.operations[0].amount)).to.equal(1); + expect(tx.operations[1].source).to.equal(source.accountId()); + expect(parseInt(tx.operations[1].amount)).to.equal(2); + + const clonedTx = StellarBase.TransactionBuilder.cloneFrom(tx); + clonedTx.clearOperationAt(0); + const newOperation = StellarBase.Operation.payment({ + source: source.accountId(), + destination: destination, + amount: '3', + asset: asset + }); + clonedTx.addOperationAt(newOperation, 0); + const newTx = clonedTx.build(); + // Assert that the operations are the same, but the first one is updated + expect(newTx.operations.length).to.equal(2); + expect(newTx.operations[0].source).to.equal(source.accountId()); + expect(parseInt(newTx.operations[0].amount)).to.equal(3); + expect(newTx.operations[1].source).to.equal(source.accountId()); + expect(parseInt(newTx.operations[1].amount)).to.equal(2); }); }); });