-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathwagmi.ts
68 lines (58 loc) · 1.8 KB
/
wagmi.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
/* eslint-disable @typescript-eslint/no-unused-vars */
import axios from 'axios';
import { mainnet } from '@wagmi/core/chains';
import {
getAccount,
getWalletClient,
sendTransaction,
waitForTransactionReceipt,
http,
createConfig,
} from '@wagmi/core';
import { injected } from '@wagmi/connectors';
import { constructSimpleSDK, txParamsToViemTxParams } from '../';
import { assert } from 'ts-essentials';
export const config = createConfig({
chains: [mainnet],
connectors: [injected()],
transports: {
[mainnet.id]: http(),
},
});
async function simpleSDKExample() {
const walletClient = await getWalletClient(config, { chainId: mainnet.id });
const account = getAccount(config).address; // make sure wallet is connected at this point
assert(account, 'account is necessary for Order signing');
const simpleSDK = constructSimpleSDK(
{
axios,
chainId: mainnet.id, // same chain as for walletClient
},
{ viemClient: walletClient, account }
);
const ETH = '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
const DAI = '0x6B175474E89094C44Da98b954EedeAC495271d0F';
const srcAmount = (1 * 1e18).toString();
const destToken = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
const priceRoute = await simpleSDK.swap.getRate({
srcToken: ETH,
destToken: DAI,
amount: srcAmount,
userAddress: account,
side: 'SELL',
srcDecimals: 18,
destDecimals: 18,
});
const txParams = await simpleSDK.swap.buildTx({
srcToken: ETH,
destToken,
srcAmount,
slippage: 250, // 2.5%
priceRoute,
userAddress: account,
});
const vTxParams = txParamsToViemTxParams(txParams);
const txHash = await sendTransaction(config, vTxParams);
const txReceipt = waitForTransactionReceipt(config, { hash: txHash });
}
// Look at examples/viem for more examples