update a few things - will start to mock shortly.

This commit is contained in:
Tommy Verrall
2022-03-03 10:28:26 +00:00
parent c40375a6b0
commit 043c4c8d86
10 changed files with 354 additions and 339 deletions
+6
View File
@@ -1,8 +1,14 @@
import axios from 'axios';
import { GasPrice } from '@cosmjs/stargate';
const mainnetPrefix = 'n';
const mainnetDenom = 'nym';
export function nymGasPrice(prefix: string): GasPrice {
if (typeof prefix === 'string') {
if (prefix === mainnetPrefix) {
prefix = mainnetDenom;
}
return GasPrice.fromString(`0.025u${prefix}`); // TODO: ideally this ugly conversion shouldn't be hardcoded here.
}
else {
@@ -1,94 +1,73 @@
import validatorClient from "../../src/index";
import { config } from '../test-utils/config';
import { config } from "../test-utils/config";
let client: validatorClient;
let mnemonic: string;
beforeEach(async () => {
mnemonic = validatorClient.randomMnemonic();
client = await validatorClient.connect(
mnemonic,
config.NYMD_URL,
config.VALIDATOR_API,
config.CURRENCY_PREFIX,
config.MIXNET_CONTRACT,
config.VESTING_CONTRACT
);
mnemonic = validatorClient.randomMnemonic();
client = await validatorClient.connect(
mnemonic,
config.NYMD_URL,
config.VALIDATOR_API,
config.CURRENCY_DENOM,
config.MIXNET_CONTRACT,
config.VESTING_CONTRACT
);
});
describe("perform a few non expensive network calls with the validator client", () => {
test("get all cached mixnodes", async () => {
try {
const response = await client.getCachedMixnodes();
test.skip("get all cached mixnodes", async () => {
try {
const response = await client.getCachedMixnodes();
//expect all mixnodes to have their owner address
response.forEach(mixnodeDetails => {
expect(mixnodeDetails.owner).toHaveLength(43)
});
}
catch (error) {
throw error;
}
});
//expect all mixnodes to have their owner address
response.forEach((mixnodeDetails) => {
expect(mixnodeDetails.owner).toHaveLength(43);
});
} catch (error) {
throw error;
}
});
test("get balance of address and denom of the network", async () => {
try {
//provide a users address and get their balance
//we expect their balance to be zero, as it's a new account
const address = await validatorClient.mnemonicToAddress(mnemonic, config.CURRENCY_PREFIX as string);
const response = await client.getBalance(address);
test.skip("get minimium pledge amount for a mixnode", async () => {
try {
const response = await client.minimumMixnodePledge();
expect(response.amount).toStrictEqual("0");
expect(response.denom).toBe("unymt");
}
catch (error) {
throw error;
}
});
expect(response.amount).toBe("100000000");
expect(response.denom).toBe(config.CURRENCY_DENOM);
} catch (error) {
throw error;
}
});
test("get minimium pledge amount for a mixnode", async () => {
try {
test.skip("get minimium gateway pledge amount", async () => {
try {
const response = await client.minimumGatewayPledge();
const response = await client.minimumMixnodePledge();
expect(response.amount).toBe("100000000");
expect(response.denom).toBe(config.CURRENCY_DENOM as string);
} catch (error) {
throw error;
}
});
expect(response.amount).toBe("100000000");
expect(response.denom).toBe(config.CURRENCY_PREFIX);
}
catch (error) {
throw error;
}
});
test.skip("ensure the correct mixnet address is being passed", () => {
try {
//should supply the given value from the client init
const mixnet_contract = client.mixnetContract;
expect(mixnet_contract).toStrictEqual(config.MIXNET_CONTRACT);
} catch (error) {
throw error;
}
});
test("get minimium gateway pledge amount", async () => {
try {
const response = await client.minimumGatewayPledge();
expect(response.amount).toBe("100000000");
expect(response.denom).toBe(config.CURRENCY_PREFIX as string);
}
catch (error) {
throw error;
}
});
test("ensure the correct mixnet address is being passed", () => {
try {
//should supply the given value from the client init
const mixnet_contract = client.mixnetContract;
expect(mixnet_contract).toStrictEqual(config.MIXNET_CONTRACT)
}
catch (error) {
throw error;
}
});
test("ensure the correct vesting address is being passed", () => {
try {
const vesting_contract = client.vestingContract;
expect(vesting_contract).toStrictEqual(config.VESTING_CONTRACT)
}
catch (error) {
throw error;
}
});
});
test.skip("ensure the correct vesting address is being passed", () => {
try {
const vesting_contract = client.vestingContract;
expect(vesting_contract).toStrictEqual(config.VESTING_CONTRACT);
} catch (error) {
throw error;
}
});
});
@@ -1,8 +1,13 @@
import validator from "../../src/index";
import { ExecuteResult } from '@cosmjs/cosmwasm-stargate';
import { Coin } from '@cosmjs/proto-signing';
import { config } from '../test-utils/config';
import { Gateway, GatewayOwnershipResponse, MixNode, MixOwnershipResponse } from '../../src/types';
import { ExecuteResult } from "@cosmjs/cosmwasm-stargate";
import { Coin } from "@cosmjs/proto-signing";
import { config } from "../test-utils/config";
import {
Gateway,
GatewayOwnershipResponse,
MixNode,
MixOwnershipResponse,
} from "../../src/types";
let response: ExecuteResult;
let validatorClient: validator;
@@ -10,175 +15,213 @@ let ownsMixNode: MixOwnershipResponse;
let ownsGateway: GatewayOwnershipResponse;
beforeEach(async () => {
validatorClient = await validator.connect(
config.USER_MNEMONIC,
config.NYMD_URL,
config.VALIDATOR_API,
config.CURRENCY_PREFIX,
config.MIXNET_CONTRACT,
config.VESTING_CONTRACT
);
validatorClient = await validator.connect(
config.USER_MNEMONIC,
config.NYMD_URL,
config.VALIDATOR_API,
config.NETWORK_BECH,
config.MIXNET_CONTRACT,
config.VESTING_CONTRACT
);
});
describe("long running e2e tests", () => {
test.only("update mixnode profit percentage", async () => {
const nodeIdentity = config.MIXNODE_IDENTITY;
const profitPercent = profitPercentage();
test.only("token transfer", async () => {
try {
//make sure there's enough balance in the wallet
let coin = buildCoin("50000", "nymt");
let userAddress = await buildWallet();
let send = await validatorClient.send(
userAddress,
Array(coin),
"auto",
"send-tokens"
);
let jsonParse = JSON.parse(send.rawLog as string);
try {
//use auto fees - simulated gas
response = await validatorClient.updateMixnodeConfig(nodeIdentity, 'auto', profitPercent);
}
catch (error) {
throw error;
}
try {
ownsMixNode = await validatorClient.client.ownsMixNode(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
}
catch (error) {
throw error;
}
expect(ownsMixNode.mixnode?.mix_node.profit_margin_percent).toStrictEqual(profitPercent);
});
//check successful network broadcast - via events
//1 - get key attributes values for sender an assert them
//2 - get key attributes for receiver assert they match
//3 - transaction hash present in response
test.only("unbond and bond mixnode", async () => {
// { array of events -> attribute -> event information }
expect(jsonParse[0].events[1].attributes[1].value).toStrictEqual(
config.USER_WALLET_ADDRESS
);
expect(jsonParse[0].events[1].attributes[0].value).toStrictEqual(
userAddress
);
expect(jsonParse[0].events[1].type).toStrictEqual(
"transfer"
);
expect(send.transactionHash).toStrictEqual(expect.any(String));
} catch (error) {
throw error;
}
});
try {
await validatorClient.unbondMixNode("auto", "unbond-mixnode");
}
catch (error) {
throw error;
}
test.only("update mixnode profit percentage", async () => {
const nodeIdentity = config.MIXNODE_IDENTITY;
const profitPercent = profitPercentage();
const profitPercent = profitPercentage();
try {
//use auto fees - simulated gas
response = await validatorClient.updateMixnodeConfig(nodeIdentity, 'auto', profitPercent);
}
catch (error) {
throw error;
}
try {
ownsMixNode = await validatorClient.client.ownsMixNode(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
}
catch (error) {
throw error;
}
expect(ownsMixNode.mixnode?.mix_node.profit_margin_percent).toStrictEqual(profitPercent);
});
const mixnodeDetails = <MixNode>{
host: config.MIXNODE_HOST,
mix_port: 1789,
verloc_port: 1790,
http_api_port: 8080,
identity_key: config.MIXNODE_IDENTITY,
sphinx_key: config.MIXNODE_SPHINX_KEY,
version: config.MIXNODE_VERSION,
profit_margin_percent: profitPercent
};
test.only("unbond and bond mixnode", async () => {
const pledgeAmount = <Coin>{
denom: "unymt",
amount: "100000000"
};
try {
await validatorClient.unbondMixNode("auto", "unbond-mixnode");
}
catch (error) {
throw error;
}
try {
response = await validatorClient.bondMixNode(
mixnodeDetails,
config.MIXNODE_SIGNATURE,
pledgeAmount,
"auto"
);
//see output of events
//remove shortly
response.logs.forEach((log) => {
log.events.forEach((member) => {
console.log(member.attributes);
console.log(member.type);
});
});
}
catch (error) {
throw error;
}
ownsMixNode = await validatorClient.client.ownsMixNode(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
expect(ownsMixNode.mixnode?.mix_node.profit_margin_percent).toStrictEqual(profitPercent);
});
const profitPercent = profitPercentage();
test.skip("unbond and bond gateway", async () => {
//gateway requires different user wallet
//init client inside test
const mixnodeDetails = <MixNode>{
host: config.MIXNODE_HOST,
mix_port: 1789,
verloc_port: 1790,
http_api_port: 8080,
identity_key: config.MIXNODE_IDENTITY,
sphinx_key: config.MIXNODE_SPHINX_KEY,
version: config.MIXNODE_VERSION,
profit_margin_percent: profitPercent
};
try {
await validatorClient.unbondGateway("auto", "unbonding gateway");
}
catch (error) {
throw error;
}
const bond = buildCoin("100000000", config.CURRENCY_DENOM)
const gateway = <Gateway>{
host: config.GATEWAY_HOST,
mix_port: 1789,
clients_port: 9000,
version: config.GATEWAY_VERSION,
sphinx_key: config.GATEWAY_SPHINX,
identity_key: config.GATEWAY_IDENTITY,
location: "earth"
};
try {
response = await validatorClient.bondMixNode(
mixnodeDetails,
config.MIXNODE_SIGNATURE,
bond,
"auto"
);
}
catch (error) {
throw error;
}
const pledge = pledgeAmount();
ownsMixNode = await validatorClient.client.ownsMixNode(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
expect(ownsMixNode.mixnode?.mix_node.profit_margin_percent).toStrictEqual(profitPercent);
});
try {
response = await validatorClient.bondGateway(
gateway,
config.GATEWAY_SIGNATURE,
pledge,
"auto"
);
}
catch (error) {
throw error;
}
ownsGateway = await validatorClient.client.ownsGateway(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
expect(ownsGateway.gateway?.bond_amount).toStrictEqual(pledge.amount);
expect(ownsGateway.address).toStrictEqual(config.USER_WALLET_ADDRESS);
});
test.skip("unbond and bond gateway", async () => {
//gateway requires different user wallet
//init inside test
//todo
test.only("delegate to mixnode, then undelegate", async () => {
try {
await validatorClient.unbondGateway("auto", "unbonding gateway");
}
catch (error) {
throw error;
}
const pledge = pledgeAmount();
const getBalance = await validatorClient.getBalance(config.USER_WALLET_ADDRESS);
console.log(getBalance);
const gateway = <Gateway>{
host: config.GATEWAY_HOST,
mix_port: 1789,
clients_port: 9000,
version: config.GATEWAY_VERSION,
sphinx_key: config.GATEWAY_SPHINX,
identity_key: config.GATEWAY_IDENTITY,
location: "earth"
};
try {
response = await validatorClient.delegateToMixNode(
config.MIXNODE_IDENTITY,
pledge,
"auto"
);
response.logs.forEach((log) => {
console.log(log.events);
console.log(log.log);
console.log(log.msg_index);
})
}
catch (error) {
throw error;
}
try {
const unbond = await validatorClient.undelegateFromMixNode(
config.MIXNODE_IDENTITY,
"auto"
);
const bond = buildCoin("100000000", config.CURRENCY_DENOM)
//see output of events
//remove shortly
unbond.logs.forEach((logs) => {
logs.events.forEach((events) => {
console.log(events.type);
console.log(events.attributes);
})
});
} catch (error) {
throw error;
}
});
try {
response = await validatorClient.bondGateway(
gateway,
config.GATEWAY_SIGNATURE,
bond,
"auto"
);
}
catch (error) {
throw error;
}
ownsGateway = await validatorClient.client.ownsGateway(config.MIXNET_CONTRACT, config.USER_WALLET_ADDRESS);
expect(ownsGateway.gateway?.bond_amount).toStrictEqual(bond.amount);
expect(ownsGateway.address).toStrictEqual(config.USER_WALLET_ADDRESS);
});
test.only("delegate to mixnode, then undelegate", async () => {
const pledge = buildCoin("100000000", config.CURRENCY_DENOM)
const getBalance = await validatorClient.getBalance(config.USER_WALLET_ADDRESS);
console.log(getBalance);
try {
response = await validatorClient.delegateToMixNode(
config.MIXNODE_IDENTITY,
pledge,
"auto"
);
response.logs.forEach((log) => {
console.log(log.events);
console.log(log.log);
console.log(log.msg_index);
})
}
catch (error) {
throw error;
}
try {
const unbond = await validatorClient.undelegateFromMixNode(
config.MIXNODE_IDENTITY,
"auto"
);
//see output of events
//remove shortly
unbond.logs.forEach((logs) => {
logs.events.forEach((events) => {
console.log(events.type);
console.log(events.attributes);
})
});
} catch (error) {
throw error;
}
});
});
const profitPercentage = (): number => {
return Math.floor(Math.random() * 100) + 1;
}
return Math.floor(Math.random() * 100) + 1;
};
const pledgeAmount = (): Coin => {
return <Coin>{
denom: "unymt",
amount: "100000000"
};
}
const buildCoin = (amount: string, denomination: string): Coin => {
return {
denom: `u${denomination}`,
amount: amount,
};
};
const buildWallet = async (): Promise<string> => {
let mnemonic = validator.randomMnemonic();
const randomAddress = await validator.mnemonicToAddress(
mnemonic,
config.NETWORK_BECH
);
console.log(randomAddress);
return randomAddress;
};
@@ -1,49 +1,55 @@
import SigningClient from '../../src/signing-client';
import SigningClient from "../../src/signing-client";
import validator from "../../src/index";
import { CosmWasmClient } from '@cosmjs/cosmwasm-stargate';
import { config } from '../test-utils/config';
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { config } from "../test-utils/config";
let cosmWasmClient: CosmWasmClient;
let mnemonic: string;
beforeEach(async () => {
cosmWasmClient = await SigningClient.connect(config.NYMD_URL);
cosmWasmClient = await SigningClient.connect(config.NYMD_URL);
});
describe("peform basic network checks with the cosmwasm client", () => {
test("retrieve a newly created users balance", async () => {
try {
const randomAddress = await validator.mnemonicToAddress(mnemonic, config.CURRENCY_PREFIX);
const balance = await cosmWasmClient.getBalance(randomAddress, config.CURRENCY_PREFIX);
describe("peform basic network checks with the client", () => {
test.skip("retrieve a newly created users balance should be empty", async () => {
try {
mnemonic = validator.randomMnemonic();
expect(balance.denom).toStrictEqual(config.CURRENCY_PREFIX);
expect(balance.amount).toStrictEqual("0");
}
catch (error) {
throw error;
}
});
const randomAddress = await validator.mnemonicToAddress(
mnemonic,
config.NETWORK_BECH
);
test("get the chain id of the network", async () => {
try {
const chainId = await cosmWasmClient.getChainId();
expect(chainId).toStrictEqual(config.CHAIN_ID);
}
catch (error) {
throw error;
}
});
const balance = await cosmWasmClient.getBalance(
randomAddress,
config.CURRENCY_DENOM
);
test("get height then search for it by it's block", async () => {
try {
const height = await cosmWasmClient.getHeight()
const block = await cosmWasmClient.getBlock(height);
expect(balance.denom).toStrictEqual(config.CURRENCY_DENOM);
expect(balance.amount).toStrictEqual("0");
} catch (error) {
throw error;
}
});
expect(block.header.chainId).toStrictEqual(config.CHAIN_ID);
expect(block.header.height).toStrictEqual(height);
}
catch (error) {
throw error;
}
});
test.skip("get the chain id of the network", async () => {
try {
const chainId = await cosmWasmClient.getChainId();
expect(chainId).toStrictEqual(config.CHAIN_ID);
} catch (error) {
throw error;
}
});
test.skip("get height then search for it by it's block", async () => {
try {
const height = await cosmWasmClient.getHeight();
const block = await cosmWasmClient.getBlock(height);
expect(block.header.chainId).toStrictEqual(config.CHAIN_ID);
expect(block.header.height).toStrictEqual(height);
} catch (error) {
throw error;
}
});
});
@@ -1,25 +1,24 @@
import ValidatorApiQuerier from '../../src/validator-api-querier';
import { config } from '../test-utils/config';
import ValidatorApiQuerier from "../../src/validator-api-querier";
import { config } from "../test-utils/config";
let client: ValidatorApiQuerier;
beforeEach(() => {
client = new ValidatorApiQuerier(config.VALIDATOR_API);
client = new ValidatorApiQuerier(config.VALIDATOR_API);
});
describe("init the validator api querier", () => {
test("get rewarded mixnodes", async () => {
try {
//all mixnodes will have their owners address
let response = await client.getRewardedMixnodes();
test.skip("get rewarded mixnodes", async () => {
try {
//all mixnodes will have their owners address
let response = await client.getRewardedMixnodes();
response.forEach((Node) => {
expect(Node.owner.length).toStrictEqual(43);
})
}
catch (error) {
throw error;
}
});
//this is dependany on config and network amend shortly
response.forEach((Node) => {
expect(Node.owner.length).toStrictEqual(43);
});
} catch (error) {
throw error;
}
});
});
+4 -2
View File
@@ -5,7 +5,7 @@ export const config = {
VESTING_CONTRACT: process.env.VESTING_CONTRACT as string,
USER_MNEMONIC: process.env.USER_MNEMONIC as string,
USER_WALLET_ADDRESS: process.env.USER_WALLET_ADDRESS as string,
CURRENCY_PREFIX: process.env.CURRENCY_PREFIX as string,
CURRENCY_DENOM: process.env.CURRENCY_DENOM as string,
CHAIN_ID: process.env.CHAIN_ID as string,
MIXNODE_IDENTITY: process.env.MIXNODE_IDENTITY as string,
MIXNODE_SPHINX_KEY: process.env.MIXNODE_SPHINX_KEY as string,
@@ -18,4 +18,6 @@ export const config = {
GATEWAY_LOCATION: process.env.GATEWAY_LOCATION as string,
GATEWAY_HOST: process.env.GATEWAY_HOST as string,
GATEWAY_VERSION: process.env.GATEWAY_VERSION as string,
}
NETWORK_BECH: process.env.NETWORK_BECH as string,
};
+8 -7
View File
@@ -2,16 +2,17 @@
// Store current time as `start`
export const now = (eventName = null) => {
if (eventName) {
console.log(`Started ${eventName}..`);
console.log(`Started ${eventName}..`);
}
return new Date().getTime();
}
//takes arg of start time
export const elapsed = (beginning: number, log = false) => {
};
//takes arg of start time
export const elapsed = (beginning: number, log = false) => {
const duration = new Date().getTime() - beginning;
if (log) {
console.log(`${duration / 1000}s`);
console.log(`${duration / 1000}s`);
}
return duration;
}
};
@@ -1,11 +1,10 @@
const currency = require('../../src/currency');
const currency = require("../../src/currency");
describe("currency module tests", () => {
test.skip("convert to native balance", () => {
const decimal = "12.0346";
const value = currency.printableBalanceToNative(decimal);
expect(value).toStrictEqual("12034600");
});
});
test.skip("convert to native balance", () => {
const decimal = "12.0346";
const value = currency.printableBalanceToNative(decimal);
expect(value).toStrictEqual("12034600");
});
});
@@ -1,28 +1,19 @@
const stargate = require("../../src/stargate-helper");
import { config } from '../test-utils/config';
import { config } from "../test-utils/config";
describe("test the stargate functions within the project", () => {
test("gas price is returned correctly", () => {
const nymCurrency = config.CURRENCY_PREFIX;
const getGasPrice = stargate.nymGasPrice(nymCurrency);
expect(getGasPrice.denom).toBe(`u${nymCurrency}`);
});
test.skip("gas price is returned correctly", () => {
const nymCurrency = config.CURRENCY_DENOM;
const getGasPrice = stargate.nymGasPrice(nymCurrency);
test("provide invalid type returns an error message", () => {
//pass invalid type
expect(() => {
const nymCurrency = 13;
stargate.nymGasPrice(nymCurrency);
}).toThrow("13 is not of type string");
});
expect(getGasPrice.denom).toBe(`u${nymCurrency}`);
});
//provide test for downloading wasm
//mock this test
// test.skip("providing nothing returns", async () => {
// //pass invalid type
// const downloadWasm = stargate.downloadWasm("http://localhost");
// console.log(downloadWasm);
// })
test.skip("provide invalid type returns an error message", () => {
//pass invalid type
expect(() => {
const nymCurrency = 13;
stargate.nymGasPrice(nymCurrency);
}).toThrow("13 is not of type string");
});
});
@@ -1,21 +1,10 @@
import validator from "../../src/index";
import { config } from '../test-utils/config';
const NETWORK_DENOM = config.CURRENCY_PREFIX;
describe("validator build network mnemonic", () => {
test.skip("get mnemonic", async () => {
const mnemonic = validator.randomMnemonic();
const mnemonicCount = mnemonic.split(" ").length;
describe("perform basic interactions with the validator", () => {
test("build client and get all mixnodes", async () => {
const mnemonic = validator.randomMnemonic();
const mnemonicCount = mnemonic.split(" ").length;
expect(mnemonicCount).toStrictEqual(24);
});
test("build client and get all mixnodes", async () => {
const buildMnemonic = validator.randomMnemonic();
const mnemonic = await validator.mnemonicToAddress(buildMnemonic, NETWORK_DENOM);
expect(mnemonic).toHaveLength(43);
expect(mnemonic).toContain(NETWORK_DENOM);
});
expect(mnemonicCount).toStrictEqual(24);
});
});