-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxdr.test.ts
More file actions
157 lines (149 loc) · 3.97 KB
/
Copy pathxdr.test.ts
File metadata and controls
157 lines (149 loc) · 3.97 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
import { assert, StructError } from '@metamask/superstruct';
import {
Account,
Asset,
Contract,
Keypair,
Networks,
Operation,
TransactionBuilder,
} from '@stellar/stellar-sdk';
import type { xdr } from '@stellar/stellar-sdk';
import { SwapTransactionXdrStruct } from './xdr';
const sourceAddress =
'GA7UCNSASSOPQYTRGJ2NC7TDBSXHMWK6JHS7AO6X2ZQAIQSTB5ELNFSO';
const contractId = 'CASUP2OPFVEHCWGP2XLBXOV7DQIQIT42AQISG4MXAZGNLVFFN63X7WRT';
const issuer = Keypair.random().publicKey();
const destination = Keypair.random().publicKey();
const bridgeDestination = Keypair.random().publicKey();
const feeDestination = Keypair.random().publicKey();
const usdc = new Asset('USDC', issuer);
/**
* Builds a transaction XDR from Stellar operations.
*
* @param operations - Operations to add in order.
* @returns Base64 transaction envelope XDR.
*/
function buildTransactionXdr(operations: xdr.Operation[]): string {
const builder = new TransactionBuilder(new Account(sourceAddress, '1'), {
fee: '100',
networkPassphrase: Networks.PUBLIC,
});
for (const operation of operations) {
builder.addOperation(operation);
}
return builder.setTimeout(60).build().toXDR();
}
describe('SwapTransactionXdrStruct', () => {
it.each([
buildTransactionXdr([new Contract(contractId).call('swap')]),
buildTransactionXdr([
Operation.payment({
destination: bridgeDestination,
asset: Asset.native(),
amount: '1',
}),
]),
buildTransactionXdr([
Operation.pathPaymentStrictSend({
sendAsset: Asset.native(),
sendAmount: '10',
destination,
destAsset: usdc,
destMin: '5',
}),
]),
buildTransactionXdr([
Operation.pathPaymentStrictReceive({
sendAsset: Asset.native(),
sendMax: '10',
destination,
destAsset: usdc,
destAmount: '5',
}),
]),
buildTransactionXdr([
Operation.pathPaymentStrictSend({
sendAsset: Asset.native(),
sendAmount: '10',
destination,
destAsset: usdc,
destMin: '5',
}),
Operation.payment({
destination: feeDestination,
asset: Asset.native(),
amount: '1',
}),
]),
buildTransactionXdr([
Operation.changeTrust({
asset: usdc,
limit: '1000',
}),
Operation.pathPaymentStrictSend({
sendAsset: Asset.native(),
sendAmount: '10',
destination: sourceAddress,
destAsset: usdc,
destMin: '5',
}),
]),
buildTransactionXdr([
Operation.changeTrust({
asset: usdc,
limit: '1000',
}),
Operation.pathPaymentStrictReceive({
sendAsset: Asset.native(),
sendMax: '10',
destination: sourceAddress,
destAsset: usdc,
destAmount: '5',
}),
]),
buildTransactionXdr([
Operation.changeTrust({
asset: usdc,
limit: '1000',
}),
Operation.pathPaymentStrictSend({
sendAsset: Asset.native(),
sendAmount: '10',
destination: sourceAddress,
destAsset: usdc,
destMin: '5',
}),
Operation.payment({
destination: feeDestination,
asset: Asset.native(),
amount: '1',
}),
]),
])('accepts a valid swap transaction XDR', (xdr) => {
expect(() => assert(xdr, SwapTransactionXdrStruct)).not.toThrow();
});
it.each([
'not-xdr',
buildTransactionXdr([
Operation.pathPaymentStrictSend({
sendAsset: Asset.native(),
sendAmount: '10',
destination: sourceAddress,
destAsset: usdc,
destMin: '5',
}),
Operation.changeTrust({
asset: usdc,
limit: '1000',
}),
Operation.payment({
destination: feeDestination,
asset: Asset.native(),
amount: '1',
}),
]),
])('rejects an invalid swap transaction XDR', (xdr) => {
expect(() => assert(xdr, SwapTransactionXdrStruct)).toThrow(StructError);
});
});