/
transfer-algos.ts
44 lines (39 loc) · 1.63 KB
/
transfer-algos.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import algosdk from 'algosdk'
import { Config } from '../config'
import { encodeLease, encodeTransactionNote, getSenderAddress, getTransactionParams, sendTransaction } from '../transaction/transaction'
import { SendTransactionResult } from '../types/transaction'
import { AlgoTransferParams } from '../types/transfer'
import Algodv2 = algosdk.Algodv2
/**
* Transfer ALGOs between two accounts.
* @param transfer The transfer definition
* @param algod An algod client
* @returns The transaction object and optionally the confirmation if it was sent to the chain (`skipSending` is `false` or unset)
*
* @example Usage example
* ```typescript
* await algokit.transferAlgos({ from, to, amount: algokit.algos(1) }, algod)
* ```
*/
export async function transferAlgos(transfer: AlgoTransferParams, algod: Algodv2): Promise<SendTransactionResult> {
const { from, to, amount, note, transactionParams, lease, ...sendParams } = transfer
const transaction = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: getSenderAddress(from),
to: getSenderAddress(to),
amount: amount.microAlgos,
note: encodeTransactionNote(note),
suggestedParams: await getTransactionParams(transactionParams, algod),
closeRemainderTo: undefined,
rekeyTo: undefined,
})
const encodedLease = encodeLease(lease)
if (encodedLease) {
transaction.addLease(encodedLease)
}
if (!sendParams.skipSending) {
Config.getLogger(sendParams.suppressLog).debug(
`Transferring ${amount.microAlgos}µALGOs from ${getSenderAddress(from)} to ${getSenderAddress(to)}`,
)
}
return sendTransaction({ transaction, from, sendParams }, algod)
}