UNPKG

@tangany/waas

Version:

node.js SDK for Tangany Wallet as a Service API

192 lines (161 loc) 7.17 kB
const { Waas, TEZOS_NETWORK } = require("../dist"); const assert = require("assert"); const { config } = require("dotenv"); const { checkEnvVars } = require("./helpers"); const { resolve } = require("path"); const path = resolve(process.cwd(), ".env"); config({ path }); checkEnvVars(); describe("Sample Tezos workflow", function () { const timeout = 120e3; this.timeout(timeout); this.slow(timeout / 4); const wallet = process.env.E2E_WALLET; const testReceivingWalletAddress = "tz2CPuVRn8d35MFSHXWgfLmSkGUtWX8s1hSC"; const tezosAmount = "0.000001"; let walletAddress; let createdWallet; let createdWalletAddress; let lastHash; // Smart contract address used for testing const contractAddress = "KT1DcdRZiVDkuJ1GRPCHXJQkRnkTJ13QvDDD"; // send tezos only to this test wallet (and not to newly created ones) so it can be transferred back at a later point const recipients = { amount: tezosAmount, to: testReceivingWalletAddress, }; const api = new Waas({ tezosNetwork: TEZOS_NETWORK.MAINNET }); after(async function () { const { recoveryId } = await api.wallet(createdWallet).delete(); console.log(`Soft deleted wallet '${recoveryId}'`); }); it("should get Tezos specs for current wallet ", async function () { const { currency, balance, address, isRevealed, delegate } = await api.wallet(wallet).xtz().get(); assert.strictEqual(currency, "XTZ"); assert.ok(balance); assert.ok(address); assert.ok(delegate || delegate == null); assert.ok(isRevealed) console.log(`Wallet holds ${balance} ${currency}`) walletAddress = address; }); it("should create a new wallet", async function () { const { security, created, version, wallet, updated } = await api.wallet().create(); assert.ok(security); assert.ok(created); assert.ok(version); assert.ok(wallet); assert.ok(updated); createdWallet = wallet; console.log(`created wallet ${createdWallet}`); }); it("should get the created wallet data", async function () { assert.ok(createdWallet, "cannot run without previous tests"); const { security, created, version, wallet, updated } = await api.wallet(createdWallet).get(); assert.ok(security); assert.ok(created); assert.ok(version); assert.ok(wallet); assert.ok(updated); console.log("wallet", { updated, wallet, version, created, security }); assert.strictEqual(wallet, createdWallet); }); it("should get the Tezos specs for created wallet", async function () { assert.ok(createdWallet, "cannot run without previous tests"); const { address, balance, currency, isRevealed, delegate } = await api.wallet(createdWallet).xtz().get(); assert.ok(address); assert.ok(isRevealed == false); assert.ok(delegate == null); assert.strictEqual(balance, "0"); assert.strictEqual(currency, "XTZ"); console.log(`address for created wallet: ${createdWalletAddress}. Balance: ${balance}`); }); it("should estimate the fee for a given tezos tx", async function () { const { totalBakerFees, totalStorageFees, maxGasLimit, maxStorageLimit } = await api.wallet(wallet).xtz().estimateFee(recipients); assert.ok(totalBakerFees); assert.ok(totalStorageFees); assert.ok(maxGasLimit); assert.ok(maxStorageLimit || maxStorageLimit === 0); console.log(`fee estimation for tezos tx: ${totalBakerFees}, ${totalStorageFees}, ${maxGasLimit}, ${maxStorageLimit}`); }); it("should estimate the fee for a given tezos tx with multiple recipients", async function () { const { totalBakerFees, totalStorageFees, maxGasLimit, maxStorageLimit } = await api.wallet(wallet).xtz().estimateFee([recipients, recipients]); assert.ok(totalBakerFees); assert.ok(totalStorageFees); assert.ok(maxGasLimit); assert.ok(maxStorageLimit || maxStorageLimit === 0); console.log(`fee estimation for tezos tx with multiple recipients: ${totalBakerFees}, ${totalStorageFees}, ${maxGasLimit}, ${maxStorageLimit}`); }); it("should send XTZ to other test wallet asynchronously", async function () { const request = await api.wallet(wallet).xtz().sendAsync(recipients); console.log(`Started request with id ${request.id} ...`); const { process } = await request.get(); assert.ok(process); console.log(`The transaction process is ${process}.`) }); it("should set a new delegate on created wallet", async function () { assert.ok(createdWallet, "cannot run without previous tests"); const request = await api.wallet(createdWallet).xtz().delegateAsync(testReceivingWalletAddress); const { process } = await request.get(); console.log(`Started request with id ${request.id} ...`); assert.ok(process); console.log(`The delegate operation process is ${process}.`) }) it("should cancel current delegate", async function () { const request = await api.wallet(wallet).xtz().delegateAsync(null); const { process } = await request.get(); console.log(`Started request with id ${request.id} ...`); assert.ok(process); console.log(`The delegate operation process is ${process}.`) }); it("should estimate fee for interaction with a smart contract", async function () { const { totalBakerFees, totalStorageFees, maxGasLimit, maxStorageLimit } = await api.wallet(wallet).xtz().contract(contractAddress).estimateFee({ function: "set_pause", inputs: ["false"] }) assert.ok(totalBakerFees); assert.ok(totalStorageFees); assert.ok(maxGasLimit); assert.ok(maxStorageLimit || maxStorageLimit === 0); console.log(`fee estimation for tezos contract tx: ${totalBakerFees}, ${totalStorageFees}, ${maxGasLimit}, ${maxStorageLimit}`); }); it("should interact with smart contract asynchronously", async function () { const req = await api.wallet(wallet).xtz().contract(contractAddress).sendAsync({ function: "set_pause", inputs: ["false"] }); console.log(`Started request with id ${req.id} ...`); let requestStatus = await req.get(); let status; // Need hash for following tests, while loops waiting until hash is present do { requestStatus = await req.get(); } while (!requestStatus.status) status = requestStatus.status; console.log(`Status is: ${status}`); do { requestStatus = await req.get(); status = requestStatus.status; } while (!requestStatus.status.hash) lastHash = requestStatus.status.hash ? requestStatus.status.hash : requestStatus.output.hash; assert.ok(requestStatus.process); console.log(`The smart contract operation process is ${requestStatus.process}. Transaction hash: ${lastHash}`); }); it("should fetch the transaction details", async function () { assert.ok(lastHash, "cannot run without previous tests"); const details = await api.xtz(lastHash).get(); console.log("initial tx status:", JSON.stringify(details, null, 2)); assert.ok(details.status); }); it("should return storage content of a smart contract", async function () { const { result } = await api.xtz().contract(contractAddress).storage(); assert.ok(result); console.log(`Contract storage content:`) console.log(result); }); it("should retrieve status of Tezos node", async function () { const response = await api.xtz().getStatus(); assert.ok(response.status); assert.ok(response.info); }); })