3swap-sdk 中文文档教程
3Swap-SDK
3Swap SDK是查询3Swap DEX相关链状态的便捷库。 利用 SDK,您可以计算与 3Swap 智能合约交互的参数。
示例:
- Fetch token data:
您可以使用链 ID 和代币地址查询链中的代币信息。
import { Fetcher, ChainId } from '3swap-sdk';
Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9'
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
// or use custom provider
Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9', 'PROVIDER_URL'
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
您还可以设置一个数字作为链 ID:
Fetcher.fetchTokenData(
97,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9' // '97' is the chain ID for Binance Testnet
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
Fetcher.fetchTokenData
函数返回一个 Promise
。
令牌类中的字段包括:
Name | Type |
---|---|
_address | string |
_chainId | ChainId or number |
_decimals | number or string |
_name | string |
_symbol | string |
- Fetch triad data:
可以使用返回 Promise
的静态辅助方法在链上获取 Triad 信息。
import { Fetcher, ChainId } from '3swap-sdk';
async function fetchTriad() {
const tokenA = await Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET, '0x57c84e7bcbab211761a0cb91484ae896aa897ae9'
);
//...2 other tokens
const triad = await Fetcher.fetchTriadData(tokenA, tokenB, tokenC);
}
// or use custom provider url
const triad = await Fetcher.fetchTriadData(tokenA, tokenB, tokenC, 'PROVIDER_URL');
triad 类中的字段包括:
Name | Type |
---|---|
liquidityToken | Token |
tokenAmounts | [TokenAmount, TokenAmount, TokenAmount] |
计算交易:
SDK 还通过 Router
类提供有效的交易计算。
import { Router } from '3swap-sdk';
Router.swapCallParameter(trade, chainId, options);
Router
类的静态 swapCallParameter
方法接收交易、链 ID 和交易选项对象,然后计算一个 SwapParams
对象,其中包含方法名称、方法参数和要在交易中发送的 Ether 的十六进制编码值(对于不涉及 Ether 的交易,这是 0)。
构建Trade
trade 类在路由器类的实际计算中很有用。 Trade
构造函数接受三个 TokenAmount
对象和一个 TradeType
枚举。
new Trade(input1amount, input2amount, outputamount, tradetype);
TokenAmount
构造函数接受一个大数字(金额)和一个令牌。
new TokenAmount(JSBI.BigInt(4000000000000000000), token);
TradeType
枚举:
enum TradeType {
EXACT_INPUT,
EXACT_OUTPUT
}
交易类型有助于确定滑点的传播方式。 EXACT_INPUT
表示不对输入金额计算滑点容差,而 EXACT_OUTPUT
表示不对输出金额应用滑点容差。
TradeOptions
接口:
/**
* Trade options.
*/
export interface TradeOptions {
/**
* Price difference during latency in transaction submission and block confirmation
*/
slippage: number;
/**
* How long the transaction should last before it becomes invalid (in seconds)
*/
deadline: number;
/**
* The account that should receive the swap output
*/
recipient: string;
}
Router.swapCallParameters
返回一个 SwapParams
对象。
export interface SwapParams {
/**
* The method to send to the 3Swap router
*/
methodName: string;
/**
* Arguments to pass to the method (all hex-encoded)
*/
args: Array<string | string[]>;
/**
* Amount of wei to send in hex
*/
value: string;
}
3Swap-SDK
The 3Swap SDK is a convenience library for querying chain state relevant to 3Swap DEX. Leveraging the SDK, you can compute parameters with which to interact with the 3Swap smart contract.
Examples:
- Fetch token data:
You can query the chain for token info using the chain ID and the token address.
import { Fetcher, ChainId } from '3swap-sdk';
Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9'
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
// or use custom provider
Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9', 'PROVIDER_URL'
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
You can also set a number as the chain id:
Fetcher.fetchTokenData(
97,'0x57c84e7bcbab211761a0cb91484ae896aa897ae9' // '97' is the chain ID for Binance Testnet
)
.then(token => {
console.log('Name: %s, Decimals: %d', token.name(), token.decimals()); // 3Swap 0x, SAPX
});
The Fetcher.fetchTokenData
function returns a Promise<Token>
.
Fields in the token class include:
Name | Type |
---|---|
_address | string |
_chainId | ChainId or number |
_decimals | number or string |
_name | string |
_symbol | string |
- Fetch triad data:
Triad information can be fetched on-chain using a static helper method that returns a Promise<Triad>
.
import { Fetcher, ChainId } from '3swap-sdk';
async function fetchTriad() {
const tokenA = await Fetcher.fetchTokenData(
ChainId.BINANCE_TESTNET, '0x57c84e7bcbab211761a0cb91484ae896aa897ae9'
);
//...2 other tokens
const triad = await Fetcher.fetchTriadData(tokenA, tokenB, tokenC);
}
// or use custom provider url
const triad = await Fetcher.fetchTriadData(tokenA, tokenB, tokenC, 'PROVIDER_URL');
Fields in the triad class include:
Name | Type |
---|---|
liquidityToken | Token |
tokenAmounts | [TokenAmount, TokenAmount, TokenAmount] |
Computing trades:
The SDK also provides the effective computation of trades through the Router
class.
import { Router } from '3swap-sdk';
Router.swapCallParameter(trade, chainId, options);
The static swapCallParameter
method of the Router
class takes in a trade, a chain ID and a trade options object and then computes a SwapParams
object which contains the method name, the arguments for the method and the hex encoded value of Ether to be sent in the transaction (this is 0 for transactions that do not involve Ether).
Constructing a Trade
A trade class is useful in the actual computation by the router class. The Trade
constructor takes in three TokenAmount
objects and a TradeType
enum.
new Trade(input1amount, input2amount, outputamount, tradetype);
The TokenAmount
constructor takes in a big number (the amount) and a token.
new TokenAmount(JSBI.BigInt(4000000000000000000), token);
The TradeType
enum:
enum TradeType {
EXACT_INPUT,
EXACT_OUTPUT
}
The trade type is useful in determining how slippage is spread. EXACT_INPUT
means no slippage tolerance is computed on the input amount while EXACT_OUTPUT
means no slippage tolerance is applied on the output amount.
The TradeOptions
interface:
/**
* Trade options.
*/
export interface TradeOptions {
/**
* Price difference during latency in transaction submission and block confirmation
*/
slippage: number;
/**
* How long the transaction should last before it becomes invalid (in seconds)
*/
deadline: number;
/**
* The account that should receive the swap output
*/
recipient: string;
}
Router.swapCallParameters
returns a SwapParams
object.
export interface SwapParams {
/**
* The method to send to the 3Swap router
*/
methodName: string;
/**
* Arguments to pass to the method (all hex-encoded)
*/
args: Array<string | string[]>;
/**
* Amount of wei to send in hex
*/
value: string;
}