Due to their short wavelength, X-rays can in principle be focused down to a few nanometres and below. At the same time, it is this short wavelength that puts stringent requirements on X-ray optics and their metrology. Both are limited by today's technology. In this work, we present accurate at wavelength measurements of residual aberrations of a refractive X-ray lens using ptychography to manufacture a corrective phase plate. Together with the fitted phase plate the optics shows diffraction-limited performance, generating a nearly Gaussian beam profile with a Strehl ratio above 0.8. This scheme can be applied to any other focusing optics, thus solving the X-ray optical problem at synchrotron radiation sources and X-ray free-electron lasers.