forked from euler-xyz/euler-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README_中文_README_CHN
186 lines (130 loc) · 7.42 KB
/
README_中文_README_CHN
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# Euler-sdk
A basic SDK for interacting with [Euler smart contracts](https://github.com/euler-xyz/euler-contracts).
Currently this is an alpha software, under intensive development.
用于与 [Euler 智能合约](https://github.com/euler-xyz/euler-contracts) 交互的基本 SDK。
目前这是一个 alpha版本,正在密集开发中。
## 安装 (Installation)
```bash
npm i @eulerxyz/euler-sdk
```
## Euler类 (Euler class)
The package provides a single named export - the Euler class
本包提供了一个单独的命名导出 - Euler 类
```js
import { Euler } from "@eulerxyz/euler-sdk"
```
The constructor takes a single required arguement - `signerOrProvider`, similar to ethers. The second argument is the `chainId`. Currently the SDK contains built in configurations for mainnet (chainId = 1), which is also default, and ropsten (chainId = 3) deployments.
构造函数接受一个必需的参数 - `signerOrProvider`,类似于 ethers。第二个参数是 `chainId`。目前 SDK 包含主网 (chainId = 1) 的内置配置,这也是默认配置,同时还有 ropsten (chainId = 3) 部署。
```js
const provider = new ethers.providers.JsonRpcProvider("<JSON_RPC_URL>")
const signer = new ethers.Wallet("<PRV_KEY>", provider)
const e = new Euler(signer);
```
## 与智能合约交互 (Interacting with the smart contracts)
### 主要模块 (Main modules)
By default, the SDK provides instances of the [ethers.Contract](https://docs.ethers.io/v5/api/contract/contract/) of the main Euler modules: Euler, Exec, Liquidation, Markets and Swap.
默认情况下,SDK 提供主要 Euler 模块的 [ethers.Contract](https://docs.ethers.io/v5/api/contract/contract/) 实例:Euler、Exec、Liquidation、Markets 和 Swap。
```js
// activate a new market
await e.contracts.markets.activateMarket(tokenAddress)
// check to see if liquidation would be profitable
const liquidationOpportunity = await e.contracts.liquidation.callStatic.checkLiquidation(liquidator, violator, underlying, collateral)
```
### 次要部分 (Peripheries)
In addition to the main modules, the SDK contains configuration for the mining contracts EulStakes and EulDistributor as well as peripheries FlashLoan and EulerGeneralView. They need to be explicitly initiated:
除主要模块外,SDK 还包含挖矿合约 EulStakes 和 EulDistributor 以及次要部分的 FlashLoan 和 EulerGeneralView 配置。它们需要被激活:
```js
e.addContract("EulStakes")
e.addContract("FlashLoan")
```
### Eul代币 (Eul token)
Euler's native governance token Eul is also available:
Euler 的原生治理代币 Eul 也可用:
```js
const balance = await e.contracts.eul.balanceOf(myAccount)
```
Additionally, the token configuration like decimals, logo, permit data are provided in `eulTokenConfig` property:
此外,在 `eulTokenConfig` 属性中提供了小数、logo、许可数据等代币配置:
```js
const { logo, extensions: { permit: { domain } } } = e.eulerTokenConfig
```
### eTokens, dTokens, pTokens
Contracts that exist for every activated market can be instantiated and accessed by helper methods `eToken(address)`, `dToken(address)`, `pToken(address)`
每个激活市场的合约都可以通过辅助方法 `eToken(address)`、`dToken(address)`、`pToken(address)` 实例化和访问
```js
const eUsdcAddress = await e.contracts.markets.underlyingToEToken(USDC_ADDRESS)
await e.eToken(eUsdcAddress).deposit(0, 1000000000)
```
In addition to that there is also `erc20(address)` for constructing standard ERC20 contract instances
除此之外,还有 `erc20(address)` 用于构建标准 ERC20 合约实例
```js
const daiBalance = await e.erc20(DAI_ADDRESS).balanceOf(myAccount)
```
### 添加外部合约 (Adding external contracts)
The SDK can attach any external contract with the `addContract` method. In this case, both abi and the contract address need to be provided.
SDK 可以使用 `addContract` 方法附加任何外部合约。在这种情况下,需要提供 abi 和合约地址。
```js
e.addContract("weth", WETH_ABI, WETH_ADDRESS)
await e.contracts.weth.deposit({ value: ONE_ETH })
```
### 批量交易 (Batch transactions)
Euler platform supports batching user operations into a single gas efficient transaction. SDK provides helper methods `buildBatch` and `decodeBatch` to make use of this feature.
`buildBatch` creates encoded payload to the Exec contract's `batchDispatch`. All public functions of Euler modules are available. The function expects an array of operations:
Euler 平台支持将用户操作批处理为单笔 gas 高效交易。 SDK 提供了帮助方法 `buildBatch` 和 `decodeBatch` 来利用此功能。
`buildBatch` 为 Exec 合约的 `batchDispatch` 创建编码的有效负载。Euler模块的所有公共功能都可用。该函数需要一个操作数组:
```js
const batchItems = [
{
contract: "eToken",
address: "0x123..",
method: "deposit",
args: [0, 1000000]
},
{
contract: "markets",
method: "enterMarket",
args: [0, "0xabc.."]
}
]
```
Note that for singleton contracts, the `address` can be omitted. The `contract` property can also be a contract instance, in which case the `address` can also be skipped.
请注意,对于单例合约,可以省略`address`。 `contract` 属性也可以是一个合约实例,在这种情况下,`address` 也可以跳过。
```js
[
{
contract: e.eToken("0x123.."),
method: "deposit",
args: [0, 1000000]
},
/* ... */
]
```
The encoded payload can be used to execute the `batchDispatch` transaction. If `batchDispatch` is static called, the SDK `decodeBatch` can be used to decode return values from each function called.
编码后的有效负载可用于执行`batchDispatch`事务。如果 `batchDispatch` 是静态调用的,那么SDK `decodeBatch` 可用于解码来自每个调用函数的返回值。
```js
const rawResults = await e.contracts.exec.callStatic.batchDispatch(e.buildBatch(batchItems), [])
const [ result1, result2 ] = e.decodeBatch(batchItems, rawResults)
```
### 签名和使用许可 (Signing and using permits)
To use EIP2612 permits on Euler, SDK provides `signPermit` and `signPermitBatchItem` functions. They both expect a token object with permit extension in [euler-tokenlist](https://github.com/euler-xyz/euler-tokenlist) format. `signPermit` is a low level function, while `signPermitBatchItem` should be used when creating calls to `batchDispatch`.
为了在 Euler 上使用 EIP2612 许可,SDK 提供了 `signPermit` 和 `signPermitBatchItem` 函数。他们都需要一个带有 [euler-tokenlist](https://github.com/euler-xyz/euler-tokenlist) 格式的许可扩展名的代币对象。 `signPermit` 是一个低级函数,而在创建对 `batchDispatch` 的调用时应该使用 `signPermitBatchItem`。
```js
const batch = [
await e.signPermitBatchItem(token),
{
contract: e.eToken(token.address),
method: "deposit",
args: [0, 10000000]
}
]
```
### 签名者和提供者 (Signers and providers)
To switch a signer or provider, call the `connect(signerOrProvider)` method. All contract instances will automatically switch to the new value.
To access currently connected signer or provider, use `getSigner()` and `getProvider()` methods.
要切换签名者或提供者,请调用 `connect(signerOrProvider)` 方法。所有合约实例都会自动切换到新值。
要访问当前连接的签名者或提供者,请使用 `getSigner()` 和 `getProvider()` 方法。
```js
e.connect(newSigner)
/* ... */
const currentSigner = e.getSigner()
```