It appears that there are many way to traverse Bitcoin addresses. I am looking best practice, and I am also looking for the documented logic of how common wallets do it so I can replicate their behaviour.
For example, Trezor creates the first address at this location: m/49'/0'/0'/0/0, and then the next address will be m/49'/0'/0'/0/1, and so on. So, it appears that it would be easy to just use an iterator over the last digit.
But, when you spend from m/49'/0'/0'/0/0, it moves the leftover to m/49'/0'/0'/1/0 . So, suddenly, I not only have to iterate over the last digit, I also have to iterate over the penultimate digit. I could have said "second last", but I said penultimate because it sounds more intelligent.
Is there standard practice for traversing addresses? Something like, keep traversing over addresses until you hit an address with no transactions? Or possibly say three addresses in a row with no transactions? Or is this really the wild west and we just do whatever we feel like?