Skip to content

Commit

Permalink
chore: tidy up the example scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
DNR500 committed Jun 5, 2024
1 parent 076f823 commit 2f07790
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 60 deletions.
9 changes: 5 additions & 4 deletions examples/node/examples/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,25 @@ async function run() {
],
})

console.info('>> Initialized, Requesting route')

const routeRequest = {
toAddress: account.address,
fromAddress: account.address,
fromChainId: ChainId.OPT, // Optimism
fromAmount: '1000000', // 1 USDC
fromTokenAddress: findDefaultToken(CoinKey.USDC, ChainId.OPT).address,
fromTokenAddress: findDefaultToken(CoinKey.USDC, ChainId.OPT).address, // USDC on Optimism
toChainId: ChainId.ARB, // Arbitrum
toTokenAddress: findDefaultToken(CoinKey.USDC, ChainId.ARB).address,
options: {
slippage: 0.03, // = 3%
},
}

console.info('>> Requesting route', routeRequest)

const routeResponse = await getRoutes(routeRequest)
const route = routeResponse.routes[0]

console.info('>> Got Route')
console.info('>> Got Route', routeResponse)

if (!(await promptConfirm('Execute Route?'))) {
return
Expand Down
39 changes: 23 additions & 16 deletions examples/node/examples/klimaRetireExactCarbon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,13 @@ const run = async () => {
],
})

// config for klima contract run
// config for klima contract run - https://docs.klimadao.finance/developers/contracts/retirement/v2-diamond/generalized-retirement
const config = {
fromChain: ChainId.OPT,
// the Klima Contract is on Polygon
toChain: ChainId.POL,
fromToken: findDefaultToken(CoinKey.USDC, ChainId.OPT).address,
retireAmount: '100000', // 1 usdc
// https://docs.klimadao.finance/developers/contracts/retirement/v2-diamond/generalized-retirement
retireAmount: '100000', // USDC
klimaContractAddress:
'0x8cE54d9625371fb2a068986d32C85De8E6e995f8' as Address, // Klima Ethereum Contract on Polygon
klimaContractSourceToken: findDefaultToken(CoinKey.USDCe, ChainId.POL), // USDCe POL
Expand All @@ -83,6 +84,12 @@ const run = async () => {
'function getSourceAmountDefaultRetirement(address,address,uint256) external view returns (uint256 amountIn)',
'function retireExactCarbonDefault(address, address, uint256, uint256, string, address, string, string, uint8)',
],
klimaContractRetiringEntityString: 'LI.FI',
klimaContractBeneficiaryAddress:
'0x552008c0f6870c2f77e5cC1d2eb9bdff03e30Ea0',
klimaContractBeneficiaryString: 'LI.FI',
klimaContractRetirementMessage: 'Cross Chain Contract Calls',
klimaContractFromMode: 0,
}

const abi = parseAbi(config.klimaContractAbi)
Expand All @@ -97,45 +104,45 @@ const run = async () => {
abi,
functionName: 'getSourceAmountDefaultRetirement',
args: [
config.klimaContractSourceToken.address, // USDCe POL - address sourceToken,
config.klimaContractPoolTokenAddress, // Base Carbon Tonne Polygon - address poolToken,
config.klimaContractSourceToken.address,
config.klimaContractPoolTokenAddress,
config.retireAmount, // uint256 retireAmount,
],
})) as bigint

const usdcAmount = parseUnits(
sourceAmountDefaultRetirement.toString(),
config.klimaContractSourceToken.decimals // USDCe POL decimals
config.klimaContractSourceToken.decimals
).toString()

