Skip to content

Commit

Permalink
Refining the structure; Adding a few minor tests; Adding gateway (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev authored Dec 16, 2022
1 parent a8cc6be commit 84badfb
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 113 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "algorealm-ui",
"description": "Claim the Crown and the Sceptre of Algorand Realm",
"version": "0.2.0",
"version": "0.3.0",
"private": true,
"author": "Cosimo Bassi <cosimo.bassi@algorand.com>",
"license": "GPL-3.0",
Expand Down
9 changes: 9 additions & 0 deletions src/commands/about.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { about } from './about';

describe(`about`, () => {
it(`should return the expected string`, () => {
expect(about()).toEqual(
`AlgoRealm UI is an open-source terminal emulator web app built by @aorumbayev for the AlgoRealm CLI game created by @cusma. AlgoRealm UI is available under the GPLv3 license. CLI commands are identical to the original commands in the AlgoRealm documentation. However, they simplify the authentication and provide a higher degree of extensibility in the future. For more details, refer to https://github.com/cusma/algorealm - another fun way to play AlgoRealm that allows learning a few lower-level nuances of interacting with the Algorand blockchain.`,
);
});
});
3 changes: 3 additions & 0 deletions src/commands/about.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const about = () => {
return `AlgoRealm UI is an open-source terminal emulator web app built by @aorumbayev for the AlgoRealm CLI game created by @cusma. AlgoRealm UI is available under the GPLv3 license. CLI commands are identical to the original commands in the AlgoRealm documentation. However, they simplify the authentication and provide a higher degree of extensibility in the future. For more details, refer to https://github.com/cusma/algorealm - another fun way to play AlgoRealm that allows learning a few lower-level nuances of interacting with the Algorand blockchain.`;
};
26 changes: 26 additions & 0 deletions src/commands/changeChain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { changeChain } from './changeChain';
import { ChainType } from '@/models/Chain';
import store from '@/redux/store';
import { setChain } from '@/redux/slices/walletConnectSlice';

describe(`changeChain`, () => {
it(`should dispatch the setChain action with the correct chain type`, () => {
const spy = jest.spyOn(store, `dispatch`);
const chain = ChainType.MainNet;
changeChain(chain);
expect(spy).toHaveBeenCalledWith(setChain(chain));
});

it(`should return the expected string`, () => {
const chain = ChainType.MainNet;
expect(changeChain(chain)).toEqual(`Chain changed to ${chain}`);
});

it(`should throw an error if the store dispatch method throws an error`, () => {
const spy = jest.spyOn(store, `dispatch`).mockImplementation(() => {
throw new Error(`dispatch error`);
});
const chain = ChainType.MainNet;
expect(() => changeChain(chain)).toThrowError(`dispatch error`);
});
});
8 changes: 8 additions & 0 deletions src/commands/changeChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ChainType } from '@/models/Chain';
import { setChain } from '@/redux/slices/walletConnectSlice';
import store from '@/redux/store';

export const changeChain = (chain: ChainType) => {
store.dispatch(setChain(chain));
return `Chain changed to ${chain}`;
};
10 changes: 10 additions & 0 deletions src/commands/changeGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IpfsGateway } from '@/models/Gateway';
import { setGateway } from '@/redux/slices/walletConnectSlice';
import store from '@/redux/store';

export const changeGateway = (gateway: string) => {
store.dispatch(
setGateway(IpfsGateway[gateway.toUpperCase() as keyof typeof IpfsGateway]),
);
return `Gateway changed to ${gateway}`;
};
53 changes: 53 additions & 0 deletions src/commands/claimCrown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ALGOREALM_CROWN_ID } from '@/common/constants';
import { optAssets } from '@/redux/slices/walletConnectSlice';
import { hasAsset } from '@/utils/assets/hasAsset';
import { getClaimAssetTxns } from '@/utils/transactions/getClaimAssetTxns';
import submitTransactions from '@/utils/transactions/submitTransactions';
import WalletManager from '@/wallets/walletManager';
import store from '@/redux/store';
import { IpfsGateway } from '@/models/Gateway';
import { ChainType } from '@/models/Chain';
import { Asset } from '@/models/Asset';

