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

feat: SDK Manager package #25

Merged
merged 73 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
f31162f
initial changes
FSM1 Oct 20, 2023
c2bb537
initial changes
FSM1 Oct 20, 2023
fc8d5b7
Merge branch 'feat/sdk-manager-package' of https://github.com/sygmapr…
FSM1 Oct 20, 2023
c729e41
add exports
FSM1 Oct 23, 2023
c2731ea
update lock file
FSM1 Oct 23, 2023
4e0bda8
initialize sdk-manager
FSM1 Oct 23, 2023
2f1582e
more lock file updates
FSM1 Oct 23, 2023
62664bf
fix deposit
FSM1 Oct 23, 2023
6e05bbe
fix approvals
FSM1 Oct 24, 2023
015810a
remove unnecessary code
FSM1 Oct 24, 2023
5508cf2
update lock file
FSM1 Oct 24, 2023
67b3ea9
chore: update lock file
wainola Oct 25, 2023
7a94ca8
Move types to separate file
FSM1 Oct 25, 2023
113a79d
update exports
FSM1 Oct 25, 2023
3d9c8b4
fix network switching
FSM1 Oct 26, 2023
7fba17b
update yarn
FSM1 Oct 31, 2023
3de7750
update lock file after yarn upgrade
FSM1 Oct 31, 2023
097207b
add network valid flag
FSM1 Oct 31, 2023
eb064d9
add connected callback
FSM1 Nov 7, 2023
ba3d6f8
chore: pr review
wainola Oct 30, 2023
6cde381
chore: ethers as peer dependency and update on lock file
wainola Oct 30, 2023
addc335
chore: fixating dependencies
wainola Oct 30, 2023
5bf4608
chore: versiong yarn
wainola Oct 30, 2023
ca68f93
chore: going to stable version of yarn
wainola Oct 30, 2023
529e286
chore: update lock file
wainola Oct 30, 2023
4dfacf0
chore: update yarnrc file
wainola Oct 30, 2023
204ed59
update lock file
FSM1 Oct 30, 2023
4835d11
chore: removing checksum behaviour flag
wainola Oct 30, 2023
0f4ec9c
chore: removing js docs comments, adding yarn folder to the gitignore
wainola Nov 2, 2023
58e86e0
chore: changing to removeAllListeners
wainola Nov 2, 2023
d178cbe
chore: change on gitignore
wainola Nov 2, 2023
a65aed8
chore: remove reference to yarn release file
wainola Nov 2, 2023
ee29c5b
chore: change method names
wainola Nov 2, 2023
865d20e
chore: PR review, changes on constructor implementation
wainola Nov 6, 2023
5b14fde
chore: update gitignore
wainola Nov 7, 2023
9ff4b4e
chore: fixing and adding some more basic tests
wainola Nov 7, 2023
a714f80
initial changes
FSM1 Oct 20, 2023
be88088
add exports
FSM1 Oct 23, 2023
8e80578
initialize sdk-manager
FSM1 Oct 23, 2023
ebd59ef
more lock file updates
FSM1 Oct 23, 2023
127c05e
update lock file
FSM1 Oct 24, 2023
6731275
chore: update lock file
wainola Oct 25, 2023
b358fb0
fix network switching
FSM1 Oct 26, 2023
85c7703
update yarn
FSM1 Oct 31, 2023
3df49d9
update lock file after yarn upgrade
FSM1 Oct 31, 2023
6d35ac6
add connected callback
FSM1 Nov 7, 2023
429788e
fix merge issues
FSM1 Nov 22, 2023
240227e
widget basics
FSM1 Nov 22, 2023
3fb0880
chore: init wallet manager on connectedCallback and not the constructor
wainola Nov 23, 2023
4c6ff2a
chore: default parameter for network value
wainola Nov 23, 2023
9ca3ed9
chore: pr review last comments
wainola Nov 28, 2023
33c3954
chore: rename networks property to network
wainola Nov 29, 2023
f216135
chore: name of packages and uncomment publish workflow
wainola Nov 29, 2023
ad3dfe7
Merge remote-tracking branch 'origin/main' into feat/sdk-manager-package
FSM1 Nov 29, 2023
fd61e24
fix merge
FSM1 Nov 29, 2023
2632e23
resolve some comments
FSM1 Nov 30, 2023
55e0ad7
fix lint
FSM1 Nov 30, 2023
7de0923
update lockfile
FSM1 Nov 30, 2023
74f19a9
implement comments
FSM1 Dec 5, 2023
7f9b999
fix linting
FSM1 Dec 5, 2023
9866a36
feat: update npmignore + peer dep for events dependency
wainola Dec 6, 2023
11f5765
feat: update lock file
wainola Dec 6, 2023
de53598
fix: lock file and package.json file
wainola Dec 6, 2023
2341b11
fix: update lock file
wainola Dec 6, 2023
a3fda17
address comments
FSM1 Dec 8, 2023
21a641b
rename methods for more clarity
FSM1 Dec 8, 2023
e2c4a4f
update to latest sdk
FSM1 Dec 11, 2023
ea0e314
fix linting
FSM1 Dec 11, 2023
e098feb
fix lint
FSM1 Dec 11, 2023
d873999
update script
FSM1 Dec 12, 2023
33f3d9d
update script
FSM1 Dec 12, 2023
0036759
add vite config and npmignore it
FSM1 Dec 12, 2023
0b06ee2
chore(main): release 0.1.0
FSM1 Dec 12, 2023
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
6 changes: 3 additions & 3 deletions .github/workflows/react-widget.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
# you should probably do this after your regular CI checks passes
# you should probably do this after your regular CI checks passes
- uses: google-github-actions/release-please-action@v3 # it will analyze commits and create PR with new version and updated CHANGELOG:md file. On merging it will create github release page with changelog
id: release
with:
Expand Down Expand Up @@ -44,15 +44,15 @@ jobs:
- name: Enable corepack
run: corepack enable
if: ${{ steps.release.outputs.releases_created }}

