Skip to content

Commit

Permalink
Add stateless transaction estimate method (#73)
Browse files Browse the repository at this point in the history
* Add stateless transaction estimate

* version

* minor fixes

* minor fix
  • Loading branch information
Rafael Cidade authored Jul 16, 2021
1 parent 9615e97 commit 43e9a54
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "etherspot",
"version": "1.13.2",
"version": "1.14.0",
"description": "Etherspot SDK",
"keywords": [
"ether",
Expand Down
29 changes: 19 additions & 10 deletions src/sdk/gateway/gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,12 @@ export class GatewayService extends Service {
return result;
}

async estimateGatewayBatch(feeToken: string): Promise<GatewayBatch> {
if (!this.gatewayBatch) {
async estimateGatewayBatch(feeToken: string, statelessBatch?: GatewayBatch): Promise<GatewayBatch> {
if (!this.gatewayBatch && !statelessBatch) {
throw new Exception('Can not estimate empty batch');
}

const { to, data } = this.extractToAndData();
const { to, data } = this.extractToAndData(statelessBatch);
const nonce = uniqueNonce();

const { accountService, apiService } = this.services;
Expand Down Expand Up @@ -419,6 +419,13 @@ export class GatewayService extends Service {
},
);

if (statelessBatch) {
return {
...statelessBatch,
estimation,
};
}

this.estimationOptions = {
nonce,
feeToken,
Expand Down Expand Up @@ -464,18 +471,18 @@ export class GatewayService extends Service {
return result;
}

async submitGatewayBatch(): Promise<GatewaySubmittedBatch> {
if (!this.gatewayBatch) {
async submitGatewayBatch(statelessBatch?: GatewayBatch): Promise<GatewaySubmittedBatch> {
if (!this.gatewayBatch && !statelessBatch) {
throw new Exception('Can not submit empty batch');
}

const { estimation } = this.gatewayBatch;
const { estimation } = statelessBatch || this.gatewayBatch;

if (!estimation || estimation.expiredAt.getTime() < estimation.createdAt.getTime()) {
throw new Exception('Can not submit not estimated batch');
}

const { to, data } = this.extractToAndData();
const { to, data } = this.extractToAndData(statelessBatch);
const {
feeTokenReceiver,
feeAmount,
Expand Down Expand Up @@ -594,7 +601,9 @@ export class GatewayService extends Service {
},
);

this.clearGatewayBatch();
if (!statelessBatch) {
this.clearGatewayBatch();
}

return result;
}
Expand Down Expand Up @@ -629,8 +638,8 @@ export class GatewayService extends Service {
return result;
}

private extractToAndData(): { to: string[]; data: string[] } {
return this.gatewayBatch.requests.reduce(
private extractToAndData(statelessBatch?: GatewayBatch): { to: string[]; data: string[] } {
return (statelessBatch || this.gatewayBatch).requests.reduce(
(result, { to, data }) => {
result.to.push(to);
result.data.push(data);
Expand Down
34 changes: 33 additions & 1 deletion src/sdk/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber } from 'ethers';
import { BigNumber, utils } from 'ethers';
import { BehaviorSubject, Subject } from 'rxjs';
import {
Account,
Expand Down Expand Up @@ -425,6 +425,38 @@ export class Sdk {
return this.services.gatewayService.estimateGatewayBatch(feeToken);
}

/**
* estimates stateless account transactions
* @param dto
* @return Promise<GatewayBatch>
*/
async estimateStatelessAccountTransactions(
transactionsDto: ExecuteAccountTransactionDto[],
estimationDto: EstimateGatewayBatchDto = {},
): Promise<GatewayBatch> {
const { gatewayService } = this.services;

const gatewayBatch: GatewayBatch = {
requests: [],
estimation: null,
};

const { feeToken } = await validateDto(estimationDto, EstimateGatewayBatchDto, {
addressKeys: ['feeToken'],
});

for (const transactionDto of transactionsDto) {
const encodedAccountTransaction = await this.encodeExecuteAccountTransaction(transactionDto);
const { to, data } = encodedAccountTransaction;
gatewayBatch.requests.push({
to,
data: utils.hexlify(data),
});
}

return gatewayService.estimateGatewayBatch(feeToken, gatewayBatch);
}

/**
* submits gateway batch
* @param dto
Expand Down

0 comments on commit 43e9a54

Please sign in to comment.