Market Purchase Algorithm
Important. Each user has just a single import address per currency/token/asset, e.g. a single wallet for each supported currency. There is nothing per-invoice/per-purchase, like 'purchase addresses'.
The system provides users with personal wallets distinct per each currency/token. This is how they spend and receive funds in the system.
For each cryptocurrency, the system provides user with a wallet, which has its own unique import address. Therefore, the user will have as many import addresses as many cryptocurrencies are in use.
Only one wallet per currency is created for a user, and any wallet operates with just a single cryptocurrency.
The wallet has an import address and can receive incoming transfers any time.
The funds imported to the wallet can be used to purchase tokens and export them to external addresses (with reasonable commission implied by the net costs of the operation).
Important: once again, there is no such thing as a "purchase address".
All purchases are performed from the user’s wallet bound to the corresponding cryptocurrency/token/asset.
Important: never try to transfer funds in currency not matching the currency of the wallet. It may not be recoverable. E.g. do not try to send some ERC20 token to the address of the wallet for a different ERC20 token, or for ETH currency.
When user makes a purchase, the purchase request (order) is created. The request is the instruction to the system to buy some token as soon as corresponding wallet will have enough funds. Orders have expiration time.
The system checks that the purchase request could be processed, e.g. the corresponding wallet has enough funds, and performs purchases in order of appearance_, e.g. oldest first.
Until the oldest purchase request is not performed or expired, other requests will not be processed.
As soon as older request is expired or performed, the system will try to execute the next oldest one.
In spite of this:
If the user wallet contains enough funds when the purchase is created, the purchase is executed immediately.
The user can tranfer funds to the import address (owned by the same user) any number of times in any amounts. These funds will not be lost.
The remaining funds in the wallet can be used for other purchases, or exported out (with commission). So, it is not a problem if you transfer less funds than your purchase price: you can either transfer more, or cancel the purchase and buy change the amount or the purchased asset. The alternative option is to export it out.
It is not a problem to occasionally transfer more funds that the purchase price. It is your wallet and the imported funds remain under your control.
In a simplified way, the process could be represented as: