@drift-labs/common
Version:
Common functions for Drift
83 lines • 3.75 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletConnectionState = exports.ConnectionStates = exports.ConnectionStateSteps = void 0;
var ConnectionStateSteps;
(function (ConnectionStateSteps) {
ConnectionStateSteps[ConnectionStateSteps["NotConnected"] = 0] = "NotConnected";
ConnectionStateSteps[ConnectionStateSteps["AdapterConnected"] = 1] = "AdapterConnected";
ConnectionStateSteps[ConnectionStateSteps["ClientConnected"] = 2] = "ClientConnected";
ConnectionStateSteps[ConnectionStateSteps["BalanceLoaded"] = 4] = "BalanceLoaded";
ConnectionStateSteps[ConnectionStateSteps["SubaccountsSubscribed"] = 8] = "SubaccountsSubscribed";
})(ConnectionStateSteps || (exports.ConnectionStateSteps = ConnectionStateSteps = {}));
var ConnectionStates;
(function (ConnectionStates) {
ConnectionStates[ConnectionStates["NotConnected"] = 0] = "NotConnected";
ConnectionStates[ConnectionStates["AdapterConnected"] = 1] = "AdapterConnected";
ConnectionStates[ConnectionStates["ClientConnected"] = 2] = "ClientConnected";
ConnectionStates[ConnectionStates["BalanceLoaded"] = 4] = "BalanceLoaded";
ConnectionStates[ConnectionStates["SubaccountsSubscribed"] = 8] = "SubaccountsSubscribed";
ConnectionStates[ConnectionStates["FullyConnected"] = 15] = "FullyConnected";
})(ConnectionStates || (exports.ConnectionStates = ConnectionStates = {}));
class WalletConnectionState {
constructor(authority) {
this.state = ConnectionStates.NotConnected;
this.authority = authority;
}
is(stateQuery) {
switch (stateQuery) {
case 'NotConnected':
return this.state === ConnectionStates.NotConnected;
case 'AdapterConnected':
return ((this.state & ConnectionStates.AdapterConnected) ===
ConnectionStates.AdapterConnected);
case 'ClientConnected':
return ((this.state & ConnectionStates.ClientConnected) ===
ConnectionStates.ClientConnected);
case 'BalanceLoaded':
return ((this.state & ConnectionStates.BalanceLoaded) ===
ConnectionStates.BalanceLoaded);
case 'FullyConnected':
return ((this.state & ConnectionStates.FullyConnected) ===
ConnectionStates.FullyConnected);
case 'SubaccountsSubscribed':
return ((this.state & ConnectionStates.SubaccountsSubscribed) ===
ConnectionStates.SubaccountsSubscribed);
default: {
// Throw a typescript error if we have an unhandled case
const nothing = stateQuery;
return nothing;
}
}
}
update(updateStep, authorityGate) {
// Check that authorities match before updating state
if (authorityGate && !this.authority.equals(authorityGate)) {
return;
}
if (updateStep === 'NotConnected') {
this.state = ConnectionStates.NotConnected;
return;
}
this.state = this.state | ConnectionStateSteps[updateStep];
}
get NotConnected() {
return this.is('NotConnected');
}
get AdapterConnected() {
return this.is('AdapterConnected');
}
get ClientConnected() {
return this.is('ClientConnected');
}
get BalanceLoaded() {
return this.is('BalanceLoaded');
}
get FullyConnected() {
return this.is('FullyConnected');
}
get SubaccountsSubscribed() {
return this.is('SubaccountsSubscribed');
}
}
exports.WalletConnectionState = WalletConnectionState;
//# sourceMappingURL=WalletConnectionState.js.map