Skip to content

Commit

Permalink
Merge pull request #1085 from hashgraph/feat/backend-integration-SDK-…
Browse files Browse the repository at this point in the history
…test

Hedera account ID in Get transactions + Multisig related SDK tests
  • Loading branch information
AlbertoMolinaIoBuilders authored Apr 2, 2024
2 parents 02edcd0 + 4a8ce16 commit d5dc225
Show file tree
Hide file tree
Showing 14 changed files with 520 additions and 1,044 deletions.
3 changes: 3 additions & 0 deletions sdk/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ CLIENT_ACCOUNT_ID_ECDSA=
HEDERA_TOKEN_MANAGER_ADDRESS=
FACTORY_ADDRESS=

# --- MultiSig Account associated to keys CLIENT_PRIVATE_KEY_ECDSA and CLIENT_PRIVATE_KEY_ED25519 ---
MULTISIG_ACCOUNT_ADDRESS=

# --- Infrastructure Endpoints ---
MIRROR_NODE_BASE_URL=
RPC_NODE_BASE_URL=
Expand Down
3 changes: 3 additions & 0 deletions sdk/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import { HederaId } from '../src/domain/context/shared/HederaId.js';
import { config } from 'dotenv';
config();

export const MULTISIG_ACCOUNT_ADDRESS =
process.env.MULTISIG_ACCOUNT_ADDRESS ?? '';

export const ENVIRONMENT = 'testnet';

