@karn_lat/protocol-sdk 0.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/dist/__tests__/setup.d.ts +14 -0
- package/dist/__tests__/setup.d.ts.map +1 -0
- package/dist/__tests__/setup.js +44 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/clients/GovernorClient.d.ts +9 -0
- package/dist/clients/GovernorClient.d.ts.map +1 -0
- package/dist/clients/GovernorClient.js +18 -0
- package/dist/clients/GovernorClient.js.map +1 -0
- package/dist/clients/TreasuryClient.d.ts +9 -0
- package/dist/clients/TreasuryClient.d.ts.map +1 -0
- package/dist/clients/TreasuryClient.js +18 -0
- package/dist/clients/TreasuryClient.js.map +1 -0
- package/dist/clients/ValocracyClient.d.ts +13 -0
- package/dist/clients/ValocracyClient.d.ts.map +1 -0
- package/dist/clients/ValocracyClient.js +32 -0
- package/dist/clients/ValocracyClient.js.map +1 -0
- package/dist/clients/index.d.ts +4 -0
- package/dist/clients/index.d.ts.map +1 -0
- package/dist/clients/index.js +4 -0
- package/dist/clients/index.js.map +1 -0
- package/dist/generated/governor/src/index.d.ts +400 -0
- package/dist/generated/governor/src/index.d.ts.map +1 -0
- package/dist/generated/governor/src/index.js +63 -0
- package/dist/generated/governor/src/index.js.map +1 -0
- package/dist/generated/treasury/src/index.d.ts +474 -0
- package/dist/generated/treasury/src/index.d.ts.map +1 -0
- package/dist/generated/treasury/src/index.js +54 -0
- package/dist/generated/treasury/src/index.js.map +1 -0
- package/dist/generated/valocracy/src/index.d.ts +807 -0
- package/dist/generated/valocracy/src/index.d.ts.map +1 -0
- package/dist/generated/valocracy/src/index.js +114 -0
- package/dist/generated/valocracy/src/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/react/hooks/useGovernor.d.ts +24 -0
- package/dist/react/hooks/useGovernor.d.ts.map +1 -0
- package/dist/react/hooks/useGovernor.js +45 -0
- package/dist/react/hooks/useGovernor.js.map +1 -0
- package/dist/react/hooks/useMultiWallet.d.ts +35 -0
- package/dist/react/hooks/useMultiWallet.d.ts.map +1 -0
- package/dist/react/hooks/useMultiWallet.js +87 -0
- package/dist/react/hooks/useMultiWallet.js.map +1 -0
- package/dist/react/hooks/useTreasury.d.ts +14 -0
- package/dist/react/hooks/useTreasury.d.ts.map +1 -0
- package/dist/react/hooks/useTreasury.js +45 -0
- package/dist/react/hooks/useTreasury.js.map +1 -0
- package/dist/react/hooks/useValocracy.d.ts +16 -0
- package/dist/react/hooks/useValocracy.d.ts.map +1 -0
- package/dist/react/hooks/useValocracy.js +54 -0
- package/dist/react/hooks/useValocracy.js.map +1 -0
- package/dist/react/hooks/useWallet.d.ts +13 -0
- package/dist/react/hooks/useWallet.d.ts.map +1 -0
- package/dist/react/hooks/useWallet.js +51 -0
- package/dist/react/hooks/useWallet.js.map +1 -0
- package/dist/react/index.d.ts +7 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +7 -0
- package/dist/react/index.js.map +1 -0
- package/dist/react/providers/KarnProvider.d.ts +25 -0
- package/dist/react/providers/KarnProvider.d.ts.map +1 -0
- package/dist/react/providers/KarnProvider.js +25 -0
- package/dist/react/providers/KarnProvider.js.map +1 -0
- package/dist/utils/decay.d.ts +19 -0
- package/dist/utils/decay.d.ts.map +1 -0
- package/dist/utils/decay.js +33 -0
- package/dist/utils/decay.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +4 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/polling.d.ts +75 -0
- package/dist/utils/polling.d.ts.map +1 -0
- package/dist/utils/polling.js +104 -0
- package/dist/utils/polling.js.map +1 -0
- package/dist/utils/simulation.d.ts +67 -0
- package/dist/utils/simulation.d.ts.map +1 -0
- package/dist/utils/simulation.js +88 -0
- package/dist/utils/simulation.js.map +1 -0
- package/dist/wallet/WalletManager.d.ts +77 -0
- package/dist/wallet/WalletManager.d.ts.map +1 -0
- package/dist/wallet/WalletManager.js +268 -0
- package/dist/wallet/WalletManager.js.map +1 -0
- package/dist/wallet/adapters/AlbedoAdapter.d.ts +47 -0
- package/dist/wallet/adapters/AlbedoAdapter.d.ts.map +1 -0
- package/dist/wallet/adapters/AlbedoAdapter.js +84 -0
- package/dist/wallet/adapters/AlbedoAdapter.js.map +1 -0
- package/dist/wallet/adapters/FreighterAdapter.d.ts +42 -0
- package/dist/wallet/adapters/FreighterAdapter.d.ts.map +1 -0
- package/dist/wallet/adapters/FreighterAdapter.js +107 -0
- package/dist/wallet/adapters/FreighterAdapter.js.map +1 -0
- package/dist/wallet/adapters/LobstrAdapter.d.ts +34 -0
- package/dist/wallet/adapters/LobstrAdapter.d.ts.map +1 -0
- package/dist/wallet/adapters/LobstrAdapter.js +89 -0
- package/dist/wallet/adapters/LobstrAdapter.js.map +1 -0
- package/dist/wallet/adapters/RabetAdapter.d.ts +39 -0
- package/dist/wallet/adapters/RabetAdapter.d.ts.map +1 -0
- package/dist/wallet/adapters/RabetAdapter.js +104 -0
- package/dist/wallet/adapters/RabetAdapter.js.map +1 -0
- package/dist/wallet/adapters/xBullAdapter.d.ts +41 -0
- package/dist/wallet/adapters/xBullAdapter.d.ts.map +1 -0
- package/dist/wallet/adapters/xBullAdapter.js +72 -0
- package/dist/wallet/adapters/xBullAdapter.js.map +1 -0
- package/dist/wallet/index.d.ts +20 -0
- package/dist/wallet/index.d.ts.map +1 -0
- package/dist/wallet/index.js +23 -0
- package/dist/wallet/index.js.map +1 -0
- package/dist/wallet/types.d.ts +165 -0
- package/dist/wallet/types.d.ts.map +1 -0
- package/dist/wallet/types.js +50 -0
- package/dist/wallet/types.js.map +1 -0
- package/examples/basic-usage.ts +28 -0
- package/jest.config.js +37 -0
- package/package.json +58 -0
- package/src/__tests__/README.md +364 -0
- package/src/__tests__/setup.ts +57 -0
- package/src/__tests__/utils/decay.test.ts +331 -0
- package/src/__tests__/wallet/WalletManager.test.ts +410 -0
- package/src/clients/GovernorClient.ts +23 -0
- package/src/clients/TreasuryClient.ts +23 -0
- package/src/clients/ValocracyClient.ts +48 -0
- package/src/clients/index.ts +3 -0
- package/src/generated/governor/README.md +54 -0
- package/src/generated/governor/package.json +17 -0
- package/src/generated/governor/src/index.ts +428 -0
- package/src/generated/governor/tsconfig.json +98 -0
- package/src/generated/treasury/README.md +54 -0
- package/src/generated/treasury/package.json +17 -0
- package/src/generated/treasury/src/index.ts +495 -0
- package/src/generated/treasury/tsconfig.json +98 -0
- package/src/generated/valocracy/README.md +54 -0
- package/src/generated/valocracy/package.json +17 -0
- package/src/generated/valocracy/src/index.ts +831 -0
- package/src/generated/valocracy/tsconfig.json +98 -0
- package/src/index.ts +4 -0
- package/src/react/hooks/useGovernor.ts +69 -0
- package/src/react/hooks/useMultiWallet.ts +169 -0
- package/src/react/hooks/useTreasury.ts +57 -0
- package/src/react/hooks/useValocracy.ts +66 -0
- package/src/react/hooks/useWallet.ts +60 -0
- package/src/react/index.ts +6 -0
- package/src/react/providers/KarnProvider.tsx +63 -0
- package/src/utils/decay.ts +44 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/polling.ts +193 -0
- package/src/utils/simulation.ts +136 -0
- package/src/wallet/WalletManager.ts +360 -0
- package/src/wallet/adapters/AlbedoAdapter.ts +140 -0
- package/src/wallet/adapters/FreighterAdapter.ts +179 -0
- package/src/wallet/adapters/LobstrAdapter.ts +142 -0
- package/src/wallet/adapters/RabetAdapter.ts +162 -0
- package/src/wallet/adapters/xBullAdapter.ts +123 -0
- package/src/wallet/index.ts +37 -0
- package/src/wallet/types.ts +204 -0
- package/tsconfig.json +40 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export * from './providers/KarnProvider.js';
|
|
2
|
+
export * from './hooks/useValocracy.js';
|
|
3
|
+
export * from './hooks/useGovernor.js';
|
|
4
|
+
export * from './hooks/useTreasury.js';
|
|
5
|
+
export * from './hooks/useWallet.js';
|
|
6
|
+
export * from './hooks/useMultiWallet.js';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/react/index.ts"],"names":[],"mappings":"AAAA,cAAc,6BAA6B,CAAC;AAC5C,cAAc,yBAAyB,CAAC;AACxC,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,sBAAsB,CAAC;AACrC,cAAc,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { ValocracyClient, GovernorClient, TreasuryClient } from '../../clients';
|
|
3
|
+
export interface KarnConfig {
|
|
4
|
+
networkPassphrase: string;
|
|
5
|
+
rpcUrl: string;
|
|
6
|
+
contracts: {
|
|
7
|
+
valocracy: string;
|
|
8
|
+
governor: string;
|
|
9
|
+
treasury: string;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
interface KarnContextValue {
|
|
13
|
+
valocracy: ValocracyClient;
|
|
14
|
+
governor: GovernorClient;
|
|
15
|
+
treasury: TreasuryClient;
|
|
16
|
+
config: KarnConfig;
|
|
17
|
+
}
|
|
18
|
+
export declare const useKarn: () => KarnContextValue;
|
|
19
|
+
interface KarnProviderProps {
|
|
20
|
+
config: KarnConfig;
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
}
|
|
23
|
+
export declare const KarnProvider: React.FC<KarnProviderProps>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=KarnProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KarnProvider.d.ts","sourceRoot":"","sources":["../../../src/react/providers/KarnProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAsC,SAAS,EAAE,MAAM,OAAO,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEhF,MAAM,WAAW,UAAU;IACzB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE;QACT,SAAS,EAAE,MAAM,CAAC;QAClB,QAAQ,EAAE,MAAM,CAAC;QACjB,QAAQ,EAAE,MAAM,CAAC;KAClB,CAAC;CACH;AAED,UAAU,gBAAgB;IACxB,SAAS,EAAE,eAAe,CAAC;IAC3B,QAAQ,EAAE,cAAc,CAAC;IACzB,QAAQ,EAAE,cAAc,CAAC;IACzB,MAAM,EAAE,UAAU,CAAC;CACpB;AAID,eAAO,MAAM,OAAO,wBAMnB,CAAC;AAEF,UAAU,iBAAiB;IACzB,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,EAAE,SAAS,CAAC;CACrB;AAED,eAAO,MAAM,YAAY,EAAE,KAAK,CAAC,EAAE,CAAC,iBAAiB,CA2BpD,CAAC"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import React, { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
import { ValocracyClient, GovernorClient, TreasuryClient } from '../../clients';
|
|
3
|
+
const KarnContext = createContext(null);
|
|
4
|
+
export const useKarn = () => {
|
|
5
|
+
const context = useContext(KarnContext);
|
|
6
|
+
if (!context) {
|
|
7
|
+
throw new Error('useKarn must be used within a KarnProvider');
|
|
8
|
+
}
|
|
9
|
+
return context;
|
|
10
|
+
};
|
|
11
|
+
export const KarnProvider = ({ config, children }) => {
|
|
12
|
+
const clients = useMemo(() => {
|
|
13
|
+
return {
|
|
14
|
+
valocracy: new ValocracyClient(config.networkPassphrase, config.rpcUrl, config.contracts.valocracy),
|
|
15
|
+
governor: new GovernorClient(config.networkPassphrase, config.rpcUrl, config.contracts.governor),
|
|
16
|
+
treasury: new TreasuryClient(config.networkPassphrase, config.rpcUrl, config.contracts.treasury),
|
|
17
|
+
};
|
|
18
|
+
}, [config.networkPassphrase, config.rpcUrl, config.contracts.valocracy, config.contracts.governor, config.contracts.treasury]);
|
|
19
|
+
const value = {
|
|
20
|
+
...clients,
|
|
21
|
+
config,
|
|
22
|
+
};
|
|
23
|
+
return React.createElement(KarnContext.Provider, { value: value }, children);
|
|
24
|
+
};
|
|
25
|
+
//# sourceMappingURL=KarnProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"KarnProvider.js","sourceRoot":"","sources":["../../../src/react/providers/KarnProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAa,MAAM,OAAO,CAAC;AAC7E,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAmBhF,MAAM,WAAW,GAAG,aAAa,CAA0B,IAAI,CAAC,CAAC;AAEjE,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,EAAE;IAC1B,MAAM,OAAO,GAAG,UAAU,CAAC,WAAW,CAAC,CAAC;IACxC,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAChE,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC;AAOF,MAAM,CAAC,MAAM,YAAY,GAAgC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,EAAE,EAAE;IAChF,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE;QAC3B,OAAO;YACL,SAAS,EAAE,IAAI,eAAe,CAC5B,MAAM,CAAC,iBAAiB,EACxB,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,SAAS,CAAC,SAAS,CAC3B;YACD,QAAQ,EAAE,IAAI,cAAc,CAC1B,MAAM,CAAC,iBAAiB,EACxB,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,SAAS,CAAC,QAAQ,CAC1B;YACD,QAAQ,EAAE,IAAI,cAAc,CAC1B,MAAM,CAAC,iBAAiB,EACxB,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,SAAS,CAAC,QAAQ,CAC1B;SACF,CAAC;IACJ,CAAC,EAAE,CAAC,MAAM,CAAC,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEhI,MAAM,KAAK,GAAqB;QAC9B,GAAG,OAAO;QACV,MAAM;KACP,CAAC;IAEF,OAAO,oBAAC,WAAW,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAAG,QAAQ,CAAwB,CAAC;AAC/E,CAAC,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side Mana decay calculation
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Calculate current Mana based on level and last activity
|
|
6
|
+
*
|
|
7
|
+
* Formula: Mana = floor + bonus
|
|
8
|
+
* - floor = MEMBER_FLOOR (fixed constant, e.g. 5)
|
|
9
|
+
* - extra_level = level - floor
|
|
10
|
+
* - bonus = (extra_level * time_remaining) / VACANCY_PERIOD
|
|
11
|
+
*
|
|
12
|
+
* @param level Total level (including permanent)
|
|
13
|
+
* @param permanentLevel Permanent level (Founder badge)
|
|
14
|
+
* @param expiry Expiry timestamp (Unix seconds)
|
|
15
|
+
* @param currentTimestamp Current timestamp (Unix seconds)
|
|
16
|
+
* @returns Current Mana
|
|
17
|
+
*/
|
|
18
|
+
export declare function calculateMana(level: number, permanentLevel: number, expiry: number, currentTimestamp: number): number;
|
|
19
|
+
//# sourceMappingURL=decay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decay.d.ts","sourceRoot":"","sources":["../../src/utils/decay.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,EACb,cAAc,EAAE,MAAM,EACtB,MAAM,EAAE,MAAM,EACd,gBAAgB,EAAE,MAAM,GACvB,MAAM,CAiBR"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-side Mana decay calculation
|
|
3
|
+
*/
|
|
4
|
+
const MEMBER_FLOOR = 5;
|
|
5
|
+
const VACANCY_PERIOD = 15552000; // 180 days in seconds
|
|
6
|
+
/**
|
|
7
|
+
* Calculate current Mana based on level and last activity
|
|
8
|
+
*
|
|
9
|
+
* Formula: Mana = floor + bonus
|
|
10
|
+
* - floor = MEMBER_FLOOR (fixed constant, e.g. 5)
|
|
11
|
+
* - extra_level = level - floor
|
|
12
|
+
* - bonus = (extra_level * time_remaining) / VACANCY_PERIOD
|
|
13
|
+
*
|
|
14
|
+
* @param level Total level (including permanent)
|
|
15
|
+
* @param permanentLevel Permanent level (Founder badge)
|
|
16
|
+
* @param expiry Expiry timestamp (Unix seconds)
|
|
17
|
+
* @param currentTimestamp Current timestamp (Unix seconds)
|
|
18
|
+
* @returns Current Mana
|
|
19
|
+
*/
|
|
20
|
+
export function calculateMana(level, permanentLevel, expiry, currentTimestamp) {
|
|
21
|
+
// 1. Calculate decaying portion
|
|
22
|
+
const decayingLevel = Math.max(0, level - permanentLevel);
|
|
23
|
+
// 2. Calculate time remaining
|
|
24
|
+
const timeRemaining = Math.max(0, expiry - currentTimestamp);
|
|
25
|
+
// 3. Calculate bonus
|
|
26
|
+
// Use Math.floor to match integer arithmetic
|
|
27
|
+
// bonus = (DecayingLevel * TimeRemaining) / VacancyPeriod
|
|
28
|
+
const bonus = Math.floor((decayingLevel * timeRemaining) / VACANCY_PERIOD);
|
|
29
|
+
// 4. Default floor logic
|
|
30
|
+
// Contract: Mana = MemberFloor + bonus + PermanentLevel
|
|
31
|
+
return MEMBER_FLOOR + bonus + permanentLevel;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=decay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decay.js","sourceRoot":"","sources":["../../src/utils/decay.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,cAAc,GAAG,QAAQ,CAAC,CAAC,sBAAsB;AAEvD;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,aAAa,CAC3B,KAAa,EACb,cAAsB,EACtB,MAAc,EACd,gBAAwB;IAExB,gCAAgC;IAChC,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,GAAG,cAAc,CAAC,CAAC;IAG1D,8BAA8B;IAC9B,MAAM,aAAa,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,GAAG,gBAAgB,CAAC,CAAC;IAE7D,qBAAqB;IACrB,6CAA6C;IAC7C,0DAA0D;IAC1D,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,aAAa,GAAG,aAAa,CAAC,GAAG,cAAc,CAAC,CAAC;IAE3E,yBAAyB;IACzB,wDAAwD;IAExD,OAAO,YAAY,GAAG,KAAK,GAAG,cAAc,CAAC;AAC/C,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { rpc } from '@stellar/stellar-sdk';
|
|
2
|
+
export interface PollingOptions {
|
|
3
|
+
/**
|
|
4
|
+
* Maximum number of polling attempts
|
|
5
|
+
* @default 30
|
|
6
|
+
*/
|
|
7
|
+
maxAttempts?: number;
|
|
8
|
+
/**
|
|
9
|
+
* Interval between polling attempts in milliseconds
|
|
10
|
+
* @default 1000 (1 second)
|
|
11
|
+
*/
|
|
12
|
+
intervalMs?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to use exponential backoff
|
|
15
|
+
* @default true
|
|
16
|
+
*/
|
|
17
|
+
exponentialBackoff?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Maximum interval between attempts when using exponential backoff (ms)
|
|
20
|
+
* @default 10000 (10 seconds)
|
|
21
|
+
*/
|
|
22
|
+
maxIntervalMs?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Callback function called on each polling attempt
|
|
25
|
+
*/
|
|
26
|
+
onAttempt?: (attempt: number, status: string) => void;
|
|
27
|
+
}
|
|
28
|
+
export interface PollingResult<T> {
|
|
29
|
+
/**
|
|
30
|
+
* Whether the operation succeeded
|
|
31
|
+
*/
|
|
32
|
+
success: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* The final transaction response (if successful)
|
|
35
|
+
*/
|
|
36
|
+
response?: T;
|
|
37
|
+
/**
|
|
38
|
+
* Error message (if failed)
|
|
39
|
+
*/
|
|
40
|
+
error?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Number of attempts made
|
|
43
|
+
*/
|
|
44
|
+
attempts: number;
|
|
45
|
+
/**
|
|
46
|
+
* Total time elapsed in milliseconds
|
|
47
|
+
*/
|
|
48
|
+
elapsedMs: number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Poll for a transaction result with exponential backoff
|
|
52
|
+
*
|
|
53
|
+
* @param server - Stellar RPC server instance
|
|
54
|
+
* @param transactionHash - The transaction hash to poll for
|
|
55
|
+
* @param options - Polling configuration options
|
|
56
|
+
* @returns Promise resolving to polling result
|
|
57
|
+
*/
|
|
58
|
+
export declare function pollTransactionResult(server: rpc.Server, transactionHash: string, options?: PollingOptions): Promise<PollingResult<rpc.Api.GetTransactionResponse>>;
|
|
59
|
+
/**
|
|
60
|
+
* Wait for a specified duration
|
|
61
|
+
*
|
|
62
|
+
* @param ms - Duration to wait in milliseconds
|
|
63
|
+
* @returns Promise that resolves after the duration
|
|
64
|
+
*/
|
|
65
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Poll for multiple transactions in parallel
|
|
68
|
+
*
|
|
69
|
+
* @param server - Stellar RPC server instance
|
|
70
|
+
* @param transactionHashes - Array of transaction hashes to poll
|
|
71
|
+
* @param options - Polling configuration options
|
|
72
|
+
* @returns Promise resolving to array of polling results
|
|
73
|
+
*/
|
|
74
|
+
export declare function pollMultipleTransactions(server: rpc.Server, transactionHashes: string[], options?: PollingOptions): Promise<PollingResult<rpc.Api.GetTransactionResponse>[]>;
|
|
75
|
+
//# sourceMappingURL=polling.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polling.d.ts","sourceRoot":"","sources":["../../src/utils/polling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAE3C,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAE7B;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,IAAI,CAAC;CACvD;AAED,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,QAAQ,CAAC,EAAE,CAAC,CAAC;IAEb;;OAEG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IAEf;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAC;IAEjB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAC;CACnB;AAED;;;;;;;GAOG;AAEH,wBAAsB,qBAAqB,CACzC,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,eAAe,EAAE,MAAM,EACvB,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC,CAyFxD;AAED;;;;;GAKG;AAEH,wBAAgB,KAAK,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE/C;AAED;;;;;;;GAOG;AAEH,wBAAsB,wBAAwB,CAC5C,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,iBAAiB,EAAE,MAAM,EAAE,EAC3B,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,sBAAsB,CAAC,EAAE,CAAC,CAI1D"}
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { rpc } from '@stellar/stellar-sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Poll for a transaction result with exponential backoff
|
|
4
|
+
*
|
|
5
|
+
* @param server - Stellar RPC server instance
|
|
6
|
+
* @param transactionHash - The transaction hash to poll for
|
|
7
|
+
* @param options - Polling configuration options
|
|
8
|
+
* @returns Promise resolving to polling result
|
|
9
|
+
*/
|
|
10
|
+
export async function pollTransactionResult(server, transactionHash, options = {}) {
|
|
11
|
+
const { maxAttempts = 30, intervalMs = 1000, exponentialBackoff = true, maxIntervalMs = 10000, onAttempt, } = options;
|
|
12
|
+
const startTime = Date.now();
|
|
13
|
+
let attempts = 0;
|
|
14
|
+
let currentInterval = intervalMs;
|
|
15
|
+
while (attempts < maxAttempts) {
|
|
16
|
+
attempts++;
|
|
17
|
+
try {
|
|
18
|
+
const response = await server.getTransaction(transactionHash);
|
|
19
|
+
const status = response.status;
|
|
20
|
+
onAttempt?.(attempts, status);
|
|
21
|
+
// SUCCESS - Transaction confirmed
|
|
22
|
+
if (response.status === rpc.Api.GetTransactionStatus.SUCCESS) {
|
|
23
|
+
return {
|
|
24
|
+
success: true,
|
|
25
|
+
response,
|
|
26
|
+
attempts,
|
|
27
|
+
elapsedMs: Date.now() - startTime,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
// FAILED - Transaction failed permanently
|
|
31
|
+
if (response.status === rpc.Api.GetTransactionStatus.FAILED) {
|
|
32
|
+
return {
|
|
33
|
+
success: false,
|
|
34
|
+
error: `Transaction failed: ${JSON.stringify(response)}`,
|
|
35
|
+
attempts,
|
|
36
|
+
elapsedMs: Date.now() - startTime,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
// NOT_FOUND - Still pending, continue polling
|
|
40
|
+
if (response.status === rpc.Api.GetTransactionStatus.NOT_FOUND) {
|
|
41
|
+
// Wait before next attempt
|
|
42
|
+
if (attempts < maxAttempts) {
|
|
43
|
+
await sleep(currentInterval);
|
|
44
|
+
// Exponential backoff: double interval each time, up to max
|
|
45
|
+
if (exponentialBackoff) {
|
|
46
|
+
currentInterval = Math.min(currentInterval * 2, maxIntervalMs);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
// Unknown status (shouldn't reach here, but handle gracefully)
|
|
52
|
+
return {
|
|
53
|
+
success: false,
|
|
54
|
+
error: `Unknown transaction status: ${status}`,
|
|
55
|
+
attempts,
|
|
56
|
+
elapsedMs: Date.now() - startTime,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
// Network error or RPC error
|
|
61
|
+
if (attempts >= maxAttempts) {
|
|
62
|
+
return {
|
|
63
|
+
success: false,
|
|
64
|
+
error: err instanceof Error ? err.message : 'Unknown error during polling',
|
|
65
|
+
attempts,
|
|
66
|
+
elapsedMs: Date.now() - startTime,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Retry on error
|
|
70
|
+
await sleep(currentInterval);
|
|
71
|
+
if (exponentialBackoff) {
|
|
72
|
+
currentInterval = Math.min(currentInterval * 2, maxIntervalMs);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
// Max attempts reached
|
|
77
|
+
return {
|
|
78
|
+
success: false,
|
|
79
|
+
error: `Transaction polling timed out after ${attempts} attempts (${Date.now() - startTime}ms)`,
|
|
80
|
+
attempts,
|
|
81
|
+
elapsedMs: Date.now() - startTime,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Wait for a specified duration
|
|
86
|
+
*
|
|
87
|
+
* @param ms - Duration to wait in milliseconds
|
|
88
|
+
* @returns Promise that resolves after the duration
|
|
89
|
+
*/
|
|
90
|
+
export function sleep(ms) {
|
|
91
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Poll for multiple transactions in parallel
|
|
95
|
+
*
|
|
96
|
+
* @param server - Stellar RPC server instance
|
|
97
|
+
* @param transactionHashes - Array of transaction hashes to poll
|
|
98
|
+
* @param options - Polling configuration options
|
|
99
|
+
* @returns Promise resolving to array of polling results
|
|
100
|
+
*/
|
|
101
|
+
export async function pollMultipleTransactions(server, transactionHashes, options = {}) {
|
|
102
|
+
return Promise.all(transactionHashes.map((hash) => pollTransactionResult(server, hash, options)));
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=polling.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"polling.js","sourceRoot":"","sources":["../../src/utils/polling.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AA4D3C;;;;;;;GAOG;AAEH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,MAAkB,EAClB,eAAuB,EACvB,UAA0B,EAAE;IAE5B,MA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,KAAK,CAAC,EAAU;IAC9B,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC;AAC3D,CAAC;AAED;;;;;;;GAOG;AAEH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,MAAkB,EAClB,iBAA2B,EAC3B,UAA0B,EAAE;IAE5B,OAAO,OAAO,CAAC,GAAG,CAChB,iBAAiB,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC,CAC9E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { rpc } from '@stellar/stellar-sdk';
|
|
2
|
+
import type { AssembledTransaction } from '@stellar/stellar-sdk/contract';
|
|
3
|
+
export interface SimulationResult<T> {
|
|
4
|
+
/**
|
|
5
|
+
* Whether the simulation succeeded
|
|
6
|
+
*/
|
|
7
|
+
success: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* The decoded result value (if successful)
|
|
10
|
+
*/
|
|
11
|
+
result?: T;
|
|
12
|
+
/**
|
|
13
|
+
* Error message (if failed)
|
|
14
|
+
*/
|
|
15
|
+
error?: string;
|
|
16
|
+
/**
|
|
17
|
+
* Estimated resource fees in stroops
|
|
18
|
+
*/
|
|
19
|
+
fee?: number;
|
|
20
|
+
/**
|
|
21
|
+
* Raw simulation response from RPC
|
|
22
|
+
*/
|
|
23
|
+
raw?: rpc.Api.SimulateTransactionResponse;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Simulate a transaction and extract the result
|
|
27
|
+
*
|
|
28
|
+
* @param tx - AssembledTransaction to simulate
|
|
29
|
+
* @returns Promise resolving to simulation result
|
|
30
|
+
*/
|
|
31
|
+
export declare function simulateTransaction<T>(tx: AssembledTransaction<T>): Promise<SimulationResult<T>>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if a transaction needs restoration (contract data needs to be restored)
|
|
34
|
+
*
|
|
35
|
+
* @param tx - AssembledTransaction to check
|
|
36
|
+
* @returns Promise resolving to boolean indicating if restoration is needed
|
|
37
|
+
*/
|
|
38
|
+
export declare function needsRestoration<T>(tx: AssembledTransaction<T>): Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* Estimate the total fee for a transaction (base fee + resource fee)
|
|
41
|
+
*
|
|
42
|
+
* @param tx - AssembledTransaction to estimate
|
|
43
|
+
* @returns Promise resolving to estimated fee in stroops
|
|
44
|
+
*/
|
|
45
|
+
export declare function estimateFee<T>(tx: AssembledTransaction<T>): Promise<number>;
|
|
46
|
+
/**
|
|
47
|
+
* Simulate multiple transactions in parallel
|
|
48
|
+
*
|
|
49
|
+
* @param transactions - Array of AssembledTransactions to simulate
|
|
50
|
+
* @returns Promise resolving to array of simulation results
|
|
51
|
+
*/
|
|
52
|
+
export declare function simulateMultiple<T>(transactions: AssembledTransaction<T>[]): Promise<SimulationResult<T>[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Extract error details from a failed simulation
|
|
55
|
+
*
|
|
56
|
+
* @param simulation - The simulation response
|
|
57
|
+
* @returns Human-readable error message
|
|
58
|
+
*/
|
|
59
|
+
export declare function getSimulationError(simulation: rpc.Api.SimulateTransactionResponse): string;
|
|
60
|
+
/**
|
|
61
|
+
* Check if a simulation result indicates success
|
|
62
|
+
*
|
|
63
|
+
* @param simulation - The simulation response
|
|
64
|
+
* @returns True if simulation succeeded
|
|
65
|
+
*/
|
|
66
|
+
export declare function isSimulationSuccess(simulation: rpc.Api.SimulateTransactionResponse): boolean;
|
|
67
|
+
//# sourceMappingURL=simulation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.d.ts","sourceRoot":"","sources":["../../src/utils/simulation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AAC3C,OAAO,KAAK,EAAE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
* Simulate a transaction and extract the result
|
|
4
|
+
*
|
|
5
|
+
* @param tx - AssembledTransaction to simulate
|
|
6
|
+
* @returns Promise resolving to simulation result
|
|
7
|
+
*/
|
|
8
|
+
export async function simulateTransaction(tx) {
|
|
9
|
+
try {
|
|
10
|
+
// AssembledTransaction has `result` property if already simulated
|
|
11
|
+
// The simulation happens automatically by default in generated clients
|
|
12
|
+
// Try to get the result directly (already simulated)
|
|
13
|
+
if (tx.result !== undefined) {
|
|
14
|
+
return {
|
|
15
|
+
success: true,
|
|
16
|
+
result: tx.result,
|
|
17
|
+
fee: 0, // Fee info not available without raw simulation
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
// If result not available, transaction likely failed or wasn't simulated
|
|
21
|
+
return {
|
|
22
|
+
success: false,
|
|
23
|
+
error: 'Transaction simulation not available',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
catch (err) {
|
|
27
|
+
return {
|
|
28
|
+
success: false,
|
|
29
|
+
error: err instanceof Error ? err.message : 'Unknown simulation error',
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a transaction needs restoration (contract data needs to be restored)
|
|
35
|
+
*
|
|
36
|
+
* @param tx - AssembledTransaction to check
|
|
37
|
+
* @returns Promise resolving to boolean indicating if restoration is needed
|
|
38
|
+
*/
|
|
39
|
+
export async function needsRestoration(tx) {
|
|
40
|
+
// For generated clients, restoration info is not directly accessible
|
|
41
|
+
// Return false as default - users should check simulation errors
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Estimate the total fee for a transaction (base fee + resource fee)
|
|
46
|
+
*
|
|
47
|
+
* @param tx - AssembledTransaction to estimate
|
|
48
|
+
* @returns Promise resolving to estimated fee in stroops
|
|
49
|
+
*/
|
|
50
|
+
export async function estimateFee(tx) {
|
|
51
|
+
// Return a default estimate since simulation details not directly accessible
|
|
52
|
+
// Users should check actual fee after building transaction
|
|
53
|
+
return 100000; // Default estimate: 100,000 stroops (~0.01 XLM)
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Simulate multiple transactions in parallel
|
|
57
|
+
*
|
|
58
|
+
* @param transactions - Array of AssembledTransactions to simulate
|
|
59
|
+
* @returns Promise resolving to array of simulation results
|
|
60
|
+
*/
|
|
61
|
+
export async function simulateMultiple(transactions) {
|
|
62
|
+
return Promise.all(transactions.map((tx) => simulateTransaction(tx)));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Extract error details from a failed simulation
|
|
66
|
+
*
|
|
67
|
+
* @param simulation - The simulation response
|
|
68
|
+
* @returns Human-readable error message
|
|
69
|
+
*/
|
|
70
|
+
export function getSimulationError(simulation) {
|
|
71
|
+
if (rpc.Api.isSimulationError(simulation)) {
|
|
72
|
+
return simulation.error || 'Unknown simulation error';
|
|
73
|
+
}
|
|
74
|
+
if (rpc.Api.isSimulationRestore(simulation)) {
|
|
75
|
+
return 'Contract requires restoration';
|
|
76
|
+
}
|
|
77
|
+
return 'Simulation did not succeed';
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Check if a simulation result indicates success
|
|
81
|
+
*
|
|
82
|
+
* @param simulation - The simulation response
|
|
83
|
+
* @returns True if simulation succeeded
|
|
84
|
+
*/
|
|
85
|
+
export function isSimulationSuccess(simulation) {
|
|
86
|
+
return rpc.Api.isSimulationSuccess(simulation);
|
|
87
|
+
}
|
|
88
|
+
//# sourceMappingURL=simulation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"simulation.js","sourceRoot":"","sources":["../../src/utils/simulation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,sBAAsB,CAAC;AA8B3C;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,EAA2B;IAE3B,IAAI,CAAC;QACH,kEAAkE;QAClE,uEAAuE;QAEvE,qDAAqD;QACrD,IAA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AAC;QAC5C,OAAO,+BAA+B,CAAC;IACzC,CAAC;IAED,OAAO,4BAA4B,CAAC;AACtC,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CACjC,UAA+C;IAE/C,OAAO,GAAG,CAAC,GAAG,CAAC,mBAAmB,CAAC,UAAU,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manages wallet selection, connection state, and provides unified API
|
|
3
|
+
* for interacting with any supported Stellar wallet.
|
|
4
|
+
*/
|
|
5
|
+
import { WalletType, WalletState, WalletMetadata, WalletConnection, WalletEvent, WalletEventListener, SignTransactionOptions } from './types.js';
|
|
6
|
+
export declare class WalletManager {
|
|
7
|
+
private adapters;
|
|
8
|
+
private currentAdapter;
|
|
9
|
+
private state;
|
|
10
|
+
private eventListeners;
|
|
11
|
+
constructor();
|
|
12
|
+
/**
|
|
13
|
+
* Get all available (installed) wallets
|
|
14
|
+
*/
|
|
15
|
+
getAvailableWallets(): Promise<WalletMetadata[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Get all wallet metadata (including unavailable wallets)
|
|
18
|
+
*/
|
|
19
|
+
getAllWallets(): WalletMetadata[];
|
|
20
|
+
/**
|
|
21
|
+
* Get current wallet state
|
|
22
|
+
*/
|
|
23
|
+
getState(): WalletState;
|
|
24
|
+
/**
|
|
25
|
+
* Connect to a specific wallet
|
|
26
|
+
*/
|
|
27
|
+
connect(walletType: WalletType): Promise<WalletConnection>;
|
|
28
|
+
/**
|
|
29
|
+
* Disconnect from current wallet
|
|
30
|
+
*/
|
|
31
|
+
disconnect(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Get current connected address
|
|
34
|
+
*/
|
|
35
|
+
getAddress(): Promise<string | null>;
|
|
36
|
+
/**
|
|
37
|
+
* Check if wallet is connected
|
|
38
|
+
*/
|
|
39
|
+
isConnected(): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Sign a transaction with current wallet
|
|
42
|
+
*/
|
|
43
|
+
signTransaction(xdr: string, options?: SignTransactionOptions): Promise<string>;
|
|
44
|
+
/**
|
|
45
|
+
* Sign a message (if supported by wallet)
|
|
46
|
+
*/
|
|
47
|
+
signMessage(message: string): Promise<string>;
|
|
48
|
+
/**
|
|
49
|
+
* Get current network (if supported by wallet)
|
|
50
|
+
*/
|
|
51
|
+
getNetwork(): Promise<string>;
|
|
52
|
+
/**
|
|
53
|
+
* Add event listener
|
|
54
|
+
*/
|
|
55
|
+
on(event: WalletEvent, listener: WalletEventListener): void;
|
|
56
|
+
/**
|
|
57
|
+
* Remove event listener
|
|
58
|
+
*/
|
|
59
|
+
off(event: WalletEvent, listener: WalletEventListener): void;
|
|
60
|
+
/**
|
|
61
|
+
* Emit event to all listeners
|
|
62
|
+
*/
|
|
63
|
+
private emitEvent;
|
|
64
|
+
/**
|
|
65
|
+
* Save connection to localStorage for auto-reconnect
|
|
66
|
+
*/
|
|
67
|
+
private saveConnection;
|
|
68
|
+
/**
|
|
69
|
+
* Clear connection from localStorage
|
|
70
|
+
*/
|
|
71
|
+
private clearConnection;
|
|
72
|
+
/**
|
|
73
|
+
* Restore previous connection from localStorage
|
|
74
|
+
*/
|
|
75
|
+
private restoreConnection;
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=WalletManager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WalletManager.d.ts","sourceRoot":"","sources":["../../src/wallet/WalletManager.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAEL,UAAU,EACV,WAAW,EACX,cAAc,EACd,gBAAgB,EAChB,WAAW,EAEX,mBAAmB,EACnB,sBAAsB,EAGvB,MAAM,YAAY,CAAC;AAQpB,qBAAa,aAAa;IACxB,OAAO,CAAC,QAAQ,CAAiC;IACjD,OAAO,CAAC,cAAc,CAA8B;IACpD,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,cAAc,CAA6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