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

(small breaking change) Merge v13's "TransactionNext" into the existing "Transaction" class #396

Merged
merged 19 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions src/converters/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { TransactionsConverter } from "./transactionsConverter";
64 changes: 64 additions & 0 deletions src/converters/transactionsConverter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { IPlainTransactionObject, ITransaction } from "../interface";
import { Transaction } from "../transaction";

export class TransactionsConverter {
danielailie marked this conversation as resolved.
Show resolved Hide resolved
transactionToPlainObject(transaction: ITransaction): IPlainTransactionObject {
const plainObject = {
nonce: Number(transaction.nonce),
value: transaction.value.toString(),
receiver: transaction.receiver,
sender: transaction.sender,
senderUsername: this.toBase64OrUndefined(transaction.senderUsername),
receiverUsername: this.toBase64OrUndefined(transaction.receiverUsername),
gasPrice: Number(transaction.gasPrice),
gasLimit: Number(transaction.gasLimit),
data: this.toBase64OrUndefined(transaction.data),
chainID: transaction.chainID.valueOf(),
version: transaction.version,
options: transaction.options == 0 ? undefined : transaction.options,
guardian: transaction.guardian ? transaction.guardian : undefined,
signature: this.toHexOrUndefined(transaction.signature),
guardianSignature: this.toHexOrUndefined(transaction.guardianSignature),
};

return plainObject;
}

private toBase64OrUndefined(value?: string | Uint8Array) {
return value && value.length ? Buffer.from(value).toString("base64") : undefined;
}

private toHexOrUndefined(value?: Uint8Array) {
return value && value.length ? Buffer.from(value).toString("hex") : undefined;
}

public plainObjectToTransaction(object: IPlainTransactionObject): Transaction {
const transaction = new Transaction({
nonce: Number(object.nonce),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be BigInt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Fixed.

value: BigInt(object.value || ""),
receiver: object.receiver,
receiverUsername: this.bufferFromBase64(object.receiverUsername).toString(),
sender: object.sender,
senderUsername: this.bufferFromBase64(object.senderUsername).toString(),
guardian: object.guardian,
gasPrice: Number(object.gasPrice),
gasLimit: Number(object.gasLimit),
data: this.bufferFromBase64(object.data),
chainID: String(object.chainID),
version: object.version,
options: object.options,
signature: this.bufferFromHex(object.signature),
guardianSignature: this.bufferFromHex(object.guardianSignature),
});

return transaction;
}

private bufferFromBase64(value?: string) {
return Buffer.from(value || "", "base64");
}

private bufferFromHex(value?: string) {
return Buffer.from(value || "", "hex");
}
}
23 changes: 23 additions & 0 deletions src/converters/transactionsConverters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { assert } from "chai";
import { Transaction } from "../transaction";

