@parifi/synthetix-sdk-ts
Version:
A Typescript SDK for interactions with the Synthetix protocol
168 lines (165 loc) • 4.99 kB
JavaScript
// src/utils/common.ts
import { formatEther, maxUint128, parseEther } from "viem";
import { mainnet, base, optimism, arbitrum, baseSepolia, arbitrumSepolia } from "viem/chains";
import { randomBytes } from "crypto";
// src/constants/common.ts
import { zeroAddress } from "viem";
var publicRpcEndpoints = {
8453: "https://base.llamarpc.com",
84532: "https://sepolia.base.org",
42161: "https://arbitrum.llamarpc.com",
421614: "https://sepolia-rollup.arbitrum.io/rpc"
};
var CUSTOM_DECIMALS = {
[421614 /* ARBITUM_SEPOLIA */]: {
2: 6,
USDC: 6
},
[42161 /* ARBITRUM */]: {
2: 6,
USDC: 6
},
[84532 /* BASE_SEPOLIA */]: {
1: 6,
USDC: 6
},
[8453 /* BASE */]: {
1: 6,
USDC: 6
}
};
// src/contracts/addreses/zap.ts
import { zeroAddress as zeroAddress2 } from "viem";
var ZAP_BY_CHAIN = {
[8453 /* BASE */]: "0x459Bbb4231f0a606DC514BacD5712A5922CBe6c8",
[84532 /* BASE_SEPOLIA */]: zeroAddress2,
[42161 /* ARBITRUM */]: "0x9ec181B2E69fB36C50031F0c87Bc0749b766A9f4",
[421614 /* ARBITUM_SEPOLIA */]: "0xb569ed692206a1d73996088ae646333b1d59d9c5"
};
var SYNTHETIX_ZAP = {
[84532 /* BASE_SEPOLIA */]: "0xA6Fab50eB36F3eB2118eE2f4C12C968F8608bbc9",
[8453 /* BASE */]: "0xA6Fab50eB36F3eB2118eE2f4C12C968F8608bbc9"
};
// src/utils/common.ts
function getPublicRpcEndpoint(chainId) {
return publicRpcEndpoints[chainId];
}
function getChain(chainId) {
const chains = [mainnet, base, optimism, arbitrum, baseSepolia, arbitrumSepolia];
for (const chain of Object.values(chains)) {
if (chain.id === chainId) {
return chain;
}
}
throw new Error(`Chain with id ${chainId} not found`);
}
function convertWeiToEther(amountInWei) {
if (amountInWei == void 0) {
throw new Error("Invalid amount received during conversion: undefined");
}
if (typeof amountInWei == "bigint") {
return Number(formatEther(amountInWei));
} else if (typeof amountInWei == "string") {
return Number(formatEther(BigInt(amountInWei)));
} else {
throw new Error("Expected string or bigint for conversion");
}
}
function convertEtherToWei(amount) {
if (amount == void 0) {
throw new Error("Invalid amount received during conversion: undefined");
}
if (typeof amount == "number") {
return parseEther(amount.toString());
} else if (typeof amount == "string") {
return parseEther(amount);
} else {
throw new Error("Expected string or bigint for conversion");
}
}
function sleep(seconds) {
return new Promise((resolve) => setTimeout(resolve, seconds * 1e3));
}
function generateRandomAccountId() {
const maxUint128Half = maxUint128 / BigInt(2);
const buffer = randomBytes(8);
const randomAccountId = BigInt("0x" + buffer.toString("hex"));
if (randomAccountId > maxUint128Half) {
throw new Error("Account ID greater than Maxuint128");
}
return randomAccountId;
}
var batchArray = (arr, batchSize) => {
return arr.reduce((acc, _, i) => i % batchSize ? acc : [...acc, arr.slice(i, i + batchSize)], []);
};
var fetcher = (url, init = {
headers: {
"Content-Type": "application/json"
}
}) => fetch(url, {
...init,
body: JSON.stringify(init.body),
headers: init.headers,
method: init.body && !init.method ? "POST" : init.method
});
var odoFetcher = async (url, options) => {
return fetcher(`https://api.odos.xyz/sor${url}`, options);
};
var assemblePath = async (user, pathId) => {
const response = await odoFetcher(`/assemble`, {
body: {
userAddr: user,
pathId
},
headers: {
"Content-Type": "application/json"
}
});
if (!response.ok) return "";
const data = await response.json();
return data.transaction.data;
};
var getOdosPath = async (quoteParams) => {
if (!quoteParams.fromToken || !quoteParams.toToken || !quoteParams.fromAmount) {
throw new Error("Missing required parameters for Odos path generation");
}
const data = {
chainId: quoteParams.fromChain,
inputTokens: [{ tokenAddress: quoteParams.fromToken, amount: quoteParams.fromAmount }],
outputTokens: [{ tokenAddress: quoteParams.toToken, proportion: 1 }],
userAddr: ZAP_BY_CHAIN[quoteParams.fromChain]
};
const response = await odoFetcher("/quote/v2", {
body: data,
headers: {
"Content-Type": "application/json"
}
});
if (!response.ok) {
console.error(`Failed to get Odos quote: ${response.status} ${response.statusText}`);
return { path: "" };
}
const quote = await response.json();
if (!quote?.pathId) {
console.error("Invalid response from Odos API: missing pathId");
return { path: "" };
}
const quoteId = quote.pathId;
const path = await assemblePath(data.userAddr, quoteId);
if (!path) {
console.error("Failed to assemble path from pathId");
}
return { path };
};
export {
batchArray,
convertEtherToWei,
convertWeiToEther,
fetcher,
generateRandomAccountId,
getChain,
getOdosPath,
getPublicRpcEndpoint,
sleep
};
//# sourceMappingURL=common.mjs.map