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

Add test for createAddressId() #776

Merged
merged 3 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion creditcoin-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "creditcoin-js",
"version": "0.7.0",
"version": "0.7.1",
"author": "Jeremy Frank <jeremy.frank@gluwa.com>",
"license": "MIT",
"main": "lib/index.js",
Expand Down
29 changes: 21 additions & 8 deletions creditcoin-js/src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,24 @@ export const lendOnEth = async (
return lendTxHash;
};

export const checkAddress = async (
ccApi: CreditcoinApi,
existingAddressId: string,
): Promise<AddressRegistered | undefined> => {
const { api } = ccApi;

const result = await api.query.creditcoin.addresses<Option<PalletCreditcoinAddress>>(existingAddressId);

if (result.isSome) {
return {
itemId: existingAddressId,
item: createAddress(result.unwrap()),
} as AddressRegistered;
}

return undefined;
};

export const tryRegisterAddress = async (
ccApi: CreditcoinApi,
externalAddress: string,
Expand All @@ -133,19 +151,14 @@ export const tryRegisterAddress = async (
checkForExisting = false,
): Promise<AddressRegistered> => {
const {
api,
extrinsics: { registerAddress },
} = ccApi;

if (checkForExisting) {
const existingAddressId = createAddressId(blockchain, externalAddress);
const result = await api.query.creditcoin.addresses<Option<PalletCreditcoinAddress>>(existingAddressId);

if (result.isSome) {
return {
itemId: existingAddressId,
item: createAddress(result.unwrap()),
} as AddressRegistered;
const result = await checkAddress(ccApi, existingAddressId);
if (result) {
return result;
}
}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@polkadot/keyring": "10.1.9",
"@types/ws": "^8.5.3",
"axios": "^1.0.0",
"creditcoin-js": "file:../creditcoin-js/creditcoin-js-v0.7.0.tgz",
"creditcoin-js": "file:../creditcoin-js/creditcoin-js-v0.7.1.tgz",
"ws": "^8.5.0"
},
"devDependencies": {
Expand Down
44 changes: 38 additions & 6 deletions integration-tests/src/test/register-address.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { ApiPromise, Keyring, WsProvider, KeyringPair, Wallet, POINT_01_CTC } from 'creditcoin-js';
import { Blockchain, Keyring, KeyringPair, Wallet, POINT_01_CTC, creditcoinApi } from 'creditcoin-js';
import { createAddressId } from 'creditcoin-js/lib/extrinsics/register-address';
import { createCreditcoinBlockchain } from 'creditcoin-js/lib/transforms';
import { checkAddress, testData } from 'creditcoin-js/lib/testUtils';
import { CreditcoinApi } from 'creditcoin-js/lib/types';
import { signAccountId } from 'creditcoin-js/lib/utils';
import { extractFee } from '../utils';

describe('RegisterAddress', () => {
let api: ApiPromise;
let ccApi: CreditcoinApi;
let alice: KeyringPair;

beforeAll(async () => {
api = await ApiPromise.create({
provider: new WsProvider((global as any).CREDITCOIN_API_URL),
});
ccApi = await creditcoinApi((global as any).CREDITCOIN_API_URL);
alice = new Keyring({ type: 'sr25519' }).addFromUri('//Alice');
});

afterAll(async () => await api.disconnect());
afterAll(async () => await ccApi.api.disconnect());

it('fee is min 0.01 CTC', async (): Promise<void> => {
const { api } = ccApi;

return new Promise((resolve, reject) => {
const wallet = Wallet.createRandom();
const unsubscribe = api.tx.creditcoin
Expand All @@ -33,4 +36,33 @@ describe('RegisterAddress', () => {
expect(fee).toBeGreaterThanOrEqual(POINT_01_CTC);
});
});

it('createAddressId works as expected', async (): Promise<void> => {
const { blockchain } = testData(
(global as any).CREDITCOIN_ETHEREUM_CHAIN as Blockchain,
(global as any).CREDITCOIN_CREATE_WALLET,
);

const {
api,
extrinsics: { registerAddress },
} = ccApi;

const lender = alice;
const lenderWallet = Wallet.createRandom();
const lenderRegAddr = await registerAddress(
lenderWallet.address,
blockchain,
signAccountId(api, lenderWallet, lender.address),
lender,
);

// manually constructed address is the same as returned by Creditcoin
const addressId = createAddressId(blockchain, lenderWallet.address);
expect(addressId).toBe(lenderRegAddr.itemId);

// manually constructed address should be reported as registered
const result = await checkAddress(ccApi, addressId);
expect(result).toBeDefined();
});
});
Loading