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

540 capture calldata cp #546

Merged
merged 6 commits into from
Sep 21, 2022
Merged
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
75 changes: 71 additions & 4 deletions packages/relay/src/lib/clients/sdkClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import {
Transaction,
TransactionRecord,
Status,
EthereumFlow
FileCreateTransaction,
FileAppendTransaction,
FileInfoQuery,
EthereumTransaction,
EthereumTransactionData,
} from '@hashgraph/sdk';
import { BigNumber } from '@hashgraph/sdk/lib/Transfer';
import { Logger } from "pino";
Expand Down Expand Up @@ -192,8 +196,23 @@ export class SDKClient {
}

async submitEthereumTransaction(transactionBuffer: Uint8Array, callerName: string, requestId?: string): Promise<TransactionResponse> {
return this.executeTransaction(new EthereumFlow()
.setEthereumData(transactionBuffer), callerName, requestId);
const ethereumTransactionData: EthereumTransactionData = EthereumTransactionData.fromBytes(transactionBuffer);
const ethereumTransaction = new EthereumTransaction();

if (ethereumTransactionData.toBytes().length <= 5120) {
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes());
} else {
const fileId = await this.createFile(ethereumTransactionData.callData, this.clientMain, requestId);

if(!fileId) {
const requestIdPrefix = formatRequestIdMessage(requestId);
throw new SDKClientError({}, `${requestIdPrefix} No fileId created for transaction. `);
}
ethereumTransactionData.callData = new Uint8Array();
ethereumTransaction.setEthereumData(ethereumTransactionData.toBytes()).setCallDataFileId(fileId)
}

return this.executeTransaction(ethereumTransaction, callerName, requestId);
}

async submitContractCallQuery(to: string, data: string, gas: number, from: string, callerName: string, requestId?: string): Promise<ContractFunctionResult> {
Expand Down Expand Up @@ -268,7 +287,7 @@ export class SDKClient {
}
};

private executeTransaction = async (transaction: Transaction | EthereumFlow, callerName: string, requestId?: string): Promise<TransactionResponse> => {
private executeTransaction = async (transaction: Transaction, callerName: string, requestId?: string): Promise<TransactionResponse> => {
const transactionType = transaction.constructor.name;
const requestIdPrefix = formatRequestIdMessage(requestId);
try {
Expand Down Expand Up @@ -351,4 +370,52 @@ export class SDKClient {
.to(HbarUnit.Tinybar)
.multipliedBy(constants.TINYBAR_TO_WEIBAR_COEF);
}

private createFile = async (callData: Uint8Array, client: Client, requestId?: string) => {
const requestIdPrefix = formatRequestIdMessage(requestId);
const hexedCallData = Buffer.from(callData).toString("hex");

const fileId = (
(
await (
await new FileCreateTransaction()
.setContents(hexedCallData.substring(0, 4096))
.setKeys(
client.operatorPublicKey
? [client.operatorPublicKey]
: []
)
.execute(client)
).getReceipt(client)
).fileId
);

if (fileId && callData.length > 4096) {
await (
await new FileAppendTransaction()
.setFileId(fileId)
.setContents(
hexedCallData.substring(4096, hexedCallData.length)
)
.setChunkSize(4096)
.execute(client)
).getReceipt(client);
}

// Ensure that the calldata file is not empty
if(fileId) {
const fileSize = await (
await new FileInfoQuery()
.setFileId(fileId)
.execute(client)
).size;

if(callData.length > 0 && fileSize.isZero()) {
throw new SDKClientError({}, `${requestIdPrefix} Created file is empty. `);
}
this.logger.trace(`${requestIdPrefix} Created file with fileId: ${fileId} and file size ${fileSize}`);
}

return fileId;
}
}