Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@1inch/cross-chain-sdk
Advanced tools
Sdk for creating atomic swaps through 1inch
npm install @1inch/cross-chain-sdk
const privateKey = '0x'
const rpc = 'https://ethereum-rpc.publicnode.com'
const authKey = 'auth-key'
const source = 'sdk-tutorial'
const web3 = new Web3(rpc)
const walletAddress = web3.eth.accounts.privateKeyToAccount(privateKey).address
const sdk = new SDK({
url: 'https://api.1inch.dev/fusion-plus',
authKey,
blockchainProvider: new PrivateKeyProviderConnector(privateKey, web3) // only required for order creation
})
To create order it is required that wallet has enough allowance of srcTokenAddress
for Limit Order Protocol
on source chain
// 10 USDT (Polygon) -> BNB (BSC)
// estimate
const quote = await sdk.getQuote({
amount: '10000000',
srcChainId: NetworkEnum.POLYGON,
dstChainId: NetworkEnum.BINANCE,
enableEstimate: true,
srcTokenAddress: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', // USDT
dstTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', // BNB
walletAddress
})
const preset = PresetEnum.fast
// generate secrets
const secrets = Array.from({
length: quote.presets[preset].secretsCount
}).map(() => '0x' + randomBytes(32).toString('hex'))
const hashLock =
secrets.length === 1
? HashLock.forSingleFill(secrets[0])
: HashLock.forMultipleFills(HashLock.getMerkleLeaves(secrets))
const secretHashes = secrets.map((s) => HashLock.hashSecret(s))
// create order
const {hash, quoteId, order} = await sdk.createOrder(quote, {
walletAddress,
hashLock,
preset,
source,
secretHashes
})
console.log({hash}, 'order created')
// submit order
const _orderInfo = await sdk.submitOrder(
quote.srcChainId,
order,
quoteId,
secretHashes
)
console.log({hash}, 'order submitted')
Each time when resolver deploys source and destination escrow - user must submit appropriate secret, so resolver can finish swap
// submit secrets for deployed escrows
while (true) {
const secretsToShare = await sdk.getReadyToAcceptSecretFills(hash)
if (secretsToShare.fills.length) {
for (const {idx} of secretsToShare.fills) {
await sdk.submitSecret(hash, secrets[idx])
console.log({idx}, 'shared secret')
}
}
// check if order finished
const {status} = await sdk.getOrderStatus(hash)
if (
status === OrderStatus.Executed ||
status === OrderStatus.Expired ||
status === OrderStatus.Refunded
) {
break
}
await sleep(1000)
}
const statusResponse = await sdk.getOrderStatus(hash)
console.log(statusResponse)
import {
HashLock,
NetworkEnum,
OrderStatus,
PresetEnum,
PrivateKeyProviderConnector,
SDK
} from '@1inch/cross-chain-sdk'
import Web3 from 'web3'
import {randomBytes} from 'node:crypto'
const privateKey = '0x'
const rpc = 'https://ethereum-rpc.publicnode.com'
const authKey = 'auth-key'
const source = 'sdk-tutorial'
const web3 = new Web3(rpc)
const walletAddress = web3.eth.accounts.privateKeyToAccount(privateKey).address
const sdk = new SDK({
url: 'https://api.1inch.dev/fusion-plus',
authKey,
blockchainProvider: new PrivateKeyProviderConnector(privateKey, web3) // only required for order creation
})
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms))
}
async function main(): Promise<void> {
// 10 USDT (Polygon) -> BNB (BSC)
// estimate
const quote = await sdk.getQuote({
amount: '10000000',
srcChainId: NetworkEnum.POLYGON,
dstChainId: NetworkEnum.BINANCE,
enableEstimate: true,
srcTokenAddress: '0xc2132d05d31c914a87c6611c10748aeb04b58e8f', // USDT
dstTokenAddress: '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', // BNB
walletAddress
})
const preset = PresetEnum.fast
// generate secrets
const secrets = Array.from({
length: quote.presets[preset].secretsCount
}).map(() => '0x' + randomBytes(32).toString('hex'))
const hashLock =
secrets.length === 1
? HashLock.forSingleFill(secrets[0])
: HashLock.forMultipleFills(HashLock.getMerkleLeaves(secrets))
const secretHashes = secrets.map((s) => HashLock.hashSecret(s))
// create order
const {hash, quoteId, order} = await sdk.createOrder(quote, {
walletAddress,
hashLock,
preset,
source,
secretHashes
})
console.log({hash}, 'order created')
// submit order
const _orderInfo = await sdk.submitOrder(
quote.srcChainId,
order,
quoteId,
secretHashes
)
console.log({hash}, 'order submitted')
// submit secrets for deployed escrows
while (true) {
const secretsToShare = await sdk.getReadyToAcceptSecretFills(hash)
if (secretsToShare.fills.length) {
for (const {idx} of secretsToShare.fills) {
await sdk.submitSecret(hash, secrets[idx])
console.log({idx}, 'shared secret')
}
}
// check if order finished
const {status} = await sdk.getOrderStatus(hash)
if (
status === OrderStatus.Executed ||
status === OrderStatus.Expired ||
status === OrderStatus.Refunded
) {
break
}
await sleep(1000)
}
const statusResponse = await sdk.getOrderStatus(hash)
console.log(statusResponse)
}
main()
FAQs
Sdk for creating atomic swaps through 1inch
The npm package @1inch/cross-chain-sdk receives a total of 68 weekly downloads. As such, @1inch/cross-chain-sdk popularity was classified as not popular.
We found that @1inch/cross-chain-sdk demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.