|
| 1 | +import { PriceServiceConnection } from "@pythnetwork/price-service-client"; |
| 2 | +import { |
| 3 | + ChainPriceListener, |
| 4 | + IPricePusher, |
| 5 | + PriceInfo, |
| 6 | + PriceItem, |
| 7 | +} from "../interface"; |
| 8 | +import { DurationInSeconds } from "../utils"; |
| 9 | +import { Logger } from "pino"; |
| 10 | +import { |
| 11 | + Address, |
| 12 | + ContractProvider, |
| 13 | + OpenedContract, |
| 14 | + Sender, |
| 15 | + TonClient, |
| 16 | + WalletContractV4, |
| 17 | +} from "@ton/ton"; |
| 18 | +import { keyPairFromSeed } from "@ton/crypto"; |
| 19 | +import { |
| 20 | + PythContract, |
| 21 | + calculateUpdatePriceFeedsFee, |
| 22 | +} from "@pythnetwork/pyth-ton-js"; |
| 23 | + |
| 24 | +export class TonPriceListener extends ChainPriceListener { |
| 25 | + private contract: OpenedContract<PythContract>; |
| 26 | + |
| 27 | + constructor( |
| 28 | + private provider: ContractProvider, |
| 29 | + private contractAddress: Address, |
| 30 | + priceItems: PriceItem[], |
| 31 | + private logger: Logger, |
| 32 | + config: { |
| 33 | + pollingFrequency: DurationInSeconds; |
| 34 | + } |
| 35 | + ) { |
| 36 | + super(config.pollingFrequency, priceItems); |
| 37 | + this.contract = this.provider.open( |
| 38 | + PythContract.createFromAddress(this.contractAddress) |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + async getOnChainPriceInfo(priceId: string): Promise<PriceInfo | undefined> { |
| 43 | + try { |
| 44 | + const priceInfo = await this.contract.getPriceUnsafe(priceId); |
| 45 | + |
| 46 | + this.logger.debug( |
| 47 | + `Polled a TON on chain price for feed ${this.priceIdToAlias.get( |
| 48 | + priceId |
| 49 | + )} (${priceId}).` |
| 50 | + ); |
| 51 | + |
| 52 | + return { |
| 53 | + conf: priceInfo.conf.toString(), |
| 54 | + price: priceInfo.price.toString(), |
| 55 | + publishTime: priceInfo.publishTime, |
| 56 | + }; |
| 57 | + } catch (err) { |
| 58 | + this.logger.error({ err, priceId }, `Polling on-chain price failed.`); |
| 59 | + return undefined; |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +export class TonPricePusher implements IPricePusher { |
| 65 | + private contract: OpenedContract<PythContract>; |
| 66 | + private sender: Sender; |
| 67 | + |
| 68 | + constructor( |
| 69 | + private client: TonClient, |
| 70 | + private privateKey: string, |
| 71 | + private contractAddress: Address, |
| 72 | + private priceServiceConnection: PriceServiceConnection, |
| 73 | + private logger: Logger |
| 74 | + ) { |
| 75 | + this.contract = this.client |
| 76 | + .provider(this.contractAddress) |
| 77 | + .open(PythContract.createFromAddress(this.contractAddress)); |
| 78 | + const keyPair = keyPairFromSeed(Buffer.from(this.privateKey, "hex")); |
| 79 | + const wallet = WalletContractV4.create({ |
| 80 | + publicKey: keyPair.publicKey, |
| 81 | + workchain: 0, |
| 82 | + }); |
| 83 | + const provider = this.client.open(wallet); |
| 84 | + this.sender = provider.sender(keyPair.secretKey); |
| 85 | + } |
| 86 | + |
| 87 | + async updatePriceFeed( |
| 88 | + priceIds: string[], |
| 89 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 90 | + pubTimesToPush: number[] |
| 91 | + ): Promise<void> { |
| 92 | + if (priceIds.length === 0) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + let priceFeedUpdateData: string[]; |
| 97 | + try { |
| 98 | + priceFeedUpdateData = await this.priceServiceConnection.getLatestVaas( |
| 99 | + priceIds |
| 100 | + ); |
| 101 | + } catch (err: any) { |
| 102 | + this.logger.error(err, "getPriceFeedsUpdateData failed"); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + try { |
| 107 | + for (const updateData of priceFeedUpdateData) { |
| 108 | + const updateDataBuffer = Buffer.from(updateData, "base64"); |
| 109 | + const updateFee = await this.contract.getUpdateFee(updateDataBuffer); |
| 110 | + const totalFee = |
| 111 | + calculateUpdatePriceFeedsFee(BigInt(updateFee)) + BigInt(updateFee); |
| 112 | + |
| 113 | + await this.contract.sendUpdatePriceFeeds( |
| 114 | + this.sender, |
| 115 | + updateDataBuffer, |
| 116 | + totalFee |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + this.logger.info("updatePriceFeed successful"); |
| 121 | + } catch (err: any) { |
| 122 | + this.logger.error(err, "updatePriceFeed failed"); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments