-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69cf65a
commit 09bb620
Showing
11 changed files
with
359 additions
and
165 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
import type { Call } from '@/transaction/types'; | ||
import { type Address, encodeFunctionData, erc20Abi } from 'viem'; | ||
|
||
type BuildTransferTransactionParams = { | ||
recipientAddress: Address; | ||
tokenAddress: Address | null; | ||
amount: bigint; | ||
}; | ||
|
||
export function buildTransferTransaction({ | ||
recipientAddress, | ||
tokenAddress, | ||
amount, | ||
}: BuildTransferTransactionParams): Call[] { | ||
// if no token address, we are sending native ETH | ||
// and the data prop is empty | ||
if (!tokenAddress) { | ||
return [ | ||
{ | ||
to: recipientAddress, | ||
data: '0x', | ||
value: amount, | ||
}, | ||
]; | ||
} | ||
|
||
const transferCallData = encodeFunctionData({ | ||
abi: erc20Abi, | ||
functionName: 'transfer', | ||
args: [recipientAddress, amount], | ||
}); | ||
|
||
return [ | ||
{ | ||
to: tokenAddress, | ||
data: transferCallData, | ||
}, | ||
]; | ||
} |
This file contains 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,44 @@ | ||
import { buildTransferTransaction } from '@/api/buildTransferTransaction'; | ||
import type { Token } from '@/token'; | ||
import type { Call } from '@/transaction/types'; | ||
import { type Address, parseUnits } from 'viem'; | ||
|
||
type UseTransferTransactionParams = { | ||
recipientAddress: Address; | ||
token: Token | null; | ||
amount: string; | ||
}; | ||
|
||
export function useTransferTransaction({ | ||
recipientAddress, | ||
token, | ||
amount, | ||
}: UseTransferTransactionParams): { calls: Call[] } { | ||
if (!token) { | ||
return { calls: [] }; | ||
} | ||
|
||
if (!token.address) { | ||
if (token.symbol !== 'ETH') { | ||
return { calls: [] }; | ||
} | ||
const parsedAmount = parseUnits(amount, token.decimals); | ||
return { | ||
calls: buildTransferTransaction({ | ||
recipientAddress, | ||
tokenAddress: null, | ||
amount: parsedAmount, | ||
}), | ||
}; | ||
} | ||
|
||
const parsedAmount = parseUnits(amount.toString(), token.decimals); | ||
|
||
return { | ||
calls: buildTransferTransaction({ | ||
recipientAddress, | ||
tokenAddress: token.address, | ||
amount: parsedAmount, | ||
}), | ||
}; | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.