Skip to content

Commit

Permalink
adapt xcm example
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaurello committed Nov 14, 2024
1 parent 5c0fb5c commit d2d2cf3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typings/
# dotenv environment variables file
.env
.env.test
.env.local

# parcel-bundler cache (https://parceljs.org/)
.cache
Expand Down
1 change: 1 addition & 0 deletions cspell.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'**/*.sol',
'pnpm-lock.yaml',
'CHANGELOG.md',
'.gitignore',
],
words: vscodeConfig['cSpell.words'],
};
2 changes: 2 additions & 0 deletions examples/sdk-simple/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
EVM_PRIVATE_KEY=
POLKADOT_PRIVATE_KEY=
51 changes: 30 additions & 21 deletions examples/sdk-simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

// ===========================================================================

Expand Down Expand Up @@ -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}`);

Expand All @@ -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}`);

Expand All @@ -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();
}

Expand Down

0 comments on commit d2d2cf3

Please sign in to comment.