export const CLIENT_PRIVATE_KEY_ECDSA = new PrivateKey({
Expand Down
180 changes: 179 additions & 1 deletion sdk/__tests__/port/in/StableCoin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ import {
GetAccountBalanceHBARRequest,
GetAccountBalanceRequest,
GetReserveAddressRequest,
GetTransactionsRequest,
InitializationRequest,
IsAccountAssociatedTokenRequest,
KYCRequest,
PauseRequest,
RescueHBARRequest,
RescueRequest,
SignTransactionRequest,
SubmitTransactionRequest,
TransfersRequest,
UpdateRequest,
UpdateReserveAddressRequest,
Expand All @@ -62,10 +65,12 @@ import ConnectRequest, {
} from '../../../src/port/in/request/ConnectRequest.js';
import GetStableCoinDetailsRequest from '../../../src/port/in/request/GetStableCoinDetailsRequest.js';
import {
BACKEND_NODE,
CLIENT_ACCOUNT_ECDSA,
CLIENT_ACCOUNT_ED25519,
FACTORY_ADDRESS,
HEDERA_TOKEN_MANAGER_ADDRESS,
MULTISIG_ACCOUNT_ADDRESS,
} from '../../config.js';
import { Client, Hbar, TransferTransaction } from '@hashgraph/sdk';
import { MirrorNode } from '../../../src/domain/context/network/MirrorNode.js';
Expand All @@ -74,13 +79,75 @@ import BaseError, {
ErrorCategory,
ErrorCode,
} from '../../../src/core/error/BaseError.js';
import BackendEndpoint from '../../../src/domain/context/network/BackendEndpoint.js';
import { Environment } from '../../../src/domain/context/network/Environment.js';
import MultiSigTransaction from '../../../src/domain/context/transaction/MultiSigTransaction.js';

const decimals = 6;
const initialSupply = 1000;
const maxSupply = 1000000;
const multisigAccountId = MULTISIG_ACCOUNT_ADDRESS;

let multiSigTransaction: MultiSigTransaction;

SDK.log = { level: 'ERROR', transports: new LoggerTransports.Console() };

jest.mock('../../../src/port/out/backend/BackendAdapter', () => {
return {
BackendAdapter: jest.fn().mockImplementation(() => ({
set: jest.fn().mockResolvedValue('mocked set'),
addTransaction: jest.fn(
(
transactionMessage: string,
description: string,
HederaAccountId: string,
keyList: string[],
threshold: number,
network: Environment,
) => {
multiSigTransaction = new MultiSigTransaction(
'1',
transactionMessage,
description,
'pending',
threshold,
keyList,
[],
[],
network,
HederaAccountId,
);
},
),
signTransaction: jest.fn(
(
transactionId: string,
transactionSignature: string,
publicKey: string,
) => {
multiSigTransaction.signed_keys.push(publicKey);
multiSigTransaction.signatures.push(transactionSignature);
if (
multiSigTransaction.signed_keys.length ==
multiSigTransaction.threshold
)
multiSigTransaction.status = 'signed';
},
),
deleteTransaction: jest
.fn()
.mockResolvedValue('mocked deleteTransaction'),
getTransactions: jest.fn(() => {
return [multiSigTransaction];
}),
getTransaction: jest.fn(() => {
return multiSigTransaction;
}),
// Add other methods as necessary
})),
};
});

describe('🧪 Stablecoin test', () => {
let stableCoinSC: StableCoinViewModel;
let stableCoinHTS: StableCoinViewModel;
Expand All @@ -100,6 +167,10 @@ describe('🧪 Stablecoin test', () => {
baseUrl: 'http://127.0.0.1:7546/api',
};

const backendEndpoint: BackendEndpoint = {
url: BACKEND_NODE.baseUrl,
};

beforeAll(async () => {
await Network.connect(
new ConnectRequest({
Expand All @@ -121,6 +192,7 @@ describe('🧪 Stablecoin test', () => {
},
mirrorNode: mirrorNode,
rpcNode: rpcNode,
backend: backendEndpoint,
}),
);
Injectable.resolveTransactionHandler();
Expand Down Expand Up @@ -209,7 +281,7 @@ describe('🧪 Stablecoin test', () => {
);

await delay();
}, 60_000);
}, 180_000);

async function checkFail(
op: () => Promise<void>,
Expand Down Expand Up @@ -540,6 +612,112 @@ describe('🧪 Stablecoin test', () => {
expect(result).not.toBeNull();
}, 60_000);

it('Performs add, multisign and submit transaction', async () => {
await Network.connect(
new ConnectRequest({
account: {
accountId: multisigAccountId,
},
network: 'testnet',
wallet: SupportedWallets.MULTISIG,
mirrorNode: mirrorNode,
rpcNode: rpcNode,
}),
);

Injectable.resolveTransactionHandler();

await StableCoin.associate(
new AssociateTokenRequest({
targetId: multisigAccountId,
tokenId: stableCoinSC?.tokenId?.toString() ?? '0.0.0',
}),
);

const trans_account = await StableCoin.getTransactions(
new GetTransactionsRequest({
page: 1,
limit: 1,
status: 'pending',
account: multisigAccountId,
}),
);

const trans_pk = await StableCoin.getTransactions(
new GetTransactionsRequest({
publicKey: {
key: CLIENT_ACCOUNT_ECDSA.privateKey
? CLIENT_ACCOUNT_ECDSA.privateKey
.toHashgraphKey()
.publicKey.toStringRaw()
: '',
type: CLIENT_ACCOUNT_ECDSA.publicKey?.type,
},
page: 1,
limit: 1,
status: 'pending',
}),
);

const trans = await StableCoin.getTransactions(
new GetTransactionsRequest({
page: 1,
limit: 1,
}),
);

const accounts = [CLIENT_ACCOUNT_ECDSA, CLIENT_ACCOUNT_ED25519];

for (const account of accounts) {
await Network.connect(
new ConnectRequest({
account: {
accountId: account.id.toString(),
privateKey: {
key: account.privateKey?.key ?? '',
type: account.privateKey?.type ?? '',
},
},
network: 'testnet',
wallet: SupportedWallets.CLIENT,
mirrorNode: mirrorNode,
rpcNode: rpcNode,
}),
);

Injectable.resolveTransactionHandler();

await StableCoin.signTransaction(
new SignTransactionRequest({
transactionId: '1',
}),
);
}

const result = await StableCoin.submitTransaction(
new SubmitTransactionRequest({
transactionId: '1',
}),
);

await Network.connect(
new ConnectRequest({
account: {
accountId: CLIENT_ACCOUNT_ED25519.id.toString(),
privateKey: CLIENT_ACCOUNT_ED25519.privateKey,
},
network: 'testnet',
wallet: SupportedWallets.CLIENT,
mirrorNode: mirrorNode,
rpcNode: rpcNode,
}),
);

expect(result).toBe(true);
expect(trans[0].id).toEqual(trans_pk[0].id);
expect(trans[0].id).toEqual(trans_account[0].id);
}, 180_000);

// ----------------------HTS--------------------------

it('Performs rescue HTS', async () => {
Expand Down
Loading

0 comments on commit d5dc225

Please sign in to comment.