-
Notifications
You must be signed in to change notification settings - Fork 318
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '06-21-joaquim_rn-connect-ui-2' into joaquim/rn-ui-connect
- Loading branch information
Showing
62 changed files
with
3,183 additions
and
605 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
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
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
94 changes: 94 additions & 0 deletions
94
packages/thirdweb/src/react/core/hooks/wallets/useSendToken.ts
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,94 @@ | ||
import { useMutation } from "@tanstack/react-query"; | ||
import type { ThirdwebClient } from "../../../../client/client.js"; | ||
import { getContract } from "../../../../contract/contract.js"; | ||
import { resolveAddress } from "../../../../extensions/ens/resolve-address.js"; | ||
import { transfer } from "../../../../extensions/erc20/write/transfer.js"; | ||
import { sendTransaction } from "../../../../transaction/actions/send-transaction.js"; | ||
import { prepareTransaction } from "../../../../transaction/prepare-transaction.js"; | ||
import { isAddress } from "../../../../utils/address.js"; | ||
import { toWei } from "../../../../utils/units.js"; | ||
import type { Wallet } from "../../../../wallets/interfaces/wallet.js"; | ||
|
||
/** | ||
* Send Native or ERC20 tokens from active wallet to given address. | ||
* @internal | ||
*/ | ||
export function useSendTokenCore(client: ThirdwebClient, wallet?: Wallet) { | ||
return useMutation({ | ||
async mutationFn(option: { | ||
tokenAddress?: string; | ||
receiverAddress: string; | ||
amount: string; | ||
}) { | ||
const { tokenAddress, receiverAddress, amount } = option; | ||
const activeChain = wallet?.getChain(); | ||
const account = wallet?.getAccount(); | ||
|
||
// state validation | ||
if (!activeChain) { | ||
throw new Error("No active chain"); | ||
} | ||
if (!account) { | ||
throw new Error("No active account"); | ||
} | ||
|
||
// input validation | ||
if ( | ||
!receiverAddress || | ||
(!receiverAddress.endsWith(".eth") && !isAddress(receiverAddress)) | ||
) { | ||
throw new Error("Invalid address"); | ||
} | ||
|
||
if (!amount || Number.isNaN(Number(amount)) || Number(amount) < 0) { | ||
throw new Error("Invalid amount"); | ||
} | ||
|
||
let to = receiverAddress; | ||
// resolve ENS if needed | ||
try { | ||
to = await resolveAddress({ | ||
client, | ||
name: receiverAddress, | ||
}); | ||
} catch (e) { | ||
throw new Error("Failed to resolve address"); | ||
} | ||
|
||
// native token transfer | ||
if (!tokenAddress) { | ||
const sendNativeTokenTx = prepareTransaction({ | ||
chain: activeChain, | ||
client, | ||
to, | ||
value: toWei(amount), | ||
}); | ||
|
||
await sendTransaction({ | ||
transaction: sendNativeTokenTx, | ||
account, | ||
}); | ||
} | ||
|
||
// erc20 token transfer | ||
else { | ||
const contract = getContract({ | ||
address: tokenAddress, | ||
client, | ||
chain: activeChain, | ||
}); | ||
|
||
const tx = transfer({ | ||
amount, | ||
contract, | ||
to, | ||
}); | ||
|
||
await sendTransaction({ | ||
transaction: tx, | ||
account, | ||
}); | ||
} | ||
}, | ||
}); | ||
} |
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
File renamed without changes.
Oops, something went wrong.