Send Transaction
signAndSendTransaction()
signAndSendTransaction()
If you are logged in to Face Wallet, users can send transactions. When sending a transaction, users would review the transaction information and enter the PIN code through Face Wallet modal.
import { Face, Network } from '@haechi-labs/face-sdk';
import * as nearAPI from 'near-api-js';
const face = new Face({
network: Network.NEAR_TESTNET,
apiKey: 'YOUR_DAPP_API_KEY'
});
const faceProvider = face.near.getProvider();
// Dapp should choose the input required to send transaction
const signerKey = (await nearProvider.getPublicKeys())[0];
const senderAddress = Buffer.from(publicKey.data).toString('hex');
const receiverAddress = 'facewallet.testnet';
const amount = '10000000000000000000000'; // 0.01 NEAR, decimal: 24
const provider = new nearAPI.providers.JsonRpcProvider({ url: 'NEAR_NODE_URL' });
const accessKey = await provider
.query<{
block_height: number;
block_hash: string;
nonce: number;
}>(`access_key/${senderAddress}/${signerKey.toString()}`, '');
const nonce = accessKey.nonce + 1;
const actions = [nearAPI.transactions.transfer(amount as any)];
const near = await nearAPI.connect({
networkId: 'testnet',
nodeUrl: 'https://rpc.testnet.near.org',
});
const status = await near.connection.provider.status();
const blockHash = status.sync_info.latest_block_hash;
const serializedBlockHash = nearAPI.utils.serialize.base_decode(blockHash);
const tx = nearAPI.transactions.createTransaction(
senderAddress,
signerKey,
receiverAddress,
nonce,
actions,
serializedBlockHash
);
const txHash = await nearProvider.signAndSendTransaction(tx);
When the transaction is sent, the Face Wallet modal switches to the [Processing] status screen. Even if the user closes the [Processing] screen, the transaction would be executed when it is mined because it has already delivered to the blockchain network. If the transaction is mined before the user closes the [Processing] screen, the Face Wallet modal switches to the [Success] status screen.
Updated about 1 year ago