describe("test transactions converter", async () => {
it("converts transaction to plain object and back", () => {
const transaction = new Transaction({
nonce: 90,
value: BigInt("123456789000000000000000000000"),
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx",
senderUsername: "alice",
receiverUsername: "bob",
gasPrice: 1000000000,
gasLimit: 80000,
data: Buffer.from("hello"),
chainID: "localnet",
});

const plainObject = transaction.toPlainObject();
const restoredTransaction = Transaction.fromPlainObject(plainObject);
assert.deepEqual(restoredTransaction, transaction);
});
});
2 changes: 1 addition & 1 deletion src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export interface ITokenTransfer {
*/
export type ITokenPayment = ITokenTransfer;

export interface ITransactionNext {
export interface ITransaction {
sender: string;
receiver: string;
gasLimit: bigint;
Expand Down
15 changes: 6 additions & 9 deletions src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class SmartContract implements ISmartContract {
Compatibility.guardAddressIsSetAndNonZero(deployer, "'deployer' of SmartContract.deploy()", "pass the actual address to deploy()");

const config = new TransactionsFactoryConfig({ chainID: chainID.valueOf() });
const scNextTransactionFactory = new SmartContractTransactionsFactory({
const factory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi,
tokenComputer: new TokenComputer()
Expand All @@ -127,7 +127,7 @@ export class SmartContract implements ISmartContract {
const bytecode = Buffer.from(code.toString(), 'hex');
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const nextTx = scNextTransactionFactory.createTransactionForDeploy({
const transaction = factory.createTransactionForDeploy({
sender: deployer,
bytecode: bytecode,
gasLimit: BigInt(gasLimit.valueOf()),
Expand All @@ -138,7 +138,6 @@ export class SmartContract implements ISmartContract {
isPayableBySmartContract: metadataAsJson.payableBySc
});

const transaction = Transaction.fromTransactionNext(nextTx);
transaction.setChainID(chainID);
transaction.setValue(value ?? 0);
transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE)
Expand Down Expand Up @@ -178,7 +177,7 @@ export class SmartContract implements ISmartContract {
this.ensureHasAddress();

const config = new TransactionsFactoryConfig({ chainID: chainID.valueOf() });
const scNextTransactionFactory = new SmartContractTransactionsFactory({
const factory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi,
tokenComputer: new TokenComputer()
Expand All @@ -187,7 +186,7 @@ export class SmartContract implements ISmartContract {
const bytecode = Uint8Array.from(Buffer.from(code.toString(), 'hex'));
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const nextTx = scNextTransactionFactory.createTransactionForUpgrade({
const transaction = factory.createTransactionForUpgrade({
sender: caller,
contract: this.getAddress(),
bytecode: bytecode,
Expand All @@ -199,7 +198,6 @@ export class SmartContract implements ISmartContract {
isPayableBySmartContract: metadataAsJson.payableBySc
})

const transaction = Transaction.fromTransactionNext(nextTx);
transaction.setChainID(chainID);
transaction.setValue(value ?? 0);
transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE)
Expand All @@ -216,7 +214,7 @@ export class SmartContract implements ISmartContract {
this.ensureHasAddress();

const config = new TransactionsFactoryConfig({ chainID: chainID.valueOf() });
const scNextTransactionFactory = new SmartContractTransactionsFactory({
const factory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi,
tokenComputer: new TokenComputer()
Expand All @@ -225,15 +223,14 @@ export class SmartContract implements ISmartContract {
args = args || [];
value = value || 0;

const nextTx = scNextTransactionFactory.createTransactionForExecute({
const transaction = factory.createTransactionForExecute({
sender: caller,
contract: receiver ? receiver : this.getAddress(),
functionName: func.toString(),
gasLimit: BigInt(gasLimit.valueOf()),
args: args
})

const transaction = Transaction.fromTransactionNext(nextTx);
transaction.setChainID(chainID);
transaction.setValue(value);
transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE)
Expand Down
4 changes: 2 additions & 2 deletions src/testutils/networkProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ITransactionStatus,
} from "../interfaceOfNetwork";
import { Query } from "../smartcontracts/query";
import { Transaction, TransactionNext } from "../transaction";
import { Transaction } from "../transaction";

export function createLocalnetProvider(): INetworkProvider {
return new ProxyNetworkProvider("http://localhost:7950", { timeout: 5000 });
Expand All @@ -23,7 +23,7 @@ export interface INetworkProvider {
getAccount(address: IAddress): Promise<IAccountOnNetwork>;
getTransaction(txHash: string, withProcessStatus?: boolean): Promise<ITransactionOnNetwork>;
getTransactionStatus(txHash: string): Promise<ITransactionStatus>;
sendTransaction(tx: Transaction | TransactionNext): Promise<string>;
sendTransaction(tx: Transaction): Promise<string>;
simulateTransaction(tx: Transaction): Promise<any>;
queryContract(query: Query): Promise<IContractQueryResponse>;
}
Loading
Loading