-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add farm confirmation notifications (#4)
* Update monitor dependency * Split apis * Add dialect connection * Add quarry events listener implementation * Implement monitor draft * Implement farm monitor * Fetch owner form mergemine * Adjust notification format * Fix to staked token. Co-authored-by: Alexey Tsymbal <[email protected]> Co-authored-by: cbosborn <[email protected]>
- Loading branch information
1 parent
6e7cf80
commit e6cbb52
Showing
18 changed files
with
702 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { QuarryEventSubscription } from '../src/saber-wars-api/quarry-event-api'; | ||
import { getOwner, quarrySDK } from '../src/saber-wars-api/quarry-sdk-factory'; | ||
import { getTokenInfo } from '../src/saber-wars-api/token-info-api'; | ||
import { toDecimals } from '../src/saber-wars-api/saber-wars-api'; | ||
|
||
const numberFormat = new Intl.NumberFormat('en-US'); | ||
|
||
async function main() { | ||
const defaultSubscription = new QuarryEventSubscription( | ||
quarrySDK.programs.Mine, | ||
async (evt) => { | ||
const resourceId = await getOwner(evt.data.authority); | ||
if ( | ||
resourceId.toBase58() !== 'GNisgcTZZ2WS5PFAEkVUbFso3wNe22cjhmZiEjGcqeHD' | ||
) { | ||
return; | ||
} | ||
if (evt.name === 'StakeEvent') { | ||
console.log(JSON.stringify(evt)); | ||
const tokenInfo = await getTokenInfo(evt.data.token); | ||
console.log(JSON.stringify(tokenInfo)); | ||
console.log( | ||
`Success! You staked ${numberFormat.format( | ||
toDecimals(evt.data.amount, tokenInfo.decimals), | ||
)} ${tokenInfo.symbol} to ${tokenInfo.name}`, | ||
); | ||
} | ||
if (evt.name === 'ClaimEvent') { | ||
const tokenInfo = await getTokenInfo(evt.data.stakedToken); | ||
console.log( | ||
`Success! You claimed ${numberFormat.format( | ||
toDecimals(evt.data.amount, tokenInfo.decimals), | ||
)} ${tokenInfo.symbol} from ${tokenInfo.name}`, | ||
); | ||
} | ||
return Promise.resolve(); | ||
}, | ||
); | ||
|
||
await defaultSubscription.start(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SaberMonitoringService } from './monitor/saber-monitoring-service'; | ||
import { ScheduleModule } from '@nestjs/schedule'; | ||
import { DialectConnection } from './monitor/dialect-connection'; | ||
|
||
@Module({ | ||
imports: [ScheduleModule.forRoot()], | ||
controllers: [], | ||
providers: [SaberMonitoringService], | ||
providers: [ | ||
{ | ||
provide: DialectConnection, | ||
useValue: DialectConnection.initialize(), | ||
}, | ||
SaberMonitoringService, | ||
], | ||
}) | ||
export class AppModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { | ||
Notification, | ||
NotificationSink, | ||
ResourceId, | ||
} from '@dialectlabs/monitor'; | ||
|
||
export class ConsoleNotificationSink<N extends Notification> | ||
implements NotificationSink<N> | ||
{ | ||
push(notification: N, recipients: ResourceId[]): Promise<void> { | ||
console.log( | ||
`Got new notification ${JSON.stringify( | ||
notification, | ||
)} for recipients ${recipients}`, | ||
); | ||
return Promise.resolve(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Connection, Keypair, PublicKey } from '@solana/web3.js'; | ||
import { Idl, Program, Provider } from '@project-serum/anchor'; | ||
import { idl, programs, Wallet_ } from '@dialectlabs/web3'; | ||
|
||
export abstract class DialectConnection { | ||
static initialize(): DialectConnection { | ||
const PRIVATE_KEY = process.env.PRIVATE_KEY; | ||
const keypair: Keypair = Keypair.fromSecretKey( | ||
new Uint8Array(JSON.parse(PRIVATE_KEY as string)), | ||
); | ||
const wallet = Wallet_.embedded(keypair.secretKey); | ||
const RPC_URL = process.env.RPC_URL || 'http://localhost:8899'; | ||
console.log('RPC url', RPC_URL); | ||
const dialectConnection = new Connection(RPC_URL, { | ||
commitment: 'recent', | ||
}); | ||
const dialectProvider = new Provider( | ||
dialectConnection, | ||
wallet, | ||
Provider.defaultOptions(), | ||
); | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
const NETWORK_NAME: 'devnet' | 'localnet' = | ||
process.env.NETWORK_NAME ?? 'localnet'; | ||
console.log('Network name', NETWORK_NAME); | ||
// const DIALECT_PROGRAM_ADDRESS = new PublicKey( | ||
// 'BTHDR8UjttR3mX3PwT8MuEKDDzDYwqENYgPHH7QjaJ3y', | ||
// ); | ||
const DIALECT_PROGRAM_ADDRESS = programs[NETWORK_NAME].programAddress; | ||
const program = new Program( | ||
idl as Idl, | ||
new PublicKey(DIALECT_PROGRAM_ADDRESS), | ||
dialectProvider, | ||
); | ||
return new DialectConnectionImpl(keypair, program); | ||
} | ||
|
||
abstract getKeypair(): Keypair; | ||
|
||
abstract getProgram(): Program; | ||
} | ||
|
||
export class DialectConnectionImpl { | ||
constructor( | ||
private readonly keypair: Keypair, | ||
private readonly program: Program, | ||
) {} | ||
|
||
getKeypair(): Keypair { | ||
return this.keypair; | ||
} | ||
|
||
getProgram(): Program { | ||
return this.program; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.