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

added new contract examples & tests #24

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
134 changes: 134 additions & 0 deletions templates/hyperweb/__tests__/bank.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
// @ts-nocheck
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';

import path from "path";
import fs from 'fs';
import { getSigningHyperwebClient, hyperweb, google } from 'hyperwebjs';
import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';

describe('Bank Contract Tests', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex, contractAddress;
let fee;
let recipientWallet, recipientAddress;

beforeAll(async () => {
({
chainInfo,
getCoin,
getRpcEndpoint,
creditFromFaucet
} = useChain('hyperweb'));

denom = (await getCoin()).base;

wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
address = (await wallet.getAccounts())[0].address;
console.log(`contract creator address: ${address}`);

recipientWallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
recipientAddress = (await recipientWallet.getAccounts())[0].address;
console.log(`recipient address: ${recipientAddress}`);

queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
rpcEndpoint: await getRpcEndpoint()
});

signingClient = await getSigningHyperwebClient({
rpcEndpoint: await getRpcEndpoint(),
signer: wallet
});

fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };

await creditFromFaucet(address);
await sleep(10000);
});

it('Check initial balance', async () => {
const balance = await signingClient.getBalance(address, denom);
expect(balance.amount).toEqual("10000000000");
expect(balance.denom).toEqual(denom);
});

it('Instantiate Bank contract', async () => {
const contractPath = path.join(
__dirname,
"../dist/contracts/bank.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");

const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
creator: address,
code: contractCode,
source: "test_source",
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
contractIndex = response.index;
contractAddress = response.address;
expect(contractIndex).toBeGreaterThan(0);
console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
});

it('Call balance function', async () => {
const args = JSON.stringify({ address, denom });
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "balance",
args: [args]
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(parseInt(response.result)).toBeGreaterThanOrEqual(0);
});

it('Transfer funds to another address and check balance', async () => {
const transferArgs = JSON.stringify({
from: address,
to: recipientAddress,
amount: 1000,
denom
});
const transferMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "transfer",
args: [transferArgs]
});

const transferResult = await signingClient.signAndBroadcast(address, [transferMsg], fee);
assertIsDeliverTxSuccess(transferResult);

const checkArgs = JSON.stringify({ address: recipientAddress, denom });
const checkMsg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "balance",
args: [checkArgs]
});

const checkResult = await signingClient.signAndBroadcast(address, [checkMsg], fee);
assertIsDeliverTxSuccess(checkResult);

const checkResponse = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(checkResult.msgResponses[0]);
console.log(`recipient balance: ${checkResponse.result}`);
expect(parseInt(checkResponse.result)).toBeGreaterThanOrEqual(1000);
});

});
93 changes: 93 additions & 0 deletions templates/hyperweb/__tests__/hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// @ts-nocheck
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';

import path from "path";
import fs from 'fs';
import { getSigningHyperwebClient, hyperweb, google } from 'hyperwebjs';
import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';

describe('Hello Contract Tests', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex, contractAddress;
let fee;

beforeAll(async () => {
({
chainInfo,
getCoin,
getRpcEndpoint,
creditFromFaucet
} = useChain('hyperweb'));

denom = (await getCoin()).base;

wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
address = (await wallet.getAccounts())[0].address;
console.log(`Contract creator address: ${address}`);

queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
rpcEndpoint: await getRpcEndpoint()
});

signingClient = await getSigningHyperwebClient({
rpcEndpoint: await getRpcEndpoint(),
signer: wallet
});

fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };

await creditFromFaucet(address);
await sleep(10000);
});

it('Check initial balance', async () => {
const balance = await signingClient.getBalance(address, denom);
expect(balance.amount).toEqual("10000000000");
expect(balance.denom).toEqual(denom);
});

it('Instantiate hello contract', async () => {
const contractPath = path.join(
__dirname,
"../dist/contracts/hello.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");

const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
creator: address,
code: contractCode,
source: "test_source",
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
contractIndex = response.index;
contractAddress = response.address;
expect(contractIndex).toBeGreaterThan(0);
console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
});

