@parifi/synthetix-sdk-ts
Version:
A Typescript SDK for interactions with the Synthetix protocol
201 lines (197 loc) • 6.43 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/utils/common.ts
var common_exports = {};
__export(common_exports, {
batchArray: () => batchArray,
convertEtherToWei: () => convertEtherToWei,
convertWeiToEther: () => convertWeiToEther,
fetcher: () => fetcher,
generateRandomAccountId: () => generateRandomAccountId,
getChain: () => getChain,
getOdosPath: () => getOdosPath,
getPublicRpcEndpoint: () => getPublicRpcEndpoint,
sleep: () => sleep
});
module.exports = __toCommonJS(common_exports);
var import_viem3 = require("viem");
var import_chains3 = require("viem/chains");
var import_crypto = require("crypto");
// src/constants/common.ts
var import_viem = require("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
var import_viem2 = require("viem");
var ZAP_BY_CHAIN = {
[8453 /* BASE */]: "0x459Bbb4231f0a606DC514BacD5712A5922CBe6c8",
[84532 /* BASE_SEPOLIA */]: import_viem2.zeroAddress,
[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 = [import_chains3.mainnet, import_chains3.base, import_chains3.optimism, import_chains3.arbitrum, import_chains3.baseSepolia, import_chains3.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((0, import_viem3.formatEther)(amountInWei));
} else if (typeof amountInWei == "string") {
return Number((0, import_viem3.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 (0, import_viem3.parseEther)(amount.toString());
} else if (typeof amount == "string") {
return (0, import_viem3.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 = import_viem3.maxUint128 / BigInt(2);
const buffer = (0, import_crypto.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 };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
batchArray,
convertEtherToWei,
convertWeiToEther,
fetcher,
generateRandomAccountId,
getChain,
getOdosPath,
getPublicRpcEndpoint,
sleep
});
//# sourceMappingURL=common.js.map