Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xorless fee #949

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/sdk/src/apiAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import type {
} from './types';
import type { OriginalIdentity } from './staking/types';
import type { CommonPrimitivesAssetId32Override } from './typeOverrides';
import { XOR } from './assets/consts';

// We don't need to know real account address for checking network fees
const mockAccountAddress = 'cnRuw2R6EVgQW3e4h8XeiFym2iU17fNsms15zRGcg9YEJndAs';
Expand Down Expand Up @@ -636,6 +637,8 @@ export class ApiAccount<T = void> extends WithAccountHistory implements ISubmitE
public shouldPairBeLocked = false;
/** If `true` you might subscribe on extrinsic statuses (`false` by default) */
public shouldObservableBeUsed = false;
/** Asset in which fees will calculated and deducted (XOR by default) */
public xorlessAsset = XOR.address;

// prettier-ignore
public async submitApiExtrinsic( // NOSONAR
Expand Down Expand Up @@ -676,7 +679,16 @@ export class ApiAccount<T = void> extends WithAccountHistory implements ISubmitE
historyData?: HistoryItem,
unsigned = false
): Promise<T> {
return await this.submitApiExtrinsic(this.api, extrinsic, accountPair, this.signer, historyData, unsigned);
let extrinsicToSubmit = null;

if (this.xorlessAsset === XOR.address) {
extrinsicToSubmit = extrinsic;
} else {
const xorlessWrapper = this.api.tx.xorFee.xorlessCall;
extrinsicToSubmit = xorlessWrapper(extrinsic, this.xorlessAsset);
}

return await this.submitApiExtrinsic(this.api, extrinsicToSubmit, accountPair, this.signer, historyData, unsigned);
}

