apiMethods.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { ApiPromise, WsProvider } from '@polkadot/api';
  2. import { Option } from '@polkadot/types';
  3. import { KeyringPair } from '@polkadot/keyring/types';
  4. import { UserInfo, PaidMembershipTerms } from '@joystream/types/lib/members';
  5. import { Balance, Index } from '@polkadot/types/interfaces';
  6. import BN = require('bn.js');
  7. import { SubmittableExtrinsic } from '@polkadot/api/types';
  8. import { Sender } from './sender';
  9. import { Utils } from './utils';
  10. export class ApiMethods {
  11. private readonly api: ApiPromise;
  12. private readonly sender: Sender;
  13. public static async create(provider: WsProvider): Promise<ApiMethods> {
  14. const api = await ApiPromise.create({ provider });
  15. return new ApiMethods(api);
  16. }
  17. constructor(api: ApiPromise) {
  18. this.api = api;
  19. this.sender = new Sender(api);
  20. }
  21. public close() {
  22. this.api.disconnect();
  23. }
  24. public async buyMembership(
  25. account: KeyringPair,
  26. paidTermsId: number,
  27. name: string,
  28. expectFailure = false
  29. ): Promise<void> {
  30. return Utils.signAndSend(
  31. this.api.tx.members.buyMembership(paidTermsId, new UserInfo({ handle: name, avatar_uri: '', about: '' })),
  32. account,
  33. await this.getNonce(account),
  34. expectFailure
  35. );
  36. }
  37. public getMembership(address: string): Promise<any> {
  38. return this.api.query.members.memberIdsByControllerAccountId(address);
  39. }
  40. public getBalance(address: string): Promise<Balance> {
  41. return this.api.query.balances.freeBalance<Balance>(address);
  42. }
  43. public async transferBalance(from: KeyringPair, to: string, amount: BN, nonce: BN = new BN(-1)): Promise<void> {
  44. const _nonce = nonce.isNeg() ? await this.getNonce(from) : nonce;
  45. return Utils.signAndSend(this.api.tx.balances.transfer(to, amount), from, _nonce);
  46. }
  47. public getPaidMembershipTerms(paidTermsId: number): Promise<Option<PaidMembershipTerms>> {
  48. return this.api.query.members.paidMembershipTermsById<Option<PaidMembershipTerms>>(paidTermsId);
  49. }
  50. public getMembershipFee(paidTermsId: number): Promise<BN> {
  51. return this.getPaidMembershipTerms(paidTermsId).then(terms => terms.unwrap().fee.toBn());
  52. }
  53. public async transferBalanceToAccounts(from: KeyringPair, to: KeyringPair[], amount: BN, nonce: BN): Promise<void[]> {
  54. return Promise.all(
  55. to.map(async keyPair => {
  56. nonce = nonce.add(new BN(1));
  57. await this.transferBalance(from, keyPair.address, amount, nonce);
  58. })
  59. );
  60. }
  61. public getNonce(account: KeyringPair): Promise<BN> {
  62. return this.api.query.system.accountNonce<Index>(account.address);
  63. }
  64. private getBaseTxFee(): BN {
  65. return this.api.createType('BalanceOf', this.api.consts.transactionPayment.transactionBaseFee).toBn();
  66. }
  67. private estimateTxFee(tx: SubmittableExtrinsic<'promise'>): BN {
  68. const baseFee: BN = this.getBaseTxFee();
  69. const byteFee: BN = this.api.createType('BalanceOf', this.api.consts.transactionPayment.transactionByteFee).toBn();
  70. return Utils.calcTxLength(tx).mul(byteFee).add(baseFee);
  71. }
  72. public estimateBuyMembershipFee(account: KeyringPair, paidTermsId: number, name: string): BN {
  73. const nonce: BN = new BN(0);
  74. return this.estimateTxFee(
  75. this.api.tx.members.buyMembership(paidTermsId, new UserInfo({ handle: name, avatar_uri: '', about: '' }))
  76. );
  77. }
  78. }