First, make sure you add the Client
and Txns
modules when initializing AlgoStack
. Yes, you need a client to be connected to be able to sign and send transactions.
import AlgoStack from 'algostack';
import Client from 'algostack/client';
import Txns from 'algostack/txns';
const algostack = new AlgoStack({}, { Client, Txns });
// algostack.txns
class Txns {
// Send a single transaction
sendTxn(params: TransactionLike): Promise<Record<string, any> | undefined>;
// Send a transaction group
// Every transaction must succeed for the group to be valid
sendGroupedTxns(paramsGroup: TransactionLike[]): Promise<Record<string, any> | undefined>;
// Send individual transactions one after the other
// Wait for each one to succeed before sendind the next one
// All transactions are signed at once though
sendSequencedTxns(paramsSequence: TransactionLike[]): Promise<any[] | undefined>;
}
The simplest implementation goes like this.
import AlgoStack from 'algostack';
import Client from 'algostack/client';
import Txns from 'algostack/txns';
const algostack = new AlgoStack({}, { Client, Txns });;
async function sendTxn() {
const txn = await algostack.txns.sendTxn({
type: 'pay',
from: algostack.client.addresses[0],
to: algostack.client.addresses[0],
amount: 0,
});
console.log('🎉 Confirmed:', txn);
}
// attach connect methods to ui elements
const sendBtn = document.getElementById('send-it');
sendBtn.addEventListener('click', sendTxn);
<button id="send-it">
Send It!
</button>
import AlgoStack from 'algostack';
import Client from 'algostack/client';
import Txns from 'algostack/txns';
const algostack = new AlgoStack({}, { Client, Txns });;
async function sendGroup() {
const txn = await algostack.txns.sendGroupedTxn([
{
type: 'pay',
from: algostack.client.addresses[0],
to: algostack.client.addresses[0],
amount: 0,
},
{
type: 'pay',
from: algostack.client.addresses[0],
to: algostack.client.addresses[0],
amount: 0,
}
]);
console.log('🎉 Grouped Confirmed:', txn);
}
// attach connect methods to ui elements
const sendBtn = document.getElementById('send-it');
sendBtn.addEventListener('click', sendGroup);
<button id="send-it">
Send It!
</button>