-
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
base: mjp-eth
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(Date.now() + ONE_HOUR_IN_MS) | ||||||
|
||||||
| const deadline = BigInt(Date.now() + ONE_HOUR_IN_MS) | |
| const deadline = BigInt(Math.floor(Date.now() / 1000) + 60 * 60) |
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.