Skip to content

Commit

Permalink
prettify
Browse files Browse the repository at this point in the history
  • Loading branch information
wphan committed Oct 30, 2023
1 parent 5846094 commit 051bf47
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 13 deletions.
1 change: 1 addition & 0 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,4 @@ Drift Protocol v2 is licensed under [Apache 2.0](./LICENSE).
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in Drift SDK by you, as defined in the Apache-2.0 license, shall be
licensed as above, without any additional terms or conditions.

47 changes: 47 additions & 0 deletions sdk/get_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import {
configs,
DriftClient,
Wallet,
} from "@drift-labs/sdk";


async function main() {

const driftConfig = configs['mainnet-beta'];
const connection = new Connection('https://api.mainnet-beta.solana.com');

const driftClient = new DriftClient({
connection: connection,
wallet: new Wallet(new Keypair()),
programID: new PublicKey(driftConfig.DRIFT_PROGRAM_ID),
userStats: true,
env: 'mainnet-beta',
});
console.log(`driftClientSubscribed: ${await driftClient.subscribe()}`);

const txHash = "3gvGQufckXGHrFDv4dNWEXuXKRMy3NZkKHMyFrAhLoYScaXXTGCp9vq58kWkfyJ8oDYZrz4bTyGayjUy9PKigeLS";

const tx = await driftClient.connection.getParsedTransaction(txHash, {
commitment: "confirmed",
maxSupportedTransactionVersion: 0,
});

let logIdx = 0;
// @ts-ignore
for (const event of driftClient.program._events._eventParser.parseLogs(tx!.meta!.logMessages)) {
console.log("----------------------------------------");
console.log(`Log ${logIdx++}`);
console.log("----------------------------------------");
console.log(`${JSON.stringify(event, null, 2)}`);
}

console.log("========================================");
console.log("Raw transaction logs");
console.log("========================================");
console.log(JSON.stringify(tx!.meta!.logMessages, null, 2));

process.exit(0);
}

main().catch(console.error);
1 change: 1 addition & 0 deletions sdk/tests/dlob/test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//
import { expect } from 'chai';
import { PublicKey, Keypair } from '@solana/web3.js';

Expand Down
23 changes: 10 additions & 13 deletions sdk/tests/user/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,9 @@ describe('User Tests', () => {
mockUser.getMaxLeverageForPerp(0).toString()
);
assert(mockUser.getMaxLeverageForPerp(0).eq(new BN('50000'))); // 5x
assert(mockUser.getMaxLeverageForPerp(0, 'Maintenance').eq(new BN('100000'))); // 10x

assert(
mockUser.getMaxLeverageForPerp(0, 'Maintenance').eq(new BN('100000'))
); // 10x
});

it('worst case token amount', async () => {
Expand Down Expand Up @@ -399,8 +400,7 @@ describe('User Tests', () => {
); // -$2k
});


it('custom margin ratio (sol spot)', async() => {
it('custom margin ratio (sol spot)', async () => {
const myMockUserAccount = _.cloneDeep(mockUserAccount);

const solMarket = Object.assign({}, _.cloneDeep(mockSpotMarkets[1]), {
Expand All @@ -427,12 +427,10 @@ describe('User Tests', () => {
);

console.log(worstCase);
assert(worstCase.weight.eq(new BN(8000)));

assert(worstCase.weight.eq(new BN(8000)));

myMockUserAccount.maxMarginRatio = MARGIN_PRECISION.toNumber(); // max 1x pls


const worstCaseAfter = getWorstCaseTokenAmounts(
spotPosition,
solMarket,
Expand All @@ -443,10 +441,9 @@ describe('User Tests', () => {

console.log(worstCaseAfter);
assert(worstCaseAfter.weight.eq(new BN(0))); // not allowed to increase exposure

});

it('custom margin ratio (sol perp)', async() => {
it('custom margin ratio (sol perp)', async () => {
const myMockPerpMarkets = _.cloneDeep(mockPerpMarkets);
const myMockSpotMarkets = _.cloneDeep(mockSpotMarkets);
const myMockUserAccount = _.cloneDeep(mockUserAccount);
Expand All @@ -469,7 +466,6 @@ describe('User Tests', () => {
[1, 1, 1, 1, 1, 1, 1, 1]
);


assert(mockUser.getTokenAmount(0).eq(new BN('10000000000')));
assert(mockUser.getNetSpotMarketValue().eq(new BN('10000000000')));
assert(
Expand All @@ -486,7 +482,9 @@ describe('User Tests', () => {
assert(iLev == 5000);
assert(mLev == 10000);

myMockUserAccount.maxMarginRatio = MARGIN_PRECISION.div(new BN(2)).toNumber(); // 2x max pls
myMockUserAccount.maxMarginRatio = MARGIN_PRECISION.div(
new BN(2)
).toNumber(); // 2x max pls

const mockUser2: User = await makeMockUser(
myMockPerpMarkets,
Expand All @@ -498,9 +496,8 @@ describe('User Tests', () => {
iLev = mockUser2.getMaxLeverageForPerp(0, 'Initial').toNumber();
mLev = mockUser2.getMaxLeverageForPerp(0, 'Maintenance').toNumber();
console.log(iLev, mLev);

assert(iLev == 2000);
assert(mLev == 10000);

});
});

0 comments on commit 051bf47

Please sign in to comment.