-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbitcoin.ts
74 lines (57 loc) · 2.22 KB
/
bitcoin.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
import { Providers, Contract, Adapter } from '@jelly-swap/bitcoin';
import { Wallet } from '@jelly-swap/btc-web-wallet';
import { BTC_CONFIG } from './config';
export const createWallet = (mnemonic, providerUrl) => {
const provider = new Providers.BitcoinProvider(providerUrl);
return new Wallet(mnemonic, provider);
};
// const userInput = {
// network: 'BTC',
// inputAmount: '0.01', // BTC amount
// outputNetwork: 'DAI',
// outputAmount: '84212086000000000000', // DAI amount
// sender: 'USER_BTC_ADDRESS', // user's BTC address
// outputAddress: '0x45ce9d7bdadb704c68242118e2b8586e491e5c51', // user DAI's address
// secret: 'film ritual cream paper try search asthma grab admit viable work auction',
// };
export const initiateSwap = async (userInput) => {
const config = BTC_CONFIG();
const mnemonic = 'choose stone donkey wheat pudding gasp harsh pupil sibling post brief afraid';
const wallet = createWallet(mnemonic, config.providerUrl);
const contract = new Contract(wallet, config);
const adapter = new Adapter(config);
const swapInput = adapter.formatInput(userInput);
const txHash = await contract.newContract(swapInput);
return txHash;
};
const onBtcEvent = (result) => {
console.log(result);
switch (result.eventName) {
case 'NEW_CONTRACT': {
if (result.isSesnder) {
// logic for when user's BTC swap is confirmed
} else {
// when result.isSender is false, this means the swap is made from LP to the user and the user needs to withdraw the BTC
// in AdEX case this won't happen.
}
break;
}
case 'WITHDRAW': {
// logic for BTC withdraw
// in AdEX case this won't happen.
break;
}
}
};
export const subscribe = (contract, userBtcAddress) => {
contract.subscribe(onBtcEvent, {
new: {
type: 'getSwapsByAddressAndBlock',
address: userBtcAddress,
},
withdraw: {
type: 'getWithdrawByReceiverAndBlock',
address: userBtcAddress,
},
});
};