Delay in getting the updated result from contract

I’m testing homomorphic operations and I’m having an issue while communicating the contract.

I’ve observed that there is a delay getting the updated value from the contract. For example I have script that subtracts two numbers 5 and 3. I have to run the script twice to get the correct answer which is 2.
If I change the values to 8 and 2, I get the answer 2 and I have to run it again to get the correct answer which is 6.

Can anyone guide me on how to fix this, if possible. I’ve attached portions of my code for reference. Thank you


const interact = async () => {
// Initialize contract with ethers
const contract = new Contract(CONTRACT_ADDRESS, abi, signer);

// Get instance to encrypt amount parameter
const instance = await getInstance();
await instance.setSignature(CONTRACT_ADDRESS, ethers.Signature)
const generatedToken = instance.generatePublicKey({
verifyingContract: CONTRACT_ADDRESS,

await contract.setNumbers(instance.encrypt32(10), instance.encrypt32(5));
await contract.subtractNumbers();
const result = await contract.reencryptSubtraction(generatedToken.publicKey);
const decResult = instance.decrypt(CONTRACT_ADDRESS, result);


.then(successful => {console.log(“success”); process.exit(0); })
.catch(wrong => {console.log(“something went wrong”, wrong); process.exit(0); });


function setNumbers(bytes calldata first, bytes calldata second) public {
    firstNum = TFHE.asEuint32(first);
    secondNum = TFHE.asEuint32(second);

function subtractNumbers() public {
    result = TFHE.sub(firstNum, secondNum);

function reencryptSubtraction(bytes32 publicKey) public view returns (bytes memory) {
    return TFHE.reencrypt(result, publicKey, 0);


In your current implementation, the execution does not wait for the completion of the transaction. The calls you’ve made only ensure that the transactions are dispatched to the network but do not wait for their confirmation:

await contract.setNumbers(instance.encrypt32(10), instance.encrypt32(5));
await contract.subtractNumbers();

The use of await in this context confirms that the transactions are sent for processing. To ensure you wait until the transactions are fully executed and confirmed, you should modify your code as follows:

const transaction1 = await contract.setNumbers(instance.encrypt32(10), instance.encrypt32(5));
const transaction2 = await contract.subtractNumbers();
await transaction1.wait();
await transaction2.wait();

This two constants get the transactions and employs the wait() method on them. This method pauses the execution until the blockchain confirms the transactions. Then, you can call reencrypt methods and see the updated values.

1 Like