diff --git a/CHANGELOG.md b/CHANGELOG.md index ccc5ed38..9078c0ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,12 @@ - `Contract.getFootprint()` now only returns a single result: the ledger key of the deployed instance for the given ID, because the key for the code entry was incorrect (it should not be the ID but rather the WASM hash, which is not calculatable w/o network access) ([#709](https://github.com/stellar/js-stellar-base/pull/709)). +## [`v10.0.0-beta.4`](https://github.com/stellar/js-stellar-base/compare/v10.0.0-beta.3...v10.0.0-beta.4) + +### Fixed +- You can now correctly clone transactions (`TransactionBuilder.cloneFrom`) with large sequence numbers ([#711](https://github.com/stellar/js-stellar-base/pull/711)). + + ## [`v10.0.0-beta.3`](https://github.com/stellar/js-stellar-base/compare/v10.0.0-beta.2...v10.0.0-beta.3) ### Fixed diff --git a/package.json b/package.json index 907bdb84..83280199 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "stellar-base", - "version": "10.0.0-beta.3", + "version": "10.0.0-beta.4", "description": "Low-level support library for the Stellar network.", "main": "./lib/index.js", "browser": { diff --git a/src/soroban.js b/src/soroban.js index 197d3e85..1c4f0b5a 100644 --- a/src/soroban.js +++ b/src/soroban.js @@ -1,25 +1,29 @@ /** - * Soroban helper class - * formatting, parsing, and etc * @class Soroban + * Soroban helper class to assist with formatting and parsing token amounts. */ export class Soroban { /** - * All arithmetic inside the contract is performed on integers to - * avoid potential precision and consistency issues of floating-point + * Given a whole number smart contract amount of a token and an amount of + * decimal places (if the token has any), it returns a "display" value. * - * This function takes the smart contract value and its decimals (if the token has any) and returns a display value - * @param {string} amount - the token amount you want to display - * @param {number} decimals - specify how many decimal places a token has - * @returns {string} - display value + * All arithmetic inside the contract is performed on integers to avoid + * potential precision and consistency issues of floating-point. + * + * @param {string} amount the token amount you want to display + * @param {number} decimals specify how many decimal places a token has + * + * @returns {string} the display value + * @throws {TypeError} if the given amount has a decimal point already + * @example + * formatTokenAmount("123000", 4) === "12.3"; */ static formatTokenAmount(amount, decimals) { - let formatted = amount; - if (amount.includes('.')) { - throw new Error('No decimal is allowed'); + throw new TypeError('No decimals are allowed'); } + let formatted = amount; if (decimals > 0) { if (decimals > formatted.length) { formatted = ['0', formatted.toString().padStart(decimals, '0')].join( @@ -38,19 +42,24 @@ export class Soroban { } /** - * parse token amount to use it on smart contract + * Parse a token amount to use it on smart contract * * This function takes the display value and its decimals (if the token has * any) and returns a string that'll be used within the smart contract. - * @param {string} value - the token amount you want to use it on smart contract - * @param {number} decimals - specify how many decimal places a token has - * @returns {string} - smart contract value * + * @param {string} value the token amount you want to use it on smart + * contract which you've been displaying in a UI + * @param {number} decimals the number of decimal places expected in the + * display value (different than the "actual" number, because suffix zeroes + * might not be present) + * + * @returns {string} the whole number token amount represented by the display + * value with the decimal places shifted over * * @example * const displayValueAmount = "123.4560" - * const parsedAmountForSmartContract = parseTokenAmount("123.4560", 5); - * parsedAmountForSmartContract === "12345600" + * const parsedAmtForSmartContract = parseTokenAmount(displayValueAmount, 5); + * parsedAmtForSmartContract === "12345600" */ static parseTokenAmount(value, decimals) { const [whole, fraction, ...rest] = value.split('.').slice(); diff --git a/src/transaction_builder.js b/src/transaction_builder.js index db318168..15e3ba85 100644 --- a/src/transaction_builder.js +++ b/src/transaction_builder.js @@ -181,7 +181,7 @@ export class TransactionBuilder { throw new TypeError(`expected a 'Transaction', got: ${tx}`); } - const sequenceNum = `${parseInt(tx.sequence, 10) - 1}`; + const sequenceNum = (BigInt(tx.sequence) - 1n).toString(); let source; // rebuild the source account based on the strkey diff --git a/types/index.d.ts b/types/index.d.ts index 22fb4d66..41d383d5 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1266,5 +1266,5 @@ export function walkInvocationTree( export namespace Soroban { function formatTokenAmount(address: string, decimals: number): string; - function parseTokenAmount(value: string, decimals: number): Address; + function parseTokenAmount(value: string, decimals: number): string; } diff --git a/yarn.lock b/yarn.lock index 705ebb49..5d57df6e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2146,7 +2146,7 @@ bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.11.9: resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88" integrity sha512-c98Bf3tPniI+scsdk237ku1Dc3ujXQTSgyiPUDEOe7tRkhrqridvh8klBv0HCEso1OLOYcHuCv/cS6DNxKH+ZA== -bn.js@^5.0.0, bn.js@^5.1.1: +bn.js@^5.0.0, bn.js@^5.2.1: version "5.2.1" resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.2.1.tgz#0bc527a6a0d18d0aa8d5b0538ce4a77dccfa7b70" integrity sha512-eXRvHzWyYPBuB4NBy0cmYQjGitUrtqwbvlzP3G6VFnNRbsZQIxQ10PbKKHt8gZ/HW/D/747aDl+QkDqg3KQLMQ== @@ -2239,7 +2239,7 @@ browserify-des@^1.0.0: inherits "^2.0.1" safe-buffer "^5.1.2" -browserify-rsa@^4.0.0, browserify-rsa@^4.0.1: +browserify-rsa@^4.0.0, browserify-rsa@^4.1.0: version "4.1.0" resolved "https://registry.yarnpkg.com/browserify-rsa/-/browserify-rsa-4.1.0.tgz#b2fd06b5b75ae297f7ce2dc651f918f5be158c8d" integrity sha512-AdEER0Hkspgno2aR97SAf6vi0y0k8NuOpGnVH3O99rcA5Q6sh8QxcngtHuJ6uXwnfAXNM4Gn1Gb7/MV1+Ymbog== @@ -2248,19 +2248,19 @@ browserify-rsa@^4.0.0, browserify-rsa@^4.0.1: randombytes "^2.0.1" browserify-sign@^4.0.0: - version "4.2.1" - resolved "https://registry.yarnpkg.com/browserify-sign/-/browserify-sign-4.2.1.tgz#eaf4add46dd54be3bb3b36c0cf15abbeba7956c3" - integrity sha512-/vrA5fguVAKKAVTNJjgSm1tRQDHUU6DbwO9IROu/0WAzC8PKhucDSh18J0RMvVeHAn5puMd+QHC2erPRNf8lmg== + version "4.2.2" + resolved "https://registry.yarnpkg.com/browserify-sign/-/browserify-sign-4.2.2.tgz#e78d4b69816d6e3dd1c747e64e9947f9ad79bc7e" + integrity sha512-1rudGyeYY42Dk6texmv7c4VcQ0EsvVbLwZkA+AQB7SxvXxmcD93jcHie8bzecJ+ChDlmAm2Qyu0+Ccg5uhZXCg== dependencies: - bn.js "^5.1.1" - browserify-rsa "^4.0.1" + bn.js "^5.2.1" + browserify-rsa "^4.1.0" create-hash "^1.2.0" create-hmac "^1.1.7" - elliptic "^6.5.3" + elliptic "^6.5.4" inherits "^2.0.4" - parse-asn1 "^5.1.5" - readable-stream "^3.6.0" - safe-buffer "^5.2.0" + parse-asn1 "^5.1.6" + readable-stream "^3.6.2" + safe-buffer "^5.2.1" browserify-zlib@^0.2.0: version "0.2.0" @@ -3015,7 +3015,7 @@ electron-to-chromium@^1.4.535: resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.546.tgz#673ff64862859b1593cebfbacc5fb6aebef7c457" integrity sha512-cz9bBM26ZqoEmGHkdHXU3LP7OofVyEzRoMqfALQ9Au9WlB4rogAHzqj/NkNvw2JJjy4xuxS1me+pP2lbCD5Mfw== -elliptic@^6.5.3: +elliptic@^6.5.3, elliptic@^6.5.4: version "6.5.4" resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.5.4.tgz#da37cebd31e79a1367e941b592ed1fbebd58abbb" integrity sha512-iLhC6ULemrljPZb+QutR5TQGB+pdW6KGD5RSegS+8sorOZT+rdQFbsQFJgvN3eRqNALqJer4oQ16YvJHlU8hzQ== @@ -5607,7 +5607,7 @@ parent-module@^1.0.0: dependencies: callsites "^3.0.0" -parse-asn1@^5.0.0, parse-asn1@^5.1.5: +parse-asn1@^5.0.0, parse-asn1@^5.1.6: version "5.1.6" resolved "https://registry.yarnpkg.com/parse-asn1/-/parse-asn1-5.1.6.tgz#385080a3ec13cb62a62d39409cb3e88844cdaed4" integrity sha512-RnZRo1EPU6JBnra2vGHj0yhp6ebyjBZpmUCLHWiFhxlzvBCCpAuZ7elsBp1PVAbQN0/04VD/19rfzlBSwLstMw== @@ -5885,7 +5885,7 @@ readable-stream@^2.0.6: string_decoder "~1.1.1" util-deprecate "~1.0.1" -readable-stream@^3.0.2, readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.5.0, readable-stream@^3.6.0: +readable-stream@^3.0.2, readable-stream@^3.1.1, readable-stream@^3.4.0, readable-stream@^3.5.0, readable-stream@^3.6.0, readable-stream@^3.6.2: version "3.6.2" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.6.2.tgz#56a9b36ea965c00c5a93ef31eb111a0f11056967" integrity sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==