Skip to content

Commit

Permalink
ci: print detailed idl differences
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Aug 29, 2024
1 parent 606a59a commit 8d1f3a9
Showing 1 changed file with 43 additions and 10 deletions.
53 changes: 43 additions & 10 deletions sdk/tests/ci/idl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ import { Connection, Keypair } from '@solana/web3.js';
import { Wallet, Program } from '@coral-xyz/anchor';
import dotenv from 'dotenv';
import { assert } from 'chai';
import driftIDL from '../../src/idl/drift.json';
import sdkIdl from '../../src/idl/drift.json';

dotenv.config();

const IDL_KEYS_TO_CHECK = [
'instructions',
'accounts',
'types',
'events',
'errors',
];

describe('Verify IDL', function () {
this.timeout(100_000);
const MAINNET_RPC_ENDPOINT = process.env.MAINNET_RPC_ENDPOINT;
Expand Down Expand Up @@ -37,23 +45,48 @@ describe('Verify IDL', function () {
});

it('verify idl', async () => {
const idl = await Program.fetchIdl(
const onChainIdl = await Program.fetchIdl(
mainnetDriftClient.program.programId,
mainnetDriftClient.provider
);

if (onChainIdl === null) {
throw new Error(`onChainIdl for ${mainnetDriftClient.program.programId.toBase58()} null`);
}

// anchor idl init seems to strip the metadata
idl['metadata'] = {
onChainIdl['metadata'] = {
address: 'dRiftyHA39MWEi3m9aunc5MzRF1JYuBsbn6VPcn33UH',
};
idl['version'] = null;

driftIDL['version'] = null;

const encodedMainnetIdl = JSON.stringify(idl);
onChainIdl['version'] = '';
sdkIdl['version'] = '';

const encodedSdkIdl = JSON.stringify(driftIDL);
const encodedMainnetIdl = JSON.stringify(onChainIdl);
const encodedSdkIdl = JSON.stringify(sdkIdl);

assert(encodedSdkIdl === encodedMainnetIdl);
try {
assert(encodedSdkIdl === encodedMainnetIdl, 'on-chain IDL does not match SDK IDL');
} catch (error) {
const diff = {};
for (const key of IDL_KEYS_TO_CHECK) {
const onChainItems = onChainIdl[key];
const sdkItems = sdkIdl[key];
for (let i = 0; i < Math.max(onChainItems.length, sdkItems.length); i++) {
let onChainItem = null;
let sdkItem = null;
if (i < onChainItems.length) {
onChainItem = onChainItems[i];
}
if (i < sdkItems.length) {
sdkItem = sdkItems[i];
}
if (JSON.stringify(onChainItem) !== JSON.stringify(sdkItem)) {
diff[`${key}[${i}]`] = { onChainIdl: onChainItem, sdkIdl: sdkItem };
}
}
}
console.error('IDL Difference:', JSON.stringify(diff, null, 2));
throw error;
}
});
});

0 comments on commit 8d1f3a9

Please sign in to comment.