Skip to content

Commit

Permalink
Cajoled the token swaps pair name to match that in Ligo
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Nov 23, 2023
1 parent c134b75 commit 803c042
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
4 changes: 3 additions & 1 deletion batcher-ui/src/components/batcher/SelectPair.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ const SelectPair = ({ isFrom }: SelectPairProps) => {
newError(
pairName +
' is not a valid pair. Only ' +
availableSwaps.join() +
availableSwaps
.map(s => {s + '\n'})
.join() +
'are supported.'
)
);
Expand Down
28 changes: 25 additions & 3 deletions batcher-ui/src/utils/token-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,34 @@ const getSwapFromBigmap = (
`${process.env.NEXT_PUBLIC_TZKT_API_URI}/v1/bigmaps/${bigMapId}/keys/${swapName}`
).then(checkStatus);


// FIXME =- This is the only way I could ge tthe string comparisons to work the same way as they do in the contract ¯\_(ツ)_/¯
const alignWithLigoLexicographicalSorting = (to: string, from: string) => {
const startsWithTzTo = to.startsWith('tz');
const startsWithTzFrom = from.startsWith('tz');
const endsWithTzFrom = from.endsWith('tz');

if (!startsWithTzTo && endsWithTzFrom) {
return from.localeCompare(to);
}
if (startsWithTzTo && endsWithTzFrom) {
return 1;
}
if (startsWithTzTo && !startsWithTzFrom) {
return -1;
}
if (!startsWithTzTo && startsWithTzFrom) {
return 1;
}

return to.localeCompare(from);
};

export const getLexicographicalPairName = (
to: string,
from: string
): string => {
const comp = to.localeCompare(from);
const comp = alignWithLigoLexicographicalSorting(to, from);
if (comp < 0) {
return `${to}-${from}`;
} else {
Expand Down Expand Up @@ -179,7 +202,6 @@ export const getTokensMetadata = async () => {
);
};


export const getTokensFromStorage = async () => {
const storage = await getTokenManagerStorage();
const validTokens = storage['valid_tokens'];
Expand Down Expand Up @@ -221,4 +243,4 @@ export const getSwapsFromStorage = async () => {
};
})
);
}
};
2 changes: 1 addition & 1 deletion batcher/marketmaker.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ let execute_liquidity_request
(vault_address:address)
(valid_tokens:ValidTokens.t_map)
(valid_swaps:ValidSwaps.t_map): operation =
let pair_name = find_lexicographical_pair_name lt.name ot.name in
let pair_name = getLexicographicalPairName lt.name ot.name in
match Map.find_opt pair_name valid_swaps with
| None -> failwith swap_does_not_exist
| Some vs -> let (lastupdated_opt, _tes) = OracleUtils.get_oracle_price pair_name unable_to_get_price_from_oracle vs (Big_map.empty: TickErrors.t) in
Expand Down
8 changes: 0 additions & 8 deletions batcher/utils.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,6 @@ let nat_to_tolerance (tolerance : nat) : tolerance =
else if tolerance = 2n then Plus
else failwith unable_to_parse_tolerance_from_external_order

[@inline]
let find_lexicographical_pair_name
(token_one_name: string)
(token_two_name: string) : string =
if token_one_name > token_two_name then
token_one_name ^ "-" ^ token_two_name
else
token_two_name ^ "-" ^ token_one_name

[@inline]
let get_rate_name_from_swap (s : swap_reduced) : string =
Expand Down

0 comments on commit 803c042

Please sign in to comment.