SHA256
Перевести донаты на Stars и обновить Devnet
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import vm from 'node:vm';
|
||||
import {
|
||||
deriveStarsPythOracleAccounts,
|
||||
getStarsQuoteFromRpc,
|
||||
parseVerifiedPythPriceAccount,
|
||||
PYTH_SOL_USD_FEED_ID,
|
||||
PYTH_USDT_USD_FEED_ID,
|
||||
PYTH_RECEIVER_PROGRAM_ID,
|
||||
} from '../js/services/shine-stars-pyth.js';
|
||||
|
||||
const validOwner = { toBase58: () => PYTH_RECEIVER_PROGRAM_ID };
|
||||
const now = 1_800_000_000;
|
||||
const discr = [34,241,35,99,157,126,244,205];
|
||||
function fakePrice(feed, value, { publish = now - 15, confidence = 20_000_000n, verified = true, owner = validOwner, exponent = -8 } = {}) {
|
||||
const bytes = new Uint8Array(133);
|
||||
bytes.set(discr);
|
||||
bytes[40] = verified ? 1 : 0;
|
||||
bytes.set(Uint8Array.from(feed.match(/../g), (hex) => parseInt(hex, 16)), 41);
|
||||
const v = new DataView(bytes.buffer);
|
||||
v.setBigInt64(73, BigInt(value), true);
|
||||
v.setBigUint64(81, confidence, true);
|
||||
v.setInt32(89, exponent, true);
|
||||
v.setBigInt64(93, BigInt(publish), true);
|
||||
return { owner, data: bytes };
|
||||
}
|
||||
const solPrice = fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n); // $150.00
|
||||
const usdtPrice = fakePrice(PYTH_USDT_USD_FEED_ID, 100_000_000n, {confidence: 1000n}); // $1.00
|
||||
|
||||
// Browser-vendored Solana web3.js; no network required, same code as production.
|
||||
const context = { Buffer, Uint8Array, TextEncoder, TextDecoder, console, setTimeout, crypto: globalThis.crypto };
|
||||
vm.createContext(context);
|
||||
vm.runInContext(readFileSync(new URL('../js/vendor/solana-web3.iife.min.js', import.meta.url), 'utf8'), context);
|
||||
const solana = context.solanaWeb3;
|
||||
|
||||
test('derive shard-0 Pyth Core oracle PDAs (official mainnet examples)', () => {
|
||||
assert.deepEqual(deriveStarsPythOracleAccounts(solana), {
|
||||
solUsd: '7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE',
|
||||
usdtUsd: 'HT2PLQBcG5EiCcNSaMHAjSgd9F98ecpATbk4Sk5oYuM',
|
||||
});
|
||||
});
|
||||
test('read raw Anchor PriceUpdateV2 without precision loss', () => {
|
||||
assert.equal(parseVerifiedPythPriceAccount(solPrice, PYTH_SOL_USD_FEED_ID, now).priceE8, 15_000_000_000n);
|
||||
assert.equal(parseVerifiedPythPriceAccount(usdtPrice, PYTH_USDT_USD_FEED_ID, now).priceE8, 100_000_000n);
|
||||
});
|
||||
test('fail closed for stale/future/unverified/malformed/foreign price accounts', () => {
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n, {publish: now - 3_601}), PYTH_SOL_USD_FEED_ID, now), /устарела/);
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n, {publish: now + 16}), PYTH_SOL_USD_FEED_ID, now), /время/);
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n, {verified:false}), PYTH_SOL_USD_FEED_ID, now), /проверку/);
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n), PYTH_USDT_USD_FEED_ID, now), /другую/);
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n, {owner:{toBase58:()=> 'Bad'}}), PYTH_SOL_USD_FEED_ID, now), /владельца/);
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n, {confidence: 150_000_001n}), PYTH_SOL_USD_FEED_ID, now), /интервал/);
|
||||
const invalid = fakePrice(PYTH_SOL_USD_FEED_ID, 15_000_000_000n);
|
||||
invalid.data[0] ^= 1;
|
||||
assert.throws(() => parseVerifiedPythPriceAccount(invalid, PYTH_SOL_USD_FEED_ID, now), /формат/);
|
||||
});
|
||||
test('request both accounts once from authenticated Solana RPC, no Hermes', async () => {
|
||||
let invoked = 0;
|
||||
const connection = { getMultipleAccountsInfo: async (keys, commitment) => {
|
||||
invoked++;
|
||||
assert.equal(keys.length, 2);
|
||||
assert.equal(commitment, 'confirmed');
|
||||
assert.equal(keys[0].toBase58(), '7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE');
|
||||
return [solPrice, usdtPrice];
|
||||
} };
|
||||
const quote = await getStarsQuoteFromRpc({ solanaEndpoint: 'https://test.shineup.me/solana-rpc', solana, connection, nowSeconds: now });
|
||||
assert.equal(quote.solUsdE8, '15000000000');
|
||||
assert.equal(quote.usdtUsdE8, '100000000');
|
||||
assert.equal(invoked, 1);
|
||||
await assert.rejects(() => getStarsQuoteFromRpc({ solanaEndpoint: 'https://test.shineup.me/solana-rpc', solana,
|
||||
connection: { getMultipleAccountsInfo: async () => [solPrice, null] }, nowSeconds: now }), /отсутствует/);
|
||||
});
|
||||
test('no server GetStarsQuote in active WS registry or UI', () => {
|
||||
const reg = readFileSync(new URL('../../SHiNE-server/shine-server-net-protocol/src/main/java/server/logic/ws_protocol/JSON/JsonHandlerRegistry.java', import.meta.url), 'utf8');
|
||||
const auth = readFileSync(new URL('../js/services/auth-service.js', import.meta.url), 'utf8');
|
||||
assert.doesNotMatch(reg, /GetStarsQuote/);
|
||||
assert.doesNotMatch(auth, /GetStarsQuote/);
|
||||
for (const p of ['../js/components/shine-support-dialog.js', '../js/pages/channel-donate-view.js']) {
|
||||
const s = readFileSync(new URL(p, import.meta.url), 'utf8');
|
||||
assert.match(s, /getStarsQuoteFromRpc/);
|
||||
assert.doesNotMatch(s, /authService\.getStarsQuote/);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user