Skip to content

Commit

Permalink
Moved account and sdk to providers.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
ashkuc committed Oct 16, 2022
1 parent c642078 commit 903a6ed
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import { TelegramService } from './telegram/service';
import { GlobalConfigModule } from './config/config.module';
import { CacheModule } from './cache.module';
import { SdkService } from './sdk/service';
import { accountProvider, sdkProvider } from './sdk/providers';

@Module({
imports: [GlobalConfigModule, CacheModule],
controllers: [],
providers: [SdkService, TelegramService],
providers: [accountProvider, sdkProvider, SdkService, TelegramService],
})
export class AppModule {}
38 changes: 38 additions & 0 deletions src/sdk/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Inject, Provider } from '@nestjs/common';
import { IClient, Sdk } from '@unique-nft/sdk';
import { KeyringProvider } from '@unique-nft/accounts/keyring';
import { Account, SignatureType } from '@unique-nft/accounts';
import { ConfigService } from '@nestjs/config';

export const InjectAccount = Inject('Account');
export const InjectSdk = Inject('Sdk');

export const accountProvider: Provider = {
provide: 'Account',
inject: [ConfigService],
useFactory: async (configService: ConfigService) => {
const keyringProvider = new KeyringProvider({
type: SignatureType.Sr25519,
});

await keyringProvider.init();

return keyringProvider.addSeed(configService.get<string>('seed'));
},
};

export const sdkProvider: Provider = {
provide: 'Sdk',
inject: ['Account', ConfigService],
useFactory: async (
account: Account,
configService: ConfigService,
): Promise<IClient> => {
const restUrl = configService.get<string>('restUrl');

return new Sdk({
signer: account,
baseUrl: restUrl,
});
},
};
40 changes: 7 additions & 33 deletions src/sdk/service.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,19 @@
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Account } from '@unique-nft/accounts';
import { KeyringProvider } from '@unique-nft/accounts/keyring';
import { SignatureType } from '@unique-nft/accounts';
import { IClient, Sdk } from '@unique-nft/sdk';
import { KeyringPair } from '@polkadot/keyring/types';
import { IClient } from '@unique-nft/sdk';
import { InjectAccount, InjectSdk } from './providers';

@Injectable()
export class SdkService {
private sdk: IClient;
private account: Account<KeyringPair>;

private readonly logger = new Logger(SdkService.name);

constructor(private readonly configService: ConfigService) {}

private async createSdk() {
const keyringProvider = new KeyringProvider({
type: SignatureType.Sr25519,
});

await keyringProvider.init();

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.account = keyringProvider.addSeed(
this.configService.get<string>('seed'),
);

const restUrl = this.configService.get<string>('restUrl');

this.sdk = new Sdk({
signer: this.account,
baseUrl: restUrl,
});
}
constructor(
@InjectSdk private readonly sdk: IClient,
@InjectAccount private readonly account,
private readonly configService: ConfigService,
) {}

public async sendTo(destination: string) {
if (!this.sdk) {
await this.createSdk();
}
const amount = this.configService.get('dropAmount');
const address = this.account.instance.address;

Expand Down

0 comments on commit 903a6ed

Please sign in to comment.