-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #265 from metaDAOproject/fix/amm-calc-wrong
fix: amm human spot price wrong
- Loading branch information
Showing
4 changed files
with
93 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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,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 |
This file contains 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 |
---|---|---|
@@ -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 }); | ||
}); | ||
}); |
This file contains 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,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); | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
@@ -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(); | ||
} | ||
} |