-
Notifications
You must be signed in to change notification settings - Fork 127
Remove Ethereum services layer from SDK #13802
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
Draft
rickyrombo
wants to merge
2
commits into
mjp-eth
Choose a base branch
from
mjp-sdk-eth
base: mjp-eth
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,248 @@ | ||
| /** | ||
| * Ethereum contract utilities for Audius. | ||
| * | ||
| * Plain functions that call Audius Ethereum contracts via viem, | ||
| * using ABIs + mainnet addresses from @audius/eth. | ||
| * No classes, no schemas — just contract calls. | ||
| */ | ||
|
|
||
| import { | ||
| AudiusToken, | ||
| type AudiusTokenTypes, | ||
| AudiusWormhole, | ||
| type AudiusWormholeTypes, | ||
| DelegateManager, | ||
| Staking | ||
| } from '@audius/eth' | ||
| import { | ||
| type Hex, | ||
| type PublicClient, | ||
| type TypedDataDefinition, | ||
| type WalletClient, | ||
| createPublicClient, | ||
| createWalletClient, | ||
| http, | ||
| parseSignature | ||
| } from 'viem' | ||
| import { mainnet } from 'viem/chains' | ||
|
|
||
| // ---------- Types ---------- | ||
|
|
||
| /** Minimal signer interface for Ethereum transactions. */ | ||
| export type EthSigner = { | ||
| getAddresses: () => Promise<Hex[]> | ||
| signTypedData: (data: any) => Promise<Hex> | ||
| } | ||
|
|
||
| // ---------- Client factories ---------- | ||
|
|
||
| /** Create a viem PublicClient for Ethereum mainnet. */ | ||
| export const createEthPublicClient = (rpcUrl: string) => | ||
| createPublicClient({ | ||
| chain: mainnet, | ||
| transport: http(rpcUrl) | ||
| }) | ||
|
|
||
| /** Create a viem WalletClient for Ethereum mainnet. */ | ||
| export const createEthWalletClient = (rpcUrl: string) => | ||
| createWalletClient({ | ||
| chain: mainnet, | ||
| transport: http(rpcUrl) | ||
| }) | ||
|
|
||
| // ---------- Reads: AudiusToken ---------- | ||
|
|
||
| /** Get AUDIO token balance for an Ethereum address. */ | ||
| export const getAudioBalance = (client: PublicClient, account: Hex) => | ||
| client.readContract({ | ||
| address: AudiusToken.address, | ||
| abi: AudiusToken.abi, | ||
| functionName: 'balanceOf', | ||
| args: [account] | ||
| }) | ||
|
|
||
| // ---------- Reads: Staking ---------- | ||
|
|
||
| /** Get total staked AUDIO for an address. */ | ||
| export const getTotalStakedFor = (client: PublicClient, account: Hex) => | ||
| client.readContract({ | ||
| address: Staking.address, | ||
| abi: Staking.abi, | ||
| functionName: 'totalStakedFor', | ||
| args: [account] | ||
| }) | ||
|
|
||
| // ---------- Reads: DelegateManager ---------- | ||
|
|
||
| /** Get total delegated stake for a delegator address. */ | ||
| export const getTotalDelegatorStake = (client: PublicClient, delegator: Hex) => | ||
| client.readContract({ | ||
| address: DelegateManager.address, | ||
| abi: DelegateManager.abi, | ||
| functionName: 'getTotalDelegatorStake', | ||
| args: [delegator] | ||
| }) | ||
|
|
||
| // ---------- Composite reads ---------- | ||
|
|
||
| /** Get full AUDIO balance: token + staked + delegated. */ | ||
| export const getFullAudioBalance = async ( | ||
| client: PublicClient, | ||
| account: Hex | ||
| ) => { | ||
| const [balance, stakedBalance, delegatedBalance] = await Promise.all([ | ||
| getAudioBalance(client, account), | ||
| getTotalStakedFor(client, account), | ||
| getTotalDelegatorStake(client, account) | ||
| ]) | ||
| return balance + stakedBalance + delegatedBalance | ||
| } | ||
|
|
||
| // ---------- Writes ---------- | ||
|
|
||
| const ONE_HOUR_IN_MS = 1000 * 60 * 60 | ||
| const ONE_HOUR_IN_S = 60 * 60 | ||
|
|
||
| /** Wormhole chain ID for Solana (always 1 in the Wormhole protocol). */ | ||
| const WORMHOLE_SOLANA_CHAIN_ID = 1 | ||
|
|
||
| /** | ||
| * EIP-2612 permit: approve a spender to transfer AUDIO tokens on behalf of | ||
| * the owner using a signed message instead of an on-chain approve() tx. | ||
| */ | ||
| export async function permitAudioToken({ | ||
| ethPublicClient, | ||
| ethWalletClient, | ||
| signer, | ||
| spender, | ||
| value | ||
| }: { | ||
| ethPublicClient: PublicClient | ||
| ethWalletClient: WalletClient | ||
| signer: EthSigner | ||
| spender: Hex | ||
| value: bigint | ||
| }): Promise<Hex> { | ||
| const owner = (await signer.getAddresses())[0] | ||
| if (!owner) { | ||
| throw new Error('No wallet address available') | ||
| } | ||
|
|
||
| const deadline = BigInt(Math.floor(Date.now() / 1000) + 60 * 60) | ||
|
|
||
| const nonce = await ethPublicClient.readContract({ | ||
| address: AudiusToken.address, | ||
| abi: AudiusToken.abi, | ||
| functionName: 'nonces', | ||
| args: [owner] | ||
| }) | ||
|
|
||
| const name = await ethPublicClient.readContract({ | ||
| abi: AudiusToken.abi, | ||
| address: AudiusToken.address, | ||
| functionName: 'name' | ||
| }) | ||
|
|
||
| const chainId = BigInt(await ethPublicClient.getChainId()) | ||
|
|
||
| const typedData: TypedDataDefinition<AudiusTokenTypes, 'Permit'> = { | ||
| primaryType: 'Permit', | ||
| domain: { | ||
| name, | ||
| version: '1', | ||
| chainId, | ||
| verifyingContract: AudiusToken.address | ||
| }, | ||
| message: { owner, spender, value, nonce, deadline }, | ||
| types: AudiusToken.types | ||
| } | ||
|
|
||
| const signature = await signer.signTypedData(typedData) | ||
| const { r, s, v } = parseSignature(signature) | ||
|
|
||
| const { request } = await ethPublicClient.simulateContract({ | ||
| address: AudiusToken.address, | ||
| abi: AudiusToken.abi, | ||
| functionName: 'permit', | ||
| args: [owner, spender, value, deadline, Number(v), r, s] as const, | ||
| account: owner | ||
| }) | ||
| return await ethWalletClient.writeContract(request) | ||
| } | ||
|
|
||
| /** | ||
| * Transfer AUDIO tokens through the Wormhole bridge to Solana. | ||
| */ | ||
| export async function wormholeTransferTokens({ | ||
| ethPublicClient, | ||
| ethWalletClient, | ||
| signer, | ||
| amount, | ||
| recipient | ||
| }: { | ||
| ethPublicClient: PublicClient | ||
| ethWalletClient: WalletClient | ||
| signer: EthSigner | ||
| amount: bigint | ||
| recipient: Hex | ||
| }): Promise<Hex> { | ||
| const from = (await signer.getAddresses())[0] | ||
| if (!from) { | ||
| throw new Error('No wallet address available') | ||
| } | ||
|
|
||
| const deadline = BigInt(Math.round(Date.now() / 1000) + ONE_HOUR_IN_S) | ||
| const arbiterFee = BigInt(0) | ||
|
|
||
| const nonce = await ethPublicClient.readContract({ | ||
| address: AudiusWormhole.address, | ||
| abi: AudiusWormhole.abi, | ||
| functionName: 'nonces', | ||
| args: [from] | ||
| }) | ||
|
|
||
| const chainId = BigInt(await ethPublicClient.getChainId()) | ||
|
|
||
| const typedData: TypedDataDefinition<AudiusWormholeTypes, 'TransferTokens'> = | ||
| { | ||
| primaryType: 'TransferTokens', | ||
| domain: { | ||
| name: 'AudiusWormholeClient', | ||
| version: '1', | ||
| chainId, | ||
| verifyingContract: AudiusWormhole.address | ||
| }, | ||
| message: { | ||
| from, | ||
| amount, | ||
| recipientChain: WORMHOLE_SOLANA_CHAIN_ID, | ||
| recipient, | ||
| artbiterFee: arbiterFee, | ||
| deadline, | ||
| nonce | ||
| }, | ||
| types: AudiusWormhole.types | ||
| } | ||
|
|
||
| const signature = await signer.signTypedData(typedData) | ||
| const { r, s, v } = parseSignature(signature) | ||
|
|
||
| const { request } = await ethPublicClient.simulateContract({ | ||
| address: AudiusWormhole.address, | ||
| abi: AudiusWormhole.abi, | ||
| functionName: 'transferTokens', | ||
| args: [ | ||
| from, | ||
| amount, | ||
| WORMHOLE_SOLANA_CHAIN_ID, | ||
| recipient, | ||
| arbiterFee, | ||
| deadline, | ||
| Number(v), | ||
| r, | ||
| s | ||
| ], | ||
| account: from | ||
| }) | ||
| return await ethWalletClient.writeContract(request) | ||
| } | ||
Oops, something went wrong.
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.
createEthWalletClientcurrently always useshttp(rpcUrl)transport. A WalletClient created this way can only send transactions if the RPC node can sign for thefromaddress (unlocked/local account), which is usually not true in browser/mobile contexts. To prevent accidental misuse, consider (a) removing this factory and requiring callers to provide a WalletClient/transport, or (b) renaming/documenting it as a server-side/unlocked-account helper and adding an alternative helper for the identity-relay/EIP-1193 transport pattern.