-
Notifications
You must be signed in to change notification settings - Fork 2
/
vault-example.ts
89 lines (74 loc) · 2.82 KB
/
vault-example.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
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
import { Vault, SorobanNetwork } from '../src/Vault';
import { SorobanRpc, Networks, Keypair } from '@stellar/stellar-sdk';
async function createSorobanContext() {
const server = new SorobanRpc.Server("https://soroban-testnet.stellar.org", {
allowHttp: true
});
const secretKey = 'SC352W6PEHWSHYKP5IYO3HWAEVGLTVLZW5WE3UXPWSGKBST5K6DKRT7F';
const keypair = Keypair.fromSecret(secretKey);
return {
server,
address: keypair.publicKey(),
activeChain: {
networkPassphrase: Networks.TESTNET,
network: "TESTNET",
id: "testnet",
networkUrl: "https://soroban-testnet.stellar.org"
},
activeConnector: undefined,
chains: [],
connectors: [],
connect: async () => { },
disconnect: async () => { }
};
}
async function vaultExample() {
const sorobanContext = await createSorobanContext();
const secretKey = 'SC352W6PEHWSHYKP5IYO3HWAEVGLTVLZW5WE3UXPWSGKBST5K6DKRT7F';
// Initialize the Vault
const vault = new Vault({
network: SorobanNetwork.TESTNET,
contractId: 'CAJVX4P2XDRVIGZ7GRHRSGBE6B23L4FQLT5SGUBHU7NT2IER7R2WIURG' // Replace with your contract ID
});
try {
const accountAddress = sorobanContext.address;
console.log('Using account:', accountAddress);
// Check initial balance
const initialBalance = await vault.balance(accountAddress, sorobanContext);
console.log('Initial balance:', initialBalance);
// Deposit 100 tokens
const depositTx = await vault.deposit(
accountAddress,
100,
true,
sorobanContext,
secretKey
);
console.log('Deposit transaction hash:', depositTx);
// Wait a few seconds for the transaction to be processed
await new Promise(resolve => setTimeout(resolve, 5000));
// Check updated balance
const updatedBalance = await vault.balance(accountAddress, sorobanContext);
console.log('Updated balance:', updatedBalance);
// Withdraw 50 tokens
const withdrawTx = await vault.withdraw(
accountAddress,
50,
true,
sorobanContext,
secretKey
);
console.log('Withdraw transaction hash:', withdrawTx);
// Wait a few seconds for the transaction to be processed
await new Promise(resolve => setTimeout(resolve, 5000));
// Check final balance
const finalBalance = await vault.balance(accountAddress, sorobanContext);
console.log('Final balance:', finalBalance);
} catch (error) {
console.error('Error:', error);
}
}
// Execute the example
vaultExample()
.then(() => console.log('Example completed'))
.catch(error => console.error('Example failed:', error));