Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code examples + snippets #81

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions example/snippets/claim_bep3_swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// TODO: Must plug these in manually
const claimKavaSwapID = "";
const claimRandomNumber = "";

var claimBEP3Swap = async () => {
// Start new Kava client
let kavaClient = new KavaClient(Env.KavaEndpoints.Local);
kavaClient.setWallet(Env.KavaAccount.Local.Mnemonic);
kavaClient.setBroadcastMode('async');
await kavaClient.initChain();

const kavaStateSwap = await kavaClient.getSwap(claimKavaSwapID);
const kavaExpiresAt = Number(kavaStateSwap.expire_height);
console.log("\nCurrent Kava height:", await getKavaHeight(kavaClient));
console.log("Can be claimed before height:", kavaExpiresAt);

let kavaCurrBlockHeight = await getKavaHeight(kavaClient);
if (kavaCurrBlockHeight >= kavaExpiresAt) {
console.log("Swap has already expired and cannot be claimed.")
return;
}

// Send claim swap tx using Kava client
const txHashClaim = await kavaClient.claimSwap(
claimKavaSwapID,
claimRandomNumber
);
console.log('Claim swap tx hash (Kava): '.concat(txHashClaim));

// Check the claim tx hash
const txRes = await kavaClient.checkTxHash(txHashClaim, 15000);
console.log('\nTx result:', txRes.raw_log);
};

var getKavaHeight = async (kavaClient: any) => {
let currBlockInfo = await kavaClient.getLatestBlock();
return Number(currBlockInfo.block.last_commit.height)
}

var main = async () => {
await claimBEP3Swap();
};

main();
49 changes: 49 additions & 0 deletions example/snippets/create_bep3_swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var createBEP3Swap = async () => {
// Start new Kava client (as the Kava deputy)
let kavaClient = new KavaClient(Env.KavaEndpoints.Local);
kavaClient.setWallet(Env.KavaDeputy.Local.Mnemonic);
kavaClient.setBroadcastMode('async');
await kavaClient.initChain();

// -------------------------------------------------------------------------------
// Kava blockchain interaction
// -------------------------------------------------------------------------------

const recipient = Env.KavaAccount.Local.Address; // user's address on Kava
const recipientOtherChain = Env.BinanceDeputy.Local; // deputy's address on bnbchain
const senderOtherChain = Env.BinanceAccount.Local.Address; // user's address on bnbchain

// Set up params
const asset = 'bnb';
const amount = 10000000;

const coins = kavaUtils.formatCoins(amount, asset);
const heightSpan = '12';

// Generate random number hash from timestamp and hex-encoded random number
const randomNumber = kavaUtils.generateRandomNumber();
const timestamp = Math.floor(Date.now() / 1000);
const randomNumberHash: string = kavaUtils.calculateRandomNumberHash(
randomNumber,
timestamp
);
console.log('\nSecret random number:', randomNumber.toUpperCase());

const txHash = await kavaClient.createSwap(
recipient,
recipientOtherChain,
senderOtherChain,
randomNumberHash,
timestamp,
coins,
heightSpan
);

console.log('\nTx hash (Create swap on Kava):', txHash);
}

var main = async () => {
await createBEP3Swap();
};

main();
50 changes: 50 additions & 0 deletions example/snippets/refund_bep3_swap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const Env = require('../static/env').env;
const KavaClient = require('../../src/client').KavaClient;

// TODO: must plug in manually
const refundKavaSwapID="";

var testFx = async () => {
// Start new Kava client
let kavaClient = new KavaClient(Env.KavaEndpoints.Local);
kavaClient.setWallet(Env.KavaAccount.Local.Mnemonic);
await kavaClient.initChain();

// Get swap by ID
const kavaStateSwap = await kavaClient.getSwap(refundKavaSwapID);
const kavaExpiresAt = Number(kavaStateSwap.expire_height)
console.log("\nCurrent Kava height:", await getKavaHeight(kavaClient))
console.log("Can be refunded at height:", kavaExpiresAt)

// Wait until swap has expired on Kava
let kavaCurrBlockHeight = await getKavaHeight(kavaClient);
while(kavaCurrBlockHeight < Number(kavaExpiresAt)) {
await sleep(1000 * 1)
kavaCurrBlockHeight = await getKavaHeight(kavaClient);
}

// Refund swap on Kava
console.log("Refunding swap on Kava...")
const kavaRefundTxHash = await kavaClient.refundSwap(refundKavaSwapID);
console.log("kavaRefundTxHash:", kavaRefundTxHash)

// Check the claim tx hash
const txRes = await kavaClient.checkTxHash(kavaRefundTxHash, 15000);
console.log('\nTx result:', txRes.raw_log);
}

// Sleep is a wait function
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

var main = async () => {
await testFx();
}

var getKavaHeight = async (kavaClient: any) => {
let currBlockInfo = await kavaClient.getLatestBlock();
return Number(currBlockInfo.block.last_commit.height)
}

main();
10 changes: 10 additions & 0 deletions example/static/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ const KavaEndpoints = {
};

const KavaDeputy = {
Local: {
Address: '',
Mnemonic: '',
},
Testnet: 'kava1tfvn5t8qwngqd2q427za2mel48pcus3z9u73fl',
Mainnet: 'kava1r4v2zdhdalfj2ydazallqvrus9fkphmglhn6u6',
};

const BinanceAccount = {
Local: {
Address: '',
Mnemonic: '',
},
Testnet: {
Address: '',
Mnemonic: '',
Expand All @@ -36,11 +44,13 @@ const BinanceAccount = {
};

const BinanceEndpoints = {
Local: 'http://localhost:8080',
Testnet: 'https://testnet-dex.binance.org',
Mainnet: 'https://dex.binance.org/',
};

const BinanceDeputy = {
Local: '',
Testnet: 'tbnb1et8vmd0dgvswjnyaf73ez8ye0jehc8a7t7fljv',
Mainnet: 'bnb1jh7uv2rm6339yue8k4mj9406k3509kr4wt5nxn',
};
Expand Down
12 changes: 8 additions & 4 deletions example/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ const Env = require('./static/env').env;
const kavaUtils = require('../src/utils').utils;
const KavaClient = require('../src/client').KavaClient;

import { Coin } from '../src/types';

const KAVA_CONVERSION_FACTOR = 10 ** 6;

var main = async () => {
const recipient = 'kava1g0qywkx6mt5jmvefv6hs7c7h333qas5ks63a6t';

// Start new Kava client
kavaClient = new KavaClient(Env.KavaEndpoints.Testnet);
kavaClient.setWallet(Env.KavaAccount.Testnet.Mnemonic);
let kavaClient = new KavaClient(Env.KavaEndpoints.Local);
kavaClient.setWallet(Env.KavaDeputy.Local.Mnemonic);
kavaClient.setBroadcastMode('async');
await kavaClient.initChain();

let acc = await kavaClient.getAccount(Env.KavaDeputy.Local.Address);

// First let's check our account balances
let balances = await kavaClient.getBalances(Env.KavaAccount.Testnet.Address);
let balances = await kavaClient.getBalances(Env.KavaAccount.Local.Address);
console.log('Balances:', balances);
// Print our KAVA balance (if we have one)
let kavaBalance = balances.find(
(item) => item.denom.toUpperCase() === 'UKAVA'
(item: Coin) => item.denom.toUpperCase() === 'UKAVA'
);
if (kavaBalance) {
console.log(
Expand Down