Skip to content

Commit 3061ee8

Browse files
authored
Merge pull request #687 from bob-collective/refactor/gateway-v4
refactor: improve sdk for v4
2 parents a49afae + 1c86ba1 commit 3061ee8

File tree

11 files changed

+266
-495
lines changed

11 files changed

+266
-495
lines changed

sdk/examples/gateway.ts

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
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';
315
import { bob } from 'viem/chains';
416

517
import { GatewaySDK } from '../src/gateway';
618

7-
const BOB_TBTC_V2_TOKEN_ADDRESS = '0xBBa2eF945D523C4e2608C9E1214C2Cc64D4fc2e2';
8-
919
export async function swapBtcForToken(evmAddress: Address) {
1020
const publicClient = createPublicClient({
1121
chain: bob,
@@ -20,15 +30,15 @@ export async function swapBtcForToken(evmAddress: Address) {
2030
});
2131
const btcSigner = new XverseConnector(Network.mainnet);
2232

23-
const gatewaySDK = new GatewaySDK('bob'); // or "mainnet"
33+
const gatewaySDK = new GatewaySDK(bob.id);
2434

2535
const quote = await gatewaySDK.getQuote({
2636
fromChain: 'bitcoin',
2737
fromToken: 'BTC',
2838
fromUserAddress: 'bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d',
2939
toChain: 'bob',
3040
toUserAddress: evmAddress,
31-
toToken: BOB_TBTC_V2_TOKEN_ADDRESS, // or "tBTC"
41+
toToken: 'wBTC',
3242
amount: 10000000, // 0.1 BTC
3343
gasRefill: 10000, // 0.0001 BTC,
3444
});
@@ -39,11 +49,11 @@ export async function swapBtcForToken(evmAddress: Address) {
3949
btcSigner,
4050
});
4151

42-
console.log(`Success! Txid = ${onrampTx}`);
52+
console.log(`Onramp success! Txid = ${onrampTx}`);
4353

4454
const offrampQuote = await gatewaySDK.getQuote({
4555
fromChain: 'bob',
46-
fromToken: BOB_TBTC_V2_TOKEN_ADDRESS, // or "tBTC"
56+
fromToken: 'wBTC',
4757
toChain: 'bitcoin',
4858
toUserAddress: 'bc1qafk4yhqvj4wep57m62dgrmutldusqde8adh20d',
4959
toToken: 'BTC',
@@ -55,5 +65,86 @@ export async function swapBtcForToken(evmAddress: Address) {
5565
publicClient: publicClient as PublicClient<Transport>,
5666
});
5767

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}`);
59150
}

sdk/package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@gobob/bob-sdk",
3-
"version": "3.3.4-alpha5",
3+
"version": "4.0.0",
44
"main": "dist/index.js",
55
"types": "dist/index.d.ts",
66
"scripts": {
@@ -47,8 +47,7 @@
4747
"@scure/btc-signer": "^1.6.0",
4848
"bitcoin-address-validation": "^3.0.0",
4949
"bitcoinjs-lib": "^6.1.7",
50-
"ethers": "^6.14.4",
5150
"viem": "^2.31.0",
5251
"global": "^4.4.0"
5352
}
54-
}
53+
}

0 commit comments

Comments
 (0)