Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
qwer951123 committed Jul 25, 2023
1 parent 5f76795 commit c28950e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
"typescript": "^5.0.4"
},
"packageManager": "yarn@3.5.0",
"stableVersion": "4.1.9-1"
"stableVersion": "4.1.9-2"
}
41 changes: 22 additions & 19 deletions packages/sdk-payment/src/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class Payment {
readonly wallet: Wallet;
readonly dex: AggregateDex;
readonly api: AnyApi;
public isReady: boolean = false;
public isReady = false;
private paymentMethods: PaymentMethod[] = [];
private storages: Storages;

Expand All @@ -21,7 +21,7 @@ export class Payment {
this.storages = createStorages(this.api);
}

public async ready () {
public async ready() {
await this.wallet.isReady;
await this.dex.isReady;

Expand All @@ -30,7 +30,7 @@ export class Payment {
return true;
}

private get defaultFeeToken (): Token[] {
private get defaultFeeToken(): Token[] {
const defaultFeeTokens = this.api.consts.transactionPayment.defaultFeeTokens;

return defaultFeeTokens.map((i) => this.wallet.getToken(i));
Expand Down Expand Up @@ -100,18 +100,23 @@ export class Payment {
return paymentMethods;
}

private getSurplusByType (type: PaymentMethodTypes) {
switch(type) {
case PaymentMethodTypes.NATIVE_TOKEN: return 1;
case PaymentMethodTypes.DEFAULT_FEE_TOKEN: return 1.2;
case PaymentMethodTypes.GLOBAL_SWAP_PATH: return 1.5;
case PaymentMethodTypes.SWAP: return 1.5;
default: return 1;
private getSurplusByType(type: PaymentMethodTypes) {
switch (type) {
case PaymentMethodTypes.NATIVE_TOKEN:
return 1;
case PaymentMethodTypes.DEFAULT_FEE_TOKEN:
return 1.2;
case PaymentMethodTypes.GLOBAL_SWAP_PATH:
return 1.5;
case PaymentMethodTypes.SWAP:
return 1.5;
default:
return 1;
}
}

@ensureReady
public createWrappedTx (tx: Tx, paymentMethod: PaymentMethod) {
public createWrappedTx(tx: Tx, paymentMethod: PaymentMethod) {
const { api } = this;
const { path, type } = paymentMethod;
const surplus = this.getSurplusByType(type);
Expand All @@ -129,32 +134,30 @@ export class Payment {
);
break;
case PaymentMethodTypes.GLOBAL_SWAP_PATH:
wrapped = api.tx.transactionPayment.withFeeCurrency(
path[0].toChainData(),
tx.method.toHex()
);
wrapped = api.tx.transactionPayment.withFeeCurrency(path[0].toChainData(), tx.method.toHex());
break;
case PaymentMethodTypes.SWAP:
wrapped = api.tx.transactionPayment.withFeePath(
path.map((i) => i.toChainData()),
tx.method.toHex(),
tx.method.toHex()
);
break;
default: throw new Error('unknown payment method');
default:
throw new Error('unknown payment method');
}

return { tx: wrapped, surplus };
}

@ensureReady
public async estimateTxFee (tx: Tx, account: string, surplus: number) {
public async estimateTxFee(tx: Tx, account: string, surplus: number) {
const { partialFee } = await toPromise(tx.paymentInfo(account));

return partialFee.muln(surplus);
}

@ensureReady
public async estimateTxFeeByCallName (account: string, section: string, method: string) {
public async estimateTxFeeByCallName(account: string, section: string, method: string) {
const { api } = this;

const call = api.tx[section][method];
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-payment/src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class NoTxFound extends Error {
}

export class NotReady extends Error {
constructor () {
constructor() {
super('SDK is not ready');
}
}
}
10 changes: 5 additions & 5 deletions packages/sdk-payment/src/payment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wallet } from "@acala-network/sdk";
import { AcalaDex, AggregateDex } from "@acala-network/sdk-swap";
import { ApiPromise, WsProvider } from "@polkadot/api";
import { Payment } from "./Payment";
import { Wallet } from '@acala-network/sdk';
import { AcalaDex, AggregateDex } from '@acala-network/sdk-swap';
import { ApiPromise, WsProvider } from '@polkadot/api';
import { Payment } from './Payment';

describe('payment', () => {
jest.setTimeout(50000);
Expand All @@ -25,4 +25,4 @@ describe('payment', () => {

await payment.isReady;
});
})
});
18 changes: 10 additions & 8 deletions packages/sdk-payment/src/storage.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { AnyApi } from "@acala-network/sdk-core";
import { Storage } from "@acala-network/sdk/utils/storage";
import { AcalaPrimitivesCurrencyCurrencyId } from "@polkadot/types/lookup";
import { AnyApi } from '@acala-network/sdk-core';
import { Storage } from '@acala-network/sdk/utils/storage';
import { AcalaPrimitivesCurrencyCurrencyId } from '@polkadot/types/lookup';
import { Option, Vec, StorageKey } from '@polkadot/types';

export function createStorages (api: AnyApi) {
export function createStorages(api: AnyApi) {
return {
globalFeeSwapPathes: () => {
return Storage.create<[StorageKey<[AcalaPrimitivesCurrencyCurrencyId]>, Option<Vec<AcalaPrimitivesCurrencyCurrencyId>>][]>({
return Storage.create<
[StorageKey<[AcalaPrimitivesCurrencyCurrencyId]>, Option<Vec<AcalaPrimitivesCurrencyCurrencyId>>][]
>({
api: api,
path: 'query.transactionPayment.globalFeeSwapPath.entries',
params: []
})
});
}
}
};
}

export type Storages = ReturnType<typeof createStorages>;
export type Storages = ReturnType<typeof createStorages>;
12 changes: 6 additions & 6 deletions packages/sdk-payment/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Wallet } from "@acala-network/sdk";
import { AnyApi, Token } from "@acala-network/sdk-core";
import { AggregateDex } from "@acala-network/sdk-swap";
import { SubmittableExtrinsic } from "@polkadot/api/types";
import { Wallet } from '@acala-network/sdk';
import { AnyApi, Token } from '@acala-network/sdk-core';
import { AggregateDex } from '@acala-network/sdk-swap';
import { SubmittableExtrinsic } from '@polkadot/api/types';

export interface PaymentConfig {
wallet: Wallet;
Expand All @@ -22,12 +22,12 @@ export enum PaymentMethodTypes {
NATIVE_TOKEN = 'native_token',
DEFAULT_FEE_TOKEN = 'default_fee_token',
GLOBAL_SWAP_PATH = 'global_swap_path',
SWAP= 'swap'
SWAP = 'swap'
}

export interface PaymentMethod {
type: PaymentMethodTypes;
path: Token[];
}

export type Tx= SubmittableExtrinsic<'promise'> | SubmittableExtrinsic<'rxjs'>;
export type Tx = SubmittableExtrinsic<'promise'> | SubmittableExtrinsic<'rxjs'>;
10 changes: 3 additions & 7 deletions packages/sdk-payment/src/utils/ensure-ready.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { MethodDeclaration } from 'typescript';
import { NotReady } from '../error';

export function ensureReady (
_target: any,
_propertyKey: string,
descriptor: PropertyDescriptor
) {
export function ensureReady(_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
const origin = descriptor.value;

descriptor.value = function (...args: any[]) {
Expand All @@ -14,7 +10,7 @@ export function ensureReady (
}

return origin.apply(this, args);
}
};

return descriptor;
}
}
4 changes: 2 additions & 2 deletions packages/sdk-payment/src/utils/to-promise.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Observable, isObservable, firstValueFrom } from 'rxjs';

export function toPromise <T extends unknown>(run: Promise<T> | Observable<T>): Promise<T> {
export function toPromise<T>(run: Promise<T> | Observable<T>): Promise<T> {
if (isObservable(run)) {
return firstValueFrom(run);
}

return run;
}
}
2 changes: 1 addition & 1 deletion packages/sdk-swap/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type DexSource = 'acala' | 'nuts';
* ["acala", ["LDOT", "lcDOT://13"]]
* ]
*/
export type TradingPath= [DexSource, Token[]];
export type TradingPath = [DexSource, Token[]];
export type CompositeTradingPath = TradingPath[];

/**
Expand Down

0 comments on commit c28950e

Please sign in to comment.