Skip to content

Commit

Permalink
Improved sdk simple example (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Elmar Kenigs authored Sep 29, 2022
1 parent be0f9e4 commit 04dbe4d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"Kintsugi",
"KUSD",
"Litentry",
"menmonic",
"parachain",
"parachains",
"Phala",
"Precompile",
"RMRK",
"Shiden",
"Statemine",
"Suri",
"TEER"
],
"editor.formatOnSave": true,
Expand Down
37 changes: 23 additions & 14 deletions packages/sdk/examples/simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import { ethers } from 'ethers';
import { setTimeout } from 'timers/promises';

/**
* Add your moonbeam address and private key
* Add your moonbeam private key
*/
const moonbeamAddress = '';
const moonbeamPrivateKey = '';

/**
* Add your polkadot address and seed phrase
* Add your polkadot menmonic or private key
*/
const polkadotAddress = '';
const polkadotSeed = '';
const polkadotSuri = '';

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

Expand All @@ -29,12 +27,12 @@ const provider = new ethers.providers.WebSocketProvider(moonbeam.moonChain.ws, {
name: moonbeam.moonChain.name,
chainId: moonbeam.moonChain.chainId,
});
const signer = new ethers.Wallet(moonbeamPrivateKey, provider);
const ethersSigner = new ethers.Wallet(moonbeamPrivateKey, provider);

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

async function deposit() {
const polkadotAccount = keyring.addFromUri(polkadotSeed);
const pair = keyring.addFromUri(polkadotSuri);

const { chains, from } = moonbeam.deposit(dot);

Expand All @@ -44,8 +42,8 @@ async function deposit() {
);

const { asset, sourceBalance, source, min, send } = await from(polkadot).get(
moonbeamAddress,
polkadotAccount,
ethersSigner.address,
pair,
);

console.log(
Expand All @@ -61,6 +59,7 @@ async function deposit() {

async function withdraw() {
const { chains, to } = moonbeam.withdraw(dot);
const polkadotAddress = keyring.getPairs().at(0)?.address!;

console.log(
`\nYou can withdraw ${dot} to these chains: `,
Expand All @@ -69,9 +68,7 @@ async function withdraw() {

const { asset, destination, destinationBalance, min, send } = await to(
polkadot,
).get(polkadotAddress, {
ethersSigner: signer,
});
).get(polkadotAddress, { ethersSigner });

console.log(
`Your ${asset.originSymbol} balance in ${destination.name}: ${toDecimal(
Expand All @@ -84,9 +81,11 @@ async function withdraw() {
}

async function main() {
console.log('starting...');

const thirtySeconds = 30 * 1000;
const unsubscribe = await moonbeam.subscribeToAssetsBalanceInfo(
moonbeamAddress,
ethersSigner.address,
(balances) => {
console.log('\nUpdated balances:');
console.log('▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄');
Expand All @@ -101,13 +100,23 @@ async function main() {
console.log('▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄');
},
);
await setTimeout(3000);

console.log('\ndepositing...');
await deposit();
await setTimeout(thirtySeconds);

console.log('\nwithdrawing...');
await withdraw();
await setTimeout(thirtySeconds);

unsubscribe();
}

main().then(() => process.exit());
// to suppress unnecessary warning from polkadot and ethers
console.warn = () => {};

main()
.then(() => console.log('done!'))
.catch(console.error)
.finally(() => process.exit());

0 comments on commit 04dbe4d

Please sign in to comment.