-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeposit-and-withdraw.test.js
204 lines (177 loc) · 5.71 KB
/
deposit-and-withdraw.test.js
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
const deployBank = require("../../src/utils/deploy-bank");
const deployTestErc20 = require("../../src/utils/deploy-test-erc20");
const { connect, web3 } = require("../../src/connections");
const { app } = require("../../src/api");
const request = require("supertest");
const util = require("util");
const sleep = util.promisify(setTimeout);
const config = require("../../src/config");
const { TRANSACTION_SIGNER_PRIVATE_KEY, TRANSACTION_SIGNER_PUBLIC_KEY } =
config;
const faker = require("faker");
const { expect } = require("chai");
const { Bank, User } = require("../../src/models");
describe("deposit and withdraw end to end test", () => {
test("should sign up user, create a receiver and deposit into and then withdraw out of it", async () => {
let user;
await connect();
const bankTransaction = await deployBank({
signerPrivateKey: TRANSACTION_SIGNER_PRIVATE_KEY,
signerPublicKey: TRANSACTION_SIGNER_PUBLIC_KEY,
});
const { banks } = await Bank.create({
input: [
{
address: bankTransaction.receipt.contractAddress,
transaction: {
create: {
node: {
transactionHash: bankTransaction.receipt.transactionHash,
transactionIndex: bankTransaction.receipt.transactionIndex,
blockHash: bankTransaction.receipt.blockHash,
blockNumber: bankTransaction.receipt.blockNumber,
gasUsed: bankTransaction.receipt.gasUsed,
cumulativeGasUsed: bankTransaction.receipt.cumulativeGasUsed,
gasPrice: bankTransaction.gasPrice,
},
},
},
},
],
});
const usdtTransaction = await deployTestErc20({
signerPrivateKey: TRANSACTION_SIGNER_PRIVATE_KEY,
signerPublicKey: TRANSACTION_SIGNER_PUBLIC_KEY,
});
config.BANK_ID = banks[0].id;
config.USDT_ADDRESS = usdtTransaction.receipt.contractAddress;
require("../../src/receiver").listen();
require("../../src/watcher").watch();
require("../../src/withdrawer").listen();
const email = faker.internet.email();
const password = faker.internet.password();
const signupResponse = await request(app).post("/signup").send({
email,
password,
});
expect(signupResponse.statusCode).to.equal(200);
// Wait for receiver to be deployed
await sleep(config.TRANSACTION_WAIT_TIME);
[user] = await User.find({
where: { email },
selectionSet: `
{
balance
receiver {
address
}
}
`,
});
expect(user.balance).to.equal(0);
const receiverAddress = user.receiver.address;
expect(receiverAddress).to.not.equal(null);
expect(receiverAddress).to.not.equal(undefined);
const transferToReceiver = usdtTransaction.contract.methods.transfer(
receiverAddress,
100
);
const transferToReceiverSigned =
await web3.client.eth.accounts.signTransaction(
{
to: usdtTransaction.receipt.contractAddress,
data: transferToReceiver.encodeABI(),
gas: config.ERC20_TRANSFER_GAS,
},
config.TRANSACTION_SIGNER_PRIVATE_KEY
);
const transferToReceiverReceipt =
await web3.client.eth.sendSignedTransaction(
transferToReceiverSigned.rawTransaction
);
const transferToBank = usdtTransaction.contract.methods.transfer(
banks[0].address,
100
);
const transferToBankSigned = await web3.client.eth.accounts.signTransaction(
{
to: usdtTransaction.receipt.contractAddress,
data: transferToBank.encodeABI(),
gas: config.ERC20_TRANSFER_GAS,
},
config.TRANSACTION_SIGNER_PRIVATE_KEY
);
await web3.client.eth.sendSignedTransaction(
transferToBankSigned.rawTransaction
);
await sleep(config.TRANSACTION_WAIT_TIME);
[user] = await User.find({
where: { email },
selectionSet: `
{
balance
deposits {
amount
transaction {
transactionHash
}
receiver {
address
bank {
id
}
}
}
}
`,
});
expect(user.balance).to.equal(100);
expect(user.deposits[0].amount).to.equal(100);
expect(user.deposits.length).to.equal(1);
expect(user.deposits[0].transaction.transactionHash).to.equal(
transferToReceiverReceipt.transactionHash
);
expect(user.deposits[0].receiver.address).to.equal(receiverAddress);
expect(user.deposits[0].receiver.bank.id).to.equal(banks[0].id);
const withdrawalTo = faker.finance.ethereumAddress();
// Withdrawal
const withdrawalResponse = await request(app)
.post("/withdraw")
.send({
email,
password,
})
.set({ authorization: `Bearer ${signupResponse.body.jwt}` })
.send({
amount: user.deposits[0].amount,
to: withdrawalTo,
});
expect(withdrawalResponse.statusCode).to.equal(200);
// Wait for transaction to happen
await sleep(config.TRANSACTION_WAIT_TIME * 2);
[user] = await User.find({
where: { email },
selectionSet: `
{
balance
withdrawals {
to
amount
bank {
id
}
}
}
`,
});
expect(user.balance).to.equal(0);
expect(user.withdrawals.length).to.equal(1);
expect(user.withdrawals[0].amount).to.equal(100);
expect(user.withdrawals[0].bank.id).to.equal(banks[0].id);
expect(
Number(
await usdtTransaction.contract.methods.balanceOf(withdrawalTo).call()
)
).to.equal(user.withdrawals[0].amount);
});
});