export const claimCrown = async (
majestyName: string,
algos: number,
chain: ChainType,
address: string,
assets: Asset[],
gateway: IpfsGateway,
connector: WalletManager,
) => {
if (!connector.connected) {
return `You must connect to PeraWallet to claim the Crown of Entropy.`;
}

if (!hasAsset(ALGOREALM_CROWN_ID(chain), assets)) {
await store.dispatch(
optAssets({
assetIndexes: [ALGOREALM_CROWN_ID(chain)],
gateway,
connector,
}),
);
}

const claimTxns = await getClaimAssetTxns(
chain,
address,
ALGOREALM_CROWN_ID(chain),
`Crown`,
majestyName,
algos,
);

const signedTxns = await connector.signTransactions(claimTxns);

if (!signedTxns) {
return undefined;
}

const txnResponse = await submitTransactions(chain, signedTxns);

return `Transactions ${txnResponse.txId} performed. 👑 Glory to ${majestyName}, the Randomic Majesty of Algorand! 🎉`;
};
53 changes: 53 additions & 0 deletions src/commands/claimSceptre.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { ALGOREALM_SCEPTRE_ID } from '@/common/constants';
import { optAssets } from '@/redux/slices/walletConnectSlice';
import { hasAsset } from '@/utils/assets/hasAsset';
import { getClaimAssetTxns } from '@/utils/transactions/getClaimAssetTxns';
import submitTransactions from '@/utils/transactions/submitTransactions';
import WalletManager from '@/wallets/walletManager';
import store from '@/redux/store';
import { IpfsGateway } from '@/models/Gateway';
import { ChainType } from '@/models/Chain';
import { Asset } from '@/models/Asset';

export const claimSceptre = async (
majestyName: string,
algos: number,
chain: ChainType,
address: string,
assets: Asset[],
gateway: IpfsGateway,
connector: WalletManager,
) => {
if (!connector.connected) {
return `You must connect to PeraWallet to claim the Sceptre of Proof.`;
}

if (!hasAsset(ALGOREALM_SCEPTRE_ID(chain), assets)) {
await store.dispatch(
optAssets({
assetIndexes: [ALGOREALM_SCEPTRE_ID(chain)],
gateway,
connector,
}),
);
}

const claimTxns = await getClaimAssetTxns(
chain,
address,
ALGOREALM_SCEPTRE_ID(chain),
`Sceptre`,
majestyName,
algos,
);

const signedTxns = await connector.signTransactions(claimTxns);

if (!signedTxns) {
return undefined;
}

const txnResponse = await submitTransactions(chain, signedTxns);

return `Transactions ${txnResponse.txId} performed. 👑 Glory to ${majestyName}, the Verifiable Majesty of Algorand! 🎉`;
};
18 changes: 18 additions & 0 deletions src/commands/printDynasty.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { ChainType } from '@/models/Chain';
import { getAlgoRealmCalls } from '@/utils/transactions/getAlgoRealmCalls';
import { getAlgoRealmHistory } from '@/utils/transactions/getAlgoRealmHistory';

export const printDynasty = async (chain: ChainType) => {
const attempts = 1;
let algoRealmCalls = [] as Record<string, any>[];
while (attempts <= 5) {
try {
algoRealmCalls = await getAlgoRealmCalls(chain);
break;
} catch (e) {
console.log(e);
}
}

return getAlgoRealmHistory(algoRealmCalls);
};
5 changes: 5 additions & 0 deletions src/commands/printPoem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ALGOREALM_POEM } from '@/common/constants';

export const printPoem = () => {
return ALGOREALM_POEM;
};
8 changes: 4 additions & 4 deletions src/models/Gateway.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum IpfsGateway {
DWEB_LINK = `dweb.link`,
IPFS_IO = `ipfs.io`,
CLOUDFLARE_IPFS = `cloudflare-ipfs.com`,
ALGONODE_IO = `ipfs.algonode.xyz`,
DWEB = `dweb.link`,
IPFS = `ipfs.io`,
CLOUDFLARE = `cloudflare-ipfs.com`,
ALGONODE = `ipfs.algonode.xyz`,
}
Loading

1 comment on commit 84badfb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for algorealm ready!

✅ Preview
https://algorealm-gf5x0xnxk-algoworldexplorer.vercel.app

Built with commit 84badfb.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.