@ 1英寸/ aqua- sdk - 1英寸水协议的 SDK 类型
一个TypeScript SDK,用于编码,解码,并与一英寸水协议智能合同互动. 这个SDK为水议定书核心业务的建筑交易和剖析活动提供公用事业.
概览
《水议定书》是流动资金管理的分权议定书。 这种SDK通过提供:
- 编码/解码: 构建打入的呼叫数据
ship, (中文(简体) ),dock业务 - 事件分析: 解码和解析
Pushed, (中文(简体) ),Pulled, (中文(简体) ),Shipped,以及Docked事件 - 多钱支持: 10+区块链网络的预配置地址
关于详细的礼宾文件,请参看 水议定书文件。 。 。 .
安装
pnpm add @1inch/aqua-sdk
快速启动
import {
AquaProtocolContract,
AQUA_CONTRACT_ADDRESSES,
Address,
HexString,
NetworkEnum
} from '@1inch/aqua-sdk'
// Initialize the contract
const contractAddress = AQUA_CONTRACT_ADDRESSES[NetworkEnum.ETHEREUM]
const aqua = new AquaProtocolContract(contractAddress)
// Build a ship transaction
const shipTx = aqua.ship({
app: new Address('0x...'),
strategy: new HexString('0x...'),
amountsAndTokens: [
{
token: new Address('0x...'),
amount: 1000000000000000000n, // 1 token with 18 decimals
},
],
})
// Use the transaction data
console.log(shipTx) // { to: '0x...', data: '0x...', value: 0n }
核心业务
船舶
启动流动性战略,为此设定虚拟代号余额.
const shipTx = aqua.ship({
app: new Address('0x...'),
strategy: new HexString('0x...'),
amountsAndTokens: [
{
token: new Address('0x...'),
amount: 1000000000000000000n,
},
],
})
参数:
app- 申请合同地址strategy- 包含执行逻辑的战略字节amountsAndTokens- 象征性地址和金额
回返情况: CallInfo 带有编码交易数据的对象
锁定
完成流动性策略,从中去除虚拟符号余额.
const strategyHash = AquaProtocolContract.calculateStrategyHash(strategy)
const dockTx = aqua.dock({
app: new Address('0x...'),
strategyHash: strategyHash,
tokens: [
new Address('0x...'),
new Address('0x...'),
],
})
参数:
app- 申请合同地址strategyHash- 战略字节的克恰克256散列tokens- 战略所有象征性地址的组合,其虚拟余额被设定为零
回返情况: CallInfo 带有编码交易数据的对象
事件分析
推动事件
将资金投入战略/Wallet时予以发放.
import { PushedEvent } from '@1inch/aqua-sdk'
const log = { data: '0x...', topics: ['0x...'] }
const event = PushedEvent.fromLog(log)
console.log(event.maker) // Address
console.log(event.app) // Address
console.log(event.strategyHash) // HexString
console.log(event.token) // Address
console.log(event.amount) // bigint
拉动事件
在从战略/Wallet中提取资金时发放.
import { PulledEvent } from '@1inch/aqua-sdk'
const event = PulledEvent.fromLog(log)
运输事件
在启动流动资金战略时予以释放.
import { ShippedEvent } from '@1inch/aqua-sdk'
const event = ShippedEvent.fromLog(log)
锁定事件
当流动性战略结束时,即予释放.
import { DockedEvent } from '@1inch/aqua-sdk'
const event = DockedEvent.fromLog(log)
工具函数
计算策略散列
将策略字节转换为它们的克恰克256散列.
import { AquaProtocolContract } from '@1inch/aqua-sdk'
import { HexString } from '@1inch/sdk-core'
const strategy = new HexString('0x...')
const hash = AquaProtocolContract.calculateStrategyHash(strategy)
编码调用数据
需要时手动编码函数调用数据 .
import { AquaProtocolContract } from '@1inch/aqua-sdk'
const encodedShip = AquaProtocolContract.encodeShipCallData({
app: new Address('0x...'),
strategy: new HexString('0x...'),
amountsAndTokens: [ /* ... */ ],
})
const encodedDock = AquaProtocolContract.encodeDockCallData({
app: new Address('0x...'),
strategyHash: new HexString('0x...'),
tokens: [ /* ... */ ],
})
支持的网络
SDK包括以下网络的预配置合同地址:
访问地址使用 :
import { AQUA_CONTRACT_ADDRESSES } from '@1inch/aqua-sdk'
import { NetworkEnum } from '@1inch/sdk-core'
const ethereumAddress = AQUA_CONTRACT_ADDRESSES[NetworkEnum.ETHEREUM]
const arbitrumAddress = AQUA_CONTRACT_ADDRESSES[NetworkEnum.ARBITRUM]
API 参考
出口
SDK出口:
AquaProtocolContract- 编码、解码和建筑物交易的主要合同类别AQUA_CONTRACT_ADDRESSES- 按网络预配置的合同地址ABI- ABI合同出口- 类型数字 :
ShipArgsDockArgsAmountsAndTokensEventAction
- 活动类别数字 :
PushedEventPulledEventShippedEventDockedEvent
现实生活实例
实例1:船舶流动性 XYCWAP.sol (英语) aqua 应用
启动流动性战略,在Aqua设定其虚拟余额. ship 不转移指使符:它们留在制作人的钱包里,水叮当用它拉出 transferFrom 当交换执行时,所以制作者必须持有符文并批准水上公司合同,以花费每个符文.
import { AquaProtocolContract, Address, HexString, AQUA_CONTRACT_ADDRESSES, NetworkEnum } from '@1inch/aqua-sdk'
import { encodeAbiParameters, parseUnits, http, createWalletClient, isHex, erc20Abi, maxUint256 } from 'viem'
import { privateKeyToAccount, privateKeyToAddress } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import assert from 'assert'
import 'dotenv/config'
const makerPrivateKey = process.env.MAKER_PRIVATE_KEY
assert(isHex(makerPrivateKey))
const maker = privateKeyToAddress(makerPrivateKey)
const app = '0xTODO_REPLACE'
const WETH = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
// Define strategy based on the smart contract app structure. Each Aqua app can have it's own strategy schema
const strategyData = {
maker,
token0: WETH,
token1: USDC,
feeBps: 0n,
salt: '0x0000000000000000000000000000000000000000000000000000000000000001'
} as const
// Encode strategy as bytes
const strategy = encodeAbiParameters(
[
{
name: 'strategy',
type: 'tuple',
components: [
{ name: 'maker', type: 'address' },
{ name: 'token0', type: 'address' },
{ name: 'token1', type: 'address' },
{ name: 'feeBps', type: 'uint256' },
{ name: 'salt', type: 'bytes32' }
]
}
],
[strategyData]
)
console.log('Encoded strategy:', strategy)
// Initialize Aqua contract
const aqua = new AquaProtocolContract(AQUA_CONTRACT_ADDRESSES[NetworkEnum.ETHEREUM])
// Create ship transaction
const shipTx = aqua.ship({
app: new Address(app),
strategy: new HexString(strategy),
amountsAndTokens: [
{
token: new Address(USDC),
amount: parseUnits('4000', 6)
},
{
token: new Address(WETH),
amount: parseUnits('1', 18)
}
]
})
const wallet = createWalletClient({
chain: mainnet,
transport: http(),
account: privateKeyToAccount(makerPrivateKey)
})
// Aqua pulls the tokens from the maker's wallet when swaps execute, so it needs an allowance
for (const token of [USDC, WETH] as const) {
await wallet.writeContract({
address: token,
abi: erc20Abi,
functionName: 'approve',
args: [aqua.address.toString(), maxUint256]
})
}
// Send transaction
await wallet.sendTransaction(shipTx)
完整测试示例 : 试验/aqua. spec.ts -- -- 应发货
例2:通过交换执行 XYCWAP.sol (英语) aqua 应用
与水口公司提供的流动性进行互换. 测试器. sol 帮助者合同被用作批准持有人和路由器.
import {
parseUnits,
http,
createWalletClient,
isHex,
encodeFunctionData,
decodeAbiParameters,
publicActions
} from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import assert from 'assert'
import 'dotenv/config'
const takerPrivateKey = process.env.TAKER_PRIVATE_KEY
assert(isHex(takerPrivateKey))
const USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const app = '0xTODOReplace'
const routerAddress = '0xTestTraderAddress'
const strategy = '0x' // parsed from ship events or fetched from api
// Send transaction
const wallet = createWalletClient({
chain: mainnet,
transport: http(),
account: privateKeyToAccount(takerPrivateKey)
}).extend(publicActions)
// decode data based on specified by app format
const [strategyData] = decodeAbiParameters(
[
{
type: 'tuple',
components: [
{ name: 'maker', type: 'address' },
{ name: 'token0', type: 'address' },
{ name: 'token1', type: 'address' },
{ name: 'feeBps', type: 'uint256' },
{ name: 'salt', type: 'bytes32' }
]
}
],
strategy
)
// Define swap parameters
const srcAmount = parseUnits('10', 6) // 10 USDC
const srcToken = USDC
const isZeroForOne = strategyData.token0 === srcToken
// Encode the swap call to helper Router contract
const swapData = encodeFunctionData({
abi: [
{
type: 'function',
name: 'swap',
inputs: [
{
name: 'app',
type: 'address'
},
{
name: 'strategy',
type: 'tuple',
components: [
{ name: 'maker', type: 'address' },
{ name: 'token0', type: 'address' },
{ name: 'token1', type: 'address' },
{ name: 'feeBps', type: 'uint256' },
{ name: 'salt', type: 'bytes32' }
]
},
{ name: 'zeroForOne', type: 'bool' },
{ name: 'amountIn', type: 'uint256' }
],
outputs: [{ name: 'amountOut', type: 'uint256' }],
stateMutability: 'nonpayable'
}
],
functionName: 'swap',
args: [app, strategyData, isZeroForOne, srcAmount]
})
// give erc20 approve to routerAddress
await wallet.writeContract({
abi: [
{
type: 'function',
name: 'approve',
inputs: [
{ name: 'spender', type: 'address', internalType: 'address' },
{ name: 'value', type: 'uint256', internalType: 'uint256' }
],
outputs: [{ name: '', type: 'bool', internalType: 'bool' }],
stateMutability: 'nonpayable'
}
],
address: srcToken,
account: wallet.account,
functionName: 'approve',
chain: wallet.chain,
args: [routerAddress, srcAmount]
})
// Execute swap
const swapTx = await wallet.sendTransaction({
to: routerAddress,
data: swapData
})
await wallet.waitForTransactionReceipt({ hash: swapTx })
完整测试示例 : 测试/aqua. spec.ts - 应互换
例3:码头流动性
关闭策略, 将其虚拟余额设置为 0 . dock 不传输令牌:它们已经在制作者的钱包中.
import { AquaProtocolContract, Address, HexString, AQUA_CONTRACT_ADDRESSES, NetworkEnum } from '@1inch/aqua-sdk'
import { http, createWalletClient, isHex } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { mainnet } from 'viem/chains'
import assert from 'assert'
import 'dotenv/config'
const makerPrivateKey = process.env.MAKER_PRIVATE_KEY
assert(isHex(makerPrivateKey))
const app = '0xTODO_REPLACE'
const WETH = '0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
const USDC = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48'
const strategy = '0x' // parsed from ship events or fetched from api
// Initialize Aqua contract
const aqua = new AquaProtocolContract(AQUA_CONTRACT_ADDRESSES[NetworkEnum.ETHEREUM])
// Create dock transaction
const dockTx = aqua.dock({
app: new Address(app),
strategyHash: AquaProtocolContract.calculateStrategyHash(new HexString(strategy)),
tokens: [new Address(USDC), new Address(WETH)]
})
// Send transaction
const wallet = createWalletClient({
chain: mainnet,
transport: http(),
account: privateKeyToAccount(makerPrivateKey)
})
await wallet.sendTransaction(dockTx)
// After the transaction is confirmed, the strategy's virtual balances are zero
// and the strategy is closed
完整测试示例 : 试验/aqua. spec.ts - 应停靠
许可证
该SDK是根据下列条款提供的: 自由主义 财务报告和审定财务报表 第三层( P)。 。 。 .
任何许可证发放问题或申请,请联系: