Skip to content

Commit

Permalink
feat: support base for payg
Browse files Browse the repository at this point in the history
  • Loading branch information
gmolki committed Aug 29, 2024
1 parent b95704a commit 2d47887
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 35 deletions.
7 changes: 4 additions & 3 deletions src/components/pages/computing/NewInstancePage/cmp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import Strong from '@/components/common/Strong'
import { useConnection } from '@/hooks/common/useConnection'
import CRNList from '../../../common/CRNList'
import BackButtonSection from '@/components/common/BackButtonSection'
import { isBlockchainSupported as isBlockchainPAYGCompatible } from '@aleph-sdk/superfluid'

export default function NewInstancePage({ mainRef }: PageProps) {
const {
Expand Down Expand Up @@ -206,7 +207,7 @@ export default function NewInstancePage({ mainRef }: PageProps) {
You are about to switch your payment method to{' '}
<Strong>Pay-as-you-go</Strong>, which will also allow you to{' '}
<Strong>manually select</Strong> your preferred CRN on{' '}
<Strong>Avalanche</Strong>.
<Strong>Avalanche or Base</Strong>.
</div>
<div>
Making this change will prompt your wallet to automatically switch
Expand Down Expand Up @@ -311,7 +312,7 @@ export default function NewInstancePage({ mainRef }: PageProps) {
You are about to switch from the automated Holder-tier setup on
Ethereum to <Strong>manually selecting</Strong> a CRN with the{' '}
<Strong>Pay-as-you-go</Strong> method on{' '}
<Strong>Avalanche</Strong>.
<Strong>Avalanche or Base</Strong>.
</div>
<div>
Making this change will prompt your wallet to automatically switch
Expand Down Expand Up @@ -384,7 +385,7 @@ export default function NewInstancePage({ mainRef }: PageProps) {
if (selectedModal) return

if (
(node && blockchain === BlockchainId.AVAX) ||
(node && isBlockchainPAYGCompatible(blockchain)) ||
(!node && blockchain === BlockchainId.ETH)
) {
return modalClose()
Expand Down
32 changes: 23 additions & 9 deletions src/domain/connect/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ import {
getAccountFromProvider as getSOLAccount,
SOLAccount,
} from '@aleph-sdk/solana'
import { getAccountFromProvider as getAVAXAccount } from '@aleph-sdk/avalanche'
import { getAccountFromProvider as getBASEAccount } from '@aleph-sdk/base'
import {
getAccountFromProvider as getAVAXAccount,
AvalancheAccount,
} from '@aleph-sdk/avalanche'
import { createFromAvalancheAccount } from '@aleph-sdk/superfluid'
createFromEVMAccount,
isAccountSupported as isAccountPAYGCompatible,
} from '@aleph-sdk/superfluid'
import { Mutex, getERC20Balance, getSOLBalance, sleep } from '@/helpers/utils'
import { MetaMaskInpageProvider } from '@metamask/providers'
import type {
Provider as EthersProvider,
CombinedProvider,
} from '@web3modal/scaffold-utils/ethers'
import Err from '@/helpers/errors'
import { EVMAccount } from '@aleph-sdk/evm'

export { BlockchainId }

Expand Down Expand Up @@ -78,6 +80,15 @@ export const blockchains: Record<BlockchainId, Blockchain> = {
explorerUrl: 'https://snowtrace.io/',
rpcUrl: 'https://avalanche.drpc.org',
},
[BlockchainId.BASE]: {
id: BlockchainId.BASE,
name: 'Base',
chainId: 8453,
eip155: true,
currency: 'ETH',
explorerUrl: 'https://basescan.org',
rpcUrl: 'https://mainnet.base.org',
},
[BlockchainId.SOL]: {
id: BlockchainId.SOL,
name: 'Solana',
Expand All @@ -90,6 +101,7 @@ export const blockchains: Record<BlockchainId, Blockchain> = {
export const networks: Record<number, Blockchain> = {
1: blockchains.ETH,
43114: blockchains.AVAX,
8453: blockchains.BASE,
900: blockchains.SOL,
}

Expand Down Expand Up @@ -235,6 +247,9 @@ export abstract class BaseConnectionProviderManager {
case BlockchainId.AVAX:
return getAVAXAccount(provider as any)

case BlockchainId.BASE:
return getBASEAccount(provider as any)

case BlockchainId.SOL:
return getSOLAccount(provider as any)

Expand All @@ -244,12 +259,11 @@ export abstract class BaseConnectionProviderManager {
}

async getBalance(account: Account): Promise<number> {
if (account instanceof AvalancheAccount) {
if (isAccountPAYGCompatible(account)) {
try {
// @note: refactor in SDK calling init inside this method
const superfluidAccount = createFromAvalancheAccount(account)
await superfluidAccount.init()

const superfluidAccount = await createFromEVMAccount(
account as EVMAccount,
)
const balance = await superfluidAccount.getALEPHBalance()
return balance.toNumber()
} catch (e) {
Expand Down
1 change: 1 addition & 0 deletions src/domain/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export class ConnectionProviderManager {
export const connectionProviderManager = new ConnectionProviderManager([
BlockchainId.ETH,
BlockchainId.AVAX,
BlockchainId.BASE,
])
5 changes: 3 additions & 2 deletions src/domain/executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import Err from '@/helpers/errors'
import { BlockchainId } from './connect/base'
import { NodeManager } from './node'
import { isBlockchainSupported as isBlockchainPAYGCompatible } from '@aleph-sdk/superfluid'

type ExecutableCapabilitiesProps = {
internetAccess?: boolean
Expand Down Expand Up @@ -370,9 +371,9 @@ export abstract class ExecutableManager {
}
if (payment.type === PaymentMethod.Stream) {
if (!payment.receiver) throw Err.ReceivedRequired
if (payment.chain === BlockchainId.AVAX)
if (isBlockchainPAYGCompatible(payment.chain))
return {
chain: BlockchainId.AVAX,
chain: payment.chain,
type: SDKPaymentType.superfluid,
receiver: payment.receiver,
}
Expand Down
2 changes: 0 additions & 2 deletions src/domain/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ export class InstanceManager

const { streamCost, streamDuration, receiver } = newInstance.payment

await account.init()

const alephxBalance = await account.getALEPHBalance()
const alephxFlow = await account.getALEPHFlow(receiver)
const totalFlow = alephxFlow.add(streamCost / getHours(streamDuration))
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default {
'Invalid Superfluid/AVAX receiver reward address. Please set it up in your CRN account profile',
),
StreamNotSupported: new Error(
'Stream payments are only supported on Avalanche',
'Stream payments are only supported on Avalanche or Base',
),
MaxFlowRate: new Error(
`Current maximum total flow rate of 1 ALEPH/hour exceeded. Delete other instances or lower the VM cost`,
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/schemas/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,8 @@ export const paymentMethodSchema = z.enum([
PaymentMethod.Stream,
])

export const blockchainSchema = z.enum([BlockchainId.ETH, BlockchainId.AVAX])
export const blockchainSchema = z.enum([
BlockchainId.ETH,
BlockchainId.AVAX,
BlockchainId.BASE,
])
16 changes: 9 additions & 7 deletions src/hooks/pages/computing/useNewInstancePage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useAppState } from '@/contexts/appState'
import { FormEvent, useCallback, useEffect, useMemo } from 'react'
import { useRouter } from 'next/router'
import { createFromAvalancheAccount } from '@aleph-sdk/superfluid'
import {
createFromEVMAccount,
isAccountSupported as isAccountPAYGCompatible,
isBlockchainSupported as isBlockchainPAYGCompatible,
} from '@aleph-sdk/superfluid'
import { useForm } from '@/hooks/common/useForm'
import { EnvVarField } from '@/hooks/form/useAddEnvVars'
import {
Expand Down Expand Up @@ -38,10 +42,10 @@ import {
} from '@/hooks/form/useCheckoutNotification'
import { EntityAddAction } from '@/store/entity'
import { useConnection } from '@/hooks/common/useConnection'
import { AvalancheAccount } from '@aleph-sdk/avalanche'
import Err from '@/helpers/errors'
import { BlockchainId } from '@/domain/connect/base'
import { PaymentConfiguration } from '@/domain/executable'
import { EVMAccount } from '@aleph-sdk/evm'

export type NewInstanceFormState = NameAndTagsField & {
image: InstanceImageField
Expand Down Expand Up @@ -161,16 +165,14 @@ export function useNewInstancePage(): UseNewInstancePage {
if (!isValid) throw Err.InvalidCRNSpecs

if (
blockchain !== BlockchainId.AVAX ||
!(account instanceof AvalancheAccount)
!isBlockchainPAYGCompatible(blockchain) ||
!isAccountPAYGCompatible(account)
) {
handleConnect({ blockchain: BlockchainId.AVAX })
throw Err.InvalidNetwork
}

// @note: refactor in SDK calling init inside this method
superfluidAccount = createFromAvalancheAccount(account)
await superfluidAccount.init()
superfluidAccount = await createFromEVMAccount(account as EVMAccount)

payment = {
chain: BlockchainId.AVAX,
Expand Down
20 changes: 10 additions & 10 deletions src/hooks/pages/solutions/manage/useManageInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { useAppState } from '@/contexts/appState'
import { useInstanceStatus } from '@/hooks/common/useInstanceStatus'
import { useSSHKeyManager } from '@/hooks/common/useManager/useSSHKeyManager'
import { SSHKey } from '@/domain/ssh'
import { createFromAvalancheAccount } from '@aleph-sdk/superfluid'
import {
createFromEVMAccount,
isAccountSupported as isAccountPAYGCompatible,
isBlockchainSupported as isBlockchainPAYGCompatible,
} from '@aleph-sdk/superfluid'
import { useConnection } from '@/hooks/common/useConnection'
import { PaymentType } from '@aleph-sdk/message'
import { AvalancheAccount } from '@aleph-sdk/avalanche'
import { useRequestInstances } from '@/hooks/common/useRequestEntity/useRequestInstances'
import { EntityDelAction } from '@/store/entity'
import {
Expand All @@ -21,6 +24,7 @@ import { BlockchainId } from '@/domain/connect/base'
import { useCopyToClipboardAndNotify, useNotification } from '@aleph-front/core'
import { useNodeManager } from '@/hooks/common/useManager/useNodeManager'
import { CRN } from '@/domain/node'
import { EVMAccount } from '@aleph-sdk/evm'

export type ManageInstance = {
instance?: Instance
Expand Down Expand Up @@ -77,22 +81,18 @@ export function useManageInstance(): ManageInstance {
}, [sshKeyManager, instance])

const handleEnsureNetwork = useCallback(async () => {
let superfluidAccount
if (!instance) return

if (instance.payment?.type === PaymentType.superfluid) {
if (
blockchain !== BlockchainId.AVAX ||
!(account instanceof AvalancheAccount)
!isBlockchainPAYGCompatible(blockchain) ||
!isAccountPAYGCompatible(account)
) {
handleConnect({ blockchain: BlockchainId.AVAX })
throw Err.ConnectYourPaymentWallet
throw Err.InvalidNetwork
}
// @note: refactor in SDK calling init inside this method
superfluidAccount = createFromAvalancheAccount(account)
await superfluidAccount.init()

return superfluidAccount
return await createFromEVMAccount(account as EVMAccount)
} else if (blockchain !== BlockchainId.ETH) {
handleConnect({ blockchain: BlockchainId.ETH })
throw Err.ConnectYourPaymentWallet
Expand Down
6 changes: 6 additions & 0 deletions src/hooks/pages/useHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ export function useHeader(): UseHeaderReturn {
name: 'Avalanche',
wallets,
},
{
id: BlockchainId.BASE,
icon: 'base',
name: 'Base',
wallets,
},
],
[wallets],
)
Expand Down

0 comments on commit 2d47887

Please sign in to comment.