public async signExtrinsic(
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/interfaces/augment-api-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3468,6 +3468,10 @@ declare module '@polkadot/api-base/types/errors' {
* White list is filled
**/
WhitelistFull: AugmentedError<ApiType>;
/**
* Remint period should not be 0 or to be greater than 600
**/
WrongRemintPeriod: AugmentedError<ApiType>;
};
xstPool: {
/**
Expand Down
4 changes: 4 additions & 0 deletions packages/types/src/interfaces/augment-api-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,10 @@ declare module '@polkadot/api-base/types/events' {
* The portion of fee is sent to the referrer. [Referral, Referrer, AssetId, Amount]
**/
ReferrerRewarded: AugmentedEvent<ApiType, [AccountId32, AccountId32, CommonPrimitivesAssetId32, u128]>;
/**
* Average remint period updated: [Period]
**/
RemintPeriodUpdated: AugmentedEvent<ApiType, [u32]>;
/**
* New small reference amount set. [New value]
**/
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/interfaces/augment-api-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,7 @@ declare module '@polkadot/api-base/types/storage' {
**/
burntForFee: AugmentedQuery<ApiType, (arg: CommonPrimitivesAssetId32 | { code?: any } | string | Uint8Array) => Observable<XorFeeAssetFee>, [CommonPrimitivesAssetId32]>;
multiplier: AugmentedQuery<ApiType, () => Observable<u128>, []>;
remintPeriod: AugmentedQuery<ApiType, () => Observable<u32>, []>;
/**
* Small fee value should be `SmallReferenceAmount` in reference asset id
**/
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/interfaces/augment-api-tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3663,6 +3663,7 @@ declare module '@polkadot/api-base/types/submittable' {
* Set 0 to stop updating
**/
setFeeUpdatePeriod: AugmentedSubmittable<(newPeriod: u32 | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>, [u32]>;
setRandomRemintPeriod: AugmentedSubmittable<(period: u32 | AnyNumber | Uint8Array) => SubmittableExtrinsic<ApiType>, [u32]>;
/**
* Set new small reference amount `xor_fee::SmallReferenceAmount`
* Small fee should tend to the amount value
Expand Down
10 changes: 7 additions & 3 deletions packages/types/src/interfaces/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ export default {
PeriodUpdated: 'u32',
SmallReferenceAmountUpdated: 'u128',
AssetRemovedFromWhiteList: 'CommonPrimitivesAssetId32',
AssetAddedToWhiteList: 'CommonPrimitivesAssetId32'
AssetAddedToWhiteList: 'CommonPrimitivesAssetId32',
RemintPeriodUpdated: 'u32'
}
},
/**
Expand Down Expand Up @@ -2642,7 +2643,10 @@ export default {
assetId: 'CommonPrimitivesAssetId32',
},
remove_asset_from_white_list: {
assetId: 'CommonPrimitivesAssetId32'
assetId: 'CommonPrimitivesAssetId32',
},
set_random_remint_period: {
period: 'u32'
}
}
},
Expand Down Expand Up @@ -5825,7 +5829,7 @@ export default {
* Lookup690: xor_fee::pallet::Error<T>
**/
XorFeeError: {
_enum: ['MultiplierCalculationFailed', 'InvalidSmallReferenceAmount', 'AssetNotFound', 'AssetAlreadyWhitelisted', 'WhitelistFull', 'FeeCalculationFailed']
_enum: ['MultiplierCalculationFailed', 'InvalidSmallReferenceAmount', 'AssetNotFound', 'AssetAlreadyWhitelisted', 'WhitelistFull', 'FeeCalculationFailed', 'WrongRemintPeriod']
},
/**
* Lookup691: pallet_multisig::MultisigAccount<sp_core::crypto::AccountId32>
Expand Down
13 changes: 10 additions & 3 deletions packages/types/src/interfaces/types-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ declare module '@polkadot/types/lookup' {
readonly asAssetRemovedFromWhiteList: CommonPrimitivesAssetId32;
readonly isAssetAddedToWhiteList: boolean;
readonly asAssetAddedToWhiteList: CommonPrimitivesAssetId32;
readonly type: 'FeeWithdrawn' | 'ReferrerRewarded' | 'WeightToFeeMultiplierUpdated' | 'PeriodUpdated' | 'SmallReferenceAmountUpdated' | 'AssetRemovedFromWhiteList' | 'AssetAddedToWhiteList';
readonly isRemintPeriodUpdated: boolean;
readonly asRemintPeriodUpdated: u32;
readonly type: 'FeeWithdrawn' | 'ReferrerRewarded' | 'WeightToFeeMultiplierUpdated' | 'PeriodUpdated' | 'SmallReferenceAmountUpdated' | 'AssetRemovedFromWhiteList' | 'AssetAddedToWhiteList' | 'RemintPeriodUpdated';
}

/** @name CommonPrimitivesAssetId32 (35) */
Expand Down Expand Up @@ -2923,7 +2925,11 @@ declare module '@polkadot/types/lookup' {
readonly asRemoveAssetFromWhiteList: {
readonly assetId: CommonPrimitivesAssetId32;
} & Struct;
readonly type: 'UpdateMultiplier' | 'SetFeeUpdatePeriod' | 'SetSmallReferenceAmount' | 'XorlessCall' | 'AddAssetToWhiteList' | 'RemoveAssetFromWhiteList';
readonly isSetRandomRemintPeriod: boolean;
readonly asSetRandomRemintPeriod: {
readonly period: u32;
} & Struct;
readonly type: 'UpdateMultiplier' | 'SetFeeUpdatePeriod' | 'SetSmallReferenceAmount' | 'XorlessCall' | 'AddAssetToWhiteList' | 'RemoveAssetFromWhiteList' | 'SetRandomRemintPeriod';
}

/** @name PalletMultisigCall (314) */
Expand Down Expand Up @@ -6156,7 +6162,8 @@ declare module '@polkadot/types/lookup' {
readonly isAssetAlreadyWhitelisted: boolean;
readonly isWhitelistFull: boolean;
readonly isFeeCalculationFailed: boolean;
readonly type: 'MultiplierCalculationFailed' | 'InvalidSmallReferenceAmount' | 'AssetNotFound' | 'AssetAlreadyWhitelisted' | 'WhitelistFull' | 'FeeCalculationFailed';
readonly isWrongRemintPeriod: boolean;
readonly type: 'MultiplierCalculationFailed' | 'InvalidSmallReferenceAmount' | 'AssetNotFound' | 'AssetAlreadyWhitelisted' | 'WhitelistFull' | 'FeeCalculationFailed' | 'WrongRemintPeriod';
}

/** @name PalletMultisigMultisigAccount (691) */
Expand Down
12 changes: 12 additions & 0 deletions scripts/examples/fees.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { api } from '@sora-substrate/sdk';

import { delay, withConnectedAccount } from './util';
import { VAL } from '@sora-substrate/sdk/assets/consts';

async function main(): Promise<void> {
await withConnectedAccount(async () => {
// performance test
const num = 50;
let total = 0;
for (let index = 0; index < num; index++) {
Expand All @@ -18,6 +20,16 @@ async function main(): Promise<void> {
console.info(`[#${index + 1}]: ${time} ms`);
}
console.info('Average time:', total / num);

// xorless fee
const extrinsic = api.api.tx.assets.transfer('', '', 0);
const xorlessWrapper = api.api.tx.xorFee.xorlessCall;

const fee = await api.getTransactionFee(extrinsic);
const xorlessFee = await api.getTransactionFee(xorlessWrapper(extrinsic, VAL.address));

console.info('fee', fee);
console.info('xorlessFee', xorlessFee);
});
}

Expand Down