|
| 1 | +import { PriceServiceConnection } from "@pythnetwork/price-service-client"; |
| 2 | +import { |
| 3 | + ChainPriceListener, |
| 4 | + IPricePusher, |
| 5 | + PriceInfo, |
| 6 | + PriceItem, |
| 7 | +} from "../interface"; |
| 8 | +import { addLeading0x, DurationInSeconds } from "../utils"; |
| 9 | +import { Logger } from "pino"; |
| 10 | +import { Provider, Contract, hexlify, arrayify, Wallet, BN } from "fuels"; |
| 11 | +import { |
| 12 | + PYTH_CONTRACT_ABI, |
| 13 | + FUEL_ETH_ASSET_ID, |
| 14 | +} from "@pythnetwork/pyth-fuel-js"; |
| 15 | + |
| 16 | +// Convert TAI64 timestamp to Unix timestamp |
| 17 | +function tai64ToUnix(tai64: BN): number { |
| 18 | + // TAI64 is 2^62 seconds ahead of Unix epoch (1970-01-01) |
| 19 | + // Additional 10-second offset accounts for TAI being ahead of UTC at Unix epoch |
| 20 | + const result = BigInt(tai64.toString()) - BigInt(2n ** 62n) - 10n; |
| 21 | + return Number(result); |
| 22 | +} |
| 23 | + |
| 24 | +export class FuelPriceListener extends ChainPriceListener { |
| 25 | + private contract: Contract; |
| 26 | + |
| 27 | + constructor( |
| 28 | + private provider: Provider, |
| 29 | + private pythContractId: string, |
| 30 | + priceItems: PriceItem[], |
| 31 | + private logger: Logger, |
| 32 | + config: { |
| 33 | + pollingFrequency: DurationInSeconds; |
| 34 | + } |
| 35 | + ) { |
| 36 | + super(config.pollingFrequency, priceItems); |
| 37 | + this.contract = new Contract( |
| 38 | + this.pythContractId, |
| 39 | + PYTH_CONTRACT_ABI, |
| 40 | + this.provider |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + async getOnChainPriceInfo(priceId: string): Promise<PriceInfo | undefined> { |
| 45 | + try { |
| 46 | + const formattedPriceId = addLeading0x(priceId); |
| 47 | + const priceInfo = await this.contract.functions |
| 48 | + .price_unsafe(formattedPriceId) |
| 49 | + .get(); |
| 50 | + |
| 51 | + console.log({ |
| 52 | + conf: priceInfo.value.confidence.toString(), |
| 53 | + price: priceInfo.value.price.toString(), |
| 54 | + publishTime: tai64ToUnix(priceInfo.value.publish_time), |
| 55 | + }); |
| 56 | + |
| 57 | + this.logger.debug( |
| 58 | + `Polled a Fuel on chain price for feed ${this.priceIdToAlias.get( |
| 59 | + priceId |
| 60 | + )} (${priceId}).` |
| 61 | + ); |
| 62 | + |
| 63 | + return { |
| 64 | + conf: priceInfo.value.confidence.toString(), |
| 65 | + price: priceInfo.value.price.toString(), |
| 66 | + publishTime: tai64ToUnix(priceInfo.value.publish_time), |
| 67 | + }; |
| 68 | + } catch (err) { |
| 69 | + this.logger.error({ err, priceId }, `Polling on-chain price failed.`); |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +export class FuelPricePusher implements IPricePusher { |
| 76 | + private contract: Contract; |
| 77 | + |
| 78 | + constructor( |
| 79 | + private wallet: Wallet, |
| 80 | + private pythContractId: string, |
| 81 | + private priceServiceConnection: PriceServiceConnection, |
| 82 | + private logger: Logger |
| 83 | + ) { |
| 84 | + this.contract = new Contract( |
| 85 | + this.pythContractId, |
| 86 | + PYTH_CONTRACT_ABI, |
| 87 | + this.wallet as Provider |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + async updatePriceFeed( |
| 92 | + priceIds: string[], |
| 93 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 94 | + pubTimesToPush: number[] |
| 95 | + ): Promise<void> { |
| 96 | + if (priceIds.length === 0) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + let priceFeedUpdateData: string[]; |
| 101 | + try { |
| 102 | + priceFeedUpdateData = await this.priceServiceConnection.getLatestVaas( |
| 103 | + priceIds |
| 104 | + ); |
| 105 | + } catch (err: any) { |
| 106 | + this.logger.error(err, "getPriceFeedsUpdateData failed"); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + const updateData = priceFeedUpdateData.map((data) => |
| 111 | + arrayify(Buffer.from(data, "base64")) |
| 112 | + ); |
| 113 | + |
| 114 | + try { |
| 115 | + const updateFee = await this.contract.functions |
| 116 | + .update_fee(updateData) |
| 117 | + .get(); |
| 118 | + |
| 119 | + const result = await this.contract.functions |
| 120 | + .update_price_feeds(updateData) |
| 121 | + .callParams({ |
| 122 | + forward: [updateFee.value, hexlify(FUEL_ETH_ASSET_ID)], |
| 123 | + }) |
| 124 | + .call(); |
| 125 | + |
| 126 | + this.logger.info( |
| 127 | + { transactionId: result.transactionId }, |
| 128 | + "updatePriceFeed successful" |
| 129 | + ); |
| 130 | + } catch (err: any) { |
| 131 | + this.logger.error(err, "updatePriceFeed failed"); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments