-
Notifications
You must be signed in to change notification settings - Fork 20
Savings Widget improvements #49
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
Open
kadiray34
wants to merge
8
commits into
GoodDollar:main
Choose a base branch
from
Ubeswap:savings-improvements
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
daf0094
Initial plan
Copilot 468df59
fix: resolve savings sdk/widget staking and error handling issues
Copilot 58f92ef
chore: refine savings network switch and demo copy
Copilot ac517fd
chore: tighten savings widget error typing
Copilot b461a9a
Merge branch 'copilot/fix-savings-sdk-bugs' into savings-improvements
kadiray34 9d75407
fix: invalid approve amount
kadiray34 49780eb
feat: XDC Network support added for savings widget
kadiray34 55c382e
feat: Superfluid streaming rewards for savings on Celo
kadiray34 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
| <title>GoodDollar Savings Widget Demo</title> | ||
| </head> | ||
| <body | ||
| style=" | ||
| margin: 0; | ||
| background: #f3f4f6; | ||
| font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; | ||
| " | ||
| > | ||
| <main | ||
| style=" | ||
| min-height: 100vh; | ||
| display: flex; | ||
| flex-direction: column; | ||
| align-items: center; | ||
| justify-content: center; | ||
| gap: 16px; | ||
| padding: 24px; | ||
| " | ||
| > | ||
| <div style="display: flex; flex-direction: column; gap: 8px; align-items: center"> | ||
| <h1 style="margin: 0; color: #111827">Savings Widget Demo</h1> | ||
| <p id="walletStatus" style="margin: 0; color: #4b5563">Wallet not connected</p> | ||
| <button | ||
| id="connectWalletButton" | ||
| style=" | ||
| border: none; | ||
| border-radius: 10px; | ||
| padding: 10px 14px; | ||
| color: #fff; | ||
| background: #00b0ff; | ||
| cursor: pointer; | ||
| font-size: 15px; | ||
| font-weight: 600; | ||
| " | ||
| > | ||
| Connect Wallet | ||
| </button> | ||
| </div> | ||
|
|
||
| <gooddollar-savings-widget id="savingsWidget"></gooddollar-savings-widget> | ||
| </main> | ||
| <script type="module" src="/src/index.js"></script> | ||
| </body> | ||
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "demo-savings-widget", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite --port 3001", | ||
| "build": "vite build", | ||
| "preview": "vite preview" | ||
| }, | ||
| "dependencies": { | ||
| "@goodsdks/savings-widget": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "vite": "6.3.5" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import "@goodsdks/savings-widget" | ||
|
|
||
| const savingsWidget = document.getElementById("savingsWidget") | ||
| const walletStatus = document.getElementById("walletStatus") | ||
| const connectWalletButton = document.getElementById("connectWalletButton") | ||
|
|
||
| const getProvider = () => window.ethereum ?? null | ||
|
|
||
| const updateWalletStatus = async () => { | ||
| const provider = getProvider() | ||
| if (!provider?.request) { | ||
| walletStatus.textContent = "No injected wallet found (install a browser wallet like MetaMask)" | ||
| connectWalletButton.disabled = true | ||
| connectWalletButton.style.opacity = "0.6" | ||
| return | ||
| } | ||
|
|
||
| const accounts = await provider.request({ method: "eth_accounts" }) | ||
| if (accounts?.length) { | ||
| const [account] = accounts | ||
| walletStatus.textContent = `Connected: ${account.slice(0, 6)}...${account.slice(-4)}` | ||
| savingsWidget.web3Provider = provider | ||
| } else { | ||
| walletStatus.textContent = "Wallet not connected" | ||
| savingsWidget.web3Provider = null | ||
| } | ||
| } | ||
|
|
||
| const connectWallet = async () => { | ||
| const provider = getProvider() | ||
| if (!provider?.request) { | ||
| walletStatus.textContent = "No injected wallet found (install a browser wallet like MetaMask)" | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| await provider.request({ method: "eth_requestAccounts" }) | ||
| await updateWalletStatus() | ||
| } catch (error) { | ||
| console.error("Wallet connection failed:", error) | ||
| walletStatus.textContent = "Wallet connection was rejected" | ||
| } | ||
| } | ||
|
|
||
| customElements.whenDefined("gooddollar-savings-widget").then(async () => { | ||
| savingsWidget.connectWallet = connectWallet | ||
| connectWalletButton.addEventListener("click", connectWallet) | ||
|
|
||
| const provider = getProvider() | ||
| provider?.on?.("accountsChanged", () => { | ||
| updateWalletStatus().catch(console.error) | ||
| }) | ||
| provider?.on?.("chainChanged", () => { | ||
| updateWalletStatus().catch(console.error) | ||
| }) | ||
|
|
||
| await updateWalletStatus() | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| import { celo, xdc, type Chain } from "viem/chains" | ||
|
|
||
| /** | ||
| * Chains where the GoodDollar Savings (G$ Staking) flow is supported. | ||
| * | ||
| * The widget and SDK validate that both the public and wallet clients are | ||
| * connected to one of these chains. Anything else is treated as a "wrong | ||
| * network" error so callers can prompt the user to switch. | ||
| */ | ||
| export enum SupportedChainId { | ||
| CELO = 42220, | ||
| XDC = 50, | ||
| } | ||
|
|
||
| export const SUPPORTED_CHAIN_IDS: SupportedChainId[] = [ | ||
| SupportedChainId.CELO, | ||
| SupportedChainId.XDC, | ||
| ] | ||
|
|
||
| export const isSupportedChainId = ( | ||
| chainId: number | undefined, | ||
| ): chainId is SupportedChainId => | ||
| typeof chainId === "number" && | ||
| SUPPORTED_CHAIN_IDS.includes(chainId as SupportedChainId) | ||
|
|
||
| export interface SavingsContracts { | ||
| /** GoodDollarStaking proxy used to stake G$ and claim rewards. */ | ||
| staking: `0x${string}` | ||
| /** G$ ERC-20 (or SuperGoodDollar) token contract. */ | ||
| gdollar: `0x${string}` | ||
| /** | ||
| * Superfluid Host. Required on streaming chains; the SDK bundles | ||
| * approve / pool connect / stake calls through `host.batchCall`. | ||
| */ | ||
| superfluidHost?: `0x${string}` | ||
| /** | ||
| * Superfluid GDA Forwarder. Required on streaming chains to connect | ||
| * the user to the GDA pool that distributes streaming rewards. | ||
| */ | ||
| gdaForwarder?: `0x${string}` | ||
| } | ||
|
|
||
| export interface SavingsChainConfig { | ||
| id: SupportedChainId | ||
| label: string | ||
| chain: Chain | ||
| isStreaming: boolean // whether this chain uses Superfluid streaming rewards | ||
| contracts: SavingsContracts | ||
| } | ||
|
|
||
| // Default contract addresses per supported chain. | ||
| export const SAVINGS_CHAIN_CONFIG: Record< | ||
| SupportedChainId, | ||
| SavingsChainConfig | ||
| > = { | ||
| [SupportedChainId.CELO]: { | ||
| id: SupportedChainId.CELO, | ||
| label: "Celo", | ||
| chain: celo, | ||
| isStreaming: true, | ||
| contracts: { | ||
| staking: "0x059ee811414230d1Fb157878D2b491240F4D8d3B", | ||
| gdollar: "0x62B8B11039FcfE5aB0C56E502b1C372A3d2a9c7A", | ||
| superfluidHost: "0xA4Ff07cF81C02CFD356184879D953970cA957585", | ||
| gdaForwarder: "0x308b7405272d11494716e30C6E972DbF6fb89555", | ||
| }, | ||
| }, | ||
| [SupportedChainId.XDC]: { | ||
| id: SupportedChainId.XDC, | ||
| label: "XDC", | ||
| chain: xdc, | ||
| isStreaming: false, | ||
| contracts: { | ||
| staking: "0x61a1Da2a81FbaE6b1B3A45D94355A6A5c5973A52", | ||
| gdollar: "0xEC2136843a983885AebF2feB3931F73A8eBEe50c", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| export function getSavingsChainConfig(chainId: number): SavingsChainConfig | undefined { | ||
| if (!isSupportedChainId(chainId)) { | ||
| return undefined | ||
| } | ||
| return SAVINGS_CHAIN_CONFIG[chainId] | ||
| } | ||
|
|
||
| /** | ||
| * Error thrown when the SDK is initialised with a client connected to a chain | ||
| * that is not in {@link SUPPORTED_CHAIN_IDS}. | ||
| */ | ||
| export class UnsupportedChainError extends Error { | ||
| readonly chainId: number | undefined | ||
| constructor(chainId: number | undefined) { | ||
| super( | ||
| `Unsupported chain ${chainId ?? "<unknown>"}.`, | ||
| ) | ||
| this.name = "UnsupportedChainError" | ||
| this.chainId = chainId | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export * from "./viem-sdk"; | ||
| export { useGooddollarSavings } from "./wagmi-sdk"; | ||
| export type { GlobalStats, UserStats } from "./viem-sdk"; | ||
| export * from "./viem-sdk" | ||
| export * from "./constants" | ||
| export { useGooddollarSavings } from "./wagmi-sdk" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion:
eth_accountscall in the demo is unguarded and can throw, which would leave the UI in an inconsistent state.In
updateWalletStatus, ifprovider.request({ method: "eth_accounts" })throws (e.g. provider error), the exception will bubble up and prevent the status text from updating. Since this is example code for integrators, consider wrapping the call in try/catch and setting a clear error message so the demo stays in a consistent state even when the injected wallet misbehaves.