Skip to content

Commit

Permalink
authz sharship example done
Browse files Browse the repository at this point in the history
  • Loading branch information
Zetazzz committed Dec 24, 2023
1 parent 6818a15 commit 14a3b0b
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 636 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"editor.formatOnSave": false
},
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"eslint.validate": ["javascript", "javascriptreact"],
"files.exclude": {
Expand Down
47 changes: 28 additions & 19 deletions examples/telescope-authz/Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
NAME = starship-getting-started
FILE = configs/starship.yaml
TINY_FILE = configs/tiny-starship.yaml

HELM_NAME = telescope-starship
HELM_FILE = configs/starship.yaml
HELM_REPO = starship
HELM_CHART = devnet
HELM_VERSION = v0.1.46
HELM_VERSION = v0.1.38

###############################################################################
### All commands ###
###############################################################################

.PHONY: setup
setup: setup-deps setup-helm setup-kind
setup: check setup-helm

.PHONY: test
test:
yarn run e2e:test

.PHONY: stop
stop: stop-forward delete
Expand All @@ -20,33 +22,36 @@ stop: stop-forward delete
clean: stop clean-kind

###############################################################################
### Dependency check ###
### Helm commands ###
###############################################################################

.PHONY: check
setup-deps:
bash $(CURDIR)/scripts/dev-setup.sh

###############################################################################
### Helm Charts ###
###############################################################################
.PHONY: setup-helm
setup-helm:
helm repo add $(HELM_REPO) https://cosmology-tech.github.io/starship/
helm repo update
helm search repo $(HELM_REPO)/$(HELM_CHART) --version $(HELM_VERSION)

.PHONY: install
install:
bash $(CURDIR)/scripts/install.sh --config $(FILE) --name $(NAME) --version $(HELM_VERSION)
@echo "Installing the helm chart. This is going to take a while....."
@echo "You can check the status with \"kubectl get pods\", run in another terminal please"
helm install -f $(HELM_FILE) $(HELM_NAME) $(HELM_REPO)/$(HELM_CHART) --version $(HELM_VERSION)

install-tiny:
$(MAKE) install FILE=$(TINY_FILE)
.PHONY: debug
debug:
helm install --dry-run --debug -f $(HELM_FILE) $(HELM_NAME) $(HELM_REPO)/$(HELM_CHART)

.PHONY: delete
delete:
-helm delete $(NAME)
-helm delete $(HELM_NAME)

###############################################################################
### Port forward ###
###############################################################################

.PHONY: port-forward
port-forward:
bash $(CURDIR)/scripts/port-forward.sh --config=$(FILE)
bash $(CURDIR)/scripts/port-forward.sh --config=$(HELM_FILE)

.PHONY: stop-forward
stop-forward:
Expand All @@ -57,6 +62,10 @@ stop-forward:
###############################################################################
KIND_CLUSTER=starship

.PHONY: check
check:
bash $(CURDIR)/scripts/dev-setup.sh

.PHONY: setup-kind
setup-kind:
kind create cluster --name $(KIND_CLUSTER)
Expand Down
184 changes: 184 additions & 0 deletions examples/telescope-authz/__tests__/authz.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
import { generateMnemonic } from '@confio/relayer/build/lib/helpers';
import {
StdFee,
assertIsDeliverTxSuccess,
createProtobufRpcClient,
} from '@cosmjs/stargate';
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
import BigNumber from 'bignumber.js';

import { useChains } from '@cosmos-kit/react-lite';

import { Tendermint34Client } from '@cosmjs/tendermint-rpc';
import { QueryClient } from '@cosmjs/stargate';

import {
EncodeObject,
TxRpc,
cosmos,
getSigningCosmosClient,
} from '../src/codegen';
import { useChain } from '../src';
import './setup.test';
import {
GenericAuthorization,
Grant,
} from '../src/codegen/cosmos/authz/v1beta1/authz';
import { MsgGrant } from '../src/codegen/cosmos/authz/v1beta1/tx';
import { MsgVote } from '../src/codegen/cosmos/gov/v1beta1/tx';
import { SendAuthorization } from '../src/codegen/cosmos/bank/v1beta1/authz';

describe('Authz testing', () => {
let wallet1, address1, denom;
let wallet2, address2;
let chainInfo, getCoin, getStargateClient, getRpcEndpoint, creditFromFaucet;

// Variables used accross testcases
let queryClient;
let msgClient1;
let msgClient2;

beforeAll(async () => {
({
chainInfo,
getCoin,
getStargateClient,
getRpcEndpoint,
creditFromFaucet,
} = useChain('cosmos'));
denom = getCoin().base;

// const chains = useChains([chainInfo.chain_name]);

// Promise.all(Object.values(wallets).forEach())

// Initialize wallet
wallet1 = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix,
});
address1 = (await wallet1.getAccounts())[0].address;

wallet2 = await DirectSecp256k1HdWallet.fromMnemonic(generateMnemonic(), {
prefix: chainInfo.chain.bech32_prefix,
});
address2 = (await wallet2.getAccounts())[0].address;

const tmClient: any = await Tendermint34Client.connect(getRpcEndpoint());
const client = new QueryClient(tmClient);
const rpc1: any = createProtobufRpcClient(client);
const rpc2: any = createProtobufRpcClient(client);

const signingClient1 = await getSigningCosmosClient({
rpcEndpoint: getRpcEndpoint(),
signer: wallet1,
});
rpc1.signAndBroadcast = (
signerAddress: string,
messages: readonly EncodeObject[],
fee: number | StdFee | 'auto',
memo?: string | undefined
) => {
return signingClient1.signAndBroadcast(
signerAddress,
messages,
fee,
memo
);
};

const signingClient2 = await getSigningCosmosClient({
rpcEndpoint: getRpcEndpoint(),
signer: wallet2,
});
rpc2.signAndBroadcast = (
signerAddress: string,
messages: readonly EncodeObject[],
fee: number | StdFee | 'auto',
memo?: string | undefined
) => {
return signingClient2.signAndBroadcast(
signerAddress,
messages,
fee,
memo
);
};

// Create custom cosmos interchain client
queryClient = await cosmos.ClientFactory.createRPCQueryClient({
rpc: rpc1,
});

msgClient1 = await cosmos.ClientFactory.createRPCMsgClient({
rpc: rpc1,
});

msgClient2 = await cosmos.ClientFactory.createRPCMsgClient({
rpc: rpc2,
});

// Transfer osmosis and ibc tokens to address, send only osmo to address
await creditFromFaucet(address1);
await creditFromFaucet(address2);
}, 200000);

it('check address1 has tokens', async () => {
const { balance } = await queryClient.cosmos.bank.v1beta1.balance({
address: address1,
denom,
});

expect(balance.amount).toEqual('10000000000');
}, 10000);

it('check address2 has tokens', async () => {
const { balance } = await queryClient.cosmos.bank.v1beta1.balance({
address: address2,
denom,
});

expect(balance.amount).toEqual('10000000000');
}, 10000);

it('grant address2', async () => {
const fee = {
amount: [
{
denom,
amount: '100000',
},
],
gas: '550000',
};

const msg = MsgGrant.fromPartial({
granter: address1,
grantee: address2,
grant: Grant.fromPartial({
authorization: SendAuthorization.fromPartial({
spendLimit: [
{
denom: denom,
amount: '100000',
},
],
}),
}),
});

const result = await msgClient1.cosmos.authz.v1beta1.grant(
address1,
msg,
fee
);

assertIsDeliverTxSuccess(result);


const authsResults = await queryClient.cosmos.authz.v1beta1.granteeGrants({
grantee: address2,
});

expect(authsResults?.grants?.length).toBeGreaterThan(0);
}, 10000);
});
Loading

0 comments on commit 14a3b0b

Please sign in to comment.