it('Invoke hello function', async () => {
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "hello",
args: [JSON.stringify("World")]
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(response.result).toEqual("Hello, World");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('State Contract Tests', () => {
fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };

await creditFromFaucet(address);
await sleep(2000); // Sleep for 2 sec to allow faucet tokens to arrive
await sleep(10000); // Sleep for 2 sec to allow faucet tokens to arrive
});

it('Check initial balance', async () => {
Expand All @@ -59,7 +59,7 @@ describe('State Contract Tests', () => {
// Read contract code from external file
const contractPath = path.join(
__dirname,
"../dist/contracts/simpleState.js"
"../dist/contracts/simple-state.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");

Expand Down
128 changes: 128 additions & 0 deletions templates/hyperweb/__tests__/token-factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// @ts-nocheck
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import { assertIsDeliverTxSuccess } from '@cosmjs/stargate';

import path from "path";
import fs from 'fs';
import { getSigningHyperwebClient, hyperweb, google } from 'hyperwebjs';
import { useChain, generateMnemonic } from 'starshipjs';
import { sleep } from '../test-utils/sleep';
import './setup.test';

describe('Token Factory Contract Tests', () => {
let wallet, denom, address, queryClient, signingClient;
let chainInfo, getCoin, getRpcEndpoint, creditFromFaucet;
let contractCode, contractIndex, contractAddress;
let fee;
let fullDenom: string;


beforeAll(async () => {
({
chainInfo,
getCoin,
getRpcEndpoint,
creditFromFaucet
} = useChain('hyperweb'));

denom = (await getCoin()).base;

wallet = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix
});
address = (await wallet.getAccounts())[0].address;
console.log(`contract creator address: ${address}`);

queryClient = await hyperweb.ClientFactory.createRPCQueryClient({
rpcEndpoint: await getRpcEndpoint()
});

signingClient = await getSigningHyperwebClient({
rpcEndpoint: await getRpcEndpoint(),
signer: wallet
});

fee = { amount: [{ denom, amount: '100000' }], gas: '550000' };

await creditFromFaucet(address);
await sleep(10000);
});

it('Check initial balance', async () => {
const balance = await signingClient.getBalance(address, denom);
expect(balance.amount).toEqual("10000000000");
expect(balance.denom).toEqual(denom);
});

it('Instantiate Token Factory contract', async () => {
const contractPath = path.join(
__dirname,
"../dist/contracts/token-factory.js"
);
contractCode = fs.readFileSync(contractPath, "utf8");

const msg = hyperweb.hvm.MessageComposer.fromPartial.instantiate({
creator: address,
code: contractCode,
source: "test_source",
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgInstantiateResponse.fromProtoMsg(result.msgResponses[0]);
contractIndex = response.index;
contractAddress = response.address;
expect(contractIndex).toBeGreaterThan(0);
console.log(`contract instantiated at index: ${contractIndex} and address ${contractAddress}`);
});

it('Create new denom', async () => {
const args = JSON.stringify({ denom: "testdenom" });
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "createDenom",
args: [args]
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
fullDenom = response.result;
console.log("created fullDenom:", fullDenom);
expect(fullDenom).toContain("factory");
});

it('Mint tokens using fullDenom', async () => {
const args = JSON.stringify({ denom: fullDenom, amount: 1000 });
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "mintTokens",
args: [args]
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);
});

it('Check balance of minted tokens', async () => {
const args = JSON.stringify({ address, denom: fullDenom });
const msg = hyperweb.hvm.MessageComposer.fromPartial.eval({
address: contractAddress,
creator: address,
callee: "getBalance",
args: [args]
});

const result = await signingClient.signAndBroadcast(address, [msg], fee);
assertIsDeliverTxSuccess(result);

const response = hyperweb.hvm.MsgEvalResponse.fromProtoMsg(result.msgResponses[0]);
expect(parseInt(response.result)).toBeGreaterThanOrEqual(1000);
});


});
19 changes: 19 additions & 0 deletions templates/hyperweb/dist/contracts/bank.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading