This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
feat: added a unique identifier to the quote within the timestamp metadata … #281
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export const numberUtils = { | ||
// from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random | ||
randomNumberInclusive: (minimumSpecified: number, maximumSpecified: number): number => { | ||
const min = Math.ceil(minimumSpecified); | ||
const max = Math.floor(maximumSpecified); | ||
return Math.floor(Math.random() * (max - min + 1)) + min; // The maximum is inclusive and the minimum is inclusive | ||
}, | ||
// creates a random hex number of desired length by stringing together | ||
// random integers from 1-15, guaranteeing the | ||
// result is a hex number of the given length | ||
randomHexNumberOfLength: (numberLength: number): string => { | ||
let res = ''; | ||
for (let i = 0; i < numberLength; i++) { | ||
// tslint:disable-next-line:custom-no-magic-numbers | ||
res = `${res}${numberUtils.randomNumberInclusive(1, 15).toString(16)}`; | ||
} | ||
return res; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,8 @@ import { GasTokenRefundInfo, GetSwapQuoteResponseLiquiditySource } from '../type | |
import { orderUtils } from '../utils/order_utils'; | ||
import { findTokenDecimalsIfExists } from '../utils/token_metadata_utils'; | ||
|
||
import { numberUtils } from './number_utils'; | ||
|
||
export const serviceUtils = { | ||
attributeSwapQuoteOrders( | ||
swapQuote: MarketSellSwapQuote | MarketBuySwapQuote, | ||
|
@@ -69,8 +71,21 @@ export const serviceUtils = { | |
stateMutability: 'view', | ||
type: 'function', | ||
}); | ||
const timestamp = new BigNumber(Date.now() / ONE_SECOND_MS).integerValue(); | ||
const encodedAffiliateData = affiliateCallDataEncoder.encode([affiliateAddressOrDefault, timestamp]); | ||
|
||
// Generate unique identiifer | ||
const HEX_DIGITS = 16; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit but can we move this to |
||
const timestampInSeconds = new BigNumber(Date.now() / ONE_SECOND_MS).integerValue(); | ||
const hexTimestamp = timestampInSeconds.toString(HEX_DIGITS); | ||
const randomNumber = numberUtils.randomHexNumberOfLength(10); | ||
|
||
// Concatenate the hex identifier with the hex timestamp | ||
// In the final encoded call data, this will leave us with a 5-byte ID followed by | ||
// a 4-byte timestamp, and won't break parsers of the timestamp made prior to the | ||
// addition of the ID | ||
const uniqueIdentifier = new BigNumber(`${randomNumber}${hexTimestamp}`, HEX_DIGITS); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. might be nice to move the unique identification generation to a separate function, and add a unit test to ensure that the timestamp you can parse out of it is "sane" you could do this with freezing time in tests (but I think this would add a new dependency), or at least ensure that the returned timestamp is within ~10 seconds of the current timestamp |
||
|
||
// Encode additional call data and return | ||
const encodedAffiliateData = affiliateCallDataEncoder.encode([affiliateAddressOrDefault, uniqueIdentifier]); | ||
const affiliatedData = `${data}${encodedAffiliateData.slice(2)}`; | ||
return affiliatedData; | ||
}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice function!