mirror of
https://github.com/alexgo-io/stacks-pyth-bridge.git
synced 2026-04-30 20:52:14 +08:00
feat: init tests with clarinet sdk and vitest
This commit is contained in:
103
tests/helpers.ts
103
tests/helpers.ts
@@ -1,69 +1,56 @@
|
||||
import { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v1.6.0/index.ts';
|
||||
import { assertEquals } from 'https://deno.land/std@0.170.0/testing/asserts.ts';
|
||||
import { Clarinet, Tx, Chain, Account, types } from "https://deno.land/x/clarinet@v1.6.0/index.ts";
|
||||
import { assertEquals } from "https://deno.land/std@0.170.0/testing/asserts.ts";
|
||||
import { hexToBuffer } from "https://deno.land/x/hextools@v1.0.0/mod.ts";
|
||||
import { mainnet_valid_guardians_set_upgrades } from "./constants.ts";
|
||||
|
||||
export const executeGuardiansRotations = (chain: Chain, account: Account) => {
|
||||
const vaaRotation1 = hexToBuffer(mainnet_valid_guardians_set_upgrades[0].vaa);
|
||||
let publicKeysRotation1 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[0].keys) {
|
||||
publicKeysRotation1.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
const vaaRotation1 = hexToBuffer(mainnet_valid_guardians_set_upgrades[0].vaa);
|
||||
let publicKeysRotation1 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[0].keys) {
|
||||
publicKeysRotation1.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
|
||||
const vaaRotation2 = hexToBuffer(mainnet_valid_guardians_set_upgrades[1].vaa);
|
||||
let publicKeysRotation2 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[1].keys) {
|
||||
publicKeysRotation2.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
const vaaRotation2 = hexToBuffer(mainnet_valid_guardians_set_upgrades[1].vaa);
|
||||
let publicKeysRotation2 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[1].keys) {
|
||||
publicKeysRotation2.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
|
||||
const vaaRotation3 = hexToBuffer(mainnet_valid_guardians_set_upgrades[2].vaa);
|
||||
let publicKeysRotation3 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[2].keys) {
|
||||
publicKeysRotation3.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
const vaaRotation3 = hexToBuffer(mainnet_valid_guardians_set_upgrades[2].vaa);
|
||||
let publicKeysRotation3 = [];
|
||||
for (let key of mainnet_valid_guardians_set_upgrades[2].keys) {
|
||||
publicKeysRotation3.push(types.buff(hexToBuffer(key)));
|
||||
}
|
||||
|
||||
let block = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
"wormhole-core-v1",
|
||||
"update-guardians-set",
|
||||
[
|
||||
types.buff(vaaRotation1),
|
||||
types.list(
|
||||
publicKeysRotation1
|
||||
)
|
||||
],
|
||||
account.address),
|
||||
Tx.contractCall(
|
||||
"wormhole-core-v1",
|
||||
"update-guardians-set",
|
||||
[
|
||||
types.buff(vaaRotation2),
|
||||
types.list(
|
||||
publicKeysRotation2
|
||||
)
|
||||
],
|
||||
account.address),
|
||||
Tx.contractCall(
|
||||
"wormhole-core-v1",
|
||||
"update-guardians-set",
|
||||
[
|
||||
types.buff(vaaRotation3),
|
||||
types.list(
|
||||
publicKeysRotation3
|
||||
)
|
||||
],
|
||||
account.address),
|
||||
]);
|
||||
let block = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
"wormhole-core-dev-preview-1",
|
||||
"update-guardians-set",
|
||||
[types.buff(vaaRotation1), types.list(publicKeysRotation1)],
|
||||
account.address
|
||||
),
|
||||
Tx.contractCall(
|
||||
"wormhole-core-dev-preview-1",
|
||||
"update-guardians-set",
|
||||
[types.buff(vaaRotation2), types.list(publicKeysRotation2)],
|
||||
account.address
|
||||
),
|
||||
Tx.contractCall(
|
||||
"wormhole-core-dev-preview-1",
|
||||
"update-guardians-set",
|
||||
[types.buff(vaaRotation3), types.list(publicKeysRotation3)],
|
||||
account.address
|
||||
),
|
||||
]);
|
||||
|
||||
// assert: review returned data, contract state, and other requirements
|
||||
assertEquals(block.receipts.length, 3);
|
||||
const rotation1 = block.receipts[0].result;
|
||||
rotation1.expectOk()
|
||||
// assert: review returned data, contract state, and other requirements
|
||||
assertEquals(block.receipts.length, 3);
|
||||
const rotation1 = block.receipts[0].result;
|
||||
rotation1.expectOk();
|
||||
|
||||
const rotation2 = block.receipts[1].result;
|
||||
rotation2.expectOk()
|
||||
const rotation2 = block.receipts[1].result;
|
||||
rotation2.expectOk();
|
||||
|
||||
const rotation3 = block.receipts[2].result;
|
||||
rotation3.expectOk()
|
||||
const rotation3 = block.receipts[2].result;
|
||||
rotation3.expectOk();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,57 +1,61 @@
|
||||
|
||||
import { Clarinet, Tx, Chain, Account, types } from 'https://deno.land/x/clarinet@v1.6.0/index.ts';
|
||||
import { assertEquals, assertObjectMatch, assertArrayIncludes } from 'https://deno.land/std@0.170.0/testing/asserts.ts';
|
||||
import { Clarinet, Tx, Chain, Account, types } from "https://deno.land/x/clarinet@v1.6.0/index.ts";
|
||||
import {
|
||||
assertEquals,
|
||||
assertObjectMatch,
|
||||
assertArrayIncludes,
|
||||
} from "https://deno.land/std@0.170.0/testing/asserts.ts";
|
||||
import { hexToBuffer } from "https://deno.land/x/hextools@v1.0.0/mod.ts";
|
||||
|
||||
import { mainnet_valid_pfs } from "./constants.ts";
|
||||
import { executeGuardiansRotations } from "./helpers.ts";
|
||||
|
||||
Clarinet.test({
|
||||
name: "Ensure that valid price attestations can be ingested and recorded",
|
||||
fn(chain: Chain, accounts: Map<string, Account>) {
|
||||
// arrange: set up the chain, state, and other required elements
|
||||
const wallet_1 = accounts.get("wallet_1")!;
|
||||
executeGuardiansRotations(chain, wallet_1);
|
||||
name: "Ensure that valid price attestations can be ingested and recorded",
|
||||
fn(chain: Chain, accounts: Map<string, Account>) {
|
||||
// arrange: set up the chain, state, and other required elements
|
||||
const wallet_1 = accounts.get("wallet_1")!;
|
||||
executeGuardiansRotations(chain, wallet_1);
|
||||
|
||||
const vaaBytes = hexToBuffer(mainnet_valid_pfs[0]);
|
||||
const vaaBytes = hexToBuffer(mainnet_valid_pfs[0]);
|
||||
|
||||
const block = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
"pyth-oracle-v1",
|
||||
"update-prices-feeds",
|
||||
[
|
||||
types.list([types.buff(vaaBytes)])
|
||||
],
|
||||
wallet_1.address)
|
||||
]);
|
||||
const block = chain.mineBlock([
|
||||
Tx.contractCall(
|
||||
"pyth-oracle-dev-preview-1",
|
||||
"update-prices-feeds",
|
||||
[types.list([types.buff(vaaBytes)])],
|
||||
wallet_1.address
|
||||
),
|
||||
]);
|
||||
|
||||
assertEquals(block.receipts.length, 1);
|
||||
assertEquals(block.height, 3);
|
||||
const priceUpdate = block.receipts[0].result;
|
||||
// Ensure that the BTC_USD price feed id was the only updated feed
|
||||
assertArrayIncludes(priceUpdate.expectOk().expectList(),
|
||||
["0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"]
|
||||
);
|
||||
assertEquals(block.receipts.length, 1);
|
||||
assertEquals(block.height, 3);
|
||||
const priceUpdate = block.receipts[0].result;
|
||||
// Ensure that the BTC_USD price feed id was the only updated feed
|
||||
assertArrayIncludes(priceUpdate.expectOk().expectList(), [
|
||||
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43",
|
||||
]);
|
||||
|
||||
const feedIdBtcUsd = hexToBuffer("0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43");
|
||||
const oracleResult = chain.callReadOnlyFn(
|
||||
"pyth-oracle-v1",
|
||||
"read-price-feed",
|
||||
[types.buff(feedIdBtcUsd)],
|
||||
wallet_1.address
|
||||
);
|
||||
assertObjectMatch(oracleResult.result.expectOk().expectTuple(), {
|
||||
"attestation-time": "u1686854317",
|
||||
"conf": "u2064471426",
|
||||
"ema-conf": "u1891952230",
|
||||
"ema-price": "2507095440000",
|
||||
"expo": "4294967288",
|
||||
"prev-conf": "u2064471426",
|
||||
"prev-price": "2515455528574",
|
||||
"prev-publish-time": "u1686854316",
|
||||
"price": "2515455528574",
|
||||
"publish-time": "u1686854317",
|
||||
"status": "u1"
|
||||
});
|
||||
},
|
||||
const feedIdBtcUsd = hexToBuffer(
|
||||
"0xe62df6c8b4a85fe1a67db44dc12de5db330f7ac66b72dc658afedf0f4a415b43"
|
||||
);
|
||||
const oracleResult = chain.callReadOnlyFn(
|
||||
"pyth-oracle-dev-preview-1",
|
||||
"read-price-feed",
|
||||
[types.buff(feedIdBtcUsd)],
|
||||
wallet_1.address
|
||||
);
|
||||
assertObjectMatch(oracleResult.result.expectOk().expectTuple(), {
|
||||
"attestation-time": "u1686854317",
|
||||
conf: "u2064471426",
|
||||
"ema-conf": "u1891952230",
|
||||
"ema-price": "2507095440000",
|
||||
expo: "4294967288",
|
||||
"prev-conf": "u2064471426",
|
||||
"prev-price": "2515455528574",
|
||||
"prev-publish-time": "u1686854316",
|
||||
price: "2515455528574",
|
||||
"publish-time": "u1686854317",
|
||||
status: "u1",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user