Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"test:coverage": "cross-env NODE_OPTIONS=--experimental-vm-modules jest --coverage"
},
"dependencies": {
"@tetherto/wdk-wallet": "^1.0.0-beta.10",
"@tetherto/wdk-wallet": "github:tetherto/wdk-wallet#feat/sda-protocol",
"bare-node-runtime": "^1.4.0",
"zod": "^4.4.3"
},
Expand Down
3 changes: 2 additions & 1 deletion src/policy/policy-account-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ const PROTOCOL_GETTERS = [
['getBridgeProtocol', 'bridge'],
['getLendingProtocol', 'lending'],
['getFiatProtocol', 'fiat'],
['getSwidgeProtocol', 'swidge']
['getSwidgeProtocol', 'swidge'],
['getSdaProtocol', 'sda']
]

/**
Expand Down
15 changes: 14 additions & 1 deletion src/wallet-account-with-protocols.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { IWalletAccount, NotImplementedError } from '@tetherto/wdk-wallet'

/** @typedef {import('@tetherto/wdk-wallet/protocols').ISwidgeProtocol} ISwidgeProtocol */

/** @typedef {import('@tetherto/wdk-wallet/protocols').ISdaProtocol} ISdaProtocol */

/** @interface */
export class IWalletAccountWithProtocols extends IWalletAccount {
/**
Expand All @@ -34,7 +36,7 @@ export class IWalletAccountWithProtocols extends IWalletAccount {
* The label must be unique in the scope of the account and the type of protocol (i.e., there can’t be two protocols of the same
* type bound to the same account with the same label).
*
* @template {typeof SwapProtocol | typeof BridgeProtocol | typeof LendingProtocol | typeof FiatProtocol | typeof SwidgeProtocol} P
* @template {typeof SwapProtocol | typeof BridgeProtocol | typeof LendingProtocol | typeof FiatProtocol | typeof SwidgeProtocol | typeof SdaProtocol} P
* @param {string} label - The label.
* @param {P} Protocol - The protocol class.
* @param {ConstructorParameters<P>[1]} config - The protocol configuration.
Expand Down Expand Up @@ -98,4 +100,15 @@ export class IWalletAccountWithProtocols extends IWalletAccount {
getSwidgeProtocol (label) {
throw new NotImplementedError('getSwidgeProtocol(label)')
}

/**
* Returns the SDA protocol with the given label.
*
* @param {string} label - The label.
* @returns {ISdaProtocol} The SDA protocol.
* @throws {Error} If no SDA protocol has been registered on this account with the given label.
*/
getSdaProtocol (label) {
throw new NotImplementedError('getSdaProtocol(label)')
}
}
30 changes: 26 additions & 4 deletions src/wdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import WalletManager from '@tetherto/wdk-wallet'

import { SwapProtocol, BridgeProtocol, LendingProtocol, FiatProtocol, SwidgeProtocol } from '@tetherto/wdk-wallet/protocols'
import { SwapProtocol, BridgeProtocol, LendingProtocol, FiatProtocol, SwidgeProtocol, SdaProtocol } from '@tetherto/wdk-wallet/protocols'

import PolicyEngine from './policy/policy-engine.js'

Expand Down Expand Up @@ -65,7 +65,7 @@ export default class WDK {
this._wallets = new Map()

/** @private */
this._protocols = { swap: Object.create(null), bridge: Object.create(null), lending: Object.create(null), fiat: Object.create(null), swidge: Object.create(null) }
this._protocols = { swap: Object.create(null), bridge: Object.create(null), lending: Object.create(null), fiat: Object.create(null), swidge: Object.create(null), sda: Object.create(null) }

/** @private */
this._middlewares = Object.create(null)
Expand Down Expand Up @@ -130,7 +130,7 @@ export default class WDK {
* same type bound to the same blockchain with the same label).
*
* @see {@link IWalletAccountWithProtocols#registerProtocol} to register protocols only for specific accounts.
* @template {typeof SwapProtocol | typeof BridgeProtocol | typeof LendingProtocol | typeof FiatProtocol | typeof SwidgeProtocol} P
* @template {typeof SwapProtocol | typeof BridgeProtocol | typeof LendingProtocol | typeof FiatProtocol | typeof SwidgeProtocol | typeof SdaProtocol} P
* @param {string} blockchain - The name of the blockchain the protocol must be bound to. Can be any string (e.g., "ethereum").
* @param {string} label - The label.
* @param {P} Protocol - The protocol class.
Expand All @@ -142,6 +142,10 @@ export default class WDK {
this._protocols.swidge[blockchain] ??= Object.create(null)

this._protocols.swidge[blockchain][label] = { Protocol, config }
} else if (Protocol.prototype instanceof SdaProtocol) {
this._protocols.sda[blockchain] ??= Object.create(null)

this._protocols.sda[blockchain][label] = { Protocol, config }
} else if (Protocol.prototype instanceof SwapProtocol) {
this._protocols.swap[blockchain] ??= Object.create(null)

Expand Down Expand Up @@ -313,13 +317,15 @@ export default class WDK {
_registerProtocols (account, { blockchain }) {
if (this._decoratedAccounts.has(account)) return

const protocols = { swap: Object.create(null), bridge: Object.create(null), lending: Object.create(null), fiat: Object.create(null), swidge: Object.create(null) }
const protocols = { swap: Object.create(null), bridge: Object.create(null), lending: Object.create(null), fiat: Object.create(null), swidge: Object.create(null), sda: Object.create(null) }

this._decoratedAccounts.add(account)

account.registerProtocol = (label, Protocol, config) => {
if (Protocol.prototype instanceof SwidgeProtocol) {
protocols.swidge[label] = new Protocol(account, config)
} else if (Protocol.prototype instanceof SdaProtocol) {
protocols.sda[label] = new Protocol(account, config)
} else if (Protocol.prototype instanceof SwapProtocol) {
protocols.swap[label] = new Protocol(account, config)
} else if (Protocol.prototype instanceof BridgeProtocol) {
Expand Down Expand Up @@ -412,5 +418,21 @@ export default class WDK {

throw new Error(`No swidge protocol registered for label: ${label}.`)
}

account.getSdaProtocol = (label) => {
if (this._protocols.sda[blockchain]?.[label]) {
const { Protocol, config } = this._protocols.sda[blockchain][label]

const protocol = new Protocol(account, config)

return protocol
}

if (protocols.sda[label]) {
return protocols.sda[label]
}

throw new Error(`No sda protocol registered for label: ${label}.`)
}
}
}