|
@@ -1,21 +1,25 @@
|
|
|
import { ApiPromise, WsProvider } from '@polkadot/api';
|
|
|
import { Option } from '@polkadot/types';
|
|
|
import { KeyringPair } from '@polkadot/keyring/types';
|
|
|
-import { Utils } from './utils';
|
|
|
import { UserInfo, PaidMembershipTerms } from '@joystream/types/lib/members';
|
|
|
-import { Balance } from '@polkadot/types/interfaces';
|
|
|
+import { Balance, Index } from '@polkadot/types/interfaces';
|
|
|
import BN = require('bn.js');
|
|
|
import { SubmittableExtrinsic } from '@polkadot/api/types';
|
|
|
+import { Sender } from './sender';
|
|
|
+import { Utils } from './utils';
|
|
|
|
|
|
export class ApiMethods {
|
|
|
+ private readonly api: ApiPromise;
|
|
|
+ private readonly sender: Sender;
|
|
|
+
|
|
|
public static async create(provider: WsProvider): Promise<ApiMethods> {
|
|
|
const api = await ApiPromise.create({ provider });
|
|
|
return new ApiMethods(api);
|
|
|
}
|
|
|
|
|
|
- private readonly api: ApiPromise;
|
|
|
constructor(api: ApiPromise) {
|
|
|
this.api = api;
|
|
|
+ this.sender = new Sender(api);
|
|
|
}
|
|
|
|
|
|
public close() {
|
|
@@ -44,7 +48,7 @@ export class ApiMethods {
|
|
|
return this.api.query.balances.freeBalance<Balance>(address);
|
|
|
}
|
|
|
|
|
|
- public async transferBalance(from: KeyringPair, to: string, amount: number, nonce: BN = new BN(-1)): Promise<void> {
|
|
|
+ public async transferBalance(from: KeyringPair, to: string, amount: BN, nonce: BN = new BN(-1)): Promise<void> {
|
|
|
const _nonce = nonce.isNeg() ? await this.getNonce(from) : nonce;
|
|
|
return Utils.signAndSend(this.api.tx.balances.transfer(to, amount), from, _nonce);
|
|
|
}
|
|
@@ -53,16 +57,11 @@ export class ApiMethods {
|
|
|
return this.api.query.members.paidMembershipTermsById<Option<PaidMembershipTerms>>(paidTermsId);
|
|
|
}
|
|
|
|
|
|
- public getMembershipFee(paidTermsId: number): Promise<number> {
|
|
|
- return this.getPaidMembershipTerms(paidTermsId).then(terms => terms.unwrap().fee.toNumber());
|
|
|
+ public getMembershipFee(paidTermsId: number): Promise<BN> {
|
|
|
+ return this.getPaidMembershipTerms(paidTermsId).then(terms => terms.unwrap().fee.toBn());
|
|
|
}
|
|
|
|
|
|
- public async transferBalanceToAccounts(
|
|
|
- from: KeyringPair,
|
|
|
- to: KeyringPair[],
|
|
|
- amount: number,
|
|
|
- nonce: BN
|
|
|
- ): Promise<void[]> {
|
|
|
+ public async transferBalanceToAccounts(from: KeyringPair, to: KeyringPair[], amount: BN, nonce: BN): Promise<void[]> {
|
|
|
return Promise.all(
|
|
|
to.map(async keyPair => {
|
|
|
nonce = nonce.add(new BN(1));
|
|
@@ -72,26 +71,23 @@ export class ApiMethods {
|
|
|
}
|
|
|
|
|
|
public getNonce(account: KeyringPair): Promise<BN> {
|
|
|
- return this.api.query.system.accountNonce(account.address).then(nonce => new BN(nonce.toString()));
|
|
|
+ return this.api.query.system.accountNonce<Index>(account.address);
|
|
|
}
|
|
|
|
|
|
- private getBaseTxFee(): number {
|
|
|
- return this.api.createType('BalanceOf', this.api.consts.transactionPayment.transactionBaseFee).toNumber();
|
|
|
+ private getBaseTxFee(): BN {
|
|
|
+ return this.api.createType('BalanceOf', this.api.consts.transactionPayment.transactionBaseFee).toBn();
|
|
|
}
|
|
|
|
|
|
- private estimateTxFee(tx: SubmittableExtrinsic<'promise'>): number {
|
|
|
- const baseFee: number = this.getBaseTxFee();
|
|
|
- const byteFee: number = this.api
|
|
|
- .createType('BalanceOf', this.api.consts.transactionPayment.transactionByteFee)
|
|
|
- .toNumber();
|
|
|
- return tx.toHex().length * byteFee + baseFee;
|
|
|
+ private estimateTxFee(tx: SubmittableExtrinsic<'promise'>): BN {
|
|
|
+ const baseFee: BN = this.getBaseTxFee();
|
|
|
+ const byteFee: BN = this.api.createType('BalanceOf', this.api.consts.transactionPayment.transactionByteFee).toBn();
|
|
|
+ return Utils.calcTxLength(tx).mul(byteFee).add(baseFee);
|
|
|
}
|
|
|
|
|
|
- public estimateBuyMembershipFee(account: KeyringPair, paidTermsId: number, name: string): number {
|
|
|
+ public estimateBuyMembershipFee(account: KeyringPair, paidTermsId: number, name: string): BN {
|
|
|
const nonce: BN = new BN(0);
|
|
|
- const signedTx = this.api.tx.members
|
|
|
- .buyMembership(paidTermsId, new UserInfo({ handle: name, avatar_uri: '', about: '' }))
|
|
|
- .sign(account, { nonce });
|
|
|
- return this.estimateTxFee(signedTx);
|
|
|
+ return this.estimateTxFee(
|
|
|
+ this.api.tx.members.buyMembership(paidTermsId, new UserInfo({ handle: name, avatar_uri: '', about: '' }))
|
|
|
+ );
|
|
|
}
|
|
|
}
|