Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Optional time supplier #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TuxedoFish
Copy link

Background

When developing locally my clock gets out of sync pretty fast and it was getting unwieldly to develop. I forked and used this locally to get around my problem. I thought it might be of use to other people as well.

local_development_clocks

Solution

Add a TimeSupplier class which can be passed a custom long supplier which can be used to adjust the time.

Potential usage

public class BinanceRestAdapter implements VenueAdapter {
    private final SpotClient client;

    public BinanceRestAdapter(String baseUrl, String apiKey, String privateKey, ObjectMapper objectMapper, List<String> tradingCoins) {
        client = new SpotClientImpl(apiKey, privateKey, baseUrl);
        client.setTimeSupplier(SysTime.INSTANCE::currentTimeMillis);
        syncTime();
    }

    ...

    private void syncTime() {
        String timeResult = client.createMarket().time();
        Long serverTimerTime = Long.valueOf(timeResult.substring(14, timeResult.length() - 1));
        Long clientTime = System.currentTimeMillis();
        long delta = clientTime - serverTimerTime;
        SysTime.INSTANCE.setDelta(-delta);
        Long updatedTime = SysTime.INSTANCE.currentTimeMillis();
        LOG.info(String.format("Updating @ local time %s | external time %s (%s ms) | new time delta = %s", clientTime, serverTimerTime, delta, updatedTime - serverTimerTime));
    }
}

With a class something like this

public class SysTime {
    public static SysTime INSTANCE = new SysTime();
    private long delta;

    public long currentTimeMillis() {
        return System.currentTimeMillis() + delta;
    }

    public void setDelta(long delta) {
        this.delta = delta;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant