Skip to content

Commit 9f356a0

Browse files
committed
Prepare allocator.tech for OnRamp contract
1 parent 851b4dc commit 9f356a0

File tree

7 files changed

+197
-31
lines changed

7 files changed

+197
-31
lines changed

src/components/cards/AppInfoCard.tsx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import useApplicationActions from '@/hooks/useApplicationActions'
1212
import useWallet from '@/hooks/useWallet'
1313
import { useAllocator } from '@/lib/AllocatorProvider'
1414
import { stateColor, stateMapping } from '@/lib/constants'
15-
import { getAllowanceForClient } from '@/lib/glifApi'
15+
import {
16+
getAllowanceForClient,
17+
getFilecoinAddressFromEvmAddress,
18+
} from '@/lib/glifApi'
1619
import {
1720
anyToBytes,
1821
bytesToiB,
@@ -113,7 +116,10 @@ const AppInfoCard: React.FC<ComponentProps> = ({
113116
mutationDecreaseAllowanceProposal,
114117
mutationDecreaseAllowanceApproval,
115118
} = useApplicationActions(initialApplication, repo, owner)
116-
const { getAllowanceFromClientContract } = useWallet()
119+
const {
120+
getAllowanceFromClientContract,
121+
getClientContractAddressFromOnRampContract,
122+
} = useWallet()
117123
const [buttonText, setButtonText] = useState('')
118124
const [modalMessage, setModalMessage] = useState<ReactNode | null>(null)
119125
const [error, setError] = useState<boolean>(false)
@@ -216,13 +222,26 @@ const AppInfoCard: React.FC<ComponentProps> = ({
216222
const address = application.Lifecycle['On Chain Address']
217223

218224
let clientAllowance
219-
220225
const contractAddress = application['Client Contract Address'] ?? address
221226
const response = await getAllowanceForClient(contractAddress)
222227
if (application['Client Contract Address']) {
228+
let onRampClientContractAddress
229+
const evmOnRampClientContractAddress =
230+
await getClientContractAddressFromOnRampContract(
231+
application['Client Contract Address'],
232+
)
233+
if (evmOnRampClientContractAddress) {
234+
const filecoinOnRampClientContractAddressResult =
235+
await getFilecoinAddressFromEvmAddress(
236+
evmOnRampClientContractAddress,
237+
)
238+
onRampClientContractAddress =
239+
filecoinOnRampClientContractAddressResult.data
240+
}
241+
223242
clientAllowance = await getAllowanceFromClientContract(
224243
address,
225-
application['Client Contract Address'],
244+
onRampClientContractAddress ?? application['Client Contract Address'],
226245
)
227246
}
228247

@@ -289,6 +308,7 @@ const AppInfoCard: React.FC<ComponentProps> = ({
289308
isApplicationUpdatedLessThanOneMinuteAgo,
290309
getAllowanceFromClientContract,
291310
clientDataCap,
311+
getClientContractAddressFromOnRampContract,
292312
])
293313

294314
useEffect(() => {

src/hooks/useApplicationActions.ts

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
} from '@/lib/apiClient'
2020
import {
2121
getEvmAddressFromFilecoinAddress,
22+
getFilecoinAddressFromEvmAddress,
2223
getStateWaitMsg,
2324
} from '@/lib/glifApi'
2425
import { config } from '@/config'
@@ -192,6 +193,7 @@ const useApplicationActions = (
192193
sendClientDecreaseAllowanceProposal,
193194
getDecreaseAllowanceProposalTx,
194195
getAllowanceFromClientContract,
196+
getClientContractAddressFromOnRampContract,
195197
} = useWallet()
196198
const { selectedAllocator } = useAllocator()
197199

@@ -550,27 +552,45 @@ const useApplicationActions = (
550552
}
551553

552554
let amountOfDataCapSentToContract
555+
let onRampClientContractAddress
553556
if (clientContractAddress && evmClientAddress) {
557+
const evmOnRampClientContractAddress =
558+
await getClientContractAddressFromOnRampContract(
559+
clientContractAddress,
560+
)
561+
if (evmOnRampClientContractAddress) {
562+
const filecoinOnRampClientContractAddressResult =
563+
await getFilecoinAddressFromEvmAddress(
564+
evmOnRampClientContractAddress,
565+
)
566+
onRampClientContractAddress =
567+
filecoinOnRampClientContractAddressResult.data
568+
}
569+
554570
amountOfDataCapSentToContract = await getDataCapToSendToContract(
555571
proposalAllocationAmount,
556572
clientContractAddress,
557573
getAllowanceFromClientContract,
574+
onRampClientContractAddress,
558575
)
559576
}
577+
560578
const proposalTx = await getProposalTx(
561579
clientAddress,
562580
proposalAllocationAmount,
563581
allocatorType,
564582
clientContractAddress,
565583
amountOfDataCapSentToContract,
584+
onRampClientContractAddress,
566585
)
567586
let messageCID
568587
if (amountOfDataCapSentToContract !== null) {
569588
if (proposalTx?.pendingVerifyClientTransaction) {
570589
throw new Error('This datacap allocation is already proposed')
571590
}
572591

573-
const addressToGrantDataCap = clientContractAddress ?? clientAddress
592+
const addressToGrantDataCap =
593+
onRampClientContractAddress ?? clientContractAddress ?? clientAddress
574594

575595
messageCID = await sendProposal({
576596
allocatorType,
@@ -855,9 +875,22 @@ const useApplicationActions = (
855875

856876
const clientAddress = getClientAddress()
857877

858-
let clientAddressAddress
878+
let clientContractAddress
879+
let onRampClientContractAddress
859880
if (initialApplication['Client Contract Address']) {
860-
clientAddressAddress = initialApplication['Client Contract Address']
881+
clientContractAddress = initialApplication['Client Contract Address']
882+
const evmOnRampClientContractAddress =
883+
await getClientContractAddressFromOnRampContract(
884+
clientContractAddress,
885+
)
886+
if (evmOnRampClientContractAddress) {
887+
const filecoinOnRampClientContractAddressResult =
888+
await getFilecoinAddressFromEvmAddress(
889+
evmOnRampClientContractAddress,
890+
)
891+
onRampClientContractAddress =
892+
filecoinOnRampClientContractAddressResult.data
893+
}
861894
}
862895

863896
const activeRequest = initialApplication['Allocation Requests'].find(
@@ -872,12 +905,14 @@ const useApplicationActions = (
872905
increaseAllowanceCid?: string
873906
} = {}
874907
let messageCID
908+
875909
const proposalTx = await getProposalTx(
876910
clientAddress,
877911
datacap,
878912
allocatorType,
879-
clientAddressAddress,
913+
clientContractAddress,
880914
activeRequest?.['Amount of Datacap Sent to Contract'],
915+
onRampClientContractAddress,
881916
)
882917
if (activeRequest?.Signers[0]['Message CID']) {
883918
if (!proposalTx?.pendingVerifyClientTransaction) {
@@ -923,12 +958,12 @@ const useApplicationActions = (
923958

924959
if (
925960
!proposalTx?.pendingIncreaseAllowanceTransaction &&
926-
clientAddressAddress
961+
clientContractAddress
927962
) {
928963
throw new Error(
929964
'This increase allowance is not proposed yet. You may need to wait some time if the proposal was just sent.',
930965
)
931-
} else if (clientAddressAddress) {
966+
} else if (clientContractAddress) {
932967
const increaseMessageCID = await sendApproval(
933968
proposalTx?.pendingIncreaseAllowanceTransaction,
934969
)

src/hooks/useWallet.ts

Lines changed: 72 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import { newFromString } from '@glif/filecoin-address'
1616
import { useCallback, useState } from 'react'
1717
import {
18+
type Address,
1819
decodeFunctionData,
1920
decodeFunctionResult,
2021
encodeFunctionData,
@@ -56,6 +57,7 @@ interface WalletState {
5657
allocatorType: AllocatorTypeEnum,
5758
clientContractAddress?: string | null,
5859
amountOfDataCapSentToContract?: string | null,
60+
onRampClientContractAddress?: string | null,
5961
) => Promise<{
6062
pendingVerifyClientTransaction: any
6163
pendingIncreaseAllowanceTransaction: any
@@ -128,6 +130,9 @@ interface WalletState {
128130
args: string[]
129131
decodedPacked?: string[]
130132
}> | null>
133+
getClientContractAddressFromOnRampContract: (
134+
clientAddress: string,
135+
) => Promise<string | null>
131136
}
132137

133138
/**
@@ -270,6 +275,7 @@ const useWallet = (): WalletState => {
270275
allocatorType: AllocatorTypeEnum,
271276
clientContractAddress?: string | null,
272277
amountOfDataCapSentToContract?: string | null,
278+
onRampClientContractAddress?: string | null,
273279
): Promise<{
274280
pendingVerifyClientTransaction: any
275281
pendingIncreaseAllowanceTransaction: any
@@ -292,12 +298,15 @@ const useWallet = (): WalletState => {
292298

293299
let pendingVerifyClientTransaction: ParsedTransaction | null = null
294300
let pendingIncreaseAllowanceTransaction: ParsedTransaction | null = null
295-
301+
const bytesAmountOfDataCapSentToContract = amountOfDataCapSentToContract
302+
? Math.floor(anyToBytes(amountOfDataCapSentToContract))
303+
: undefined
296304
for (const transaction of pendingTxs) {
297-
if (clientContractAddress && !pendingIncreaseAllowanceTransaction) {
298-
if (typeof transaction.tx.calldata === 'undefined') {
299-
continue
300-
}
305+
if (
306+
clientContractAddress &&
307+
!pendingIncreaseAllowanceTransaction &&
308+
typeof transaction.tx.calldata !== 'undefined'
309+
) {
301310
const paramsHex = transaction.tx.calldata.toString('hex')
302311
const dataHex: Hex = `0x${paramsHex}`
303312
try {
@@ -333,10 +342,16 @@ const useWallet = (): WalletState => {
333342
) {
334343
continue
335344
}
336-
const addressToGrantDataCap = clientContractAddress ?? clientAddress
345+
const addressToGrantDataCap =
346+
onRampClientContractAddress ??
347+
clientContractAddress ??
348+
clientAddress
337349
if (
338350
transaction.tx.address === addressToGrantDataCap &&
339-
transaction.tx.cap === BigInt(bytesAllocationAmount)
351+
transaction.tx.cap ===
352+
BigInt(
353+
bytesAmountOfDataCapSentToContract ?? bytesAllocationAmount,
354+
)
340355
) {
341356
pendingVerifyClientTransaction = transaction
342357
continue
@@ -355,13 +370,11 @@ const useWallet = (): WalletState => {
355370

356371
const [clientAddressData, amount] = decodedData.args
357372
const address = newFromString(
358-
clientContractAddress ?? clientAddress,
373+
onRampClientContractAddress ??
374+
clientContractAddress ??
375+
clientAddress,
359376
)
360377
const addressHex: Hex = `0x${Buffer.from(address.bytes).toString('hex')}`
361-
const bytesAmountOfDataCapSentToContract =
362-
amountOfDataCapSentToContract
363-
? Math.floor(anyToBytes(amountOfDataCapSentToContract))
364-
: undefined
365378

366379
if (
367380
clientAddressData === addressHex &&
@@ -834,24 +847,64 @@ const useWallet = (): WalletState => {
834847
[wallet, multisigAddress, activeAccountIndex],
835848
)
836849

850+
const getClientContractAddressFromOnRampContract = useCallback(
851+
async (contractAddress: string): Promise<string | null> => {
852+
const abi = parseAbi([
853+
'function CLIENT_CONTRACT() external view returns (address)',
854+
])
855+
856+
const evmContractAddress =
857+
await getEvmAddressFromFilecoinAddress(contractAddress)
858+
const calldataHex: Hex = encodeFunctionData({
859+
abi,
860+
})
861+
862+
const response = await makeStaticEthCall(
863+
evmContractAddress.data,
864+
calldataHex,
865+
)
866+
867+
if (response.error) {
868+
return null
869+
}
870+
871+
const clientContractAddress = decodeFunctionResult({
872+
abi,
873+
data: response.data as `0x${string}`,
874+
})
875+
876+
return clientContractAddress
877+
},
878+
[],
879+
)
880+
837881
const getClientSPs = useCallback(
838882
async (client: string, contractAddress: string): Promise<string[]> => {
883+
let evmContractAddress =
884+
await getClientContractAddressFromOnRampContract(contractAddress)
839885
const abi = parseAbi([
840886
'function clientSPs(address client) external view returns (uint256[] memory providers)',
841887
])
842888

843-
const [evmClientAddress, evmContractAddress] = await Promise.all([
844-
getEvmAddressFromFilecoinAddress(client),
845-
getEvmAddressFromFilecoinAddress(contractAddress),
846-
])
889+
let evmClientAddress
890+
if (evmContractAddress) {
891+
evmClientAddress = await getEvmAddressFromFilecoinAddress(client)
892+
} else {
893+
let evmContractAddressResult
894+
;[evmClientAddress, evmContractAddressResult] = await Promise.all([
895+
getEvmAddressFromFilecoinAddress(client),
896+
getEvmAddressFromFilecoinAddress(contractAddress),
897+
])
898+
evmContractAddress = evmContractAddressResult.data
899+
}
847900

848901
const calldataHex: Hex = encodeFunctionData({
849902
abi,
850903
args: [evmClientAddress.data],
851904
})
852905

853906
const response = await makeStaticEthCall(
854-
evmContractAddress.data,
907+
evmContractAddress as Address,
855908
calldataHex,
856909
)
857910

@@ -867,7 +920,7 @@ const useWallet = (): WalletState => {
867920
const result: string[] = decodedData.map((x) => x.toString())
868921
return result
869922
},
870-
[],
923+
[getClientContractAddressFromOnRampContract],
871924
)
872925

873926
const getAllowanceFromClientContract = useCallback(
@@ -1213,6 +1266,7 @@ const useWallet = (): WalletState => {
12131266
getAllowanceFromClientContract,
12141267
sendClientDecreaseAllowanceProposal,
12151268
getDecreaseAllowanceProposalTx,
1269+
getClientContractAddressFromOnRampContract,
12161270
}
12171271
}
12181272

src/lib/glifApi.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type {
22
ApiAllowanceResponse,
33
ApiEthCallResponse,
44
ApiFilecoinAddressToEthAddressResponse,
5+
ApiEthAddressToFilecoinAddressResponse,
56
ApiStateWaitMsgResponse,
67
MsigPendingTransactions,
78
} from '@/type'
@@ -100,6 +101,31 @@ export const getEvmAddressFromFilecoinAddress = async (
100101
}
101102
}
102103

104+
/**
105+
* Get the filecoin address for a client from the API.
106+
*
107+
* @param {string} address - The address to get the filecoin address for.
108+
* @returns {Promise<ApiEthAddressToFilecoinAddressResponse>} ApiEthAddressToFilecoinAddressResponse - The response from the API.
109+
*/
110+
export const getFilecoinAddressFromEvmAddress = async (
111+
address: string,
112+
): Promise<ApiEthAddressToFilecoinAddressResponse> => {
113+
try {
114+
const result = await filecoinClient.ethAddressToFilecoinAddress(address)
115+
116+
return {
117+
data: result ?? '',
118+
error: '',
119+
success: true,
120+
}
121+
} catch (error: unknown) {
122+
const errMessage = `Error accessing GLIF API Filecoin.EthAddressToFilecoinAddress: ${
123+
(error as Error).message
124+
}`
125+
throw new Error(errMessage)
126+
}
127+
}
128+
103129
/**
104130
* Call the evm contract.
105131
*

0 commit comments

Comments
 (0)