-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: extracts price updater #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/price-update/price-update-confirmed/price-update-confirmed.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { mock } from "vitest-mock-extended"; | ||
| import type { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; | ||
| import { PriceUpdateConfirmed } from "./price-update-confirmed"; | ||
| import type { PriceUpdate, PriceUpdateOptions } from "../../types"; | ||
|
|
||
| describe(PriceUpdateConfirmed.name, () => { | ||
| it("executes contract with correct message and funds", async () => { | ||
| const { signingClient, updater } = setup(); | ||
| signingClient.execute.mockResolvedValue({ | ||
| transactionHash: "tx123", | ||
| gasUsed: 200_000n, | ||
| gasWanted: 300_000n, | ||
| height: 1, | ||
| logs: [], | ||
| events: [], | ||
| }); | ||
|
|
||
| await updater.updatePrice(priceUpdate, options); | ||
|
|
||
| expect(signingClient.execute).toHaveBeenCalledWith( | ||
| "akash1sender", | ||
| "akash1contract", | ||
| { update_price_feed: { vaa: "base64-encoded-vaa" } }, | ||
| "auto", | ||
| undefined, | ||
| [{ denom: "uakt", amount: "1000" }], | ||
| ); | ||
| }); | ||
|
|
||
| it("returns transactionHash and gasUsed from result", async () => { | ||
| const { signingClient, updater } = setup(); | ||
| signingClient.execute.mockResolvedValue({ | ||
| transactionHash: "abc", | ||
| gasUsed: 150_000n, | ||
| gasWanted: 200_000n, | ||
| height: 1, | ||
| logs: [], | ||
| events: [], | ||
| }); | ||
|
|
||
| const result = await updater.updatePrice(priceUpdate, options); | ||
|
|
||
| expect(result).toEqual({ | ||
| transactionHash: "abc", | ||
| gasUsed: 150_000n, | ||
| }); | ||
| }); | ||
|
|
||
| it("propagates execution errors", async () => { | ||
| const { signingClient, updater } = setup(); | ||
| signingClient.execute.mockRejectedValue(new Error("out of gas")); | ||
|
|
||
| await expect(updater.updatePrice(priceUpdate, options)).rejects.toThrow("out of gas"); | ||
| }); | ||
|
|
||
| const options: PriceUpdateOptions = { | ||
| senderAddress: "akash1sender", | ||
| contractAddress: "akash1contract", | ||
| denom: "uakt", | ||
| updateFee: "1000", | ||
| }; | ||
|
|
||
| const priceUpdate: PriceUpdate = { | ||
| priceData: { | ||
| id: "price-feed-id", | ||
| price: { price: "100", conf: "1", expo: -8, publish_time: 1000 }, | ||
| ema_price: { price: "99", conf: "2", expo: -8, publish_time: 1000 }, | ||
| }, | ||
| vaa: "base64-encoded-vaa", | ||
| }; | ||
|
|
||
| function setup() { | ||
| const signingClient = mock<SigningCosmWasmClient>(); | ||
| const updater = new PriceUpdateConfirmed(signingClient); | ||
| return { signingClient, updater }; | ||
| } | ||
|
|
||
| }); |
36 changes: 36 additions & 0 deletions
36
src/price-update/price-update-confirmed/price-update-confirmed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; | ||
| import type { PriceUpdate, PriceUpdateOptions, PriceUpdater, UpdatePriceFeedMsg } from "../../types"; | ||
|
stalniy marked this conversation as resolved.
Outdated
|
||
|
|
||
| export class PriceUpdateConfirmed implements PriceUpdater { | ||
| readonly #signingClient: SigningCosmWasmClient; | ||
|
|
||
| constructor(signingClient: SigningCosmWasmClient) { | ||
| this.#signingClient = signingClient; | ||
| } | ||
|
|
||
| async updatePrice(priceUpdate: PriceUpdate, options: PriceUpdateOptions): Promise<{ transactionHash: string; gasUsed: bigint }> { | ||
| // Prepare execute message with VAA | ||
| // The contract will: | ||
| // 1. Verify VAA via Wormhole contract | ||
| // 2. Parse Pyth price attestation from VAA payload | ||
| // 3. Validate price feed ID matches expected | ||
| // 4. Relay validated price to x/oracle module | ||
| const msg: UpdatePriceFeedMsg = { | ||
| update_price_feed: { | ||
| vaa: priceUpdate.vaa, | ||
| }, | ||
| }; | ||
| const result = await this.#signingClient.execute( | ||
| options.senderAddress, | ||
| options.contractAddress, | ||
| msg, | ||
| "auto", | ||
| undefined, | ||
| [{ denom: options.denom, amount: options.updateFee }], | ||
| ); | ||
| return { | ||
| transactionHash: result.transactionHash, | ||
| gasUsed: result.gasUsed, | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.