Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Nov 13, 2024
1 parent bb29def commit f1c9b9c
Show file tree
Hide file tree
Showing 13 changed files with 95 additions and 111 deletions.
5 changes: 2 additions & 3 deletions src/abi/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { Address } from "../address";
import { Compatibility } from "../compatibility";
import { TRANSACTION_VERSION_DEFAULT } from "../constants";
import { IAddress, IChainID, IGasLimit, IGasPrice, INonce, ITokenTransfer, ITransactionValue } from "../interface";
import { SmartContractTransactionsFactory } from "../smartContracts";
import { TokenTransfer } from "../tokens";
import { Transaction } from "../transaction";
import { SmartContractTransactionsFactory } from "../transactionsFactories";
import { TransactionsFactoryConfig } from "../transactionsFactoryConfig";
import { ContractFunction } from "./function";
import { InteractionChecker } from "./interactionChecker";
Expand Down Expand Up @@ -98,8 +98,7 @@ export class Interaction {
config: factoryConfig,
});

const transaction = factory.createTransactionForExecute({
sender: this.sender,
const transaction = factory.createTransactionForExecute(this.sender, {
contract: this.contract.getAddress(),
function: this.function.valueOf(),
gasLimit: BigInt(this.gasLimit.valueOf()),
Expand Down
13 changes: 5 additions & 8 deletions src/abi/smartContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,10 @@ export class SmartContract implements ISmartContract {
const bytecode = Buffer.from(code.toString(), "hex");
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const transaction = factory.createTransactionForDeploy({
sender: deployer,
const transaction = factory.createTransactionForDeploy(deployer, {
bytecode: bytecode,
gasLimit: BigInt(gasLimit.valueOf()),
arguments: initArguments,
arguments: initArguments ?? [],
isUpgradeable: metadataAsJson.upgradeable,
isReadable: metadataAsJson.readable,
isPayable: metadataAsJson.payable,
Expand Down Expand Up @@ -211,12 +210,11 @@ export class SmartContract implements ISmartContract {
const bytecode = Uint8Array.from(Buffer.from(code.toString(), "hex"));
const metadataAsJson = this.getMetadataPropertiesAsObject(codeMetadata);

const transaction = factory.createTransactionForUpgrade({
sender: caller,
const transaction = factory.createTransactionForUpgrade(caller, {
contract: this.getAddress(),
bytecode: bytecode,
gasLimit: BigInt(gasLimit.valueOf()),
arguments: initArguments,
arguments: initArguments ?? [],
isUpgradeable: metadataAsJson.upgradeable,
isReadable: metadataAsJson.readable,
isPayable: metadataAsJson.payable,
Expand Down Expand Up @@ -251,8 +249,7 @@ export class SmartContract implements ISmartContract {
args = args || [];
value = value || 0;

const transaction = factory.createTransactionForExecute({
sender: caller,
const transaction = factory.createTransactionForExecute(caller, {
contract: receiver ? receiver : this.getAddress(),
function: func.toString(),
gasLimit: BigInt(gasLimit.valueOf()),
Expand Down
44 changes: 15 additions & 29 deletions src/delegation/delegationTransactionsFactory.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { assert } from "chai";
import { Address } from "../address";
import { DELEGATION_MANAGER_SC_ADDRESS_HEX } from "../constants";
import { TransactionsFactoryConfig } from "../transactionsFactories/transactionsFactoryConfig";
import { TransactionsFactoryConfig } from "../transactionsFactoryConfig";
import { ValidatorPublicKey } from "../wallet";
import { DelegationTransactionsFactory } from "./delegationTransactionsFactory";

Expand All @@ -15,8 +15,7 @@ describe("test delegation transactions factory", function () {
const serviceFee = 10n;
const value = 1250000000000000000000n;

const transaction = delegationFactory.createTransactionForNewDelegationContract({
sender: sender,
const transaction = delegationFactory.createTransactionForNewDelegationContract(sender, {
totalDelegationCap: delagationCap,
serviceFee: serviceFee,
amount: value,
Expand Down Expand Up @@ -52,8 +51,7 @@ describe("test delegation transactions factory", function () {
),
};

const transaction = delegationFactory.createTransactionForAddingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForAddingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
signedMessages: [mockMessage.getSignature()],
Expand Down Expand Up @@ -81,8 +79,7 @@ describe("test delegation transactions factory", function () {
},
};

const transaction = delegationFactory.createTransactionForRemovingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForRemovingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
});
Expand All @@ -104,8 +101,7 @@ describe("test delegation transactions factory", function () {
},
};

const transaction = delegationFactory.createTransactionForStakingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForStakingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
});
Expand All @@ -127,8 +123,7 @@ describe("test delegation transactions factory", function () {
},
};

const transaction = delegationFactory.createTransactionForUnbondingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForUnbondingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
});
Expand All @@ -151,8 +146,7 @@ describe("test delegation transactions factory", function () {
},
};

const transaction = delegationFactory.createTransactionForUnstakingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForUnstakingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
});
Expand All @@ -175,8 +169,7 @@ describe("test delegation transactions factory", function () {
},
};

const transaction = delegationFactory.createTransactionForUnjailingNodes({
sender: sender,
const transaction = delegationFactory.createTransactionForUnjailingNodes(sender, {
delegationContract: delegationContract,
publicKeys: [publicKey],
amount: 25000000000000000000n,
Expand All @@ -194,8 +187,7 @@ describe("test delegation transactions factory", function () {
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const serviceFee = 10n;

const transaction = delegationFactory.createTransactionForChangingServiceFee({
sender: sender,
const transaction = delegationFactory.createTransactionForChangingServiceFee(sender, {
delegationContract: delegationContract,
serviceFee: serviceFee,
});
Expand All @@ -212,8 +204,7 @@ describe("test delegation transactions factory", function () {
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");
const delegationCap = 5000000000000000000000n;

const transaction = delegationFactory.createTransactionForModifyingDelegationCap({
sender: sender,
const transaction = delegationFactory.createTransactionForModifyingDelegationCap(sender, {
delegationContract: delegationContract,
delegationCap: delegationCap,
});
Expand All @@ -229,8 +220,7 @@ describe("test delegation transactions factory", function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const transaction = delegationFactory.createTransactionForSettingAutomaticActivation({
sender: sender,
const transaction = delegationFactory.createTransactionForSettingAutomaticActivation(sender, {
delegationContract: delegationContract,
});

Expand All @@ -245,8 +235,7 @@ describe("test delegation transactions factory", function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const transaction = delegationFactory.createTransactionForUnsettingAutomaticActivation({
sender: sender,
const transaction = delegationFactory.createTransactionForUnsettingAutomaticActivation(sender, {
delegationContract: delegationContract,
});

Expand All @@ -261,8 +250,7 @@ describe("test delegation transactions factory", function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const transaction = delegationFactory.createTransactionForSettingCapCheckOnRedelegateRewards({
sender: sender,
const transaction = delegationFactory.createTransactionForSettingCapCheckOnRedelegateRewards(sender, {
delegationContract: delegationContract,
});

Expand All @@ -277,8 +265,7 @@ describe("test delegation transactions factory", function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const transaction = delegationFactory.createTransactionForUnsettingCapCheckOnRedelegateRewards({
sender: sender,
const transaction = delegationFactory.createTransactionForUnsettingCapCheckOnRedelegateRewards(sender, {
delegationContract: delegationContract,
});

Expand All @@ -293,8 +280,7 @@ describe("test delegation transactions factory", function () {
const sender = Address.fromBech32("erd18s6a06ktr2v6fgxv4ffhauxvptssnaqlds45qgsrucemlwc8rawq553rt2");
const delegationContract = Address.fromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqtllllls002zgc");

const transaction = delegationFactory.createTransactionForSettingMetadata({
sender: sender,
const transaction = delegationFactory.createTransactionForSettingMetadata(sender, {
delegationContract: delegationContract,
name: "name",
website: "website",
Expand Down
79 changes: 45 additions & 34 deletions src/entrypoints/entrypoints.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from "chai";
import { readFileSync } from "fs";
import { Account } from "../account";
import { Account } from "../accounts/account";
import { Address } from "../address";
import { loadAbiRegistry, loadTestWallet, TestWallet } from "../testutils";
import { TransactionComputer } from "../transactionComputer";
Expand All @@ -20,15 +20,18 @@ describe("TestEntrypoint", () => {

it("native transfer", async () => {
const controller = entrypoint.createTransfersController();
const sender = Account.fromPem(alicePem.pemFileText);
const sender = Account.newFromPem(alicePem.pemFileText);
sender.nonce = 77777;

const transaction = await controller.createTransactionForTransfer(sender, {
nonce: BigInt(sender.getNonceThenIncrement().valueOf()),
receiver: sender.address,
nativeTransferAmount: BigInt(0),
data: Buffer.from("hello"),
});
const transaction = await controller.createTransactionForTransfer(
sender,
BigInt(sender.getNonceThenIncrement().valueOf()),
{
receiver: sender.address,
nativeAmount: BigInt(0),
data: Buffer.from("hello"),
},
);
assert.equal(
Buffer.from(transaction.signature).toString("hex"),
"69bc7d1777edd0a901e6cf94830475716205c5efdf2fd44d4be31badead59fc8418b34f0aa3b2c80ba14aed5edd30031757d826af58a1abb690a0bee89ba9309",
Expand All @@ -38,19 +41,22 @@ describe("TestEntrypoint", () => {
it("contract flow", async function () {
this.timeout(30000);
const abi = await loadAbiRegistry("src/testdata/adder.abi.json");
const sender = Account.fromPem(alicePem.pemFileText);
const sender = Account.newFromPem(alicePem.pemFileText);
const accountAddress = new Address(sender.address.bech32());
sender.nonce = await entrypoint.recallAccountNonce(accountAddress);

const controller = entrypoint.createSmartContractController(abi);
const bytecode = readFileSync("src/testdata/adder.wasm");

const transaction = await controller.createTransactionForDeploy(sender, {
nonce: BigInt(sender.getNonceThenIncrement().valueOf()),
bytecode,
gasLimit: BigInt(10_000_000),
arguments: [0],
});
const transaction = await controller.createTransactionForDeploy(
sender,
BigInt(sender.getNonceThenIncrement().valueOf()),
{
bytecode,
gasLimit: BigInt(10_000_000),
arguments: [0],
},
);

const txHash = await entrypoint.sendTransaction(transaction);
const outcome = await controller.awaitCompletedDeploy(txHash);
Expand All @@ -59,13 +65,16 @@ describe("TestEntrypoint", () => {

const contractAddress = Address.fromBech32(outcome.contracts[0].address);

const executeTransaction = await controller.createTransactionForExecute(sender, {
nonce: BigInt(sender.getNonceThenIncrement().valueOf()),
contract: contractAddress,
gasLimit: BigInt(10_000_000),
function: "add",
arguments: [7],
});
const executeTransaction = await controller.createTransactionForExecute(
sender,
BigInt(sender.getNonceThenIncrement().valueOf()),
{
contract: contractAddress,
gasLimit: BigInt(10_000_000),
function: "add",
arguments: [7],
},
);

const txHashExecute = await entrypoint.sendTransaction(executeTransaction);
await entrypoint.awaitCompletedTransaction(txHashExecute);
Expand All @@ -77,28 +86,30 @@ describe("TestEntrypoint", () => {

it("create relayed transaction", async function () {
const transferController = entrypoint.createTransfersController();
const sender = Account.fromPem(alicePem.pemFileText);
const sender = Account.newFromPem(alicePem.pemFileText);
sender.nonce = 77777;

const relayer = Account.fromPem(bobPem.pemFileText);
const relayer = Account.newFromPem(bobPem.pemFileText);
relayer.nonce = 7;

const transaction = await transferController.createTransactionForTransfer(sender, {
nonce: BigInt(sender.getNonceThenIncrement().valueOf()),
receiver: sender.address,
data: Buffer.from("hello"),
});
const transaction = await transferController.createTransactionForTransfer(
sender,
BigInt(sender.getNonceThenIncrement().valueOf()),
{
receiver: sender.address,
data: Buffer.from("hello"),
},
);
const innerTransactionGasLimit = transaction.gasLimit;
transaction.gasLimit = BigInt(0);
transaction.signature = await sender.sign(txComputer.computeBytesForSigning(transaction));

const relayedController = entrypoint.createRelayedController();
const relayedTransaction = relayedController.createRelayedV2Transaction(
relayer,
BigInt(relayer.getNonceThenIncrement().valueOf()),
transaction,
const relayedTransaction = relayedController.createRelayedV2Transaction(relayer, {
nonce: BigInt(relayer.getNonceThenIncrement().valueOf()),
innerTransaction: transaction,
innerTransactionGasLimit,
);
});

assert.equal((await relayedTransaction).chainID, "D");
});
Expand Down
18 changes: 9 additions & 9 deletions src/entrypoints/entrypoints.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { AbiRegistry } from "../abi";
import { AccountController } from "../accountManagment";
import { IAccount } from "../accounts/interfaces";
import { Address } from "../address";
import { AccountController } from "../controllers/accountController";
import { DelegationController } from "../controllers/delegationController";
import { TokenManagementController } from "../controllers/tokenManagementController";
import { DelegationController } from "../delegation";
import { ErrInvalidNetworkProviderKind } from "../errors";
import { Message, MessageComputer } from "../message";
import { ApiNetworkProvider, ProxyNetworkProvider, TransactionOnNetwork } from "../networkProviders";
import { RelayedController } from "../relayed/relayedController";
import { SmartContractController } from "../smartContracts/smartContractController";
import { TokenManagementController } from "../tokenManagement";
import { Transaction } from "../transaction";
import { TransactionComputer } from "../transactionComputer";
import { TransactionWatcher } from "../transactionWatcher";
Expand Down Expand Up @@ -86,27 +86,27 @@ class NetworkEntrypoint {
}

createDelegationController(): DelegationController {
return new DelegationController(this.chainId, this.networkProvider);
return new DelegationController({ chainID: this.chainId, networkProvider: this.networkProvider });
}

createAccountController(): AccountController {
return new AccountController(this.chainId);
return new AccountController({ chainID: this.chainId });
}

createRelayedController(): RelayedController {
return new RelayedController(this.chainId);
return new RelayedController({ chainID: this.chainId });
}

createSmartContractController(abi?: AbiRegistry): SmartContractController {
return new SmartContractController(this.chainId, this.networkProvider, abi);
return new SmartContractController({ chainID: this.chainId, networkProvider: this.networkProvider, abi });
}

createTokenManagementController(): TokenManagementController {
return new TokenManagementController(this.chainId, this.networkProvider);
return new TokenManagementController({ chainID: this.chainId, networkProvider: this.networkProvider });
}

createTransfersController(): TransfersController {
return new TransfersController(this.chainId);
return new TransfersController({ chainID: this.chainId });
}
}

Expand Down
Loading

0 comments on commit f1c9b9c

Please sign in to comment.