@layerzerolabs/lz-v2-stellar-sdk 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -0
- package/dist/generated/bml.d.ts +452 -0
- package/dist/generated/bml.js +72 -0
- package/dist/generated/counter.d.ts +824 -0
- package/dist/generated/counter.js +125 -0
- package/dist/generated/endpoint.d.ts +1676 -0
- package/dist/generated/endpoint.js +216 -0
- package/dist/generated/sml.d.ts +810 -0
- package/dist/generated/sml.js +132 -0
- package/dist/generated/uln302.d.ts +1227 -0
- package/dist/generated/uln302.js +185 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/package.json +40 -0
- package/src/generated/bml.ts +506 -0
- package/src/generated/counter.ts +869 -0
- package/src/generated/endpoint.ts +1697 -0
- package/src/generated/sml.ts +864 -0
- package/src/generated/uln302.ts +1274 -0
- package/src/index.ts +5 -0
- package/test/index.test.ts +271 -0
- package/test/suites/constants.ts +13 -0
- package/test/suites/deploy.ts +277 -0
- package/test/suites/localnet.ts +42 -0
- package/test/suites/scan.ts +189 -0
- package/tsconfig.json +106 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { StrKey, TransactionBuilder } from '@stellar/stellar-sdk';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
|
|
4
|
+
import { $ } from 'zx';
|
|
5
|
+
|
|
6
|
+
import { getFullyQualifiedRepoRootPath } from '@layerzerolabs/common-node-utils';
|
|
7
|
+
import { PacketSerializer, PacketV1Codec } from '@layerzerolabs/lz-v2-utilities';
|
|
8
|
+
|
|
9
|
+
import { Client as CounterClient } from '../src/generated/counter';
|
|
10
|
+
import { Client as EndpointClient } from '../src/generated/endpoint';
|
|
11
|
+
import { Client as SMLClient } from '../src/generated/sml';
|
|
12
|
+
import { DEFAULT_DEPLOYER, EID, MSG_TYPE_VANILLA, NATIVE_TOKEN_ADDRESS, NETWORK_PASSPHRASE, ZRO_TOKEN_ADDRESS } from './suites/constants';
|
|
13
|
+
import { deployContract } from './suites/deploy';
|
|
14
|
+
import { startStellarLocalnet, stopStellarLocalnet } from './suites/localnet';
|
|
15
|
+
import { PacketSentEvent, scanPacketSentEvents } from './suites/scan';
|
|
16
|
+
|
|
17
|
+
$.verbose = true;
|
|
18
|
+
$.stdio = ['inherit', 'pipe', process.stderr];
|
|
19
|
+
|
|
20
|
+
const CONTRACT_ADDRESSES = {
|
|
21
|
+
endpointV2: '',
|
|
22
|
+
sml: '',
|
|
23
|
+
counter: '',
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let endpointClient: EndpointClient;
|
|
27
|
+
let smlClient: SMLClient;
|
|
28
|
+
let counterClient: CounterClient;
|
|
29
|
+
|
|
30
|
+
// NOTE: run `stellar contract build --features sandbox` before running the test
|
|
31
|
+
|
|
32
|
+
describe('Protocol testing', async () => {
|
|
33
|
+
const repoRoot = await getFullyQualifiedRepoRootPath();
|
|
34
|
+
const wasmDir = path.join(repoRoot, 'contracts', 'protocol', 'stellar', 'target', 'wasm32v1-none', 'release');
|
|
35
|
+
|
|
36
|
+
const ENDPOINT_WASM_PATH = path.join(wasmDir, 'endpoint_v2.wasm');
|
|
37
|
+
const SML_WASM_PATH = path.join(wasmDir, 'simple_message_lib.wasm');
|
|
38
|
+
const COUNTER_WASM_PATH = path.join(wasmDir, 'counter.wasm');
|
|
39
|
+
|
|
40
|
+
beforeAll(async () => {
|
|
41
|
+
await startStellarLocalnet();
|
|
42
|
+
}, 120000); // 2 minute timeout for setup
|
|
43
|
+
|
|
44
|
+
afterAll(async () => {
|
|
45
|
+
await stopStellarLocalnet();
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
describe('Contract Deployments', () => {
|
|
49
|
+
it('Deploy Endpoint', async () => {
|
|
50
|
+
endpointClient = await deployContract<EndpointClient>(
|
|
51
|
+
EndpointClient,
|
|
52
|
+
ENDPOINT_WASM_PATH,
|
|
53
|
+
{
|
|
54
|
+
eid: EID,
|
|
55
|
+
owner: DEFAULT_DEPLOYER.publicKey(),
|
|
56
|
+
native_token: NATIVE_TOKEN_ADDRESS,
|
|
57
|
+
},
|
|
58
|
+
DEFAULT_DEPLOYER,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
CONTRACT_ADDRESSES.endpointV2 = endpointClient.options.contractId;
|
|
62
|
+
console.log('✅ Endpoint deployed:', CONTRACT_ADDRESSES.endpointV2);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('Deploy SimpleMessageLib', async () => {
|
|
66
|
+
smlClient = await deployContract<SMLClient>(
|
|
67
|
+
SMLClient,
|
|
68
|
+
SML_WASM_PATH,
|
|
69
|
+
{
|
|
70
|
+
owner: DEFAULT_DEPLOYER.publicKey(),
|
|
71
|
+
endpoint: CONTRACT_ADDRESSES.endpointV2,
|
|
72
|
+
fee_recipient: DEFAULT_DEPLOYER.publicKey(),
|
|
73
|
+
},
|
|
74
|
+
DEFAULT_DEPLOYER,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
CONTRACT_ADDRESSES.sml = smlClient.options.contractId;
|
|
78
|
+
console.log('✅ SimpleMessageLib deployed:', CONTRACT_ADDRESSES.sml);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('Deploy Counter', async () => {
|
|
82
|
+
counterClient = await deployContract<CounterClient>(
|
|
83
|
+
CounterClient,
|
|
84
|
+
COUNTER_WASM_PATH,
|
|
85
|
+
{
|
|
86
|
+
owner: DEFAULT_DEPLOYER.publicKey(),
|
|
87
|
+
endpoint: CONTRACT_ADDRESSES.endpointV2,
|
|
88
|
+
delegate: DEFAULT_DEPLOYER.publicKey(),
|
|
89
|
+
},
|
|
90
|
+
DEFAULT_DEPLOYER,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
CONTRACT_ADDRESSES.counter = counterClient.options.contractId;
|
|
94
|
+
console.log('✅ Counter deployed:', CONTRACT_ADDRESSES.counter);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('Verify all contracts deployed', () => {
|
|
98
|
+
console.log('\n📋 All deployed contracts:');
|
|
99
|
+
console.log(' Endpoint:', CONTRACT_ADDRESSES.endpointV2);
|
|
100
|
+
console.log(' SimpleMessageLib:', CONTRACT_ADDRESSES.sml);
|
|
101
|
+
console.log(' Counter:', CONTRACT_ADDRESSES.counter);
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('Wire Contracts', async () => {
|
|
106
|
+
it('Register Library', async () => {
|
|
107
|
+
const assembledTx = await endpointClient.register_library({
|
|
108
|
+
new_lib: CONTRACT_ADDRESSES.sml,
|
|
109
|
+
});
|
|
110
|
+
await assembledTx.signAndSend();
|
|
111
|
+
const {result: libs} = await endpointClient.get_registered_libraries({
|
|
112
|
+
start: 0n,
|
|
113
|
+
max_count: 100n,
|
|
114
|
+
});
|
|
115
|
+
expect(libs.length).toBe(1);
|
|
116
|
+
expect(libs[0]).toBe(CONTRACT_ADDRESSES.sml);
|
|
117
|
+
console.log('✅ Library registered');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('Set Default Send Library', async () => {
|
|
121
|
+
const assembledTx = await endpointClient.set_default_send_library({
|
|
122
|
+
dst_eid: EID,
|
|
123
|
+
new_lib: CONTRACT_ADDRESSES.sml,
|
|
124
|
+
});
|
|
125
|
+
await assembledTx.signAndSend();
|
|
126
|
+
const {result: defaultSendLib} = await endpointClient.default_send_library({
|
|
127
|
+
dst_eid: EID,
|
|
128
|
+
});
|
|
129
|
+
expect(defaultSendLib).toBe(CONTRACT_ADDRESSES.sml);
|
|
130
|
+
console.log('✅ Default send library set');
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('Set Default Receive Library', async () => {
|
|
134
|
+
const assembledTx = await endpointClient.set_default_receive_library({
|
|
135
|
+
src_eid: EID,
|
|
136
|
+
new_lib: CONTRACT_ADDRESSES.sml,
|
|
137
|
+
grace_period: 0n,
|
|
138
|
+
});
|
|
139
|
+
await assembledTx.signAndSend();
|
|
140
|
+
const {result: defaultReceiveLib} = await endpointClient.default_receive_library({
|
|
141
|
+
src_eid: EID,
|
|
142
|
+
});
|
|
143
|
+
expect(defaultReceiveLib).toBe(CONTRACT_ADDRESSES.sml);
|
|
144
|
+
console.log('✅ Default receive library set');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('Set ZRO Token (Required even for 0 fee)', async () => {
|
|
148
|
+
const setZroTx = await endpointClient.set_zro({
|
|
149
|
+
zro: ZRO_TOKEN_ADDRESS,
|
|
150
|
+
});
|
|
151
|
+
await setZroTx.signAndSend();
|
|
152
|
+
|
|
153
|
+
const {result: newZroToken} = await endpointClient.zro();
|
|
154
|
+
expect(newZroToken).toBe(ZRO_TOKEN_ADDRESS);
|
|
155
|
+
console.log('✅ ZRO token set:', ZRO_TOKEN_ADDRESS);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('Set Counter Peer', async () => {
|
|
159
|
+
const peerBytes = StrKey.decodeContract(CONTRACT_ADDRESSES.counter);
|
|
160
|
+
|
|
161
|
+
const assembledTx = await counterClient.set_peer({
|
|
162
|
+
eid: EID,
|
|
163
|
+
peer: Buffer.from(peerBytes),
|
|
164
|
+
});
|
|
165
|
+
await assembledTx.signAndSend();
|
|
166
|
+
|
|
167
|
+
const {result: peer} = await counterClient.peers({
|
|
168
|
+
eid: EID,
|
|
169
|
+
});
|
|
170
|
+
expect(peer?.toString()).toBe(Buffer.from(peerBytes).toString());
|
|
171
|
+
console.log('✅ Counter peer set for EID', EID);
|
|
172
|
+
});
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
describe('Counter', async () => {
|
|
176
|
+
let incrementLedger = 0;
|
|
177
|
+
let packetSentEvent: PacketSentEvent;
|
|
178
|
+
let guid: Buffer;
|
|
179
|
+
let message: Buffer;
|
|
180
|
+
it('Counter Increment', async () => {
|
|
181
|
+
const {result: fee} = await counterClient.quote({
|
|
182
|
+
dst_eid: EID,
|
|
183
|
+
msg_type: MSG_TYPE_VANILLA,
|
|
184
|
+
options: Buffer.from([]),
|
|
185
|
+
pay_in_zro: true,
|
|
186
|
+
});
|
|
187
|
+
console.log('✅ Fee:', fee);
|
|
188
|
+
|
|
189
|
+
const assembledTx = await counterClient.increment({
|
|
190
|
+
sender: DEFAULT_DEPLOYER.publicKey(),
|
|
191
|
+
dst_eid: EID,
|
|
192
|
+
msg_type: MSG_TYPE_VANILLA,
|
|
193
|
+
options: Buffer.from([]),
|
|
194
|
+
fee: fee,
|
|
195
|
+
});
|
|
196
|
+
const sentTx = await assembledTx.signAndSend();
|
|
197
|
+
|
|
198
|
+
// Extract ledger number from transaction response
|
|
199
|
+
const txResponse = sentTx.getTransactionResponse;
|
|
200
|
+
if (txResponse && 'ledger' in txResponse) {
|
|
201
|
+
incrementLedger = txResponse.ledger;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const {result: outboundCount} = await counterClient.outbound_count({
|
|
205
|
+
eid: EID,
|
|
206
|
+
});
|
|
207
|
+
expect(outboundCount).toBe(1n);
|
|
208
|
+
console.log('✅ Counter incremented, outbound count:', outboundCount);
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
it('Scan PacketSent Events', async () => {
|
|
212
|
+
const packetSentEvents = await scanPacketSentEvents(
|
|
213
|
+
CONTRACT_ADDRESSES.endpointV2,
|
|
214
|
+
incrementLedger,
|
|
215
|
+
);
|
|
216
|
+
expect(packetSentEvents.length).toBeGreaterThan(0);
|
|
217
|
+
packetSentEvent = packetSentEvents[0];
|
|
218
|
+
console.log(`✅ PacketSent events scanned successfully. Found ${packetSentEvents.length} events`);
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('Verify Counter Message', async () => {
|
|
222
|
+
const packet = PacketSerializer.deserialize(packetSentEvent.encoded_packet);
|
|
223
|
+
guid = Buffer.from(packet.guid.replace('0x', ''), 'hex');
|
|
224
|
+
message = Buffer.from(packet.message.replace('0x', ''), 'hex');
|
|
225
|
+
const codec = PacketV1Codec.from(packet)
|
|
226
|
+
const packetHeader = codec.header()
|
|
227
|
+
const payloadHash = codec.payloadHash()
|
|
228
|
+
const assembledTx = await smlClient.validate_packet({
|
|
229
|
+
caller: DEFAULT_DEPLOYER.publicKey(),
|
|
230
|
+
header_bytes: Buffer.from(packetHeader.replace('0x', ''), 'hex'),
|
|
231
|
+
payload_hash: Buffer.from(payloadHash.replace('0x', ''), 'hex'),
|
|
232
|
+
});
|
|
233
|
+
await assembledTx.signAndSend();
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('Receive Counter Message', async () => {
|
|
237
|
+
const assembledTx = await counterClient.lz_receive({
|
|
238
|
+
executor: DEFAULT_DEPLOYER.publicKey(),
|
|
239
|
+
origin: {
|
|
240
|
+
nonce: 1n,
|
|
241
|
+
sender: Buffer.from(StrKey.decodeContract(CONTRACT_ADDRESSES.counter)),
|
|
242
|
+
src_eid: EID,
|
|
243
|
+
},
|
|
244
|
+
guid,
|
|
245
|
+
message,
|
|
246
|
+
extra_data: Buffer.from([]),
|
|
247
|
+
value: 0n,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
const sentTx = await assembledTx.signAndSend();
|
|
251
|
+
|
|
252
|
+
// Extract and print the transaction fee from the result
|
|
253
|
+
const txResponse = sentTx.getTransactionResponse;
|
|
254
|
+
if (txResponse && 'envelopeXdr' in txResponse && txResponse.envelopeXdr) {
|
|
255
|
+
try {
|
|
256
|
+
const txXdr = txResponse.envelopeXdr.toXDR('base64');
|
|
257
|
+
const parsedTx = TransactionBuilder.fromXDR(txXdr, NETWORK_PASSPHRASE);
|
|
258
|
+
const fee = parsedTx.fee;
|
|
259
|
+
console.log(`💰 lz_receive transaction fee: ${fee} stroops (${(Number(fee) / 10000000).toFixed(7)} XLM)`);
|
|
260
|
+
} catch (_e) {
|
|
261
|
+
// Ignore if we can't extract the fee
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const {result: inboundCount} = await counterClient.inbound_count({
|
|
265
|
+
eid: EID,
|
|
266
|
+
});
|
|
267
|
+
expect(inboundCount).toBe(1n);
|
|
268
|
+
console.log('✅ Counter message received, inbox count:', inboundCount);
|
|
269
|
+
});
|
|
270
|
+
})
|
|
271
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Asset, Keypair, Networks} from '@stellar/stellar-sdk';
|
|
2
|
+
|
|
3
|
+
const CORE_URL = 'http://localhost:8000';
|
|
4
|
+
export const FRIENDBOT_URL = `${CORE_URL}/friendbot`;
|
|
5
|
+
export const RPC_URL = `${CORE_URL}/rpc`;
|
|
6
|
+
export const NETWORK_PASSPHRASE = Networks.STANDALONE;
|
|
7
|
+
export const DEFAULT_DEPLOYER = Keypair.fromSecret('SDLCA3JUES3G6R4FTI6XXDIWW7QCNMZNWPYQQIKQ26TEIZUFOLIVIUDK');
|
|
8
|
+
export const ZRO_DISTRIBUTOR = Keypair.fromSecret('SB6QAFXFRR2MXYHW4RRZ23JDGKHDCYCT5YTQEGG3WNT5VKZADJQFVNWG');
|
|
9
|
+
export const EID = 30111;
|
|
10
|
+
export const NATIVE_TOKEN_ADDRESS = Asset.native().contractId(NETWORK_PASSPHRASE);
|
|
11
|
+
export const ZRO_ASSET = new Asset('ZRO', DEFAULT_DEPLOYER.publicKey());
|
|
12
|
+
export const ZRO_TOKEN_ADDRESS = ZRO_ASSET.contractId(NETWORK_PASSPHRASE);
|
|
13
|
+
export const MSG_TYPE_VANILLA = 1;
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Asset,
|
|
3
|
+
BASE_FEE,
|
|
4
|
+
hash,
|
|
5
|
+
Keypair,
|
|
6
|
+
Operation,
|
|
7
|
+
rpc,
|
|
8
|
+
TransactionBuilder,
|
|
9
|
+
xdr
|
|
10
|
+
} from '@stellar/stellar-sdk';
|
|
11
|
+
import { readFileSync } from 'fs';
|
|
12
|
+
|
|
13
|
+
import { DEFAULT_DEPLOYER, NETWORK_PASSPHRASE, RPC_URL, ZRO_ASSET, ZRO_DISTRIBUTOR } from './constants';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Query and display the TTL (Time To Live) of uploaded WASM code
|
|
17
|
+
*
|
|
18
|
+
* @param wasmHash - The hex-encoded SHA-256 hash of the WASM code
|
|
19
|
+
* @param server - The Stellar RPC server instance
|
|
20
|
+
* @param rpcUrl - Optional RPC URL (defaults to RPC_URL constant)
|
|
21
|
+
*/
|
|
22
|
+
async function queryWasmTtl(
|
|
23
|
+
wasmHash: string,
|
|
24
|
+
server: rpc.Server,
|
|
25
|
+
rpcUrl?: string
|
|
26
|
+
): Promise<void> {
|
|
27
|
+
try {
|
|
28
|
+
const latestLedger = await server.getLatestLedger();
|
|
29
|
+
const currentLedger = latestLedger.sequence;
|
|
30
|
+
|
|
31
|
+
// Create the LedgerKey for contract code using XDR encoding
|
|
32
|
+
const wasmHashBuffer = Buffer.from(wasmHash, 'hex');
|
|
33
|
+
// Ensure hash is exactly 32 bytes
|
|
34
|
+
const hashBytes = wasmHashBuffer.length === 32 ? wasmHashBuffer : wasmHashBuffer.slice(0, 32);
|
|
35
|
+
// Create LedgerKeyContractCode with hash
|
|
36
|
+
const ledgerKeyContractCode = new xdr.LedgerKeyContractCode({
|
|
37
|
+
hash: hashBytes
|
|
38
|
+
});
|
|
39
|
+
const ledgerKey = xdr.LedgerKey.contractCode(ledgerKeyContractCode);
|
|
40
|
+
const ledgerKeyXdr = ledgerKey.toXDR('base64');
|
|
41
|
+
|
|
42
|
+
// Query contract code entry using direct RPC call
|
|
43
|
+
const rpcEndpoint = rpcUrl || (server as any).serverURL || RPC_URL;
|
|
44
|
+
const response = await fetch(rpcEndpoint, {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: { 'Content-Type': 'application/json' },
|
|
47
|
+
body: JSON.stringify({
|
|
48
|
+
jsonrpc: '2.0',
|
|
49
|
+
id: 1,
|
|
50
|
+
method: 'getLedgerEntries',
|
|
51
|
+
params: {
|
|
52
|
+
keys: [ledgerKeyXdr]
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const result = await response.json();
|
|
58
|
+
if (result.error) {
|
|
59
|
+
console.warn(`⚠️ Could not retrieve WASM TTL: ${result.error.message}`);
|
|
60
|
+
} else if (result.result?.entries?.[0]?.liveUntilLedgerSeq) {
|
|
61
|
+
const liveUntilLedgerSeq = result.result.entries[0].liveUntilLedgerSeq;
|
|
62
|
+
const ttlLedgers = liveUntilLedgerSeq - currentLedger;
|
|
63
|
+
const ttlDays = (ttlLedgers * 5) / (24 * 3600); // ~5 seconds per ledger
|
|
64
|
+
console.log(`⏰ WASM TTL: live until ledger ${liveUntilLedgerSeq} (${ttlLedgers} ledgers remaining, ~${ttlDays.toFixed(2)} days)`);
|
|
65
|
+
}
|
|
66
|
+
} catch (error) {
|
|
67
|
+
// If querying TTL fails, it might be because the code isn't indexed yet
|
|
68
|
+
// This is non-fatal, so we just log a warning
|
|
69
|
+
console.warn(`⚠️ Could not retrieve WASM TTL: ${error instanceof Error ? error.message : String(error)}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export async function uploadWasm(
|
|
74
|
+
wasmBuffer: Buffer,
|
|
75
|
+
keypair: Keypair,
|
|
76
|
+
server: rpc.Server,
|
|
77
|
+
): Promise<string> {
|
|
78
|
+
console.log(`📦 WASM buffer size: ${wasmBuffer.length} bytes (${(wasmBuffer.length / 1024).toFixed(2)} KB)`);
|
|
79
|
+
|
|
80
|
+
const account = await server.getAccount(keypair.publicKey());
|
|
81
|
+
|
|
82
|
+
const uploadTx = new TransactionBuilder(account, {
|
|
83
|
+
fee: BASE_FEE,
|
|
84
|
+
networkPassphrase: NETWORK_PASSPHRASE,
|
|
85
|
+
})
|
|
86
|
+
.addOperation(Operation.uploadContractWasm({ wasm: wasmBuffer }))
|
|
87
|
+
.setTimeout(30)
|
|
88
|
+
.build();
|
|
89
|
+
|
|
90
|
+
const simulated = await server.simulateTransaction(uploadTx);
|
|
91
|
+
const preparedTx = rpc.assembleTransaction(uploadTx, simulated).build();
|
|
92
|
+
|
|
93
|
+
console.log(`💰 Upload transaction fee: ${preparedTx.fee} stroops (${(Number(preparedTx.fee) / 10000000).toFixed(7)} XLM)`);
|
|
94
|
+
|
|
95
|
+
preparedTx.sign(keypair);
|
|
96
|
+
|
|
97
|
+
const sendResult = await server.sendTransaction(preparedTx);
|
|
98
|
+
|
|
99
|
+
if (sendResult.status !== 'PENDING') {
|
|
100
|
+
throw new Error(`Transaction failed: ${JSON.stringify(sendResult)}`);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Wait for transaction to be confirmed
|
|
104
|
+
const txResult = await server.pollTransaction(sendResult.hash);
|
|
105
|
+
|
|
106
|
+
if (txResult.status !== 'SUCCESS') {
|
|
107
|
+
throw new Error(`Transaction not successful: ${JSON.stringify(txResult)}`);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Compute the WASM hash (SHA-256 of the WASM bytes)
|
|
111
|
+
const wasmHash = hash(wasmBuffer).toString('hex');
|
|
112
|
+
|
|
113
|
+
// Query and display the WASM code TTL
|
|
114
|
+
await queryWasmTtl(wasmHash, server);
|
|
115
|
+
|
|
116
|
+
return wasmHash;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Generic contract deployment helper that works with any contract Client
|
|
121
|
+
*
|
|
122
|
+
* @param ClientClass - The contract Client class (e.g., EndpointClient, SMLClient)
|
|
123
|
+
* @param wasmFilePath - Path to the compiled WASM file
|
|
124
|
+
* @param constructorArgs - Arguments for the contract's constructor
|
|
125
|
+
* @param deployer - The keypair that will deploy the contract
|
|
126
|
+
* @param options - Optional deployment options (salt, fee, timeout, etc.)
|
|
127
|
+
* @returns The deployed contract's Client instance with the contractId
|
|
128
|
+
*/
|
|
129
|
+
export async function deployContract<T extends { options: { contractId: string } }>(
|
|
130
|
+
ClientClass: {
|
|
131
|
+
deploy: (
|
|
132
|
+
args: any,
|
|
133
|
+
options: any,
|
|
134
|
+
) => Promise<{ signAndSend: () => Promise<{ result: T }> }>;
|
|
135
|
+
},
|
|
136
|
+
wasmFilePath: string,
|
|
137
|
+
constructorArgs: any,
|
|
138
|
+
deployer: Keypair,
|
|
139
|
+
options: {
|
|
140
|
+
salt?: Buffer;
|
|
141
|
+
rpcUrl?: string;
|
|
142
|
+
networkPassphrase?: string;
|
|
143
|
+
allowHttp?: boolean;
|
|
144
|
+
} = {},
|
|
145
|
+
): Promise<T> {
|
|
146
|
+
const {
|
|
147
|
+
rpcUrl = RPC_URL,
|
|
148
|
+
networkPassphrase = NETWORK_PASSPHRASE,
|
|
149
|
+
allowHttp = true,
|
|
150
|
+
salt,
|
|
151
|
+
} = options;
|
|
152
|
+
|
|
153
|
+
const server = new rpc.Server(rpcUrl, {
|
|
154
|
+
allowHttp: allowHttp,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// Step 1: Read WASM file
|
|
158
|
+
console.log('📖 Reading WASM file from:', wasmFilePath);
|
|
159
|
+
const wasmBuffer = readFileSync(wasmFilePath);
|
|
160
|
+
|
|
161
|
+
// Step 2: Upload WASM and get hash
|
|
162
|
+
console.log('📤 Uploading WASM...');
|
|
163
|
+
const wasmHash = await uploadWasm(wasmBuffer, deployer, server);
|
|
164
|
+
console.log('✅ WASM uploaded, hash:', wasmHash);
|
|
165
|
+
|
|
166
|
+
// Step 3: Deploy the contract
|
|
167
|
+
console.log('🚀 Deploying contract...');
|
|
168
|
+
const deployTx = await ClientClass.deploy(constructorArgs, {
|
|
169
|
+
wasmHash: wasmHash,
|
|
170
|
+
publicKey: deployer.publicKey(),
|
|
171
|
+
signTransaction: async (tx: string) => {
|
|
172
|
+
const transaction = TransactionBuilder.fromXDR(tx, networkPassphrase);
|
|
173
|
+
transaction.sign(deployer);
|
|
174
|
+
return {
|
|
175
|
+
signedTxXdr: transaction.toXDR(),
|
|
176
|
+
signerAddress: deployer.publicKey(),
|
|
177
|
+
};
|
|
178
|
+
},
|
|
179
|
+
rpcUrl: rpcUrl,
|
|
180
|
+
networkPassphrase: networkPassphrase,
|
|
181
|
+
allowHttp: allowHttp,
|
|
182
|
+
salt: salt,
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Step 4: Sign and send
|
|
186
|
+
const sentTx = await deployTx.signAndSend();
|
|
187
|
+
|
|
188
|
+
// Step 5: Extract contract ID from result
|
|
189
|
+
const contractClient = sentTx.result;
|
|
190
|
+
const contractId = contractClient.options.contractId;
|
|
191
|
+
console.log('✅ Contract deployed at:', contractId);
|
|
192
|
+
|
|
193
|
+
return contractClient;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export async function deployNativeSac(): Promise<void> {
|
|
197
|
+
await deployAssetSac(Asset.native());
|
|
198
|
+
console.log('✅ Native SAC deployed');
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export async function deployZroToken(): Promise<void> {
|
|
202
|
+
const server = new rpc.Server(RPC_URL, {
|
|
203
|
+
allowHttp: true,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
// First, issue the ZRO token.
|
|
207
|
+
// We can't changeTrust of Issuer account, because the Issuer can't hold the asset.
|
|
208
|
+
const account = await server.getAccount(DEFAULT_DEPLOYER.publicKey());
|
|
209
|
+
const transaction = new TransactionBuilder(account, {
|
|
210
|
+
fee: BASE_FEE,
|
|
211
|
+
networkPassphrase: NETWORK_PASSPHRASE,
|
|
212
|
+
})
|
|
213
|
+
.addOperation(Operation.changeTrust({ asset: ZRO_ASSET, source: ZRO_DISTRIBUTOR.publicKey() }))
|
|
214
|
+
.addOperation(Operation.payment({ asset: ZRO_ASSET, amount: '10000', destination: ZRO_DISTRIBUTOR.publicKey() }))
|
|
215
|
+
.setTimeout(10)
|
|
216
|
+
.build();
|
|
217
|
+
transaction.sign(DEFAULT_DEPLOYER, ZRO_DISTRIBUTOR);
|
|
218
|
+
|
|
219
|
+
const sendResult = await server.sendTransaction(transaction);
|
|
220
|
+
if (sendResult.status !== 'PENDING') {
|
|
221
|
+
throw new Error(`Failed to issue ZRO token: ${JSON.stringify(sendResult)}`);
|
|
222
|
+
}
|
|
223
|
+
const txResult = await server.pollTransaction(sendResult.hash);
|
|
224
|
+
if (txResult.status !== 'SUCCESS') {
|
|
225
|
+
throw new Error(`Failed to issue ZRO token: ${JSON.stringify(txResult)}`);
|
|
226
|
+
}
|
|
227
|
+
console.log('✅ ZRO asset issued');
|
|
228
|
+
|
|
229
|
+
// Deploy the Stellar Asset Contract (SAC)
|
|
230
|
+
await deployAssetSac(ZRO_ASSET);
|
|
231
|
+
console.log('✅ ZRO SAC deployed');
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Deploy SAC for a custom asset using TypeScript
|
|
236
|
+
*/
|
|
237
|
+
export async function deployAssetSac(asset: Asset): Promise<string> {
|
|
238
|
+
console.log('Deploying SAC for asset:', asset.toString());
|
|
239
|
+
|
|
240
|
+
const server = new rpc.Server(RPC_URL, { allowHttp: true });
|
|
241
|
+
const account = await server.getAccount(DEFAULT_DEPLOYER.publicKey());
|
|
242
|
+
|
|
243
|
+
// Build transaction with createStellarAssetContract operation
|
|
244
|
+
const deployTx = new TransactionBuilder(account, {
|
|
245
|
+
fee: BASE_FEE,
|
|
246
|
+
networkPassphrase: NETWORK_PASSPHRASE,
|
|
247
|
+
})
|
|
248
|
+
.addOperation(
|
|
249
|
+
Operation.createStellarAssetContract({
|
|
250
|
+
asset: asset,
|
|
251
|
+
})
|
|
252
|
+
)
|
|
253
|
+
.setTimeout(30)
|
|
254
|
+
.build();
|
|
255
|
+
|
|
256
|
+
// Simulate transaction first (required for contract operations)
|
|
257
|
+
const simulated = await server.simulateTransaction(deployTx);
|
|
258
|
+
|
|
259
|
+
// Check if simulation was successful
|
|
260
|
+
if (rpc.Api.isSimulationError(simulated)) {
|
|
261
|
+
throw new Error(`Transaction simulation failed: ${JSON.stringify(simulated)}`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const preparedTx = rpc.assembleTransaction(deployTx, simulated).build();
|
|
265
|
+
|
|
266
|
+
// Sign and send
|
|
267
|
+
preparedTx.sign(DEFAULT_DEPLOYER);
|
|
268
|
+
const sendResult = await server.sendTransaction(preparedTx);
|
|
269
|
+
if (sendResult.status !== 'PENDING') {
|
|
270
|
+
throw new Error(`Failed to deploy SAC: ${JSON.stringify(sendResult)}`);
|
|
271
|
+
}
|
|
272
|
+
const txResult = await server.pollTransaction(sendResult.hash);
|
|
273
|
+
if (txResult.status !== 'SUCCESS') {
|
|
274
|
+
throw new Error(`SAC deployment not successful: ${JSON.stringify(txResult)}`);
|
|
275
|
+
}
|
|
276
|
+
return asset.contractId(NETWORK_PASSPHRASE);
|
|
277
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import axios from 'axios';
|
|
2
|
+
import { $,sleep } from 'zx';
|
|
3
|
+
|
|
4
|
+
import { DEFAULT_DEPLOYER, FRIENDBOT_URL, ZRO_DISTRIBUTOR } from './constants';
|
|
5
|
+
import { deployNativeSac, deployZroToken } from './deploy';
|
|
6
|
+
|
|
7
|
+
export async function startStellarLocalnet(): Promise<void> {
|
|
8
|
+
console.log('🚀 Starting Stellar localnet...');
|
|
9
|
+
|
|
10
|
+
//TODO: pnpm localnet up --only stellar
|
|
11
|
+
await $`stellar container start local`;
|
|
12
|
+
|
|
13
|
+
// Wait for Stellar to start
|
|
14
|
+
for (let i = 0; i < 60; i++) {
|
|
15
|
+
try {
|
|
16
|
+
// Ensure faucet service is started and fund the default deployer account
|
|
17
|
+
await fundAccount(DEFAULT_DEPLOYER.publicKey());
|
|
18
|
+
await fundAccount(ZRO_DISTRIBUTOR.publicKey());
|
|
19
|
+
console.log(`✅ Account ${DEFAULT_DEPLOYER.publicKey()} funded`);
|
|
20
|
+
console.log('✅ Stellar localnet started');
|
|
21
|
+
break;
|
|
22
|
+
} catch (_e) {
|
|
23
|
+
await sleep(1000);
|
|
24
|
+
console.log('⏳ Waiting for Stellar localnet to start...');
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
await deployNativeSac();
|
|
28
|
+
await deployZroToken();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function fundAccount(publicKey: string): Promise<void> {
|
|
32
|
+
await axios.get(FRIENDBOT_URL, {
|
|
33
|
+
params: {
|
|
34
|
+
addr: publicKey,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export async function stopStellarLocalnet(): Promise<void> {
|
|
40
|
+
await $`stellar container stop`;
|
|
41
|
+
console.log('✅ Stellar localnet stopped');
|
|
42
|
+
}
|