stellar / soroban-examples

Example Soroban Contracts
Apache License 2.0
65 stars 68 forks source link

Why are signed integers used in the API for the token example? #209

Closed willemneal closed 1 year ago

willemneal commented 1 year ago

For example here: https://github.com/stellar/soroban-examples/blob/622ce394bc1a6f3b40aa539a2dca58bbf7d5543e/token/src/contract.rs#L18

There is this function to ensure that it's an error if a negative number is sent:

https://github.com/stellar/soroban-examples/blob/622ce394bc1a6f3b40aa539a2dca58bbf7d5543e/token/src/contract.rs#L51

However, if it's a u128 then a negative number cannot be encoded into XDR. Is there a reason not to make this change?

leighmcculloch commented 1 year ago

There was some discussion on why i128 vs u128 in Discord in this thread: https://discord.com/channels/897514728459468821/1047281257312161832

The main reason is that amounts aren't always positive. Even if they aren't neg in the token contract, someone might be passing amounts to a token contract and another contract, and converting to between integer types introduces new failure cases (e.g. overflow) that aren't trivial to deal with safely and that folks often ignore.

There are example cases in the Discord thread.