-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartContractInteraction.kt
More file actions
217 lines (183 loc) · 7.54 KB
/
Copy pathSmartContractInteraction.kt
File metadata and controls
217 lines (183 loc) · 7.54 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package examples
import com.mazekine.nekoton.crypto.*
import com.mazekine.nekoton.models.*
import com.mazekine.nekoton.transport.*
import com.mazekine.nekoton.abi.*
import kotlinx.coroutines.runBlocking
/**
* Smart contract interaction example.
*
* Demonstrates:
* - Loading contract ABI
* - Encoding function calls
* - Calling contract methods
* - Decoding return values
* - Event monitoring
*/
class SmartContractInteraction {
private val transport = JrpcTransport("https://rpc-testnet.tychoprotocol.com/")
// Sample TIP3 Token contract ABI
private val tokenAbi = """
{
"ABI version": 2,
"functions": [
{
"name": "transfer",
"inputs": [
{"name": "to", "type": "address"},
{"name": "tokens", "type": "uint128"},
{"name": "grams", "type": "uint128"},
{"name": "send_gas_to", "type": "address"},
{"name": "notify", "type": "bool"},
{"name": "payload", "type": "cell"}
],
"outputs": []
},
{
"name": "balance",
"inputs": [],
"outputs": [
{"name": "value0", "type": "uint128"}
]
},
{
"name": "details",
"inputs": [],
"outputs": [
{"name": "name", "type": "string"},
{"name": "symbol", "type": "string"},
{"name": "decimals", "type": "uint8"},
{"name": "root_public_key", "type": "uint256"},
{"name": "root_address", "type": "address"}
]
}
],
"events": [
{
"name": "Transfer",
"inputs": [
{"name": "from", "type": "address"},
{"name": "to", "type": "address"},
{"name": "tokens", "type": "uint128"}
]
}
]
}
""".trimIndent()
fun demonstrateContractInteraction() = runBlocking {
println("=== Smart Contract Interaction ===")
// 1. Load contract ABI
val contractAbi = ContractAbi.fromJson(tokenAbi)
println("Loaded ABI with ${contractAbi.functions.size} functions")
// 2. Contract and wallet setup
val walletKeyPair = KeyPair.generate()
val contractAddress = Address("0:6bfe535f0b054fcd04f2a5c8c70bbbcb5e8f339d44b499a0307b69e7545c4241")
val recipientAddress = Address("0:abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")
// 3. Get token details
getTokenDetails(contractAbi, contractAddress)
// 4. Check token balance
getTokenBalance(contractAbi, contractAddress)
// 5. Transfer tokens
transferTokens(
contractAbi = contractAbi,
contractAddress = contractAddress,
senderKeyPair = walletKeyPair,
recipientAddress = recipientAddress,
amount = Tokens.fromTokens("10.5")
)
transport.close()
}
private suspend fun getTokenDetails(abi: ContractAbi, contractAddress: Address) {
println("\n--- Getting Token Details ---")
try {
val detailsFunction = abi.getFunction("details")
?: throw IllegalStateException("Details function not found")
// Create call message
val callMessage = UnsignedExternalMessage(
dst = contractAddress,
stateInit = null,
body = detailsFunction.encodeCall(emptyMap())
)
// For getter methods, we would typically use a different approach
// This is a simplified example
println("Token details call encoded successfully")
} catch (e: Exception) {
println("Error getting token details: ${e.message}")
}
}
private suspend fun getTokenBalance(abi: ContractAbi, contractAddress: Address) {
println("\n--- Getting Token Balance ---")
try {
val balanceFunction = abi.getFunction("balance")
?: throw IllegalStateException("Balance function not found")
val callMessage = UnsignedExternalMessage(
dst = contractAddress,
stateInit = null,
body = balanceFunction.encodeCall(emptyMap())
)
println("Balance call encoded successfully")
} catch (e: Exception) {
println("Error getting balance: ${e.message}")
}
}
private suspend fun transferTokens(
contractAbi: ContractAbi,
contractAddress: Address,
senderKeyPair: KeyPair,
recipientAddress: Address,
amount: Tokens
) {
println("\n--- Transferring Tokens ---")
try {
val transferFunction = contractAbi.getFunction("transfer")
?: throw IllegalStateException("Transfer function not found")
// Prepare function inputs
val inputs = mapOf(
"to" to recipientAddress.toString(),
"tokens" to amount.nanoTokens.toString(),
"grams" to "100000000", // 0.1 EVER for fees
"send_gas_to" to senderKeyPair.publicKey.address.toString(),
"notify" to "true",
"payload" to Cell.empty().toBoc().let { "te6ccgEBAQEAAgAAAA==" } // Empty cell in base64
)
// Encode function call
val functionCall = transferFunction.encodeCall(inputs)
// Create external message
val message = UnsignedExternalMessage(
dst = contractAddress,
stateInit = null,
body = functionCall
)
// Sign and send
val signedMessage = message.sign(senderKeyPair)
val txHash = transport.sendExternalMessage(signedMessage)
println("Token transfer sent!")
println("Transaction hash: $txHash")
println("Amount: ${amount.toTokens()} tokens")
println("Recipient: $recipientAddress")
} catch (e: Exception) {
println("Error transferring tokens: ${e.message}")
}
}
private suspend fun monitorTransferEvents(abi: ContractAbi, contractAddress: Address) {
println("\n--- Monitoring Transfer Events ---")
try {
val transferEvent = abi.getEvent("Transfer")
?: throw IllegalStateException("Transfer event not found")
// Subscribe to account state changes to detect new transactions
transport.subscribeToTransactions(contractAddress).collect { transaction ->
// In a real implementation, you would parse the transaction
// to extract event data
println("New transaction detected: ${transaction.id}")
// Decode event data from transaction
// val eventData = transferEvent.decodeData(eventCell)
// println("Transfer: ${eventData}")
}
} catch (e: Exception) {
println("Error monitoring events: ${e.message}")
}
}
}
fun main() {
SmartContractInteraction().demonstrateContractInteraction()
}