const retireTxData = encodeFunctionData({
abi,
functionName: 'retireExactCarbonDefault',
args: [
config.klimaContractSourceToken.address, // USDCe POL - address sourceToken,
config.klimaContractPoolTokenAddress, // Base Carbon Tonne Polygon - address poolToken,
config.klimaContractSourceToken.address, // address sourceToken,
config.klimaContractPoolTokenAddress, // address poolToken,
usdcAmount, // uint256 maxAmountIn,
config.retireAmount, // uint256 retireAmount,
'LI.FI', // string memory retiringEntityString,
'0x552008c0f6870c2f77e5cC1d2eb9bdff03e30Ea0', // address beneficiaryAddress,
'LI.FI', // string memory beneficiaryString,
'Cross Chain Contract Calls', // string memory retirementMessage,
0, // LibTransfer.From fromMode],
config.klimaContractRetiringEntityString, // string memory retiringEntityString,
config.klimaContractBeneficiaryAddress, // address beneficiaryAddress,
config.klimaContractBeneficiaryString, // string memory beneficiaryString,
config.klimaContractRetirementMessage, // string memory retirementMessage,
config.klimaContractFromMode, // LibTransfer.From fromMode],
],
})

const contractCallsQuoteRequest: ContractCallsQuoteRequest = {
fromChain: config.fromChain,
fromToken: config.fromToken,
fromAddress: account.address,
toChain: ChainId.POL,
toToken: config.klimaContractSourceToken.address, // USDCe POL address
toChain: config.toChain,
toToken: config.klimaContractSourceToken.address,
toAmount: usdcAmount,
allowBridges: ['hop', 'across', 'amarok'],
contractCalls: [
{
fromAmount: usdcAmount,
fromTokenAddress: config.klimaContractSourceToken.address, // USDCe POL address
fromTokenAddress: config.klimaContractSourceToken.address,
toContractAddress: config.klimaContractAddress,
toContractCallData: retireTxData,
toContractGasLimit: config.klimaContractGasLimit,
Expand Down
15 changes: 7 additions & 8 deletions examples/node/examples/multihop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ const run = async () => {
intermediateToken: findDefaultToken(CoinKey.USDC, ChainId.POL).address, // USDC POL
toChain: ChainId.OPT, // Optimism
toToken: findDefaultToken(CoinKey.USDC, ChainId.OPT).address, // USDC OPT
amount: '100000', // 1 usdc
amount: '100000', // USDC
}

const secondBridgeQuoteRequest: QuoteRequest = {
fromChain: config.intermediateChain, // Polygon
fromToken: config.intermediateToken, // USDC POL
fromChain: config.intermediateChain,
fromToken: config.intermediateToken,
fromAmount: config.amount,
toChain: config.toChain, // Optimism
toToken: config.toToken, // USDC OPT
toChain: config.toChain,
toToken: config.toToken,
fromAddress: account.address, // will actually be a relayer
allowBridges: ['hop', 'stargate', 'across', 'amarok'],
maxPriceImpact: 0.4,
Expand All @@ -94,10 +94,9 @@ const run = async () => {
const secondBridgeQuote = await getQuote(secondBridgeQuoteRequest)
console.info('>> got second quote', secondBridgeQuote)

// quote
const quoteRequest: ContractCallsQuoteRequest = {
fromChain: config.fromChain, // Arbitrum
fromToken: config.fromToken, // USDC ARB
fromChain: config.fromChain,
fromToken: config.fromToken,
fromAddress: account.address,
toChain: secondBridgeQuote.action.fromChainId,
toToken: secondBridgeQuote.action.fromToken.address,
Expand Down
20 changes: 8 additions & 12 deletions examples/node/examples/polynomialDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ const run = async () => {
const config = {
// For polynomial deposit, quotes are available for optimism, ethereum, arbitrum or polygon
fromChain: ChainId.ARB,
// token can be either ETH or USDCe (the site says USDC but USDCe is the one only I can get a quote with)
fromToken: findDefaultToken(CoinKey.USDCe, ChainId.ARB).address, // ETH on Etheruem
// NOTE: anything less than '1000000000' results in a 400 response from the '/contractCalls' endpoint
// with the message "The from amount must be greater than zero.",
amount: '1000000000',
// Polynomial Ethereum Contract is on OPT
toChain: ChainId.OPT,
fromToken: findDefaultToken(CoinKey.USDCe, ChainId.ARB).address,
amount: '100000000000000', // sETH amount
polynomialContractAddress: '0x2D46292cbB3C601c6e2c74C32df3A4FCe99b59C7', // Polynomial Ethereum Contract on Optimism
polynomialContractToken: '0xE405de8F52ba7559f9df3C368500B6E6ae6Cee49', // sETH on Optimism
polynomialContractGasLimit: '200000',
Expand All @@ -82,26 +81,23 @@ const run = async () => {
],
}

const abi = parseAbi(config.polynomialContractAbi)

const stakeTxData = encodeFunctionData({
abi,
abi: parseAbi(config.polynomialContractAbi),
functionName: 'initiateDeposit',
args: [account.address, config.amount],
})

// TODO: check if these are the correct values
const contractCallsQuoteRequest: ContractCallsQuoteRequest = {
fromChain: config.fromChain,
fromToken: config.fromToken,
fromAddress: account.address,
toChain: ChainId.OPT,
toToken: config.polynomialContractToken, // sETH on Optimism
toChain: config.toChain,
toToken: config.polynomialContractToken,
toAmount: config.amount,
contractCalls: [
{
fromAmount: config.amount,
fromTokenAddress: config.polynomialContractToken, // sETH on Optimism
fromTokenAddress: config.polynomialContractToken,
toContractAddress: config.polynomialContractAddress,
toContractCallData: stakeTxData,
toContractGasLimit: config.polynomialContractGasLimit,
Expand Down
39 changes: 19 additions & 20 deletions examples/node/examples/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
ChainId,
CoinKey,
} from '@lifi/sdk'
import type { PrivateKeyAccount, Address, Chain } from 'viem'
import type { Address, Chain } from 'viem'
import { createWalletClient, http } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { optimism } from 'viem/chains'
Expand All @@ -17,22 +17,6 @@ import { reportStepsExecutionToTerminal } from '../helpers/reportStepsExecutionT

const dataTypes = (lifiDataTypes as any).default

// In the example below we are exchanging USDC and USDT tokens on Optimism
const getRequestRoute = ({ address }: PrivateKeyAccount) => ({
toAddress: address,
fromAddress: address,
fromChainId: ChainId.OPT, // Optimisim
fromAmount: '100000', // 1 USDT
fromTokenAddress: dataTypes.findDefaultToken(CoinKey.USDC, ChainId.OPT)
.address,
toChainId: ChainId.OPT, // Optimisim
toTokenAddress: dataTypes.findDefaultToken(CoinKey.USDT, ChainId.OPT).address,
options: {
slippage: 0.03, // = 3%
allowSwitchChain: false, // execute all transaction on starting chain
},
})

async function run() {
console.info('>> Starting Swap Demo')

Expand All @@ -59,12 +43,27 @@ async function run() {
],
})

console.info('>> Initialized, Requesting route')
const routeRequest = getRequestRoute(account)
const routeRequest = {
toAddress: account.address,
fromAddress: account.address,
fromChainId: ChainId.OPT, // Optimisim
fromAmount: '100000', // USDT
fromTokenAddress: dataTypes.findDefaultToken(CoinKey.USDC, ChainId.OPT)
.address,
toChainId: ChainId.OPT, // Optimisim
toTokenAddress: dataTypes.findDefaultToken(CoinKey.USDT, ChainId.OPT)
.address,
options: {
slippage: 0.03, // = 3%
allowSwitchChain: false, // execute all transaction on starting chain
},
}
console.info('>> Requesting route', routeRequest)

const routeResponse = await getRoutes(routeRequest)
const route = routeResponse.routes[0]

console.info('>> Got Route')
console.info('>> Got Route', route)

if (!(await promptConfirm('Execute Route?'))) {
return
Expand Down

0 comments on commit 2f07790

Please sign in to comment.