-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxion-transactions.js
More file actions
118 lines (105 loc) · 3.32 KB
/
Copy pathxion-transactions.js
File metadata and controls
118 lines (105 loc) · 3.32 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
// xion-transactions.js
const { getSigningClient, getSigningCosmWasmClient } = require("./xion-connect");
const { getMyAddress } = require("./xion-wallets");
const config = require('./config');
/**
* Sends tokens from your address to a recipient
* Uses the mnemonic from environment variables by default
*
* @param {string} recipientAddress - The recipient's address
* @param {string} amount - The amount to send
* @param {string} denom - The token denomination (default: uxion)
* @param {string} memo - Optional transaction memo
* @returns {object} Transaction result with hash and gas usage
*/
async function sendTokens(recipientAddress, amount, denom = "uxion", memo = "") {
const client = await getSigningClient();
const senderAddress = await getMyAddress();
const result = await client.sendTokens(
senderAddress,
recipientAddress,
[{ denom, amount }],
"auto", // fee calculation
memo || "Transfer via XION backend"
);
return {
transactionHash: result.transactionHash,
gasUsed: result.gasUsed,
gasWanted: result.gasWanted
};
}
/**
* Executes a smart contract function
*
* @param {string} contractAddress - The contract address
* @param {object} msg - The execute message in JSON format
* @param {array} funds - Optional funds to send with the execution
* @returns {object} Transaction result with hash and gas usage
*/
async function executeContract(contractAddress, msg, funds = []) {
const client = await getSigningCosmWasmClient();
const senderAddress = await getMyAddress();
const result = await client.execute(
senderAddress,
contractAddress,
msg,
"auto",
"Execute contract via XION backend",
funds
);
return {
transactionHash: result.transactionHash,
gasUsed: result.gasUsed,
gasWanted: result.gasWanted
};
}
/**
* Uploads a smart contract WASM binary
*
* @param {Uint8Array} wasmBinary - The contract WASM binary
* @returns {object} Upload result with code ID and transaction hash
*/
async function uploadContract(wasmBinary) {
const client = await getSigningClient();
const senderAddress = await getMyAddress();
const result = await client.upload(
senderAddress,
wasmBinary,
"auto"
);
return {
codeId: result.codeId,
transactionHash: result.transactionHash
};
}
/**
* Instantiates a smart contract from an uploaded code ID
*
* @param {number} codeId - The uploaded contract code ID
* @param {object} initMsg - Initialization message in JSON format
* @param {string} label - Human-readable label for the contract
* @param {array} funds - Optional funds to send with instantiation
* @returns {object} Result with contract address and transaction hash
*/
async function instantiateContract(codeId, initMsg, label, funds = []) {
const client = await getSigningClient();
const senderAddress = await getMyAddress();
const result = await client.instantiate(
senderAddress,
codeId,
initMsg,
label,
"auto",
{ funds }
);
return {
contractAddress: result.contractAddress,
transactionHash: result.transactionHash
};
}
module.exports = {
sendTokens,
executeContract,
uploadContract,
instantiateContract
};