Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

feat: added a unique identifier to the quote within the timestamp metadata … #281

Merged
merged 3 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 19 additions & 0 deletions src/utils/number_utils.ts
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 => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nice function!

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;
},
};
19 changes: 17 additions & 2 deletions src/utils/service_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit but can we move this to constants.ts?

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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
},
Expand Down