|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { mock, mockDeep } from "vitest-mock-extended"; |
| 3 | +import type { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; |
| 4 | +import type { OfflineDirectSigner } from "@cosmjs/proto-signing"; |
| 5 | +import type { Account } from "@cosmjs/stargate"; |
| 6 | +import { GasPrice } from "@cosmjs/stargate"; |
| 7 | +import { fromBase64, toBase64, toUtf8 } from "@cosmjs/encoding"; |
| 8 | +import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx"; |
| 9 | +import { PriceUpdateFireAndForget } from "./price-update-fire-and-forget"; |
| 10 | +import type { PriceUpdate, PriceUpdateOptions } from "../../types"; |
| 11 | + |
| 12 | +describe(PriceUpdateFireAndForget.name, () => { |
| 13 | + it("returns the transaction hash from the broadcast", async () => { |
| 14 | + const { updater, signingClient } = setup(); |
| 15 | + signingClient.broadcastTxSync.mockResolvedValue("broadcast-hash"); |
| 16 | + |
| 17 | + const result = await updater.updatePrice(priceUpdate, options); |
| 18 | + |
| 19 | + expect(result).toEqual({ transactionHash: "broadcast-hash" }); |
| 20 | + }); |
| 21 | + |
| 22 | + it("executes the price feed contract with the VAA and update fee", async () => { |
| 23 | + const { updater, signingClient } = setup(); |
| 24 | + |
| 25 | + await updater.updatePrice(priceUpdate, options); |
| 26 | + |
| 27 | + expect(signingClient.registry.encodeAsAny).toHaveBeenCalledWith({ |
| 28 | + typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", |
| 29 | + value: expect.objectContaining({ |
| 30 | + sender: "akash1sender", |
| 31 | + contract: "akash1contract", |
| 32 | + msg: toUtf8(JSON.stringify({ update_price_feed: { vaa: "base64-encoded-vaa" } })), |
| 33 | + funds: [{ denom: "uakt", amount: "1000" }], |
| 34 | + }), |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + it("estimates the fee from a simulation with a 1.5x gas buffer", async () => { |
| 39 | + const { updater, signingClient } = setup(); |
| 40 | + signingClient.simulate.mockResolvedValue(100_000); |
| 41 | + |
| 42 | + await updater.updatePrice(priceUpdate, options); |
| 43 | + |
| 44 | + expect(signingClient.simulate).toHaveBeenCalledWith("akash1sender", expect.any(Array), ""); |
| 45 | + }); |
| 46 | + |
| 47 | + it("signs with the sender address and broadcasts the signed bytes", async () => { |
| 48 | + const { updater, signingClient, signer } = setup(); |
| 49 | + |
| 50 | + await updater.updatePrice(priceUpdate, options); |
| 51 | + |
| 52 | + expect(signer.signDirect).toHaveBeenCalledWith("akash1sender", expect.anything()); |
| 53 | + |
| 54 | + const expectedTx = TxRaw.encode(TxRaw.fromPartial({ |
| 55 | + bodyBytes: signedBodyBytes, |
| 56 | + authInfoBytes: signedAuthInfoBytes, |
| 57 | + signatures: [fromBase64(signatureBase64)], |
| 58 | + })).finish(); |
| 59 | + expect(signingClient.broadcastTxSync).toHaveBeenCalledWith(expectedTx); |
| 60 | + }); |
| 61 | + |
| 62 | + it("caches the chain id and account across calls", async () => { |
| 63 | + const { updater, signingClient } = setup(); |
| 64 | + |
| 65 | + await updater.updatePrice(priceUpdate, options); |
| 66 | + await updater.updatePrice(priceUpdate, options); |
| 67 | + |
| 68 | + expect(signingClient.getChainId).toHaveBeenCalledTimes(1); |
| 69 | + expect(signingClient.getAccount).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | + |
| 72 | + it("throws when the account is not found on chain", async () => { |
| 73 | + const { updater, signingClient } = setup(); |
| 74 | + signingClient.getAccount.mockResolvedValue(null); |
| 75 | + |
| 76 | + await expect(updater.updatePrice(priceUpdate, options)).rejects.toThrow( |
| 77 | + "Account akash1sender not found on chain", |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it("throws when the account has no public key on chain", async () => { |
| 82 | + const { updater, signingClient } = setup(); |
| 83 | + signingClient.getAccount.mockResolvedValue({ ...account, pubkey: null }); |
| 84 | + |
| 85 | + await expect(updater.updatePrice(priceUpdate, options)).rejects.toThrow( |
| 86 | + "has no public key on chain", |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("propagates broadcast errors", async () => { |
| 91 | + const { updater, signingClient } = setup(); |
| 92 | + signingClient.broadcastTxSync.mockRejectedValue(new Error("mempool full")); |
| 93 | + |
| 94 | + await expect(updater.updatePrice(priceUpdate, options)).rejects.toThrow("mempool full"); |
| 95 | + }); |
| 96 | + |
| 97 | + const options: PriceUpdateOptions = { |
| 98 | + senderAddress: "akash1sender", |
| 99 | + contractAddress: "akash1contract", |
| 100 | + denom: "uakt", |
| 101 | + updateFee: "1000", |
| 102 | + gasPrice: GasPrice.fromString("0.025uakt"), |
| 103 | + }; |
| 104 | + |
| 105 | + const priceUpdate: PriceUpdate = { |
| 106 | + priceData: { |
| 107 | + id: "price-feed-id", |
| 108 | + price: { price: "100", conf: "1", expo: -8, publish_time: 1000 }, |
| 109 | + ema_price: { price: "99", conf: "2", expo: -8, publish_time: 1000 }, |
| 110 | + }, |
| 111 | + vaa: "base64-encoded-vaa", |
| 112 | + }; |
| 113 | + |
| 114 | + // A valid compressed secp256k1 public key (33 bytes starting with 0x02) so the |
| 115 | + // real encodePubkey/makeAuthInfoBytes pipeline accepts it. |
| 116 | + const compressedPubkey = new Uint8Array(33); |
| 117 | + compressedPubkey[0] = 0x02; |
| 118 | + |
| 119 | + const account: Account = { |
| 120 | + address: "akash1sender", |
| 121 | + pubkey: { type: "tendermint/PubKeySecp256k1", value: toBase64(compressedPubkey) }, |
| 122 | + accountNumber: 7, |
| 123 | + sequence: 3, |
| 124 | + }; |
| 125 | + |
| 126 | + const signedBodyBytes = new Uint8Array([1, 2, 3]); |
| 127 | + const signedAuthInfoBytes = new Uint8Array([4, 5, 6]); |
| 128 | + const signatureBase64 = toBase64(new Uint8Array(64)); |
| 129 | + |
| 130 | + function setup() { |
| 131 | + const signingClient = mockDeep<SigningCosmWasmClient>(); |
| 132 | + const signer = mock<OfflineDirectSigner>(); |
| 133 | + |
| 134 | + signingClient.getChainId.mockResolvedValue("akash-testnet"); |
| 135 | + signingClient.getAccount.mockResolvedValue(account); |
| 136 | + signingClient.simulate.mockResolvedValue(100_000); |
| 137 | + signingClient.registry.encodeAsAny.mockReturnValue({ typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract", value: new Uint8Array([9]) }); |
| 138 | + signingClient.broadcastTxSync.mockResolvedValue("tx-hash"); |
| 139 | + signer.signDirect.mockResolvedValue({ |
| 140 | + signature: { pub_key: { type: "", value: "" }, signature: signatureBase64 }, |
| 141 | + signed: { |
| 142 | + bodyBytes: signedBodyBytes, |
| 143 | + authInfoBytes: signedAuthInfoBytes, |
| 144 | + chainId: "akash-testnet", |
| 145 | + accountNumber: 7n, |
| 146 | + }, |
| 147 | + }); |
| 148 | + |
| 149 | + const updater = new PriceUpdateFireAndForget(signingClient, signer); |
| 150 | + return { signingClient, signer, updater }; |
| 151 | + } |
| 152 | +}); |
0 commit comments