Skip to content

Commit

Permalink
Fetch owner form mergemine
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Tsymbal authored and tsmbl committed May 25, 2022
1 parent e8b7251 commit e93cbb6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
15 changes: 10 additions & 5 deletions examples/quarry-events-example.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
import { ClaimEvent, StakeEvent } from '@quarryprotocol/quarry-sdk';
import { getTokenInfo } from '../src/saber-wars-api/token-info-api';
import { QuarryEventSubscription } from '../src/saber-wars-api/quarry-event-api';
import { quarrySDK } from '../src/saber-wars-api/quarry-sdk-factory';
import { getOwner, quarrySDK } from '../src/saber-wars-api/quarry-sdk-factory';
import { getTokenInfo } from '../src/saber-wars-api/token-info-api';
import { ClaimEvent, StakeEvent } from '@quarryprotocol/quarry-sdk';

async function main() {
const defaultSubscription = new QuarryEventSubscription(
quarrySDK.programs.Mine,
async (it) => {
const owner = await getOwner(it.data.authority);
switch (it.name) {
case 'StakeEvent': {
const stakeEvent = it as StakeEvent;
const optionalParams = await getTokenInfo(
stakeEvent.data.token.toBase58(),
);
console.log(stakeEvent, optionalParams);
console.log(
`Stake event from ${owner.toBase58()}, ${stakeEvent.data.amount.toNumber()}`,
);
break;
}
case 'ClaimEvent': {
const claimEvent = it as ClaimEvent;
const optionalParams = await getTokenInfo(
claimEvent.data.rewardsToken.toBase58(),
);
console.log(claimEvent, optionalParams);
console.log(
`Claim event from ${owner.toBase58()}, ${claimEvent.data.amount.toNumber()}`,
);
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@nestjs/schedule": "^1.0.2",
"@project-serum/anchor": "0.23.0",
"@quarryprotocol/gauge": "^0.2.2",
"@quarryprotocol/quarry-sdk": "^2.0.1",
"@quarryprotocol/quarry-sdk": "^5.0.2",
"@saberhq/anchor-contrib": "^1.12.61",
"@saberhq/solana-contrib": "^1.12.61",
"@saberhq/token-utils": "^1.12.61",
Expand Down
4 changes: 2 additions & 2 deletions src/monitor/saber-monitoring-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Cron } from '@nestjs/schedule';
import { DialectConnection } from './dialect-connection';
import { Subject } from 'rxjs';
import { QuarryEventSubscription } from '../saber-wars-api/quarry-event-api';
import { quarrySDK } from '../saber-wars-api/quarry-sdk-factory';
import { getOwner, quarrySDK } from '../saber-wars-api/quarry-sdk-factory';
import { getTokenInfo } from '../saber-wars-api/token-info-api';
import { TwitterNotificationSink } from './twitter-notification-sink';
import { OnChainSubscriberRepository } from '@dialectlabs/monitor/lib/cjs/internal/on-chain-subscriber.repository';
Expand Down Expand Up @@ -108,7 +108,7 @@ Time remaining in epoch: ${epochInfo.currentEpochRemainingTime.toFormat(
}
const resourceId = process.env.TEST_MODE
? (await subscriberRepository.findAll())[0]
: evt.data.authority;
: await getOwner(evt.data.authority);
if (evt.name === 'StakeEvent') {
const tokenInfo = await getTokenInfo(evt.data.token);
quarryEvents.next({
Expand Down
21 changes: 20 additions & 1 deletion src/saber-wars-api/quarry-sdk-factory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GaugeSDK } from '@quarryprotocol/gauge';
import { Connection, Keypair } from '@solana/web3.js';
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
import { Provider } from '@project-serum/anchor';
import NodeWallet from '@project-serum/anchor/dist/cjs/nodewallet';
import { makeSaberProvider } from '@saberhq/anchor-contrib';
Expand All @@ -23,3 +23,22 @@ export const gaugeSdk = GaugeSDK.load({
export const quarrySDK = QuarrySDK.load({
provider,
});

const mmPkToOwnerPk: Map<string, PublicKey> = new Map<string, PublicKey>();

export async function getOwner(authority: PublicKey): Promise<PublicKey> {
const cacheKey = authority.toBase58();
if (!mmPkToOwnerPk.has(cacheKey)) {
try {
const mm = await quarrySDK.mergeMine.fetchMergeMinerData(
new PublicKey(authority),
);
mmPkToOwnerPk.set(cacheKey, mm.data.owner);
} catch (e) {
mmPkToOwnerPk.set(cacheKey, authority);
// console.error(e);
}
}
const ownerPkBase58 = mmPkToOwnerPk.get(cacheKey)!;
return ownerPkBase58 && new PublicKey(ownerPkBase58);
}

0 comments on commit e93cbb6

Please sign in to comment.