Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nativeToScVal to convert strings to all number sizes. #763

Merged
merged 5 commits into from
Jul 19, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/scval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/transaction_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
11 changes: 10 additions & 1 deletion test/unit/scval_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -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 () {
Expand Down
79 changes: 44 additions & 35 deletions test/unit/transaction_builder_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewers' note: this is just whitespace changes from a stray yarn fmt (not sure how the unformatted versions snuck in)


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);
});
});
});
Loading