@d8x/perpetuals-sdk
Version:
Node TypeScript SDK for D8X Perpetual Futures
73 lines • 3.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.STUSDToUSDC = void 0;
const d8XMath_1 = require("./d8XMath");
const utils_1 = require("./utils");
const onChainPxFeed_1 = __importDefault(require("./onChainPxFeed"));
const ethers_1 = require("ethers");
/**
* OnChainPxFeedAngle: get STUSD-USDC exchange rate
*/
class OnChainPxFeedAngle extends onChainPxFeed_1.default {
constructor(rpcs) {
super(rpcs);
this.delayMs = 5000;
this.fetchInProgress = false;
}
async fetchPrice(delay) {
if (!this.fetchInProgress) {
this.fetchInProgress = true;
this.lastResponseTs = Date.now();
let hasErr = false;
for (let trial = 0; trial < this.rpcs.length; trial++) {
try {
this.lastPx = await STUSDToUSDC(this.provider);
break;
}
catch (err) {
console.log(`onChainPxSources error ${this.rpcs[this.lastRpc]}: retrying with other rpc`);
hasErr = true;
}
this.setRpc();
}
if (hasErr) {
console.log("onChainPxSources not successful");
}
if (delay) {
await (0, utils_1.sleepForSec)(this.delayMs / 1000);
}
this.fetchInProgress = false;
}
}
}
exports.default = OnChainPxFeedAngle;
/**
* Gets the price of one Angle stUSD in USDC from
* on-chain
* @param provider arbitrum provider
* @returns STUSD-USDC
*/
async function STUSDToUSDC(provider) {
// ABIs for IERC4626 and ITransmuter contracts
const ierc4626ABI = ["function previewMint(uint256) external view returns (uint256)"];
const iTransmuterABI = ["function quoteOut(uint256, address, address) external view returns (uint256)"];
// addresses
const STUSD = "0x0022228a2cc5E7eF0274A7Baa600d44da5aB5776"; //stUSD, savings
const USDC = "0xaf88d065e77c8cC2239327C5EDb3A432268e5831"; //native USDC, 6 decimals
const USDA = "0x0000206329b97DB379d5E1Bf586BbDB969C63274"; //USDA on Arbitrum, 18 decimals
const transmuterAddr = "0xD253b62108d1831aEd298Fc2434A5A8e4E418053"; //for USDA, arbitrum
// execute
const ierc4626Contract = new ethers_1.Contract(STUSD, ierc4626ABI, provider);
const iTransmuterContract = new ethers_1.Contract(transmuterAddr, iTransmuterABI, provider);
// Call previewMint to get amountUSDA
const ONE_STUSD = 10n ** 18n; // BigNumber.from(10).pow(BigNumber.from(18));
const amountUSDA = await ierc4626Contract.previewMint(ONE_STUSD);
// Call quoteOut to get amountIn
const amountUSDC = await iTransmuterContract.quoteOut(amountUSDA, USDC, USDA);
return (0, d8XMath_1.decNToFloat)(amountUSDC, 6);
}
exports.STUSDToUSDC = STUSDToUSDC;
//# sourceMappingURL=onChainPxFeedAngle.js.map