Skip to content

Commit 84badfb

Browse files
authored
Refining the structure; Adding a few minor tests; Adding gateway (#8)
1 parent a8cc6be commit 84badfb

17 files changed

+250
-113
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "algorealm-ui",
33
"description": "Claim the Crown and the Sceptre of Algorand Realm",
4-
"version": "0.2.0",
4+
"version": "0.3.0",
55
"private": true,
66
"author": "Cosimo Bassi <cosimo.bassi@algorand.com>",
77
"license": "GPL-3.0",

src/commands/about.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { about } from './about';
2+
3+
describe(`about`, () => {
4+
it(`should return the expected string`, () => {
5+
expect(about()).toEqual(
6+
`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.`,
7+
);
8+
});
9+
});

src/commands/about.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const about = () => {
2+
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.`;
3+
};

src/commands/changeChain.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { changeChain } from './changeChain';
2+
import { ChainType } from '@/models/Chain';
3+
import store from '@/redux/store';
4+
import { setChain } from '@/redux/slices/walletConnectSlice';
5+
6+
describe(`changeChain`, () => {
7+
it(`should dispatch the setChain action with the correct chain type`, () => {
8+
const spy = jest.spyOn(store, `dispatch`);
9+
const chain = ChainType.MainNet;
10+
changeChain(chain);
11+
expect(spy).toHaveBeenCalledWith(setChain(chain));
12+
});
13+
14+
it(`should return the expected string`, () => {
15+
const chain = ChainType.MainNet;
16+
expect(changeChain(chain)).toEqual(`Chain changed to ${chain}`);
17+
});
18+
19+
it(`should throw an error if the store dispatch method throws an error`, () => {
20+
const spy = jest.spyOn(store, `dispatch`).mockImplementation(() => {
21+
throw new Error(`dispatch error`);
22+
});
23+
const chain = ChainType.MainNet;
24+
expect(() => changeChain(chain)).toThrowError(`dispatch error`);
25+
});
26+
});

src/commands/changeChain.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ChainType } from '@/models/Chain';
2+
import { setChain } from '@/redux/slices/walletConnectSlice';
3+
import store from '@/redux/store';
4+
5+
export const changeChain = (chain: ChainType) => {
6+
store.dispatch(setChain(chain));
7+
return `Chain changed to ${chain}`;
8+
};

src/commands/changeGateway.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { IpfsGateway } from '@/models/Gateway';
2+
import { setGateway } from '@/redux/slices/walletConnectSlice';
3+
import store from '@/redux/store';
4+
5+
export const changeGateway = (gateway: string) => {
6+
store.dispatch(
7+
setGateway(IpfsGateway[gateway.toUpperCase() as keyof typeof IpfsGateway]),
8+
);
9+
return `Gateway changed to ${gateway}`;
10+
};

src/commands/claimCrown.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { ALGOREALM_CROWN_ID } from '@/common/constants';
2+
import { optAssets } from '@/redux/slices/walletConnectSlice';
3+
import { hasAsset } from '@/utils/assets/hasAsset';
4+
import { getClaimAssetTxns } from '@/utils/transactions/getClaimAssetTxns';
5+
import submitTransactions from '@/utils/transactions/submitTransactions';
6+
import WalletManager from '@/wallets/walletManager';
7+
import store from '@/redux/store';
8+
import { IpfsGateway } from '@/models/Gateway';
9+
import { ChainType } from '@/models/Chain';
10+
import { Asset } from '@/models/Asset';
11+
12+
export const claimCrown = async (
13+
majestyName: string,
14+
algos: number,
15+
chain: ChainType,
16+
address: string,
17+
assets: Asset[],
18+
gateway: IpfsGateway,
19+
connector: WalletManager,
20+
) => {
21+
if (!connector.connected) {
22+
return `You must connect to PeraWallet to claim the Crown of Entropy.`;
23+
}
24+
25+
if (!hasAsset(ALGOREALM_CROWN_ID(chain), assets)) {
26+
await store.dispatch(
27+
optAssets({
28+
assetIndexes: [ALGOREALM_CROWN_ID(chain)],
29+
gateway,
30+
connector,
31+
}),
32+
);
33+
}
34+
35+
const claimTxns = await getClaimAssetTxns(
36+
chain,
37+
address,
38+
ALGOREALM_CROWN_ID(chain),
39+
`Crown`,
40+
majestyName,
41+
algos,
42+
);
43+
44+
const signedTxns = await connector.signTransactions(claimTxns);
45+
46+
if (!signedTxns) {
47+
return undefined;
48+
}
49+
50+
const txnResponse = await submitTransactions(chain, signedTxns);
51+
52+
return `Transactions ${txnResponse.txId} performed. 👑 Glory to ${majestyName}, the Randomic Majesty of Algorand! 🎉`;
53+
};

src/commands/claimSceptre.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { ALGOREALM_SCEPTRE_ID } from '@/common/constants';
2+
import { optAssets } from '@/redux/slices/walletConnectSlice';
3+
import { hasAsset } from '@/utils/assets/hasAsset';
4+
import { getClaimAssetTxns } from '@/utils/transactions/getClaimAssetTxns';
5+
import submitTransactions from '@/utils/transactions/submitTransactions';
6+
import WalletManager from '@/wallets/walletManager';
7+
import store from '@/redux/store';
8+
import { IpfsGateway } from '@/models/Gateway';
9+
import { ChainType } from '@/models/Chain';
10+
import { Asset } from '@/models/Asset';
11+
12+
export const claimSceptre = async (
13+
majestyName: string,
14+
algos: number,
15+
chain: ChainType,
16+
address: string,
17+
assets: Asset[],
18+
gateway: IpfsGateway,
19+
connector: WalletManager,
20+
) => {
21+
if (!connector.connected) {
22+
return `You must connect to PeraWallet to claim the Sceptre of Proof.`;
23+
}
24+
25+
if (!hasAsset(ALGOREALM_SCEPTRE_ID(chain), assets)) {
26+
await store.dispatch(
27+
optAssets({
28+
assetIndexes: [ALGOREALM_SCEPTRE_ID(chain)],
29+
gateway,
30+
connector,
31+
}),
32+
);
33+
}
34+
35+
const claimTxns = await getClaimAssetTxns(
36+
chain,
37+
address,
38+
ALGOREALM_SCEPTRE_ID(chain),
39+
`Sceptre`,
40+
majestyName,
41+
algos,
42+
);
43+
44+
const signedTxns = await connector.signTransactions(claimTxns);
45+
46+
if (!signedTxns) {
47+
return undefined;
48+
}
49+
50+
const txnResponse = await submitTransactions(chain, signedTxns);
51+
52+
return `Transactions ${txnResponse.txId} performed. 👑 Glory to ${majestyName}, the Verifiable Majesty of Algorand! 🎉`;
53+
};

src/commands/printDynasty.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { ChainType } from '@/models/Chain';
2+
import { getAlgoRealmCalls } from '@/utils/transactions/getAlgoRealmCalls';
3+
import { getAlgoRealmHistory } from '@/utils/transactions/getAlgoRealmHistory';
4+
5+
export const printDynasty = async (chain: ChainType) => {
6+
const attempts = 1;
7+
let algoRealmCalls = [] as Record<string, any>[];
8+
while (attempts <= 5) {
9+
try {
10+
algoRealmCalls = await getAlgoRealmCalls(chain);
11+
break;
12+
} catch (e) {
13+
console.log(e);
14+
}
15+
}
16+
17+
return getAlgoRealmHistory(algoRealmCalls);
18+
};

src/commands/printPoem.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ALGOREALM_POEM } from '@/common/constants';
2+
3+
export const printPoem = () => {
4+
return ALGOREALM_POEM;
5+
};

0 commit comments

Comments
 (0)