- name: Install yarn
run: yarn set version stable
if: ${{ steps.release.outputs.releases_created }}

- name: Install dependencies
run: yarn install
if: ${{ steps.release.outputs.releases_created }}

- name: Build all packages
run: yarn build:all
if: ${{ steps.release.outputs.releases_created }}
Expand Down
7 changes: 3 additions & 4 deletions .github/workflows/sdk-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
# you should probably do this after your regular CI checks passes
# you should probably do this after your regular CI checks passes
- uses: google-github-actions/release-please-action@v3 # it will analyze commits and create PR with new version and updated CHANGELOG:md file. On merging it will create github release page with changelog
id: release
with:
Expand All @@ -23,7 +23,7 @@ jobs:
path: packages/sdk-manager
monorepo-tags: true
default-branch: main
package-name: '@buildwithsygma/sdk-manager'
package-name: '@buildwithsygma/sygmaprotocol-sdk-manager'
changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":false},{"type":"revert","hidden":true}]'

- uses: actions/checkout@v3
Expand All @@ -43,7 +43,7 @@ jobs:
- name: Enable corepack
run: corepack enable
if: ${{ steps.release.outputs.releases_created }}

- name: Install yarn
run: yarn set version stable
if: ${{ steps.release.outputs.releases_created }}
Expand All @@ -60,4 +60,3 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: ${{ steps.release.outputs.releases_created }}

7 changes: 3 additions & 4 deletions .github/workflows/wallet-manager.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
# you should probably do this after your regular CI checks passes
# you should probably do this after your regular CI checks passes
- uses: google-github-actions/release-please-action@v3 # it will analyze commits and create PR with new version and updated CHANGELOG:md file. On merging it will create github release page with changelog
id: release
with:
Expand All @@ -23,7 +23,7 @@ jobs:
path: packages/wallet-manager
monorepo-tags: true
default-branch: main
package-name: '@buildwithsygma/wallet-manager'
package-name: '@buildwithsygma/sygmaprotocol-wallet-manager'
changelog-types: '[{"type":"feat","section":"Features","hidden":false},{"type":"fix","section":"Bug Fixes","hidden":false},{"type":"chore","section":"Miscellaneous","hidden":false},{"type":"revert","hidden":true}]'

- uses: actions/checkout@v3
Expand All @@ -43,7 +43,7 @@ jobs:
- name: Enable corepack
run: corepack enable
if: ${{ steps.release.outputs.releases_created }}

- name: Install yarn
run: yarn set version stable
if: ${{ steps.release.outputs.releases_created }}
Expand All @@ -60,4 +60,3 @@ jobs:
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: ${{ steps.release.outputs.releases_created }}

