New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@1inch/cross-chain-sdk

Package Overview
Dependencies
Maintainers
0
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@1inch/cross-chain-sdk

Sdk for creating atomic swaps through 1inch

  • 0.1.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
74
increased by45.1%
Maintainers
0
Weekly downloads
 
Created
Source

Cross Chain SDK

Sdk for creating atomic swaps through 1inch

Resources

  • Dev portal

Docs

Installation

npm install @1inch/cross-chain-sdk

Setup

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  
})  

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')  

Order submission

// submit order  
const _orderInfo = await sdk.submitOrder(  
	quote.srcChainId,  
	order,  
	quoteId,  
	secretHashes  
)  
console.log({hash}, 'order submitted')  

Secret submission

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)  

Whole script

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

Package last updated on 26 Nov 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc