@bayswap/sdk
Version:
SDK for BaySwap smart contract
57 lines • 1.74 kB
JavaScript
const SUPPLY_TYPE_ARG_REGEX = /^0x2::balance::Supply<(.+)>$/;
export const getCoinTypeFromFullType = (fullType) => {
return fullType.substring(fullType.lastIndexOf('<') + 1, fullType.lastIndexOf('>'));
};
export const getSymbolFromType = (type) => {
const temp = type.split('::');
if (temp.length > 2) {
return temp[2];
}
return '';
};
export const getSymbolFromFullType = (fullType) => {
const coinType = getCoinTypeFromFullType(fullType);
return getSymbolFromType(coinType);
};
// 0x2::sui::Sui => 0x2::coin::Coin<0x2::sui::Sui>
export const wrapCoinType = (type) => {
return '0x2::coin::Coin<' + type + '>';
};
// 0x2::coin::Coin<0x2::sui::Sui> => 0x2::sui::Sui
export const unwrapCoinType = (fullType) => {
return fullType.substring(fullType.lastIndexOf('<') + 1, fullType.lastIndexOf('>'));
};
export const sortCoin = (coinX, coinY) => {
if (coinY < coinX) {
return [coinY, coinX];
}
return [coinX, coinY];
};
export const getTypeFromSupply = (supply) => {
if (supply.length == 0) {
return '';
}
const [, res] = supply.match(SUPPLY_TYPE_ARG_REGEX) ?? [];
return res;
};
export const buildPoolName = (coin1, coin2) => {
return `${getSymbolFromType(coin1)}-${getSymbolFromType(coin2)}`;
};
export const isCoinOrderSorted = (coin1, coin2) => {
if (coin1.length < coin2.length) {
return true;
}
else if (coin1.length > coin2.length) {
return false;
}
for (let i = 0; i < coin1.length; i++) {
const chr1 = coin1.charAt(i);
const chr2 = coin2.charAt(i);
if (chr1 == chr2) {
continue;
}
return chr1 < chr2;
}
return true;
};
//# sourceMappingURL=coin.js.map