Skip to content

Provable.if in runtimeMethod does not work #26

@dloghin

Description

@dloghin

We discovered this issue during the ETHGlobal Singapore Hackathon.

Suppose we want to update a balance based on a certain amount, such that if amount > balance we top up the balance (balance += amount - balance), else we withdraw (balance -= balance - amount). We do this with provable if:

    const condition = amount.greaterThan(currBalance);
    const diff = Provable.if(
      condition,
      Balance,
      amount.sub(currBalance),
      currBalance.sub(amount)
    );
    const to = Provable.if(
      condition,
      PublicKey,
      address,
      addressPool
    );
    const from = Provable.if(
      condition,
      PublicKey,
      addressPool,
      address
    );
    const d = UInt64.Unsafe.fromField(diff.value);
    await this.transfer(tokenId, from, to, d);

(see balances.ts in my fork)

The test fails either when amount > balance or amount < balance. It only passes when amount == balance (see the test at line 50 of balances.test.ts in my fork).

starter-kit commit: ed6ced5
node version: v18.20.4
pnpm version: 9.8.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions