1

I have been facing issues with the use of u128.add(a, b) function. The two u128 values do not get added and I am afraid I am doing something wrong. I have checked LEARN-NEAR github page for sample projects and even changed my code to follow the patterns used, however the values don't get added.

signWithFunds(amount: u128): bool {
  assert(context.sender, "Petition must be signed by an account");
  assert(this.isFunded, `not a funded petition`);
  assert(u128.ge(amount, this.minFundAmount), 
  `amount provided is less than minimum funding amount: at least ${asNEAR(this.minFundAmount)} required to sign this petition`);
  const currentTotal = this.funding;
  this.funding = u128.add(amount, currentTotal); 
  this.signature.push(context.sender);
  return true;
}  

model.ts

enter image description here

main.ts

enter image description here

aspect test file

enter image description here

test result showing unexpected behaviour

enter image description here

John
  • 10,165
  • 5
  • 55
  • 71

2 Answers2

1

From the images, it looks like the test aren't receiving the expected values. The test receives 0, but expected some other values. I don't think there's anything wrong with the u128add function in itself.

From the test, you are calling a function that relies on Context's deposit, I think you need to add that to your test *(it("should sign a funded ...."), as well:

VMContext.setAttached_deposit(someDeposit)

Second, signWithFunds is relies on this.founding as well, which I believe is the funding in the petition itself. Maybe petitions[0] in your test isn't the newly created petition? We need to look at the beforeEach function to make sure, because otherwise, you are adding a new petition to the array, but you're referencing an older one.

John
  • 10,165
  • 5
  • 55
  • 71
1

I discovered that the 7th line of signWithFunds should have been this.funding.set(u128.add(amount, currentTotal));