Skip to content

Commit

Permalink
Merge pull request #203 from logion-network/feature/fees-auto-publish
Browse files Browse the repository at this point in the history
Add storage fee to estimates on batch publish.
  • Loading branch information
benoitdevos authored Oct 27, 2023
2 parents f1f3b38 + da9c259 commit a44a78d
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
6 changes: 5 additions & 1 deletion packages/client/integration/Loc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export async function openTransactionLocWithAutoPublish(state: State, linkTarget
draftRequest = await draftRequest.addFile({
fileName: "test.txt",
nature: "Some file nature",
file: HashOrContent.fromContent(Buffer.from("test1")),
file: HashOrContent.fromContent(Buffer.from("test123")),
}) as DraftRequest;
const hash1 = draftRequest.data().files[1].hash;

Expand All @@ -280,6 +280,10 @@ export async function openTransactionLocWithAutoPublish(state: State, linkTarget
await alicePendingLoc.legalOfficer.accept({ signer });

let acceptedLoc = await pendingRequest.refresh() as AcceptedRequest;

const fees = await acceptedLoc.estimateFeesOpen({ autoPublish: true });
expect(fees.storageFee).toEqual(1200000000000n);

let openLoc = await acceptedLoc.open({ signer, autoPublish: true });
expect(openLoc).toBeInstanceOf(OpenLoc);

Expand Down
2 changes: 1 addition & 1 deletion packages/client/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/client",
"version": "0.33.0-10",
"version": "0.33.0-11",
"description": "logion SDK for client applications",
"main": "dist/index.js",
"packageManager": "[email protected]",
Expand Down
12 changes: 6 additions & 6 deletions packages/client/src/Loc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1410,21 +1410,19 @@ export class AcceptedRequest extends ReviewedRequest {
await this.locSharedState.client.openTransactionLoc({
...this.checkOpenParams(autoPublish),
...parameters,
autoPublish,
})
} else if (this.request.locType === "Identity") {
await this.locSharedState.client.openIdentityLoc({
...this.checkOpenParams(autoPublish),
...parameters,
autoPublish,
})
} else {
throw Error("Collection LOCs are opened with openCollection()");
}
return await this.refresh() as OpenLoc
}

private checkOpenParams(autoPublish: boolean): OpenPolkadotLocParams {
private checkOpenParams(autoPublish: boolean): OpenPolkadotLocParams & AutoPublish {
const requesterAddress = this.request.requesterAddress;
if (requesterAddress === undefined || requesterAddress?.type !== "Polkadot") {
throw Error("Only Polkadot requester can open LOC");
Expand All @@ -1436,6 +1434,7 @@ export class AcceptedRequest extends ReviewedRequest {
metadata: this.toAddMetadataParams(autoPublish),
files: this.toAddFileParams(autoPublish),
links: this.toAddLinkParams(autoPublish),
autoPublish,
}
}

Expand Down Expand Up @@ -1473,7 +1472,8 @@ export class AcceptedRequest extends ReviewedRequest {
}
}

async estimateFeesOpen(autoPublish: boolean): Promise<FeesClass> {
async estimateFeesOpen(parameters: AutoPublish): Promise<FeesClass> {
const { autoPublish } = parameters;
if (this.request.locType === "Transaction") {
return this.locSharedState.client.estimateFeesOpenTransactionLoc(this.checkOpenParams(autoPublish))
} else if (this.request.locType === "Identity") {
Expand All @@ -1488,12 +1488,11 @@ export class AcceptedRequest extends ReviewedRequest {
await this.locSharedState.client.openCollectionLoc({
...this.checkOpenCollectionParams(autoPublish),
...parameters,
autoPublish,
});
return await this.refresh() as OpenLoc;
}

private checkOpenCollectionParams(autoPublish: boolean): OpenPolkadotLocParams & { valueFee: bigint } {
private checkOpenCollectionParams(autoPublish: boolean): OpenPolkadotLocParams & { valueFee: bigint } & AutoPublish {
const requesterAddress = this.request.requesterAddress;
if (requesterAddress === undefined || requesterAddress?.type !== "Polkadot") {
throw Error("Only Polkadot requester can open, or estimate fees of, a Collection LOC");
Expand All @@ -1511,6 +1510,7 @@ export class AcceptedRequest extends ReviewedRequest {
metadata: this.toAddMetadataParams(autoPublish),
files: this.toAddFileParams(autoPublish),
links: this.toAddLinkParams(autoPublish),
autoPublish,
}
} else {
throw Error("Other LOCs are opened/estimated with open()/estimateFeesOpen()");
Expand Down
19 changes: 16 additions & 3 deletions packages/client/src/LocClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,13 @@ export class AuthenticatedLocClient extends LocClient {
}
}

private storageSize(autoPublish: boolean, files: AddFileParams[]): bigint | undefined {
if (!autoPublish || files.length === 0) {
return undefined;
}
return files.map(file => file.file.size).reduce((a, b) => a + b);
}

private openTransactionLocSubmittable(parameters: OpenPolkadotLocParams): SubmittableExtrinsic {
const { locId, legalOfficerAddress, metadata, files, links } = parameters;
return this.nodeApi.polkadot.tx.logionLoc.createPolkadotTransactionLoc(
Expand All @@ -1771,12 +1778,14 @@ export class AuthenticatedLocClient extends LocClient {
);
}

async estimateFeesOpenTransactionLoc(parameters: OpenPolkadotLocParams): Promise<FeesClass> {
async estimateFeesOpenTransactionLoc(parameters: OpenPolkadotLocParams & AutoPublish): Promise<FeesClass> {
const storageSize = this.storageSize(parameters.autoPublish, parameters.files);
return await this.nodeApi.fees.estimateCreateLoc({
origin: this.currentAddress?.address || "",
submittable: this.openTransactionLocSubmittable(parameters),
locType: 'Transaction',
legalFee: parameters.legalFee,
storageSize,
});
}

Expand Down Expand Up @@ -1946,12 +1955,14 @@ export class AuthenticatedLocClient extends LocClient {
}
}

async estimateFeesOpenIdentityLoc(parameters: OpenPolkadotLocParams) {
async estimateFeesOpenIdentityLoc(parameters: OpenPolkadotLocParams & AutoPublish) {
const storageSize = this.storageSize(parameters.autoPublish, parameters.files);
return await this.nodeApi.fees.estimateCreateLoc({
origin: this.currentAddress?.address || "",
submittable: this.openIdentityLocSubmittable(parameters),
locType: 'Identity',
legalFee: parameters.legalFee,
storageSize,
});
}

Expand Down Expand Up @@ -2024,13 +2035,15 @@ export class AuthenticatedLocClient extends LocClient {
);
}

async estimateFeesOpenCollectionLoc(parameters: { valueFee: bigint } & OpenPolkadotLocParams & EstimateFeesOpenCollectionLocParams) {
async estimateFeesOpenCollectionLoc(parameters: { valueFee: bigint } & OpenPolkadotLocParams & EstimateFeesOpenCollectionLocParams & AutoPublish) {
const storageSize = this.storageSize(parameters.autoPublish, parameters.files);
return await this.nodeApi.fees.estimateCreateLoc({
origin: this.currentAddress.address,
submittable: this.openCollectionLocSubmittable(parameters),
locType: 'Collection',
valueFee: parameters.valueFee,
legalFee: parameters.legalFee,
storageSize,
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@logion/node-api",
"version": "0.24.0-2",
"version": "0.24.0-3",
"description": "logion API",
"main": "dist/cjs/index.js",
"module": "dist/esm/index.js",
Expand Down
6 changes: 4 additions & 2 deletions packages/node-api/src/FeesEstimator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,13 @@ export class FeesEstimator {
locType: LocType,
valueFee?: bigint,
legalFee?: bigint,
storageSize?: bigint,
}): Promise<Fees> {
const { locType, valueFee } = params;
const { locType, valueFee, storageSize } = params;
const inclusionFee = await this.estimateInclusionFee(params.origin, params.submittable);
const legalFee = params.legalFee !== undefined ? params.legalFee : await this.estimateLegalFee({ locType });
return new Fees({ inclusionFee, legalFee, valueFee });
const storageFee = storageSize !== undefined ? await this.estimateStorageFee({ numOfEntries: 1n, totSize: storageSize }) : undefined;
return new Fees({ inclusionFee, legalFee, valueFee, storageFee });
}

async estimateCertificateFee(params: { tokenIssuance: bigint }): Promise<bigint> {
Expand Down

0 comments on commit a44a78d

Please sign in to comment.