Skip to content

Commit

Permalink
[sdk][typescript] Fix more styling issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jjleng authored and aptos-bot committed Jun 3, 2022
1 parent 86ca1d1 commit c3eb8ed
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 143 deletions.
1 change: 1 addition & 0 deletions ecosystem/typescript/sdk/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src/api/*
44 changes: 23 additions & 21 deletions ecosystem/typescript/sdk/src/aptos_account.test.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
import { AptosAccount, AptosAccountObject } from "./aptos_account";
import { AptosAccount, AptosAccountObject } from './aptos_account';

const AptosAccountObject: AptosAccountObject = {
address: "0x978c213990c4833df71548df7ce49d54c759d6b6d932de22b24d56060b7af2aa",
const aptosAccountObject: AptosAccountObject = {
address: '0x978c213990c4833df71548df7ce49d54c759d6b6d932de22b24d56060b7af2aa',
privateKeyHex:
"0xc5338cd251c22daa8c9c9cc94f498cc8a5c7e1d2e75287a5dda91096fe64efa5de19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c",
publicKeyHex: "0xde19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c",
// eslint-disable-next-line max-len
'0xc5338cd251c22daa8c9c9cc94f498cc8a5c7e1d2e75287a5dda91096fe64efa5de19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c',
publicKeyHex: '0xde19e5d1880cac87d57484ce9ed2e84cf0f9599f12e7cc3a52e4e7657a763f2c',
};

test("generates random accounts", () => {
test('generates random accounts', () => {
const a1 = new AptosAccount();
const a2 = new AptosAccount();
expect(a1.authKey()).not.toBe(a2.authKey());
expect(a1.address().hex()).not.toBe(a2.address().hex());
});

test("accepts custom address", () => {
const address = "0x777";
test('accepts custom address', () => {
const address = '0x777';
const a1 = new AptosAccount(null, address);
expect(a1.address().hex()).toBe(address);
});

test("Deserializes from AptosAccountObject", () => {
const a1 = AptosAccount.fromAptosAccountObject(AptosAccountObject);
expect(a1.address().hex()).toBe(AptosAccountObject.address);
expect(a1.pubKey().hex()).toBe(AptosAccountObject.publicKeyHex);
test('Deserializes from AptosAccountObject', () => {
const a1 = AptosAccount.fromAptosAccountObject(aptosAccountObject);
expect(a1.address().hex()).toBe(aptosAccountObject.address);
expect(a1.pubKey().hex()).toBe(aptosAccountObject.publicKeyHex);
});

test("Deserializes from AptosAccountObject without address", () => {
const privateKeyObject = { privateKeyHex: AptosAccountObject.privateKeyHex };
test('Deserializes from AptosAccountObject without address', () => {
const privateKeyObject = { privateKeyHex: aptosAccountObject.privateKeyHex };
const a1 = AptosAccount.fromAptosAccountObject(privateKeyObject);
expect(a1.address().hex()).toBe(AptosAccountObject.address);
expect(a1.pubKey().hex()).toBe(AptosAccountObject.publicKeyHex);
expect(a1.address().hex()).toBe(aptosAccountObject.address);
expect(a1.pubKey().hex()).toBe(aptosAccountObject.publicKeyHex);
});

test("Serializes/Deserializes", () => {
test('Serializes/Deserializes', () => {
const a1 = new AptosAccount();
const a2 = AptosAccount.fromAptosAccountObject(a1.toPrivateKeyObject());
expect(a1.authKey().hex()).toBe(a2.authKey().hex());
expect(a1.address().hex()).toBe(a2.address().hex());
});

test("Signs Strings", () => {
const a1 = AptosAccount.fromAptosAccountObject(AptosAccountObject);
expect(a1.signHexString("0x77777").hex()).toBe(
"0xc5de9e40ac00b371cd83b1c197fa5b665b7449b33cd3cdd305bb78222e06a671a49625ab9aea8a039d4bb70e275768084d62b094bc1b31964f2357b7c1af7e0d",
test('Signs Strings', () => {
const a1 = AptosAccount.fromAptosAccountObject(aptosAccountObject);
expect(a1.signHexString('0x77777').hex()).toBe(
// eslint-disable-next-line max-len
'0xc5de9e40ac00b371cd83b1c197fa5b665b7449b33cd3cdd305bb78222e06a671a49625ab9aea8a039d4bb70e275768084d62b094bc1b31964f2357b7c1af7e0d',
);
});
92 changes: 46 additions & 46 deletions ecosystem/typescript/sdk/src/aptos_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
import { AxiosResponse } from "axios";
import { AptosClient, raiseForStatus } from "./aptos_client";
import { AnyObject } from "./util";
import { AxiosResponse } from 'axios';
import { AptosClient, raiseForStatus } from './aptos_client';
import { AnyObject } from './util';

import { FAUCET_URL, NODE_URL } from "./util.test";
import { FaucetClient } from "./faucet_client";
import { AptosAccount } from "./aptos_account";
import { FAUCET_URL, NODE_URL } from './util.test';
import { FaucetClient } from './faucet_client';
import { AptosAccount } from './aptos_account';
import {
ChainId,
RawTransaction,
ScriptFunction,
StructTag,
TransactionPayloadVariantScriptFunction,
TypeTagVariantstruct,
} from "./transaction_builder/aptos_types";
import { AccountAddress } from "./transaction_builder/aptos_types";
import { bcsSerializeUint64, bcsToBytes } from "./transaction_builder/bcs";
AccountAddress,
} from './transaction_builder/aptos_types';
import { bcsSerializeUint64, bcsToBytes } from './transaction_builder/bcs';

test("gets genesis account", async () => {
test('gets genesis account', async () => {
const client = new AptosClient(NODE_URL);
const account = await client.getAccount("0x1");
const account = await client.getAccount('0x1');
expect(account.authentication_key.length).toBe(66);
expect(account.sequence_number).not.toBeNull();
});

test("gets transactions", async () => {
test('gets transactions', async () => {
const client = new AptosClient(NODE_URL);
const transactions = await client.getTransactions();
expect(transactions.length).toBeGreaterThan(0);
});

test("gets genesis resources", async () => {
test('gets genesis resources', async () => {
const client = new AptosClient(NODE_URL);
const resources = await client.getAccountResources("0x1");
const accountResource = resources.find((r) => r.type === "0x1::Account::Account");
expect((accountResource.data as AnyObject).self_address).toBe("0x1");
const resources = await client.getAccountResources('0x1');
const accountResource = resources.find((r) => r.type === '0x1::Account::Account');
expect((accountResource.data as AnyObject).self_address).toBe('0x1');
});

test("gets the Account resource", async () => {
test('gets the Account resource', async () => {
const client = new AptosClient(NODE_URL);
const accountResource = await client.getAccountResource("0x1", "0x1::Account::Account");
expect((accountResource.data as AnyObject).self_address).toBe("0x1");
const accountResource = await client.getAccountResource('0x1', '0x1::Account::Account');
expect((accountResource.data as AnyObject).self_address).toBe('0x1');
});

test("gets ledger info", async () => {
test('gets ledger info', async () => {
const client = new AptosClient(NODE_URL);
const ledgerInfo = await client.getLedgerInfo();
expect(ledgerInfo.chain_id).toBeGreaterThan(1);
expect(parseInt(ledgerInfo.ledger_version, 10)).toBeGreaterThan(0);
});

test("gets account modules", async () => {
test('gets account modules', async () => {
const client = new AptosClient(NODE_URL);
const modules = await client.getAccountModules("0x1");
const module = modules.find((r) => r.abi.name === "TestCoin");
expect(module.abi.address).toBe("0x1");
const modules = await client.getAccountModules('0x1');
const module = modules.find((r) => r.abi.name === 'TestCoin');
expect(module.abi.address).toBe('0x1');
});

test("gets the TestCoin module", async () => {
test('gets the TestCoin module', async () => {
const client = new AptosClient(NODE_URL);
const module = await client.getAccountModule("0x1", "TestCoin");
expect(module.abi.address).toBe("0x1");
const module = await client.getAccountModule('0x1', 'TestCoin');
expect(module.abi.address).toBe('0x1');
});

test("test raiseForStatus", async () => {
const testData = { hello: "wow" };
test('test raiseForStatus', async () => {
const testData = { hello: 'wow' };
const fakeResponse: AxiosResponse = {
status: 200,
statusText: "Status Text",
data: "some string",
statusText: 'Status Text',
data: 'some string',
request: {
host: "host",
path: "/path",
host: 'host',
path: '/path',
},
} as AxiosResponse;

Expand All @@ -94,47 +94,47 @@ test("test raiseForStatus", async () => {
});

test(
"submits bcs transaction",
'submits bcs transaction',
async () => {
const client = new AptosClient(NODE_URL);
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL, null);

const account1 = new AptosAccount();
await faucetClient.fundAccount(account1.address(), 5000);
let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as any).coin.value).toBe("5000");
let accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as any).coin.value).toBe('5000');

const account2 = new AptosAccount();
await faucetClient.fundAccount(account2.address(), 0);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as any).coin.value).toBe("0");
accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as any).coin.value).toBe('0');

const token = new TypeTagVariantstruct(StructTag.fromString("0x1::TestCoin::TestCoin"));
const token = new TypeTagVariantstruct(StructTag.fromString('0x1::TestCoin::TestCoin'));

const scriptFunctionPayload = new TransactionPayloadVariantScriptFunction(
ScriptFunction.natual(
"0x1::Coin",
"transfer",
'0x1::Coin',
'transfer',
[token],
[bcsToBytes(AccountAddress.fromHex(account2.address())), bcsSerializeUint64(717)],
),
);

const [{ sequence_number }, chain_id] = await Promise.all([
const [{ sequence_number: sequnceNumber }, chainId] = await Promise.all([
client.getAccount(account1.address()),
client.getChainId(),
]);

const rawTxn = new RawTransaction(
AccountAddress.fromHex(account1.address()),
BigInt(sequence_number),
BigInt(sequnceNumber),
scriptFunctionPayload,
1000n,
1n,
BigInt(Math.floor(Date.now() / 1000) + 10),
new ChainId(chain_id),
new ChainId(chainId),
);

const bcsTxn = await AptosClient.generateBCSTransaction(account1, rawTxn);
Expand All @@ -143,8 +143,8 @@ test(
await client.waitForTransaction(transactionRes.hash);

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as any).coin.value).toBe("717");
accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as any).coin.value).toBe('717');
},
30 * 1000,
);
56 changes: 28 additions & 28 deletions ecosystem/typescript/sdk/src/faucet_client.test.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
import { AptosClient } from "./aptos_client";
import { FaucetClient } from "./faucet_client";
import { AptosAccount } from "./aptos_account";
import { Types } from "./types";
import { UserTransaction } from "./api/data-contracts";
import { HexString } from "./hex_string";
import { AptosClient } from './aptos_client';
import { FaucetClient } from './faucet_client';
import { AptosAccount } from './aptos_account';
import { Types } from './types';
import { UserTransaction } from './api/data-contracts';
import { HexString } from './hex_string';

import { NODE_URL, FAUCET_URL } from "./util.test";
import { NODE_URL, FAUCET_URL } from './util.test';

test(
"full tutorial faucet flow",
'full tutorial faucet flow',
async () => {
const client = new AptosClient(NODE_URL);
const faucetClient = new FaucetClient(NODE_URL, FAUCET_URL);

const account1 = new AptosAccount();
const txns = await faucetClient.fundAccount(account1.address(), 5000);
const tx1 = await client.getTransaction(txns[1]);
expect(tx1.type).toBe("user_transaction");
expect(tx1.type).toBe('user_transaction');
let resources = await client.getAccountResources(account1.address());
let accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("5000");
let accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe('5000');

const account2 = new AptosAccount();
await faucetClient.fundAccount(account2.address(), 0);
resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("0");
accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe('0');

const payload: Types.TransactionPayload = {
type: "script_function_payload",
function: "0x1::Coin::transfer",
type_arguments: ["0x1::TestCoin::TestCoin"],
arguments: [account2.address().hex(), "717"],
type: 'script_function_payload',
function: '0x1::Coin::transfer',
type_arguments: ['0x1::TestCoin::TestCoin'],
arguments: [account2.address().hex(), '717'],
};
const txnRequest = await client.generateTransaction(account1.address(), payload);
const signedTxn = await client.signTransaction(account1, txnRequest);
const transactionRes = await client.submitTransaction(signedTxn);
await client.waitForTransaction(transactionRes.hash);

resources = await client.getAccountResources(account2.address());
accountResource = resources.find((r) => r.type === "0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>");
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe("717");
accountResource = resources.find((r) => r.type === '0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>');
expect((accountResource.data as { coin: { value: string } }).coin.value).toBe('717');

const res = await client.getAccountTransactions(account1.address(), { start: 0 });
const tx = res.find((e) => e.type === "user_transaction") as UserTransaction;
const tx = res.find((e) => e.type === 'user_transaction') as UserTransaction;
expect(new HexString(tx.sender).toShortString()).toBe(account1.address().toShortString());

const events = await client.getEventsByEventHandle(
tx.sender,
"0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>",
"withdraw_events",
'0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>',
'withdraw_events',
);
expect(events[0].type).toBe("0x1::Coin::WithdrawEvent");
expect(events[0].type).toBe('0x1::Coin::WithdrawEvent');

const event_subset = await client.getEventsByEventHandle(
const eventSubset = await client.getEventsByEventHandle(
tx.sender,
"0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>",
"withdraw_events",
'0x1::Coin::CoinStore<0x1::TestCoin::TestCoin>',
'withdraw_events',
{ start: 0, limit: 1 },
);
expect(event_subset[0].type).toBe("0x1::Coin::WithdrawEvent");
expect(eventSubset[0].type).toBe('0x1::Coin::WithdrawEvent');

const events2 = await client.getEventsByEventKey(events[0].key);
expect(events2[0].type).toBe("0x1::Coin::WithdrawEvent");
expect(events2[0].type).toBe('0x1::Coin::WithdrawEvent');
},
30 * 1000,
);
Loading

0 comments on commit c3eb8ed

Please sign in to comment.