How to change domain without losing ranking?

Use the change of address tool from Google Webmasters. AFAIK it won’t affect SEO if all old website URLs are smoothly redirected to the new website.

Do I lose credit for links when I redirect to new URLs?
No, 301 or 302 redirects do not cause a loss in PageRank.