Skip to content
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

Add alph_signAndSubmitUnsignedTx #1

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/components/TokenDapp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
mintToken,
TokenBalance,
transferToken,
transferTokenSignAndSubmitUnsignedTx,
withdrawMintedToken
} from "../services/token.service"
import {
Expand Down Expand Up @@ -172,6 +173,23 @@ export const TokenDapp: FC<{
}
}

const handleTransferSubmitUnsignedTx = async (e: React.FormEvent) => {
try {
e.preventDefault()
setTransactionStatus("approve")

console.log("transfer", { transferTo, transferAmount })
const result = await transferTokenSignAndSubmitUnsignedTx(alephium, transferTokenAddress, transferTo, transferAmount)
console.log(result)

setLastTransactionHash(result.txId)
setTransactionStatus("pending")
} catch (e) {
console.error(e)
setTransactionStatus("idle")
}
}

const handleDestroyTokenSubmit = async (e: React.FormEvent) => {
try {
e.preventDefault()
Expand Down Expand Up @@ -404,6 +422,38 @@ export const TokenDapp: FC<{
<br />
<input type="submit" disabled={buttonsDisabled} value="Transfer" />
</form>
<form onSubmit={handleTransferSubmitUnsignedTx}>
<h2 className={styles.title}>Transfer token (sign & submit unsigned tx)</h2>

<label htmlFor="transfer-token-address">Token Id</label>
<input
type="text"
id="transfer-to"
name="fname"
value={transferTokenAddress}
onChange={(e) => setTransferTokenAddress(e.target.value)}
/>

<label htmlFor="transfer-to">To</label>
<input
type="text"
id="transfer-to"
name="fname"
value={transferTo}
onChange={(e) => setTransferTo(e.target.value)}
/>

<label htmlFor="transfer-amount">Amount</label>
<input
type="number"
id="transfer-amount"
name="fname"
value={transferAmount}
onChange={(e) => setTransferAmount(e.target.value)}
/>
<br />
<input type="submit" disabled={buttonsDisabled} value="Transfer" />
</form>
<form onSubmit={handleDestroyTokenSubmit}>
<h2 className={styles.title}>Destroy token contract</h2>

Expand Down
39 changes: 38 additions & 1 deletion src/services/token.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as web3 from '@alephium/web3'
import { binToHex, contractIdFromAddress, DUST_AMOUNT, stringToHex } from '@alephium/web3'
import { binToHex, contractIdFromAddress, DUST_AMOUNT, stringToHex, TransactionBuilder } from '@alephium/web3'
import { Destroy, ShinyToken, ShinyTokenInstance, Transfer } from '../../artifacts/ts'
import { setCurrentNodeProvider } from '@alephium/web3/dist/src/global'

Expand Down Expand Up @@ -124,6 +124,43 @@ export const transferToken = async (
})
}

export const transferTokenSignAndSubmitUnsignedTx = async (
alephium: web3.SignerProvider | undefined,
tokenId: string,
transferTo: string,
transferAmount: string
): Promise<web3.SignTransferTxResult> => {
if (alephium === undefined) {
throw Error("alephium object not initialized");
}

const builder = TransactionBuilder.from(alephium.nodeProvider!);
const account = await alephium.getSelectedAccount();
const buildResult = await builder.buildTransferTx(
{
signerAddress: account.address,
destinations: [
{
address: transferTo,
attoAlphAmount: DUST_AMOUNT,
tokens: [
{
id: tokenId,
amount: BigInt(transferAmount),
},
],
},
],
},
account.publicKey
);

return await alephium.signAndSubmitUnsignedTx({
signerAddress: account.address,
unsignedTx: buildResult.unsignedTx,
});
};

export const destroyTokenContract = async (
alephium: web3.SignerProvider | undefined,
tokenId: string,
Expand Down