-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicWallet.kt
More file actions
118 lines (97 loc) · 3.65 KB
/
Copy pathBasicWallet.kt
File metadata and controls
118 lines (97 loc) · 3.65 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package examples
import com.mazekine.nekoton.crypto.*
import com.mazekine.nekoton.models.*
import com.mazekine.nekoton.transport.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.delay
/**
* Basic wallet operations example.
*
* Demonstrates:
* - Key pair generation
* - Address creation
* - Balance checking
* - Simple transfers
* - Transaction monitoring
*/
class BasicWallet {
private val transport = JrpcTransport("https://rpc-testnet.tychoprotocol.com/")
fun demonstrateBasicOperations() = runBlocking {
println("=== Basic Wallet Operations ===")
// 1. Generate wallet
val wallet = generateWallet()
println("Generated wallet address: ${wallet.address}")
// 2. Check balance
val balance = getBalance(wallet.address)
println("Current balance: ${balance?.toTokens() ?: "0"} TYCHO")
// 3. Generate recipient
val recipient = generateWallet()
println("Recipient address: ${recipient.address}")
// 4. Send tokens (if balance available)
if (balance != null && balance > Tokens.fromTokens("0.1")) {
val txHash = sendTokens(
from = wallet,
to = recipient.address,
amount = Tokens.fromTokens("0.05")
)
println("Transaction sent: $txHash")
// 5. Wait for confirmation
waitForTransaction(recipient.address, txHash)
} else {
println("Insufficient balance for transfer. Fund the wallet first.")
}
transport.close()
}
private fun generateWallet(): Wallet {
val keyPair = KeyPair.generate()
val address = Address.fromPublicKey(keyPair.publicKey, 0) // workchain 0
return Wallet(keyPair, address)
}
private suspend fun getBalance(address: Address): Tokens? {
return try {
val accountState = transport.getAccountState(address)
accountState?.balance
} catch (e: Exception) {
println("Error getting balance: ${e.message}")
null
}
}
private suspend fun sendTokens(from: Wallet, to: Address, amount: Tokens): String {
// Create simple transfer message
val message = UnsignedExternalMessage(
dst = to,
stateInit = null,
body = createTransferBody(amount)
)
// Sign the message
val signedMessage = message.sign(from.keyPair)
// Send to blockchain
return transport.sendExternalMessage(signedMessage)
}
private fun createTransferBody(amount: Tokens): Cell {
// Simple transfer body (in real implementation, this would be more complex)
val builder = CellBuilder()
builder.storeUint(amount.nanoTokens, 128) // Amount in nano tokens
return builder.build()
}
private suspend fun waitForTransaction(address: Address, expectedTxHash: String) {
println("Waiting for transaction confirmation...")
repeat(30) { // Wait up to 30 seconds
delay(1000)
val transactions = transport.getTransactions(address, fromLt = null, count = 1)
if (transactions.isNotEmpty()) {
val latestTx = transactions.first()
println("Transaction confirmed: ${latestTx.id}")
return
}
}
println("Transaction not confirmed within timeout")
}
data class Wallet(
val keyPair: KeyPair,
val address: Address
)
}
fun main() {
BasicWallet().demonstrateBasicOperations()
}