2 changes: 1 addition & 1 deletion .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.0.1.cjs

checksumBehavior: update
checksumBehavior: update
7 changes: 5 additions & 2 deletions packages/sdk-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"module": "build/index.js",
"license": "LGPL-3.0-or-later",
"type": "module",
"types": "./build/index.d.ts",
"scripts": {
"build": "tsc --build --clean && tsc --build ./tsconfig.json",
"dev": "tsc --build --clean && tsc --build ./tsconfig.json --watch",
Expand All @@ -15,10 +16,12 @@
},
"author": "Sygmaprotocol Product Team",
"dependencies": {
"@buildwithsygma/sygma-sdk-core": "^2.2.0",
"@buildwithsygma/sygma-sdk-core": "2.4.0",
"@ethersproject/contracts": "^5.7.0",
"@ethersproject/transactions": "^5.7.0",
"@polkadot/api": "10.7.2"
"@lit/context": "1.0.0",
"@polkadot/api": "10.7.2",
"lit": "3.0.0"
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
},
"devDependencies": {
"@polkadot/types": "10.7.2",
Expand Down
126 changes: 126 additions & 0 deletions packages/sdk-manager/src/SdkManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import {
EVMAssetTransfer,
Environment,
EvmFee,
Fungible,
Transfer
} from '@buildwithsygma/sygma-sdk-core';
import { BaseProvider, TransactionRequest } from '@ethersproject/providers';
import { Signer } from 'ethers';
import { UnsignedTransaction } from '@ethersproject/transactions';
import { SdkManagerState, SdkManagerStatus } from './types';

export class SdkManager implements SdkManagerState {
assetTransfer: EVMAssetTransfer;
status: SdkManagerStatus;
transfer?: Transfer<Fungible>;
fee?: EvmFee;
approvalTxs?: UnsignedTransaction[];
depositTx?: UnsignedTransaction;

constructor() {
this.assetTransfer = new EVMAssetTransfer();
this.status = 'idle';
}

async checkSourceNetwork(provider: BaseProvider) {
const providerChainId = (await provider.getNetwork()).chainId;
const validEnvDomains = this.assetTransfer.config
.getDomains()
.map((domain) => domain.chainId);

if (!validEnvDomains.includes(providerChainId)) {
this.status = 'invalidSourceNetwork';
} else {
this.status = 'initialized';
}
}
Comment on lines +32 to +37
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: negative logic adds a bit more cognitive load, so maybe it'd be a good idea to remove ! in the condition and switch clauses 🤷


async initializeSdk(
provider: BaseProvider,
env: Environment = Environment.MAINNET
) {
await this.assetTransfer.init(provider, env);
await this.checkSourceNetwork(provider);
}

async initializeTransfer(
fromAddress: string,
destinationChainId: number,
destinationAddress: string,
resourceId: string,
amount: string
) {
const transfer = await this.assetTransfer.createFungibleTransfer(
fromAddress,
destinationChainId,
destinationAddress,
resourceId,
amount
);

const fee = await this.assetTransfer.getFee(transfer);

const approvals = await this.assetTransfer.buildApprovals(transfer, fee);

this.transfer = transfer;
this.fee = fee;
this.approvalTxs = approvals;
this.status =
approvals.length > 0 ? 'transferCreated' : 'approvalsCompleted';

this.depositTx = await this.assetTransfer.buildTransferTransaction(
transfer,
fee
);
}

async performApprovals(signer: Signer) {
if (!this.transfer) {
throw new Error('No transfer');
}

if (!this.approvalTxs) {
throw new Error('No approvals');
}

if (!this.fee) {
throw new Error('No fee');
}

for (const approval of this.approvalTxs) {
await (
await signer.sendTransaction(approval as TransactionRequest)
).wait();
}

const approvals = await this.assetTransfer.buildApprovals(
this.transfer,
this.fee
);

this.approvalTxs = approvals;
if (!approvals?.length) {
this.status = 'approvalsCompleted';
this.depositTx = await this.assetTransfer.buildTransferTransaction(
this.transfer,
this.fee
);
}
}

async performDeposit(signer: Signer) {
if (!this.transfer) {
throw new Error('No transfer');
}

if (!this.depositTx) {
throw new Error('No deposit');
}

await (
await signer.sendTransaction(this.depositTx as TransactionRequest)
).wait();
this.status = 'deposited';
}
}
100 changes: 100 additions & 0 deletions packages/sdk-manager/src/SdkManagerContextProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Environment } from '@buildwithsygma/sygma-sdk-core';
import { consume, createContext, provide } from '@lit/context';
import { LitElement, html } from 'lit';
import { customElement, state } from 'lit/decorators.js';
import {
WalletManagerContext,
WalletManagerController
} from '@buildwithsygma/sygmaprotocol-wallet-manager';
import { SdkManagerState } from './types';
import { SdkManager } from './SdkManager';

