diff --git a/.gitignore b/.gitignore index 1dc3fa32..9befab58 100644 --- a/.gitignore +++ b/.gitignore @@ -77,6 +77,7 @@ typings/ # dotenv environment variables file .env .env.test +.env.local # parcel-bundler cache (https://parceljs.org/) .cache diff --git a/cspell.config.cjs b/cspell.config.cjs index 38176103..33aecf78 100644 --- a/cspell.config.cjs +++ b/cspell.config.cjs @@ -12,6 +12,7 @@ module.exports = { '**/*.sol', 'pnpm-lock.yaml', 'CHANGELOG.md', + '.gitignore', ], words: vscodeConfig['cSpell.words'], }; diff --git a/examples/sdk-simple/.env.example b/examples/sdk-simple/.env.example new file mode 100644 index 00000000..10c52760 --- /dev/null +++ b/examples/sdk-simple/.env.example @@ -0,0 +1,2 @@ +EVM_PRIVATE_KEY= +POLKADOT_PRIVATE_KEY= diff --git a/examples/sdk-simple/index.ts b/examples/sdk-simple/index.ts index 145ac476..0b106fc7 100644 --- a/examples/sdk-simple/index.ts +++ b/examples/sdk-simple/index.ts @@ -5,26 +5,33 @@ import { cryptoWaitReady } from '@polkadot/util-crypto'; import { http, type Address, createWalletClient } from 'viem'; import { privateKeyToAccount } from 'viem/accounts'; +const { EVM_PRIVATE_KEY, POLKADOT_PRIVATE_KEY } = process.env; + +if (!EVM_PRIVATE_KEY || !POLKADOT_PRIVATE_KEY) { + throw new Error( + 'Env variables EVM_PRIVATE_KEY and POLKADOT_PRIVATE_KEY must be defined', + ); +} // Moonbeam Signer =========================================================== -const moonbeamPrivateKey = ''; -const account = privateKeyToAccount(moonbeamPrivateKey as Address); +const account = privateKeyToAccount(EVM_PRIVATE_KEY as Address); const walletClient = createWalletClient({ account, chain: moonbeam.getViemChain(), transport: http(), }); -// Polkadot Signer =========================================================== +console.log(`\nMoonbeam address: ${account.address}`); -const polkadotPrivateKey = ''; +// Polkadot Signer =========================================================== await cryptoWaitReady(); const keyring = new Keyring({ ss58Format: polkadot.ss58Format, type: 'sr25519', }); -const pair = keyring.createFromUri(polkadotPrivateKey); +const pair = keyring.createFromUri(POLKADOT_PRIVATE_KEY); +console.log(`Substrate address: ${pair.address}`); // =========================================================================== @@ -64,19 +71,19 @@ export function logTxDetails(data: TransferData): void { export async function fromPolkadot() { console.log('\nTransfer from Polkadot to Moonbeam\n'); - const data = await Sdk().getTransferData({ - destinationAddress: account.address, - destinationKeyOrChain: moonbeam, - keyOrAsset: dot, - polkadotSigner: pair, - sourceAddress: pair.address, - sourceKeyOrChain: polkadot, - }); + const data = await Sdk() + .setAsset(dot) + .setSource(polkadot) + .setDestination(moonbeam) + .setAddresses({ + sourceAddress: pair.address, + destinationAddress: account.address, + }); logBalances(data); logTxDetails(data); - const amount = +data.min.toDecimal() * 2; + const amount = +data.min.toDecimal() * 1.5; console.log(`Sending from ${data.source.chain.name} amount: ${amount}`); @@ -89,16 +96,18 @@ export async function fromMoonbeam() { console.log('\nTransfer from Moonbeam to Polkadot\n'); const data = await Sdk() - .assets() - .asset(dot) - .source(moonbeam) - .destination(polkadot) - .accounts(account.address, pair.address); + .setAsset(dot) + .setSource(moonbeam) + .setDestination(polkadot) + .setAddresses({ + sourceAddress: account.address, + destinationAddress: pair.address, + }); logBalances(data); logTxDetails(data); - const amount = +data.min.toDecimal() * 2; + const amount = +data.min.toDecimal() * 1.5; console.log(`Sending from ${data.source.chain.name} amount: ${amount}`); @@ -119,7 +128,7 @@ async function main() { await fromPolkadot(); console.log('\nWaiting 30 seconds...'); - await setTimeout(30000); + await new Promise((resolve) => setTimeout(resolve, 30000)); await fromMoonbeam(); }