Skip to content

Commit

Permalink
Fix: remove process depedency which causes browser integration failure
Browse files Browse the repository at this point in the history
- Fixed all examples
- Removed `env` dependancy
- Fixed tests
  • Loading branch information
saadahmsiddiqui committed Oct 10, 2024
1 parent 640a0cb commit 488935b
Show file tree
Hide file tree
Showing 30 changed files with 205 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Environment } from '@buildwithsygma/core';
import type { Environment } from "@buildwithsygma/core";

declare global {
namespace NodeJS {
Expand Down
37 changes: 19 additions & 18 deletions examples/bitcoin-to-evm-fungible-transfer/src/transfer.p2tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import { initEccLib, networks, Signer } from "bitcoinjs-lib";
import dotenv from "dotenv";
import * as tinysecp from "tiny-secp256k1";

import {
calculateSize,
} from "./blockstreamApi.js";
import { calculateSize } from "./blockstreamApi.js";
import { Environment } from "@buildwithsygma/core";

dotenv.config();

Expand All @@ -32,7 +31,6 @@ const {
AMOUNT,
} = process.env;


if (
!SOURCE_CAIPID ||
!DESTINATION_ADDRESS ||
Expand All @@ -54,20 +52,21 @@ async function btcToEvmTransfer(): Promise<void> {
const bip32 = BIP32Factory(tinysecp);
console.log("Transfer BTC to EVM");

const { tweakedSigner, publicKeyDropedDERHeader } = await getPublicKey({
const { tweakedSigner, publicKeyDropedDERHeader } = (await getPublicKey({
bip32,
mnemonic: MNEMONIC as string,
derivationPath: DERIVATION_PATH as string,
network: networks.testnet,
typeOfAddress: TypeOfAddress.P2TR
}
) as { publicKeyDropedDERHeader: Buffer, tweakedSigner: Signer };
typeOfAddress: TypeOfAddress.P2TR,
})) as { publicKeyDropedDERHeader: Buffer; tweakedSigner: Signer };

const feeRate = await getFeeEstimates('5');
const feeRate = await getFeeEstimates(process.env.SYGMA_ENV, "5");
const utxos = await fetchUTXOS(
ADDRESS as unknown as string);
process.env.SYGMA_ENV,
ADDRESS as unknown as string
);

const processedUtxos = processUtxos(utxos, AMOUNT);
const processedUtxos = processUtxos(utxos, Number(BigInt(AMOUNT!)));

const mapedUtxos = processedUtxos.map((utxo) => ({
utxoTxId: utxo.txid,
Expand All @@ -81,26 +80,28 @@ async function btcToEvmTransfer(): Promise<void> {
publicKey: publicKeyDropedDERHeader,
depositAddress: ADDRESS as unknown as string,
domainId: DESTINATION_CHAIN_ID,
amount: AMOUNT,
amount: BigInt(AMOUNT!),
feeValue: BigInt(0),
changeAddress: ADDRESS as unknown as string,
signer: tweakedSigner,
typeOfAddress: TypeOfAddress.P2TR,
});

const transferParams: BitcoinTransferParams = {
source: SOURCE_CAIPID,
source: SOURCE_CAIPID!,
destination: DESTINATION_CHAIN_ID,
destinationAddress: DESTINATION_ADDRESS,
amount: AMOUNT,
resource: RESOURCE_ID,
destinationAddress: DESTINATION_ADDRESS!,
amount: BigInt(AMOUNT!),
resource: RESOURCE_ID!,
utxoData: mapedUtxos,
feeRate: BigInt(Math.ceil(feeRate)),
publicKey: publicKeyDropedDERHeader,
typeOfAddress: TypeOfAddress.P2TR,
network: networks.testnet,
changeAddress: ADDRESS,
size: BigInt(size),
environment: Environment.TESTNET,
sourceAddress: "",
};

const transfer = await createBitcoinFungibleTransfer(transferParams);
Expand All @@ -116,8 +117,8 @@ async function btcToEvmTransfer(): Promise<void> {
const tx = psbt.extractTransaction(true);
console.log("Transaction hex", tx.toHex());

const txId = await broadcastTransaction(tx.toHex());
const txId = await broadcastTransaction(process.env.SYGMA_ENV, tx.toHex());
console.log("Transaction broadcasted", `${EXPLORER_URL}/tx/${txId}`);
}

btcToEvmTransfer().finally(() => { });
btcToEvmTransfer().finally(() => {});
45 changes: 23 additions & 22 deletions examples/bitcoin-to-evm-fungible-transfer/src/transfer.p2wpkh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ import {
createBitcoinFungibleTransfer,
TypeOfAddress,
getPublicKey,
getFeeEstimates,
fetchUTXOS,
processUtxos,
broadcastTransaction
getFeeEstimates,
fetchUTXOS,
processUtxos,
broadcastTransaction,
} from "@buildwithsygma/bitcoin";
import { BIP32Factory, BIP32Interface } from "bip32";
import { initEccLib, networks } from "bitcoinjs-lib";
import dotenv from "dotenv";
import * as tinysecp from "tiny-secp256k1";

import {
calculateSize,
} from "./blockstreamApi.js";
import { calculateSize } from "./blockstreamApi.js";

dotenv.config();

Expand Down Expand Up @@ -53,19 +51,21 @@ async function btcToEvmTransfer(): Promise<void> {
const bip32 = BIP32Factory(tinysecp);
console.log("Transfer BTC to EVM");

const { derivedNode } = await getPublicKey({
const { derivedNode } = (await getPublicKey({
bip32,
mnemonic: MNEMONIC as string,
derivationPath: DERIVATION_PATH as string,
network: networks.testnet,
typeOfAddress: TypeOfAddress.P2WPKH
}
) as { derivedNode: BIP32Interface };
typeOfAddress: TypeOfAddress.P2WPKH,
})) as { derivedNode: BIP32Interface };

const feeRate = await getFeeEstimates('5');
const utxos = await fetchUTXOS(ADDRESS as unknown as string);
const feeRate = await getFeeEstimates(process.env.SYGMA_ENV, "5");
const utxos = await fetchUTXOS(
process.env.SYGMA_ENV,
ADDRESS as unknown as string
);

const processedUtxos = processUtxos(utxos, AMOUNT);
const processedUtxos = processUtxos(utxos, Number(BigInt(AMOUNT!)));

const mapedUtxos = processedUtxos.map((utxo) => ({
utxoTxId: utxo.txid,
Expand All @@ -79,26 +79,27 @@ async function btcToEvmTransfer(): Promise<void> {
publicKey: derivedNode.publicKey,
depositAddress: ADDRESS as unknown as string,
domainId: DESTINATION_CHAIN_ID,
amount: AMOUNT,
amount: BigInt(AMOUNT!),
feeValue: BigInt(0),
changeAddress: ADDRESS as unknown as string,
signer: derivedNode,
typeOfAddress: TypeOfAddress.P2WPKH,
}); // aprox estimation of the size of the tx

const transferParams: BitcoinTransferParams = {
source: SOURCE_CAIPID,
destination: DESTINATION_CHAIN_ID,
destinationAddress: DESTINATION_ADDRESS,
amount: AMOUNT,
resource: RESOURCE_ID,
source: SOURCE_CAIPID!,
destination: DESTINATION_CHAIN_ID!,
destinationAddress: DESTINATION_ADDRESS!,
amount: BigInt(AMOUNT!),
resource: RESOURCE_ID!,
utxoData: mapedUtxos,
publicKey: derivedNode.publicKey,
typeOfAddress: TypeOfAddress.P2WPKH,
network: networks.testnet,
changeAddress: ADDRESS,
feeRate: BigInt(Math.ceil(feeRate)),
size: BigInt(size),
sourceAddress: "",
};

const transfer = await createBitcoinFungibleTransfer(transferParams);
Expand All @@ -114,8 +115,8 @@ async function btcToEvmTransfer(): Promise<void> {
const tx = psbt.extractTransaction(true);
console.log("Transaction hex", tx.toHex());

const txId = await broadcastTransaction(tx.toHex());
const txId = await broadcastTransaction(process.env.SYGMA_ENV, tx.toHex());
console.log("Transaction broadcasted", `${EXPLORER_URL}/tx/${txId}`);
}

btcToEvmTransfer().finally(() => { });
btcToEvmTransfer().finally(() => {});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Environment } from './types.js';
import type { Environment } from "@buildwithsygma/core";

declare global {
namespace NodeJS {
Expand Down
40 changes: 28 additions & 12 deletions examples/evm-to-bitcoin-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createEvmFungibleAssetTransfer } from "@buildwithsygma/evm";
import { Eip1193Provider } from "@buildwithsygma/core";
import {
createFungibleAssetTransfer,
FungibleTransferParams,
} from "@buildwithsygma/evm";
import dotenv from "dotenv";
import { Wallet, providers } from "ethers";
import Web3HttpProvider from "web3-providers-http";
Expand All @@ -13,42 +17,54 @@ if (!privateKey) {

const SEPOLIA_CHAIN_ID = 11155111;
const BITCOIN_DOMAIN_CAIPID = "bip122:000000000933ea01ad0ee984209779ba";
const RESOURCE_ID = "0x0000000000000000000000000000000000000000000000000000000000000700";
const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL || "https://eth-sepolia-public.unifra.io"
const RESOURCE_ID =
"0x0000000000000000000000000000000000000000000000000000000000000700";
const SEPOLIA_RPC_URL =
process.env.SEPOLIA_RPC_URL || "https://eth-sepolia-public.unifra.io";
const BTC_DESTINATION_ADDRESS = process.env.BTC_DESTINATION_ADDRESS;

const explorerUrls: Record<number, string> = { [SEPOLIA_CHAIN_ID]: 'https://sepolia.etherscan.io' };
const getTxExplorerUrl = (params: { txHash: string; chainId: number }): string => `${explorerUrls[params.chainId]}/tx/${params.txHash}`;
const explorerUrls: Record<number, string> = {
[SEPOLIA_CHAIN_ID]: "https://sepolia.etherscan.io",
};
const getTxExplorerUrl = (params: {
txHash: string;
chainId: number;
}): string => `${explorerUrls[params.chainId]}/tx/${params.txHash}`;

export async function erc20Transfer(): Promise<void> {
const web3Provider = new Web3HttpProvider(SEPOLIA_RPC_URL);
const ethersWeb3Provider = new providers.Web3Provider(web3Provider);
const wallet = new Wallet(privateKey!, ethersWeb3Provider);

const params = {
const params: FungibleTransferParams = {
source: SEPOLIA_CHAIN_ID,
destination: BITCOIN_DOMAIN_CAIPID,
sourceNetworkProvider: web3Provider,
sourceNetworkProvider: web3Provider as unknown as Eip1193Provider,
resource: RESOURCE_ID,
amount: BigInt(1) * BigInt(1e8), // or any amount to send
destinationAddress: BTC_DESTINATION_ADDRESS,
recipientAddress: BTC_DESTINATION_ADDRESS!,
sourceAddress: await wallet.getAddress(),
environment: process.env.SYGMA_ENV,
};

const transfer = await createEvmFungibleAssetTransfer(params);
const transfer = await createFungibleAssetTransfer(params);

const approvals = await transfer.getApprovalTransactions();
console.log(`Approving Tokens (${approvals.length})...`);
for (const approval of approvals) {
const response = await wallet.sendTransaction(approval);
await response.wait();
console.log(`Approved, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`);
console.log(
`Approved, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`
);
}

const transferTx = await transfer.getTransferTransaction();
const response = await wallet.sendTransaction(transferTx);
await response.wait();
console.log(`Deposited, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`);
console.log(
`Deposited, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`
);
}

erc20Transfer().finally(() => { });
erc20Transfer().finally(() => {});
1 change: 1 addition & 0 deletions examples/evm-to-evm-erc20-contract-call/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export async function erc20Transfer(): Promise<void> {
recipientAddress: ethers.constants.AddressZero,
sourceAddress: sourceAddress,
optionalGas: BigInt(5_000_000),
environment: process.env.SYGMA_ENV,
optionalMessage: {
receiver: destinationAddress,
transactionId: ethers.utils.formatBytes32String("EVM-ERC20+GENERIC"),
Expand Down
1 change: 1 addition & 0 deletions examples/evm-to-evm-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export async function erc20Transfer(): Promise<void> {
amount: BigInt(1) * BigInt(1e6),
recipientAddress: destinationAddress,
sourceAddress: sourceAddress,
environment: process.env.SYGMA_ENV,
};

const transfer = await createFungibleAssetTransfer(params);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Environment } from '@buildwithsygma/core';
import type { Environment } from "@buildwithsygma/core";

declare global {
namespace NodeJS {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export async function genericMessage(): Promise<void> {
sourceNetworkProvider: sourceProvider as unknown as Eip1193Provider,
sourceAddress: walletAddress,
resource: RESOURCE_ID,
environment: process.env.SYGMA_ENV,
});
// transaction that can be sent to the chain
const transaction = await transfer.getTransferTransaction();
Expand Down
1 change: 1 addition & 0 deletions examples/evm-to-evm-non-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export async function erc721Transfer(): Promise<void> {
tokenId: process.env.TOKEN_ID as string,
recipientAddress: destinationAddress,
sourceAddress,
environment: process.env.SYGMA_ENV,
};

const transfer = await createNonFungibleAssetTransfer(params);
Expand Down
16 changes: 10 additions & 6 deletions examples/evm-to-substrate-fungible-transfer/src/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Eip1193Provider } from "@buildwithsygma/core";
import { createEvmFungibleAssetTransfer } from "@buildwithsygma/evm";
import {
createFungibleAssetTransfer,
FungibleTransferParams,
} from "@buildwithsygma/evm";
import dotenv from "dotenv";
import { Wallet, providers } from "ethers";
import Web3HttpProvider from "web3-providers-http";
Expand Down Expand Up @@ -35,32 +38,33 @@ export async function erc20Transfer(): Promise<void> {
const sourceAddress = await wallet.getAddress();
const destinationAddress = "5GjowPEaFNnwbrmpPuDmBVdF2e7n3cHwk2LnUwHXsaW5KtEL";

const params = {
const params: FungibleTransferParams = {
source: SEPOLIA_CHAIN_ID,
destination: TANGLE_CHAIN_ID,
sourceNetworkProvider: web3Provider as unknown as Eip1193Provider,
resource: RESOURCE_ID,
amount: BigInt(1) * BigInt(1e18),
destinationAddress: destinationAddress,
recipientAddress: destinationAddress,
sourceAddress: sourceAddress,
environment: process.env.SYGMA_ENV,
};

const transfer = await createEvmFungibleAssetTransfer(params);
const transfer = await createFungibleAssetTransfer(params);
const approvals = await transfer.getApprovalTransactions();
console.log(`Approving Tokens (${approvals.length})...`);
for (const approval of approvals) {
const response = await wallet.sendTransaction(approval);
await response.wait();
console.log(
`Approved, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`,
`Approved, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`
);
}

const transferTx = await transfer.getTransferTransaction();
const response = await wallet.sendTransaction(transferTx);
await response.wait();
console.log(
`Deposited, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`,
`Deposited, transaction: ${getTxExplorerUrl({ txHash: response.hash, chainId: SEPOLIA_CHAIN_ID })}`
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Environment } from '@buildwithsygma/core';
import type { Environment } from "@buildwithsygma/core";

declare global {
namespace NodeJS {
Expand Down
Loading

0 comments on commit 488935b

Please sign in to comment.