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

Refactoring DraftTransaction to TransactionNext #376

Merged
merged 10 commits into from
Feb 13, 2024
23 changes: 0 additions & 23 deletions src/draftTransaction.ts

This file was deleted.

18 changes: 9 additions & 9 deletions src/smartcontracts/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { Query } from "./query";
import { WasmVirtualMachine } from "./transactionPayloadBuilders";
import { EndpointDefinition, TypedValue } from "./typesystem";
const createKeccakHash = require("keccak");

Check warning on line 21 in src/smartcontracts/smartContract.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

Require statement not part of import statement

interface IAbi {
constructorDefinition: EndpointDefinition;
Expand Down Expand Up @@ -118,7 +118,7 @@
Compatibility.guardAddressIsSetAndNonZero(deployer, "'deployer' of SmartContract.deploy()", "pass the actual address to deploy()");

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

const draftTx = scDraftTransactionFactory.createTransactionForDeploy({
const nextTx = scNextTransactionFactory.createTransactionForDeploy({
sender: deployer,
bytecode: bytecode,
gasLimit: gasLimit.valueOf(),
Expand All @@ -138,7 +138,7 @@
isPayableBySmartContract: metadataAsJson.payableBySc
});

const transaction = Transaction.fromDraft(draftTx);
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 +178,7 @@
this.ensureHasAddress();

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

const draftTx = scDraftTransactionFactory.createTransactionForUpgrade({
const nextTx = scNextTransactionFactory.createTransactionForUpgrade({
sender: caller,
contract: this.getAddress(),
bytecode: bytecode,
Expand All @@ -199,7 +199,7 @@
isPayableBySmartContract: metadataAsJson.payableBySc
})

const transaction = Transaction.fromDraft(draftTx);
const transaction = Transaction.fromTransactionNext(nextTx);
transaction.setChainID(chainID);
transaction.setValue(value ?? 0);
transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE)
Expand All @@ -216,7 +216,7 @@
this.ensureHasAddress();

const config = new TransactionsFactoryConfig(chainID.valueOf());
const scDraftTransactionFactory = new SmartContractTransactionsFactory({
const scNextTransactionFactory = new SmartContractTransactionsFactory({
config: config,
abi: this.abi,
tokenComputer: new TokenComputer()
Expand All @@ -225,15 +225,15 @@
args = args || [];
value = value || 0;

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

const transaction = Transaction.fromDraft(draftTx);
const transaction = Transaction.fromTransactionNext(nextTx);
transaction.setChainID(chainID);
transaction.setValue(value);
transaction.setGasPrice(gasPrice ?? TRANSACTION_MIN_GAS_PRICE)
Expand Down
22 changes: 0 additions & 22 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { TestWallet, loadTestWallets } from "./testutils";
import { TokenTransfer } from "./tokenTransfer";
import { Transaction, TransactionNext } from "./transaction";
import { TransactionPayload } from "./transactionPayload";
import { DraftTransaction } from "./draftTransaction";
import { TRANSACTION_MIN_GAS_PRICE } from "./constants";


Expand All @@ -19,27 +18,6 @@ describe("test transaction construction", async () => {
wallets = await loadTestWallets();
});

it("create transaction from draft transaction", async () => {
const draftTransaction = new DraftTransaction({
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
receiver: "erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx",
gasLimit: 56000,
value: "1000000000000000000",
data: Buffer.from("test")
});

const transaction = Transaction.fromDraft(draftTransaction);
assert.deepEqual(transaction.getSender(), Address.fromBech32("erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th"));
assert.deepEqual(transaction.getReceiver(), Address.fromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"));
assert.equal(transaction.getGasLimit().valueOf(), 56000);
assert.equal(transaction.getValue().toString(), "1000000000000000000");
assert.equal(transaction.getData().toString(), "test");
assert.equal(transaction.getChainID().valueOf(), "");
assert.equal(transaction.getNonce().valueOf(), 0);
assert.equal(transaction.getGasPrice().valueOf(), TRANSACTION_MIN_GAS_PRICE);
assert.deepEqual(transaction.getSignature(), Buffer.from([]));
});

it("create transaction from transaction next", async () => {
const plainTransactionNextObject = {
sender: "erd1qyu5wthldzr8wx5c9ucg8kjagg0jfs53s8nr3zpz3hypefsdd8ssycr6th",
Expand Down
15 changes: 0 additions & 15 deletions src/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { BigNumber } from "bignumber.js";
import { Address } from "./address";
import { Compatibility } from "./compatibility";
import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants";
import { DraftTransaction } from "./draftTransaction";
import * as errors from "./errors";
import { Hash } from "./hash";
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, IPlainTransactionObject, ISignature, ITransactionNext, ITransactionOptions, ITransactionPayload, ITransactionValue, ITransactionVersion } from "./interface";
Expand Down Expand Up @@ -422,20 +421,6 @@ export class Transaction {
return feeForMove.plus(processingFee);
}

/**
* Creates a new Transaction object from a DraftTransaction.
*/
static fromDraft(draft: DraftTransaction): Transaction {
return new Transaction({
sender: Address.fromBech32(draft.sender),
receiver: Address.fromBech32(draft.receiver),
gasLimit: new BigNumber(draft.gasLimit).toNumber(),
chainID: "",
value: draft.value,
data: new TransactionPayload(Buffer.from(draft.data))
})
}

/**
* Creates a new Transaction object from a TransactionNext object.
*/
Expand Down
Loading
Loading