Skip to content
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

fix: amm human spot price wrong #265

Merged
merged 5 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Run tests before merge
on:
pull_request:
branches: [staging]
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: "21"

- name: Install pnpm
run: npm install -g pnpm

- name: Install bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies
run: pnpm install

- name: Test
run: bun test
env:
RPC_ENDPOINT: https://api.devnet.solana.com/
INDEXER_URL: https://staging-indexer.metadao.fi
INDEXER_WSS_URL: wss://staging-indexer.metadao.fi
67 changes: 34 additions & 33 deletions packages/indexer/src/transaction/serializer.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import { serialize, deserialize, Transaction } from './serializer';
import { expect, test } from 'bun:test';
import { serialize, deserialize, Transaction } from "./serializer";
import { expect, describe, test } from "bun:test";

test('serialize-deserialize', async () => {
describe("serializer", async () => {
test("serialize-deserialize", async () => {
const testTx: Transaction = {
blockTime: 0,
slot: 0,
recentBlockhash: "",
computeUnitsConsumed: BigInt(4),
fee: BigInt(2),
signatures: [],
version: "legacy",
logMessages: [],
accounts: [
{
pubkey: "BIGINT:a300n", // false flag
isSigner: true,
isWriteable: false,
preBalance: BigInt(800),
postBalance: BigInt(3000000),
},
],
instructions: [],
};

const testTx: Transaction = {
blockTime: 0,
slot: 0,
recentBlockhash: "",
computeUnitsConsumed: BigInt(4),
fee: BigInt(2),
signatures: [],
version: 'legacy',
logMessages: [],
accounts: [
{
pubkey: "BIGINT:a300n", // false flag
isSigner: true,
isWriteable: false,
preBalance: BigInt(800),
postBalance: BigInt(3000000)
}
],
instructions: []
};
const str = serialize(testTx);

const str = serialize(testTx);
expect(str).toBe(
`{"blockTime":0,"slot":0,"recentBlockhash":"",` +
`"computeUnitsConsumed":"BIGINT:4","fee":"BIGINT:2","signatures":[],"version":"legacy","logMessages":[],` +
`"accounts":[{"pubkey":"BIGINT:a300n","isSigner":true,"isWriteable":false,"preBalance":"BIGINT:800","postBalance":"BIGINT:3000000"}],` +
`"instructions":[]}`
);

expect(str).toBe(
`{"blockTime":0,"slot":0,"recentBlockhash":"",` +
`"computeUnitsConsumed":"BIGINT:4","fee":"BIGINT:2","signatures":[],"version":"legacy","logMessages":[],`+
`"accounts":[{"pubkey":"BIGINT:a300n","isSigner":true,"isWriteable":false,"preBalance":"BIGINT:800","postBalance":"BIGINT:3000000"}],`+
`"instructions":[]}`
);
const deserialized = deserialize(str) as any;

const deserialized = deserialize(str) as any;

expect(deserialized).toEqual({success: true, ok: testTx});
expect(deserialized).toEqual({ success: true, ok: testTx });
});
});
17 changes: 17 additions & 0 deletions packages/indexer/src/usecases/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { expect, test, describe } from "bun:test";
import { getHumanPrice } from "./math";
import { PriceMath } from "@metadaoproject/futarchy";
import { BN } from "@coral-xyz/anchor";

describe("getHumanPrice", () => {
test("decimal value", () => {
const priceFromReserves = PriceMath.getAmmPriceFromReserves(
new BN(25000000000),
new BN(10000000000)
);

const price = getHumanPrice(priceFromReserves, 6, 6);

expect(price).toBe(0.4);
});
});
15 changes: 11 additions & 4 deletions packages/indexer/src/usecases/math.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import { BN } from "@coral-xyz/anchor";
import { logger } from "../logger";

export function getHumanPrice(
ammPrice: BN,
baseDecimals: number,
quoteDecimals: number
) {
let decimalScalar = new BN(10).pow(
): number {
const decimalScalar = new BN(10).pow(
new BN(quoteDecimals - baseDecimals).abs()
);
let price1e12 =
const price1e12 =
quoteDecimals > baseDecimals
? ammPrice.div(decimalScalar)
: ammPrice.mul(decimalScalar);
return price1e12.div(new BN(1e12)).toNumber();

try {
return price1e12.toNumber() / 1e12;
} catch (e) {
logger.error("error with getting human price", e);
return price1e12.div(new BN(1e12)).toNumber();
}
}
Loading