Skip to content

Commit 8b7260b

Browse files
committed
Add alph_signAndSubmitUnsignedTx
1 parent 72b38ef commit 8b7260b

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

src/components/TokenDapp.tsx

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
mintToken,
88
TokenBalance,
99
transferToken,
10+
transferTokenSignAndSubmitUnsignedTx,
1011
withdrawMintedToken
1112
} from "../services/token.service"
1213
import {
@@ -172,6 +173,23 @@ export const TokenDapp: FC<{
172173
}
173174
}
174175

176+
const handleTransferSubmitUnsignedTx = async (e: React.FormEvent) => {
177+
try {
178+
e.preventDefault()
179+
setTransactionStatus("approve")
180+
181+
console.log("transfer", { transferTo, transferAmount })
182+
const result = await transferTokenSignAndSubmitUnsignedTx(alephium, transferTokenAddress, transferTo, transferAmount)
183+
console.log(result)
184+
185+
setLastTransactionHash(result.txId)
186+
setTransactionStatus("pending")
187+
} catch (e) {
188+
console.error(e)
189+
setTransactionStatus("idle")
190+
}
191+
}
192+
175193
const handleDestroyTokenSubmit = async (e: React.FormEvent) => {
176194
try {
177195
e.preventDefault()
@@ -404,6 +422,38 @@ export const TokenDapp: FC<{
404422
<br />
405423
<input type="submit" disabled={buttonsDisabled} value="Transfer" />
406424
</form>
425+
<form onSubmit={handleTransferSubmitUnsignedTx}>
426+
<h2 className={styles.title}>Transfer token (sign & submit unsigned tx)</h2>
427+
428+
<label htmlFor="transfer-token-address">Token Id</label>
429+
<input
430+
type="text"
431+
id="transfer-to"
432+
name="fname"
433+
value={transferTokenAddress}
434+
onChange={(e) => setTransferTokenAddress(e.target.value)}
435+
/>
436+
437+
<label htmlFor="transfer-to">To</label>
438+
<input
439+
type="text"
440+
id="transfer-to"
441+
name="fname"
442+
value={transferTo}
443+
onChange={(e) => setTransferTo(e.target.value)}
444+
/>
445+
446+
<label htmlFor="transfer-amount">Amount</label>
447+
<input
448+
type="number"
449+
id="transfer-amount"
450+
name="fname"
451+
value={transferAmount}
452+
onChange={(e) => setTransferAmount(e.target.value)}
453+
/>
454+
<br />
455+
<input type="submit" disabled={buttonsDisabled} value="Transfer" />
456+
</form>
407457
<form onSubmit={handleDestroyTokenSubmit}>
408458
<h2 className={styles.title}>Destroy token contract</h2>
409459

src/services/token.service.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as web3 from '@alephium/web3'
2-
import { binToHex, contractIdFromAddress, DUST_AMOUNT, stringToHex } from '@alephium/web3'
2+
import { binToHex, contractIdFromAddress, DUST_AMOUNT, stringToHex, TransactionBuilder } from '@alephium/web3'
33
import { Destroy, ShinyToken, ShinyTokenInstance, Transfer } from '../../artifacts/ts'
44
import { setCurrentNodeProvider } from '@alephium/web3/dist/src/global'
55

@@ -124,6 +124,43 @@ export const transferToken = async (
124124
})
125125
}
126126

127+
export const transferTokenSignAndSubmitUnsignedTx = async (
128+
alephium: web3.SignerProvider | undefined,
129+
tokenId: string,
130+
transferTo: string,
131+
transferAmount: string
132+
): Promise<web3.SignTransferTxResult> => {
133+
if (alephium === undefined) {
134+
throw Error("alephium object not initialized");
135+
}
136+
137+
const builder = TransactionBuilder.from(alephium.nodeProvider!);
138+
const account = await alephium.getSelectedAccount();
139+
const buildResult = await builder.buildTransferTx(
140+
{
141+
signerAddress: account.address,
142+
destinations: [
143+
{
144+
address: transferTo,
145+
attoAlphAmount: DUST_AMOUNT,
146+
tokens: [
147+
{
148+
id: tokenId,
149+
amount: BigInt(transferAmount),
150+
},
151+
],
152+
},
153+
],
154+
},
155+
account.publicKey
156+
);
157+
158+
return await alephium.signAndSubmitUnsignedTx({
159+
signerAddress: account.address,
160+
unsignedTx: buildResult.unsignedTx,
161+
});
162+
};
163+
127164
export const destroyTokenContract = async (
128165
alephium: web3.SignerProvider | undefined,
129166
tokenId: string,

0 commit comments

Comments
 (0)