-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdai.ts
45 lines (37 loc) · 1.23 KB
/
dai.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
import { Providers, Contract } from '@jelly-swap/erc20';
import { DAI_CONFIG } from './config';
export const withdraw = async ({ id, secret, tokenAddress }) => {
const config = DAI_CONFIG();
const privateKey = 'PRIVATE_KEY';
const provider = new Providers.WalletProvider(privateKey, config.providerUrl); // web wallet
const contract = new Contract(provider, config);
const txHash = await contract.withdraw({ id, secret, tokenAddress });
return txHash;
};
const onErc20Event = (result) => {
console.log(result);
switch (result.eventName) {
case 'NEW_CONTRACT': {
if (result.isSesnder) {
// in AdEX case this won't happen, because user's won't send DAI, only BTC
} else {
// Handle DAI withdraw here.
}
break;
}
case 'WITHDRAW': {
// logic for when user's DAI withdraw is confirmed
break;
}
}
};
export const subscribe = (contract, userDAIAddress) => {
contract.subscribe(onErc20Event, {
new: {
receiver: userDAIAddress,
},
withdraw: {
receiver: userDAIAddress,
},
});
};