1
1
import { Network , XverseConnector } from '@gobob/sats-wagmi' ;
2
- import { Address , createPublicClient , createWalletClient , http , PublicClient , Transport , zeroAddress } from 'viem' ;
2
+ import {
3
+ Address ,
4
+ createPublicClient ,
5
+ createWalletClient ,
6
+ encodeAbiParameters ,
7
+ encodeFunctionData ,
8
+ http ,
9
+ parseAbi ,
10
+ parseAbiParameters ,
11
+ PublicClient ,
12
+ Transport ,
13
+ zeroAddress ,
14
+ } from 'viem' ;
3
15
import { bob } from 'viem/chains' ;
4
16
5
17
import { GatewaySDK } from '../src/gateway' ;
6
18
7
- const BOB_TBTC_V2_TOKEN_ADDRESS = '0xBBa2eF945D523C4e2608C9E1214C2Cc64D4fc2e2' ;
8
-
9
19
export async function swapBtcForToken ( evmAddress : Address ) {
10
20
const publicClient = createPublicClient ( {
11
21
chain : bob ,
@@ -20,15 +30,15 @@ export async function swapBtcForToken(evmAddress: Address) {
20
30
} ) ;
21
31
const btcSigner = new XverseConnector ( Network . mainnet ) ;
22
32
23
- const gatewaySDK = new GatewaySDK ( ' bob' ) ; // or "mainnet"
33
+ const gatewaySDK = new GatewaySDK ( bob . id ) ;
24
34
25
35
const quote = await gatewaySDK . getQuote ( {
26
36
fromChain : 'bitcoin' ,
27
37
fromToken : 'BTC' ,
28
38
fromUserAddress : 'bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d' ,
29
39
toChain : 'bob' ,
30
40
toUserAddress : evmAddress ,
31
- toToken : BOB_TBTC_V2_TOKEN_ADDRESS , // or "tBTC"
41
+ toToken : 'wBTC' ,
32
42
amount : 10000000 , // 0.1 BTC
33
43
gasRefill : 10000 , // 0.0001 BTC,
34
44
} ) ;
@@ -39,11 +49,11 @@ export async function swapBtcForToken(evmAddress: Address) {
39
49
btcSigner,
40
50
} ) ;
41
51
42
- console . log ( `Success ! Txid = ${ onrampTx } ` ) ;
52
+ console . log ( `Onramp success ! Txid = ${ onrampTx } ` ) ;
43
53
44
54
const offrampQuote = await gatewaySDK . getQuote ( {
45
55
fromChain : 'bob' ,
46
- fromToken : BOB_TBTC_V2_TOKEN_ADDRESS , // or "tBTC"
56
+ fromToken : 'wBTC' ,
47
57
toChain : 'bitcoin' ,
48
58
toUserAddress : 'bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d' ,
49
59
toToken : 'BTC' ,
@@ -55,5 +65,86 @@ export async function swapBtcForToken(evmAddress: Address) {
55
65
publicClient : publicClient as PublicClient < Transport > ,
56
66
} ) ;
57
67
58
- console . log ( `Success! Txid = ${ offrampTx } ` ) ;
68
+ console . log ( `Offramp success! Txid = ${ offrampTx } ` ) ;
69
+ }
70
+
71
+ function generateMessageForMulticallHandler (
72
+ userAddress : Address ,
73
+ aaveAddress : Address ,
74
+ depositAmount : bigint ,
75
+ depositCurrency : Address ,
76
+ aaveReferralCode : number
77
+ ) {
78
+ const approveFunction = 'function approve(address spender, uint256 value)' ;
79
+ const depositFunction = 'function deposit(address asset, uint256 amount, address onBehalfOf, uint16 referralCode)' ;
80
+
81
+ const erc20Interface = parseAbi ( [ approveFunction ] ) ;
82
+ const aaveInterface = parseAbi ( [ depositFunction ] ) ;
83
+
84
+ const approveCalldata = encodeFunctionData ( {
85
+ abi : erc20Interface ,
86
+ functionName : 'approve' ,
87
+ args : [ aaveAddress , depositAmount ] ,
88
+ } ) ;
89
+ const depositCalldata = encodeFunctionData ( {
90
+ abi : aaveInterface ,
91
+ functionName : 'deposit' ,
92
+ args : [ depositCurrency , depositAmount , userAddress , aaveReferralCode ] ,
93
+ } ) ;
94
+
95
+ return encodeAbiParameters (
96
+ parseAbiParameters ( '((address target, bytes callData, uint256 value)[], address fallbackRecipient)' ) ,
97
+ [
98
+ [
99
+ [
100
+ { target : depositCurrency , callData : approveCalldata , value : 0n } ,
101
+ { target : aaveAddress , callData : depositCalldata , value : 0n } ,
102
+ ] ,
103
+ userAddress ,
104
+ ] ,
105
+ ]
106
+ ) ;
107
+ }
108
+
109
+ export async function onrampAndDeposit ( evmAddress : Address ) {
110
+ const publicClient = createPublicClient ( {
111
+ chain : bob ,
112
+ transport : http ( ) ,
113
+ } ) ;
114
+
115
+ const walletClient = createWalletClient ( {
116
+ chain : bob ,
117
+ transport : http ( ) ,
118
+ account : evmAddress ,
119
+ } ) ;
120
+
121
+ const btcSigner = new XverseConnector ( Network . mainnet ) ;
122
+
123
+ const gatewaySDK = new GatewaySDK ( bob . id ) ;
124
+
125
+ const quote = await gatewaySDK . getQuote ( {
126
+ fromChain : 'bitcoin' ,
127
+ fromToken : 'BTC' ,
128
+ fromUserAddress : 'bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d' ,
129
+ toChain : 'bob' ,
130
+ toUserAddress : evmAddress ,
131
+ toToken : 'wBTC' ,
132
+ amount : 10000000 , // 0.1 BTC
133
+ gasRefill : 10000 , // 0.0001 BTC,
134
+ message : generateMessageForMulticallHandler (
135
+ evmAddress ,
136
+ '0x35B3F1BFe7cbE1e95A3DC2Ad054eB6f0D4c879b6' , // Avalon pool
137
+ 10000000n ,
138
+ '0xd6890176e8d912142AC489e8B5D8D93F8dE74D60' , // WBTC-AToken-BOB
139
+ 0
140
+ ) ,
141
+ } ) ;
142
+
143
+ const onrampTx = await gatewaySDK . executeQuote ( quote , {
144
+ walletClient,
145
+ publicClient : publicClient as PublicClient < Transport > ,
146
+ btcSigner,
147
+ } ) ;
148
+
149
+ console . log ( `Onramp success! Txid = ${ onrampTx } ` ) ;
59
150
}
0 commit comments