We can take the approach used in the ENS script: https://gist.github.com/feliam/ec8b9aa102055948524838ffe0a09769