Skip to content

Commit

Permalink
sdk: add getEntriesForDonation (#33)
Browse files Browse the repository at this point in the history
* sdk: add getEntriesForDonation

* fix typo

* add script

* tweaks
  • Loading branch information
crispheaney authored Dec 10, 2023
1 parent 9402d23 commit d74cd96
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"update-types": "cp target/types/drift_competitions.ts ts/sdk/src/types/drift_competitions.ts && prettier --write ts/sdk/src/types/drift_competitions.ts",
"update-idl": "cp target/idl/drift_competitions.json ts/sdk/src/idl/drift_competitions.json",
"settle-competition": "ts-node -T scripts/settleCompetitionRound.ts",
"mine-entries": "ts-node -T scripts/mineAdditionalEntries.ts"
"mine-entries": "ts-node -T scripts/mineAdditionalEntries.ts",
"donate": "ts-node -T scripts/donateForEntries.ts"
},
"dependencies": {
"@coral-xyz/anchor": "^0.26.0",
Expand Down
110 changes: 110 additions & 0 deletions scripts/donateForEntries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import * as anchor from '@coral-xyz/anchor';
import { Program } from '@coral-xyz/anchor';
import { DriftCompetitions } from '../ts/sdk/src/types/drift_competitions';
import { CompetitionsClient } from '../ts/sdk/src';
import { DriftClient, PublicKey, isVariant } from '@drift-labs/sdk';
import dotenv from 'dotenv';
import { getCompetitorAddressSync } from '../ts/sdk/src/addresses';
import { program, Option } from 'commander';

dotenv.config();

function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
const ENV = 'mainnet-beta';

const RPC_ENDPOINT =
process.env.RPC_OVERRIDE ?? 'https://api.' + ENV + '.solana.com';

async function donateForEntries(provider, authority: PublicKey, t: number, m: number) {
// Configure client to use the provider.
anchor.setProvider(provider);

const payer = (provider.wallet as anchor.Wallet).payer;
console.log(`Payer: ${payer.publicKey}`);
console.log(`Recipient: ${authority}`);

console.log('m:', m, 't:', t);

const marketIndex = Number(m);
const tokenAmountNoPrecision = Number(t);

const driftClient = new DriftClient({
connection: provider.connection,
wallet: provider.wallet,
authority: authority,
spotMarketIndexes: [marketIndex],
});

await driftClient.subscribe();

const competitionClient = new CompetitionsClient({
driftClient,
});

const program = competitionClient.program as Program<DriftCompetitions>;

const name = 'sweepstakes';
const competitionKey = competitionClient.getCompetitionPublicKey(name);

const competitorKey = getCompetitorAddressSync(
competitionClient.program.programId,
competitionKey,
authority
);

const spotMarket = competitionClient.driftClient.getSpotMarketAccount(marketIndex);

const tokenAmount = new anchor.BN(tokenAmountNoPrecision * 10 ** 6);

const entries = competitionClient.getEntriesForDonation(tokenAmount, spotMarket);

console.log('entries:', entries.toNumber());

const tokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex);

const txSig = await competitionClient.claimMultipleEntries(entries, tokenAccount, competitionKey, competitorKey);

console.log(txSig);

console.log('DONE!');
}

if (!process.env.ANCHOR_WALLET) {
throw new Error('ANCHOR_WALLET must be set.');
}
require('dotenv').config();
program
.option(
'--authority <string>',
'authority of competition account for mining entries'
)
.option('-t, --number-of-tokens <number>', 'number of tokens to donate')
.option('-m, --market-index <number>', 'market index to donate for')
.parse();
const opts = program.opts();

if (!opts.authority) {
throw new Error('authority not set');
}

if (!opts.numberOfTokens) {
throw new Error('number of tokens not set');
}

if (!opts.marketIndex) {
throw new Error('market index not set');
}

console.log('RPC:', RPC_ENDPOINT);
donateForEntries(
anchor.AnchorProvider.local(RPC_ENDPOINT, {
preflightCommitment: 'confirmed',
skipPreflight: true,
commitment: 'confirmed',
}),
new PublicKey(opts.authority),
opts.numberOfTokens,
opts.marketIndex
);
8 changes: 7 additions & 1 deletion ts/sdk/src/competitionClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
QUOTE_PRECISION,
PERCENTAGE_PRECISION,
fetchLogs,
getSpotMarketVaultPublicKey,
getSpotMarketVaultPublicKey, SpotMarketAccount, PRICE_PRECISION,
} from '@drift-labs/sdk';
import { Program } from '@coral-xyz/anchor';
import { DriftCompetitions, IDL } from './types/drift_competitions';
Expand Down Expand Up @@ -742,4 +742,10 @@ export class CompetitionsClient {

return events;
}

public getEntriesForDonation(tokenAmount: BN, spotMarket: SpotMarketAccount) {
const strictPrice = BN.min(spotMarket.historicalOracleData.lastOraclePriceTwap5Min, spotMarket.historicalOracleData.lastOraclePrice);

return tokenAmount.mul(strictPrice).muln(20000).div(PRICE_PRECISION).divn(10 ** spotMarket.decimals);
}
}
5 changes: 4 additions & 1 deletion ts/sdk/src/idl/drift_competitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -1294,5 +1294,8 @@
"name": "CompetitorHasPendingInsuranceWithdraw",
"msg": "CompetitorHasPendingInsuranceWithdraw"
}
]
],
"metadata": {
"address": "DraWMeQX9LfzQQSYoeBwHAgM5JcqFkgrX7GbTfjzVMVL"
}
}

0 comments on commit d74cd96

Please sign in to comment.