export const SdkManagerContext = createContext<SdkManagerState | undefined>(
'sdk-context'
);

/**
* @name SdkManagerContextProvider
* @description This component is responsible for providing the SdkManagerController as a context to all its children.
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* For you to consume the context objects, you need to wrap up your component with the sdk-manager-context on your render method.
*
* <wallet-manager-context>
* <sdk-manager-context-provider>
* <your-component></your-component>
* </sdk-manager-context-provider>
* </wallet-manager-context>
*/

@customElement('sdk-manager-context-provider')
Comment on lines +23 to +30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be good to have consistent naming with context (providers)

export class SdkManagerContextProvider extends LitElement {
@consume({ context: WalletManagerContext, subscribe: true })
@state()
walletManager?: WalletManagerController;

@provide({ context: SdkManagerContext })
@state()
sdkManager?: SdkManager;
Comment on lines +34 to +38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @wainola
when refactoring, it'd be good that both managers follow same naming convention


constructor() {
super();
this.sdkManager = new SdkManager();
}

async initializeSdk(env?: Environment) {
if (!this.walletManager?.provider) {
throw new Error('No wallet connected');
}

await this.sdkManager?.initializeSdk(this.walletManager.provider, env);
}

async initializeTransfer(
fromAddress: string,
destinationChainId: number,
destinationAddress: string,
resourceId: string,
amount: string
) {
if (!this.walletManager?.provider) {
throw new Error('No wallet connected');
}
Comment on lines +60 to +62
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently this is repeated only twice, but if more methods will need to do these checks, it'd be good to extract them in a method


if (!this.sdkManager) {
throw new Error('SdkManager not initialized');
}

if (this.sdkManager.status !== 'initialized') {
throw new Error('SdkManager not initialized');
}

await this.sdkManager.initializeTransfer(
fromAddress,
destinationChainId,
destinationAddress,
resourceId,
amount
);
}

async connectedCallback(): Promise<void> {
super.connectedCallback();

this.walletManager?.addAccountChangedEventListener(() => {
this.requestUpdate();
});

this.walletManager?.addChainChangedEventListener(async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this async here?

const provider = this.walletManager?.evmWallet?.web3Provider;
if (provider) {
this.sdkManager?.checkSourceNetwork(provider);
}
this.requestUpdate();
});
}

render() {
return html`<slot></slot>`;
}
}
8 changes: 8 additions & 0 deletions packages/sdk-manager/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export {
SdkManagerContext,
SdkManagerContextProvider
} from './SdkManagerContextProvider';

export { SdkManager } from './SdkManager';

export type { SdkManagerState, SdkManagerStatus } from './types/index';
33 changes: 33 additions & 0 deletions packages/sdk-manager/src/types/SdkManagerState.ts
FSM1 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
EVMAssetTransfer,
Environment,
EvmFee,
Fungible,
Transfer
} from '@buildwithsygma/sygma-sdk-core';
import { UnsignedTransaction } from '@ethersproject/transactions';
import { SdkManagerStatus } from './SdkManagerStatus';
import { BaseProvider } from '@ethersproject/providers';
import { Signer } from 'ethers';

export type SdkManagerState = {
assetTransfer: EVMAssetTransfer;
status: SdkManagerStatus;
transfer?: Transfer<Fungible>;
fee?: EvmFee;
approvalTxs?: UnsignedTransaction[];
depositTx?: UnsignedTransaction;

initializeSdk: (provider: BaseProvider, env?: Environment) => Promise<void>;
initializeTransfer: (
fromAddress: string,
destinationChainId: number,
destinationAddress: string,
resourceId: string,
amount: string
) => Promise<void>;

performApprovals(signer: Signer): Promise<void>;
performDeposit(signer: Signer): Promise<void>;
checkSourceNetwork(provider: BaseProvider): Promise<void>;
};
Loading
Loading