-
Notifications
You must be signed in to change notification settings - Fork 37
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
Update tests to use account.signTransaction #571
Merged
danielailie
merged 7 commits into
feat/next
from
TOOL-451-use-account-sign-transaction-in-tests
Feb 3, 2025
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
94d246c
Update tests to use account.signTransaction
danielailie ff2520e
Fix tests
danielailie 206d6ca
Fix integration tests
danielailie fe68e65
Fix integration tests
danielailie 4fa62b5
Fix integration tests
danielailie 16999cb
Fix unit test
danielailie f69cd4b
Fix smart contract result integration test
danielailie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { assert } from "chai"; | ||
import { promises } from "fs"; | ||
import { Account } from "../accounts"; | ||
import { Transaction } from "../core/transaction"; | ||
import { TransactionComputer } from "../core/transactionComputer"; | ||
import { TransactionsFactoryConfig } from "../core/transactionsFactoryConfig"; | ||
import { TransactionWatcher } from "../core/transactionWatcher"; | ||
import { | ||
SmartContractController, | ||
SmartContractTransactionsFactory, | ||
SmartContractTransactionsOutcomeParser, | ||
} from "../smartContracts"; | ||
import { loadAbiRegistry, loadTestWallets, prepareDeployment, TestWallet } from "../testutils"; | ||
import { loadAbiRegistry, prepareDeployment } from "../testutils"; | ||
import { createLocalnetProvider } from "../testutils/networkProviders"; | ||
import { getTestWalletsPath } from "../testutils/utils"; | ||
import { Interaction } from "./interaction"; | ||
import { SmartContract } from "./smartContract"; | ||
import { ManagedDecimalValue } from "./typesystem"; | ||
|
||
describe("test smart contract interactor", function () { | ||
let provider = createLocalnetProvider(); | ||
let alice: TestWallet; | ||
const transactionComputer = new TransactionComputer(); | ||
let alice: Account; | ||
|
||
before(async function () { | ||
({ alice } = await loadTestWallets()); | ||
alice = await Account.newFromPem(`${getTestWalletsPath()}/alice.pem`); | ||
}); | ||
|
||
it("should interact with 'answer' (local testnet) using the SmartContractTransactionsFactory", async function () { | ||
|
@@ -31,7 +30,6 @@ describe("test smart contract interactor", function () { | |
let abiRegistry = await loadAbiRegistry("src/testdata/answer.abi.json"); | ||
|
||
let network = await provider.getNetworkConfig(); | ||
await alice.sync(provider); | ||
|
||
const config = new TransactionsFactoryConfig({ chainID: network.chainID }); | ||
const factory = new SmartContractTransactionsFactory({ | ||
|
@@ -40,19 +38,17 @@ describe("test smart contract interactor", function () { | |
}); | ||
|
||
const bytecode = await promises.readFile("src/testdata/answer.wasm"); | ||
alice.nonce = (await provider.getAccount(alice.address)).nonce; | ||
|
||
const deployTransaction = factory.createTransactionForDeploy(alice.address, { | ||
bytecode: bytecode, | ||
gasLimit: 3000000n, | ||
}); | ||
deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
deployTransaction.nonce = alice.getNonceThenIncrement(); | ||
|
||
deployTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), | ||
); | ||
deployTransaction.signature = alice.signTransaction(deployTransaction); | ||
|
||
const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); | ||
alice.account.incrementNonce(); | ||
const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); | ||
|
||
const transactionCompletionAwaiter = new TransactionWatcher({ | ||
getTransaction: async (hash: string) => { | ||
|
@@ -89,12 +85,8 @@ describe("test smart contract interactor", function () { | |
function: "getUltimateAnswer", | ||
gasLimit: 3000000n, | ||
}); | ||
transaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
transaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(transaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
transaction.nonce = alice.getNonceThenIncrement(); | ||
transaction.signature = alice.signTransaction(transaction); | ||
|
||
await provider.sendTransaction(transaction); | ||
|
||
|
@@ -104,12 +96,8 @@ describe("test smart contract interactor", function () { | |
function: "getUltimateAnswer", | ||
gasLimit: 3000000n, | ||
}); | ||
transaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
transaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(transaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
transaction.nonce = alice.getNonceThenIncrement(); | ||
transaction.signature = alice.signTransaction(transaction); | ||
|
||
const executeTxHash = await provider.sendTransaction(transaction); | ||
transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(executeTxHash); | ||
|
@@ -132,8 +120,7 @@ describe("test smart contract interactor", function () { | |
}); | ||
|
||
let network = await provider.getNetworkConfig(); | ||
await alice.sync(provider); | ||
|
||
alice.nonce = (await provider.getAccount(alice.address)).nonce; | ||
// Deploy the contract | ||
let deployTransaction = await prepareDeployment({ | ||
contract: contract, | ||
|
@@ -159,7 +146,7 @@ describe("test smart contract interactor", function () { | |
// returnEgld() | ||
let returnEgldTransaction = returnEgldInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.useThenIncrementNonceOf(alice) | ||
.buildTransaction(); | ||
|
||
let additionInteraction = <Interaction>contract.methods | ||
|
@@ -172,7 +159,7 @@ describe("test smart contract interactor", function () { | |
// addition() | ||
let additionTransaction = additionInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.useThenIncrementNonceOf(alice) | ||
.buildTransaction(); | ||
|
||
// log | ||
|
@@ -186,7 +173,7 @@ describe("test smart contract interactor", function () { | |
// mdLn() | ||
let mdLnTransaction = mdLnInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.useThenIncrementNonceOf(alice) | ||
.buildTransaction(); | ||
|
||
let additionVarInteraction = <Interaction>contract.methods | ||
|
@@ -202,7 +189,7 @@ describe("test smart contract interactor", function () { | |
// addition() | ||
let additionVarTransaction = additionVarInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.useThenIncrementNonceOf(alice) | ||
.buildTransaction(); | ||
|
||
let lnVarInteraction = <Interaction>contract.methods | ||
|
@@ -215,7 +202,7 @@ describe("test smart contract interactor", function () { | |
// managed_decimal_ln_var() | ||
let lnVarTransaction = lnVarInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.useThenIncrementNonceOf(alice) | ||
.buildTransaction(); | ||
|
||
// returnEgld() | ||
|
@@ -266,7 +253,6 @@ describe("test smart contract interactor", function () { | |
let abiRegistry = await loadAbiRegistry("src/testdata/counter.abi.json"); | ||
|
||
let network = await provider.getNetworkConfig(); | ||
await alice.sync(provider); | ||
|
||
const config = new TransactionsFactoryConfig({ chainID: network.chainID }); | ||
const factory = new SmartContractTransactionsFactory({ | ||
|
@@ -276,19 +262,17 @@ describe("test smart contract interactor", function () { | |
const parser = new SmartContractTransactionsOutcomeParser({ abi: abiRegistry }); | ||
|
||
const bytecode = await promises.readFile("src/testdata/counter.wasm"); | ||
alice.nonce = (await provider.getAccount(alice.address)).nonce; | ||
|
||
const deployTransaction = factory.createTransactionForDeploy(alice.address, { | ||
bytecode: bytecode, | ||
gasLimit: 3000000n, | ||
}); | ||
deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
deployTransaction.nonce = alice.getNonceThenIncrement(); | ||
|
||
deployTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), | ||
); | ||
deployTransaction.signature = alice.signTransaction(deployTransaction); | ||
|
||
const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); | ||
alice.account.incrementNonce(); | ||
const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); | ||
|
||
const transactionCompletionAwaiter = new TransactionWatcher({ | ||
getTransaction: async (hash: string) => { | ||
|
@@ -312,13 +296,9 @@ describe("test smart contract interactor", function () { | |
function: "increment", | ||
gasLimit: 3000000n, | ||
}); | ||
incrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
|
||
incrementTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(incrementTransaction)), | ||
); | ||
incrementTransaction.nonce = alice.getNonceThenIncrement(); | ||
|
||
alice.account.incrementNonce(); | ||
incrementTransaction.signature = alice.signTransaction(incrementTransaction); | ||
|
||
// Query "get()" | ||
const query = queryController.createQuery({ | ||
|
@@ -341,19 +321,13 @@ describe("test smart contract interactor", function () { | |
function: "decrement", | ||
gasLimit: 3000000n, | ||
}); | ||
decrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
decrementTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(decrementTransaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
decrementTransaction.nonce = alice.getNonceThenIncrement(); | ||
decrementTransaction.signature = alice.signTransaction(decrementTransaction); | ||
|
||
await provider.sendTransaction(decrementTransaction); | ||
|
||
decrementTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
decrementTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(decrementTransaction)), | ||
); | ||
decrementTransaction.nonce = alice.nonce; | ||
decrementTransaction.signature = alice.signTransaction(decrementTransaction); | ||
|
||
const decrementTxHash = await provider.sendTransaction(decrementTransaction); | ||
transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(decrementTxHash); | ||
|
@@ -365,9 +339,9 @@ describe("test smart contract interactor", function () { | |
|
||
let abiRegistry = await loadAbiRegistry("src/testdata/lottery-esdt.abi.json"); | ||
let parser = new SmartContractTransactionsOutcomeParser({ abi: abiRegistry }); | ||
alice.nonce = (await provider.getAccount(alice.address)).nonce; | ||
|
||
let network = await provider.getNetworkConfig(); | ||
await alice.sync(provider); | ||
|
||
const config = new TransactionsFactoryConfig({ chainID: network.chainID }); | ||
const factory = new SmartContractTransactionsFactory({ | ||
|
@@ -382,14 +356,11 @@ describe("test smart contract interactor", function () { | |
bytecode: bytecode, | ||
gasLimit: 100000000n, | ||
}); | ||
deployTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
deployTransaction.nonce = alice.getNonceThenIncrement(); | ||
|
||
deployTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(deployTransaction)), | ||
); | ||
deployTransaction.signature = alice.signTransaction(deployTransaction); | ||
|
||
const contractAddress = SmartContract.computeAddress(alice.address, alice.account.nonce); | ||
alice.account.incrementNonce(); | ||
const contractAddress = SmartContract.computeAddress(alice.address, alice.nonce); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment as above |
||
|
||
const transactionCompletionAwaiter = new TransactionWatcher({ | ||
getTransaction: async (hash: string) => { | ||
|
@@ -409,12 +380,8 @@ describe("test smart contract interactor", function () { | |
arguments: ["lucky", "EGLD", 1, null, null, 1, null, null], | ||
gasLimit: 30000000n, | ||
}); | ||
startTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
startTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(startTransaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
startTransaction.nonce = alice.getNonceThenIncrement(); | ||
startTransaction.signature = alice.signTransaction(startTransaction); | ||
|
||
const startTxHash = await provider.sendTransaction(startTransaction); | ||
transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(startTxHash); | ||
|
@@ -429,12 +396,8 @@ describe("test smart contract interactor", function () { | |
arguments: ["lucky"], | ||
gasLimit: 5000000n, | ||
}); | ||
lotteryStatusTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
lotteryStatusTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(lotteryStatusTransaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
lotteryStatusTransaction.nonce = alice.getNonceThenIncrement(); | ||
lotteryStatusTransaction.signature = alice.signTransaction(lotteryStatusTransaction); | ||
|
||
const statusTxHash = await provider.sendTransaction(lotteryStatusTransaction); | ||
transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(statusTxHash); | ||
|
@@ -450,12 +413,8 @@ describe("test smart contract interactor", function () { | |
arguments: ["lucky"], | ||
gasLimit: 5000000n, | ||
}); | ||
lotteryInfoTransaction.nonce = BigInt(alice.account.nonce.valueOf()); | ||
lotteryInfoTransaction.signature = await alice.signer.sign( | ||
Buffer.from(transactionComputer.computeBytesForSigning(lotteryInfoTransaction)), | ||
); | ||
|
||
alice.account.incrementNonce(); | ||
lotteryInfoTransaction.nonce = alice.getNonceThenIncrement(); | ||
lotteryInfoTransaction.signature = alice.signTransaction(lotteryInfoTransaction); | ||
|
||
const infoTxHash = await provider.sendTransaction(lotteryInfoTransaction); | ||
transactionOnNetwork = await transactionCompletionAwaiter.awaitCompleted(infoTxHash); | ||
|
@@ -477,10 +436,10 @@ describe("test smart contract interactor", function () { | |
}); | ||
}); | ||
|
||
async function signTransaction(options: { transaction: Transaction; wallet: TestWallet }) { | ||
async function signTransaction(options: { transaction: Transaction; wallet: Account }) { | ||
const transaction = options.transaction; | ||
const wallet = options.wallet; | ||
|
||
transaction.signature = await wallet.signer.sign(transactionComputer.computeBytesForSigning(transaction)); | ||
transaction.signature = wallet.signTransaction(transaction); | ||
} | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you use
getNonceThenIncrement()
and then try to compute the contract's address usingalice.nonce
you'll have a higher nonce than the one on the transaction, thus computing an invalid address.