Skip to content

Commit

Permalink
lint & prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Aug 12, 2024
1 parent dc8939e commit c53a446
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 295 deletions.
Original file line number Diff line number Diff line change
@@ -1,72 +1,92 @@
import Web3 from "web3";
import * as dotenv from "dotenv";
import axios from "axios";
import { getEnvVar } from "../../../utils/env";
import { sendRawTransaction } from "../../../utils/web3";
import { Big } from "big.js";
import { ensure } from "../../../utils/utils";
import { TransactionConfig } from "web3-core";
import Web3 from 'web3';
import * as dotenv from 'dotenv';
import axios from 'axios';
import { getEnvVar } from '../../../utils/env';
import { sendRawTransaction } from '../../../utils/web3';
import { Big } from 'big.js';
import { ensure } from '../../../utils/utils';
import { TransactionConfig } from 'web3-core';

dotenv.config({ path: ".env" });
dotenv.config({ path: '.env' });
const main = async () => {
const baseUrl = getEnvVar("REST_API_URL");
const baseUrl = getEnvVar('REST_API_URL');
// sender address
const fromAddress = getEnvVar("ETH_ACCOUNT_ADDRESS");
const fromAddress = getEnvVar('ETH_ACCOUNT_ADDRESS');
// recipient address
const toAddress = getEnvVar("POL_ACCOUNT_ADDRESS");
const toAddress = getEnvVar('POL_ACCOUNT_ADDRESS');

// configure web3
const web3 = new Web3(getEnvVar("WEB3_PROVIDER_URL"));
const account = web3.eth.accounts.privateKeyToAccount(getEnvVar("ETH_PRIVATE_KEY"));
const web3 = new Web3(getEnvVar('WEB3_PROVIDER_URL'));
const account = web3.eth.accounts.privateKeyToAccount(
getEnvVar('ETH_PRIVATE_KEY'),
);
web3.eth.accounts.wallet.add(account);

const chains = (await axios.get(`${baseUrl}/chains`)).data;

const sourceChain = chains['ETH'];
const sourceTokenInfo = ensure(sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDC"));
const sourceTokenInfo = ensure(
sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === 'USDC'),
);

const destinationChain = chains['POL'];
const destinationTokenInfo = ensure(destinationChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDC"));
const destinationTokenInfo = ensure(
destinationChain.tokens.find(
(tokenInfo: any) => tokenInfo.symbol === 'USDC',
),
);

const amountToSendInt = parseFloat("5") * (10 ** sourceTokenInfo.decimals);
const gasFeeOptions = (await axios.get(
`${baseUrl}/gas/fee`
+ `?sourceToken=${sourceTokenInfo.tokenAddress}`
+ `&destinationToken=${destinationTokenInfo.tokenAddress}`
+ `&messenger=ALLBRIDGE`
)).data;
const amountToSendInt = parseFloat('5') * 10 ** sourceTokenInfo.decimals;
const gasFeeOptions = (
await axios.get(
`${baseUrl}/gas/fee` +
`?sourceToken=${sourceTokenInfo.tokenAddress}` +
`&destinationToken=${destinationTokenInfo.tokenAddress}` +
`&messenger=ALLBRIDGE`,
)
).data;
const gasFeeAmount = ensure(gasFeeOptions['stablecoin']);

// authorize the bridge to transfer tokens from sender's address
const rawTransactionApprove = await axios.get(`${baseUrl}/raw/approve?ownerAddress=${fromAddress}&tokenAddress=${sourceTokenInfo.tokenAddress}`);
const approveTxReceipt = await sendRawTransaction(web3, rawTransactionApprove.data as TransactionConfig);
console.log("approve tx id:", approveTxReceipt.transactionHash);
const rawTransactionApprove = await axios.get(
`${baseUrl}/raw/approve?ownerAddress=${fromAddress}&tokenAddress=${sourceTokenInfo.tokenAddress}`,
);
const approveTxReceipt = await sendRawTransaction(
web3,
rawTransactionApprove.data as TransactionConfig,
);
console.log('approve tx id:', approveTxReceipt.transactionHash);

const gasFeeAmountInt = gasFeeAmount.int;
const totalAmountInt = new Big(amountToSendInt).add(gasFeeAmountInt).toFixed();
const totalAmountInt = new Big(amountToSendInt)
.add(gasFeeAmountInt)
.toFixed();
console.log(
`Sending ${amountToSendInt} ${sourceTokenInfo.symbol} (gas fee ${gasFeeAmountInt} ${sourceTokenInfo.symbol}). Total amount: ${totalAmountInt} ${sourceTokenInfo.symbol}`
`Sending ${amountToSendInt} ${sourceTokenInfo.symbol} (gas fee ${gasFeeAmountInt} ${sourceTokenInfo.symbol}). Total amount: ${totalAmountInt} ${sourceTokenInfo.symbol}`,
);

// initiate transfer
const rawTransactionTransfer = await axios.get(
`${baseUrl}/raw/bridge?amount=${totalAmountInt}`
+ `&sender=${fromAddress}`
+ `&recipient=${toAddress}`
+ `&sourceToken=${sourceTokenInfo.tokenAddress}`
+ `&destinationToken=${destinationTokenInfo.tokenAddress}`
+ `&messenger=ALLBRIDGE`
+ `&feePaymentMethod=WITH_STABLECOIN`
+ `&fee=${gasFeeAmount.int}`
`${baseUrl}/raw/bridge?amount=${totalAmountInt}` +
`&sender=${fromAddress}` +
`&recipient=${toAddress}` +
`&sourceToken=${sourceTokenInfo.tokenAddress}` +
`&destinationToken=${destinationTokenInfo.tokenAddress}` +
`&messenger=ALLBRIDGE` +
`&feePaymentMethod=WITH_STABLECOIN` +
`&fee=${gasFeeAmount.int}`,
);

const txReceipt = await sendRawTransaction(web3, rawTransactionTransfer.data as TransactionConfig);
console.log("tx id:", txReceipt.transactionHash);
const txReceipt = await sendRawTransaction(
web3,
rawTransactionTransfer.data as TransactionConfig,
);
console.log('tx id:', txReceipt.transactionHash);
};

main()
.then(() => {
console.log("Done");
console.log('Done');
})
.catch((e) => {
console.error(e);
Expand Down
86 changes: 52 additions & 34 deletions examples/src/usage/bridge/evm/evm-build-approve-and-send-tx.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,76 @@
import Web3 from "web3";
import * as dotenv from "dotenv";
import axios from "axios";
import { getEnvVar } from "../../../utils/env";
import { sendRawTransaction } from "../../../utils/web3";
import { ensure } from "../../../utils/utils";
import { TransactionConfig } from "web3-core";

dotenv.config({ path: ".env" });
import Web3 from 'web3';
import * as dotenv from 'dotenv';
import axios from 'axios';
import { getEnvVar } from '../../../utils/env';
import { sendRawTransaction } from '../../../utils/web3';
import { ensure } from '../../../utils/utils';
import { TransactionConfig } from 'web3-core';

dotenv.config({ path: '.env' });
const main = async () => {
const baseUrl = getEnvVar("REST_API_URL");
const baseUrl = getEnvVar('REST_API_URL');
// sender address
const fromAddress = getEnvVar("ETH_ACCOUNT_ADDRESS");
const fromAddress = getEnvVar('ETH_ACCOUNT_ADDRESS');
// recipient address
const toAddress = getEnvVar("TRX_ACCOUNT_ADDRESS");
const toAddress = getEnvVar('TRX_ACCOUNT_ADDRESS');

// configure web3
const web3 = new Web3(getEnvVar("WEB3_PROVIDER_URL"));
const account = web3.eth.accounts.privateKeyToAccount(getEnvVar("ETH_PRIVATE_KEY"));
const web3 = new Web3(getEnvVar('WEB3_PROVIDER_URL'));
const account = web3.eth.accounts.privateKeyToAccount(
getEnvVar('ETH_PRIVATE_KEY'),
);
web3.eth.accounts.wallet.add(account);

const chains = (await axios.get(`${baseUrl}/chains`)).data;

const sourceChain = chains['ETH'];
const sourceTokenInfo = ensure(sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDC"));
const sourceTokenInfo = ensure(
sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === 'USDC'),
);

const destinationChain = chains["TRX"];
const destinationTokenInfo = ensure(destinationChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDT"));
const destinationChain = chains['TRX'];
const destinationTokenInfo = ensure(
destinationChain.tokens.find(
(tokenInfo: any) => tokenInfo.symbol === 'USDT',
),
);

const amount = parseFloat("1.01") * (10 ** sourceTokenInfo.decimals);
const amount = parseFloat('1.01') * 10 ** sourceTokenInfo.decimals;

// authorize the bridge to transfer tokens from sender's address
const rawTransactionApprove = await axios.get(`${baseUrl}/raw/approve?ownerAddress=${fromAddress}&tokenAddress=${sourceTokenInfo.tokenAddress}`);
const approveTxReceipt = await sendRawTransaction(web3, rawTransactionApprove.data as TransactionConfig);
console.log("approve tx id:", approveTxReceipt.transactionHash);
const rawTransactionApprove = await axios.get(
`${baseUrl}/raw/approve?ownerAddress=${fromAddress}&tokenAddress=${sourceTokenInfo.tokenAddress}`,
);
const approveTxReceipt = await sendRawTransaction(
web3,
rawTransactionApprove.data as TransactionConfig,
);
console.log('approve tx id:', approveTxReceipt.transactionHash);

// initiate transfer
const rawTransactionTransfer = await axios.get(
`${baseUrl}/raw/bridge?amount=${amount}`
+ `&sender=${fromAddress}`
+ `&recipient=${toAddress}`
+ `&sourceToken=${sourceTokenInfo.tokenAddress}`
+ `&destinationToken=${destinationTokenInfo.tokenAddress}`
+ `&messenger=ALLBRIDGE`
+ `&feePaymentMethod=WITH_NATIVE_CURRENCY`
);

console.log(`Sending ${amount / (10 ** sourceTokenInfo.decimals)} ${sourceTokenInfo.symbol}`);
const txReceipt = await sendRawTransaction(web3, rawTransactionTransfer.data as TransactionConfig);
console.log("tx id:", txReceipt.transactionHash);
`${baseUrl}/raw/bridge?amount=${amount}` +
`&sender=${fromAddress}` +
`&recipient=${toAddress}` +
`&sourceToken=${sourceTokenInfo.tokenAddress}` +
`&destinationToken=${destinationTokenInfo.tokenAddress}` +
`&messenger=ALLBRIDGE` +
`&feePaymentMethod=WITH_NATIVE_CURRENCY`,
);

console.log(
`Sending ${amount / 10 ** sourceTokenInfo.decimals} ${sourceTokenInfo.symbol}`,
);
const txReceipt = await sendRawTransaction(
web3,
rawTransactionTransfer.data as TransactionConfig,
);
console.log('tx id:', txReceipt.transactionHash);
};

main()
.then(() => {
console.log("Done");
console.log('Done');
})
.catch((e) => {
console.error(e);
Expand Down
83 changes: 49 additions & 34 deletions examples/src/usage/bridge/evm/evm-build-swap-tx.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,74 @@
import Web3 from "web3";
import * as dotenv from "dotenv";
import axios from "axios";
import { getEnvVar } from "../../../utils/env";
import { sendRawTransaction } from "../../../utils/web3";
import { ensure } from "../../../utils/utils";
import { TransactionConfig } from "web3-core";

dotenv.config({ path: ".env" });
import Web3 from 'web3';
import * as dotenv from 'dotenv';
import axios from 'axios';
import { getEnvVar } from '../../../utils/env';
import { sendRawTransaction } from '../../../utils/web3';
import { ensure } from '../../../utils/utils';
import { TransactionConfig } from 'web3-core';

dotenv.config({ path: '.env' });
const main = async () => {
const baseUrl = getEnvVar("REST_API_URL");
const baseUrl = getEnvVar('REST_API_URL');
// sender address
const fromAddress = getEnvVar("ETH_ACCOUNT_ADDRESS");
const fromAddress = getEnvVar('ETH_ACCOUNT_ADDRESS');
// recipient address
const toAddress = getEnvVar("ETH_ACCOUNT_ADDRESS");
const toAddress = getEnvVar('ETH_ACCOUNT_ADDRESS');

// configure web3
const web3 = new Web3(getEnvVar("WEB3_PROVIDER_URL"));
const account = web3.eth.accounts.privateKeyToAccount(getEnvVar("ETH_PRIVATE_KEY"));
const web3 = new Web3(getEnvVar('WEB3_PROVIDER_URL'));
const account = web3.eth.accounts.privateKeyToAccount(
getEnvVar('ETH_PRIVATE_KEY'),
);
web3.eth.accounts.wallet.add(account);

const chains = (await axios.get(`${baseUrl}/chains`)).data;

const sourceChain = chains['ETH'];
const sourceTokenInfo = ensure(sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDT"));
const sourceTokenInfo = ensure(
sourceChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === 'USDT'),
);

const destinationChain = chains['ETH'];
const destinationTokenInfo = ensure(destinationChain.tokens.find((tokenInfo: any) => tokenInfo.symbol === "USDC"));
const destinationTokenInfo = ensure(
destinationChain.tokens.find(
(tokenInfo: any) => tokenInfo.symbol === 'USDC',
),
);

const amount = parseFloat("1.01") * (10 ** sourceTokenInfo.decimals);
const amount = parseFloat('1.01') * 10 ** sourceTokenInfo.decimals;

const minimumReceiveAmount = (await axios.get(
`${baseUrl}/bridge/receive/calculate`
+ `?sourceToken=${sourceTokenInfo.tokenAddress}`
+ `&destinationToken=${destinationTokenInfo.tokenAddress}`
+ `&amount=${amount}`
+ `&messenger=ALLBRIDGE`
)).data;
const minimumReceiveAmount = (
await axios.get(
`${baseUrl}/bridge/receive/calculate` +
`?sourceToken=${sourceTokenInfo.tokenAddress}` +
`&destinationToken=${destinationTokenInfo.tokenAddress}` +
`&amount=${amount}` +
`&messenger=ALLBRIDGE`,
)
).data;
// initiate transfer
const rawTransactionTransfer = await axios.get(
`${baseUrl}/raw/swap?amount=${amount}`
+ `&sender=${fromAddress}`
+ `&recipient=${toAddress}`
+ `&sourceToken=${sourceTokenInfo.tokenAddress}`
+ `&destinationToken=${destinationTokenInfo.tokenAddress}`
+ `&minimumReceiveAmount=${parseFloat(minimumReceiveAmount.amountReceivedInFloat) * (10 ** sourceTokenInfo.decimals)}`
`${baseUrl}/raw/swap?amount=${amount}` +
`&sender=${fromAddress}` +
`&recipient=${toAddress}` +
`&sourceToken=${sourceTokenInfo.tokenAddress}` +
`&destinationToken=${destinationTokenInfo.tokenAddress}` +
`&minimumReceiveAmount=${parseFloat(minimumReceiveAmount.amountReceivedInFloat) * 10 ** sourceTokenInfo.decimals}`,
);

console.log(`Swaping ${amount / (10 ** sourceTokenInfo.decimals)} ${sourceTokenInfo.symbol}`);
const txReceipt = await sendRawTransaction(web3, rawTransactionTransfer.data as TransactionConfig);
console.log("tx id:", txReceipt.transactionHash);
console.log(
`Swaping ${amount / 10 ** sourceTokenInfo.decimals} ${sourceTokenInfo.symbol}`,
);
const txReceipt = await sendRawTransaction(
web3,
rawTransactionTransfer.data as TransactionConfig,
);
console.log('tx id:', txReceipt.transactionHash);
};

main()
.then(() => {
console.log("Done");
console.log('Done');
})
.catch((e) => {
console.error(e);
Expand Down
Loading

0 comments on commit c53a446

Please sign in to comment.