Skip to content

Commit

Permalink
Merge pull request #265 from metaDAOproject/fix/amm-calc-wrong
Browse files Browse the repository at this point in the history
fix: amm  human spot price wrong
  • Loading branch information
R-K-H authored Aug 28, 2024
2 parents 0dfb797 + 88a1a74 commit 90b25e6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 37 deletions.
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();
}
}

0 comments on commit 90b25e6

Please sign in to comment.