Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

LIVE-1004 Hedera integration #1887

Open
wants to merge 131 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
dcc5d54
Add support for Hedera
mehcode Sep 7, 2021
4488bd0
hedera: remove virtualAddress in favor of hederaResources.accountId
mehcode Oct 19, 2021
2448146
hedera: move mirror API URL to env.ts
mehcode Oct 19, 2021
8e44720
hedera: update @hashgraph/sdk
mehcode Oct 19, 2021
142f5a8
hedera: handle hederaResources in asReusltAccount in cross.ts
mehcode Oct 19, 2021
a369da6
hedera: hide system accounts in recipients and only show node if this…
mehcode Nov 23, 2021
018a47b
hedera: use transaction ID format that works in the explorer
mehcode Nov 23, 2021
d5dd7a5
update hbar.svg from icon by ledger
mehcode Nov 23, 2021
d8516df
hedera: run prettier and replace let with const where possible (lint)
mehcode Nov 23, 2021
ff09156
hedera: remove unused imports, run prettier on one more file (lint)
mehcode Nov 23, 2021
4e73c1f
hedera: fix getOperationsForAccount, off by 1
mehcode Nov 23, 2021
648b093
test: add unit tests
jackthta Dec 16, 2021
0f5fcf5
refactor: remove double type assertion in js-transaction.ts
jackthta Dec 17, 2021
66a19f2
refactor: use estimateMaxSpendable() in utils.ts
jackthta Dec 20, 2021
16d287a
test: fix utils.test.ts
jackthta Dec 20, 2021
3bee37c
refactor: set block properties to null
jackthta Dec 28, 2021
fe77922
refactor: optimize getAccountsForPublicKey() by setting balance param…
jackthta Dec 28, 2021
fb0865c
chore: update @hashgraph/sdk
jackthta Jan 6, 2022
269aa6a
fix: fix invalid recipient address error for Send flow
jackthta Jan 6, 2022
1e94268
fix: fix looping prompt bug during sign transaction step for Send flow
jackthta Jan 6, 2022
6878b78
feat: show verification info on sign transaction step
jackthta Jan 11, 2022
3ed1fca
refactor: optimize sync operation
jackthta Jan 11, 2022
974d54a
docs: remove obsolete comment
jackthta Jan 18, 2022
5fbb269
fix: live mobile add acc flow's grpc error
jackthta Mar 7, 2022
c1ea0e2
wip: add hedera bot
jackthta Mar 9, 2022
8cb1235
wip(hedera bot): finish `speculos-deviceActions` file
jackthta Mar 10, 2022
ece8510
fix(hedera bot): correct `speculos-deviceActions`
jackthta Mar 11, 2022
c228e13
refactor: `speculos-deviceActions` to remove `expectedValue` field in…
jackthta Mar 15, 2022
2509d77
fix: add missing 'Max Fee' step to `speculos-deviceActions`
jackthta Mar 17, 2022
9055039
refactor: update specs.ts
jackthta Mar 18, 2022
551dd95
fix(hedera bot): add missing amount for send max mutation
jackthta Mar 18, 2022
d61adae
refactor: use `calculateAmount` in `prepareTransaction` instead of in…
jackthta Mar 21, 2022
cde9a99
fix(hedera bot): set expected value of Max Fee step to 1 hbar
jackthta Mar 23, 2022
3adeeaa
test: fix unit tests
jackthta Mar 25, 2022
c0e9695
LL-1589 more robust bitcoin retry (#1835)
hzheng-ledger Mar 29, 2022
3d69586
Merge branch 'release/v21.36.0' into develop
valpinkman Apr 4, 2022
c171d0b
fix bch address convert issue (#1847)
hzheng-ledger Apr 5, 2022
c2256fe
Hotfix - v21.36.1 on Develop (#1860)
lambertkevin Apr 6, 2022
7e6888e
Remove all libcore codebase (#1831)
gre Apr 5, 2022
324ee63
Add ETH goerli (#1841)
hzheng-ledger Apr 5, 2022
e22739e
Cosmos fixes (#1846)
Apr 5, 2022
25bc993
Family/crypto org (#1852)
Apr 5, 2022
cee4a00
[LIVE-1174] - Feature: Upgrade NFT Architecture (#1805)
lambertkevin Apr 5, 2022
f42af0b
Add polygon to NFT currencies (#1848)
lambertkevin Apr 5, 2022
196130a
[LIVE-1909] - Bugfix: OpenSea Lazy mint quantity fix (#1862)
lambertkevin Apr 6, 2022
84a21df
Reset CLI LLC dep
lambertkevin Apr 5, 2022
7ef865d
LIVE-1890 Replace all deprecated uses of open(deviceId) with withDevi…
Apr 7, 2022
2ea9121
Fix nft operations wrong order to infer balance (#1865)
lambertkevin Apr 7, 2022
8eeb10d
Replace all deprecated uses of open(deviceId) with withDevice (#1864)
Apr 7, 2022
20b56c4
[LIVE-1911][LIVE-1912] Feature - Add collection name resolver to eth …
lambertkevin Apr 7, 2022
83286af
Bump plist from 3.0.4 to 3.0.5 in /mobile-test-app (#1849)
dependabot[bot] Apr 7, 2022
f34367d
Bump cocoapods-downloader from 1.3.0 to 1.6.3 in /mobile-test-app (#1…
dependabot[bot] Apr 7, 2022
e842162
Bump ansi-regex from 3.0.0 to 3.0.1 (#1850)
dependabot[bot] Apr 7, 2022
80d7186
Solana staking (#1844)
Apr 7, 2022
0ed12b7
v22.0.0
lambertkevin Apr 5, 2022
78650fe
Merge pull request #1853 from LedgerHQ/release/v22.0.0
lambertkevin Apr 7, 2022
347a38f
(Solana): fix missing superstruct dependency -> making app crashes
LFBarreto Apr 8, 2022
6d8d927
v21.36.2
LFBarreto Apr 8, 2022
95d7033
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik Apr 8, 2022
ea70c12
Better integration of Hedera sync into Ledger Live's framework
hakim-adamik Feb 25, 2022
7a1d46a
Remove HederaResources
hakim-adamik Mar 3, 2022
b3a30d4
Simplify use of accountId as simple string
hakim-adamik Mar 4, 2022
af47ff7
New bot for Hedera
hakim-adamik Mar 7, 2022
e99d68f
Update Hedera bot spec
hakim-adamik Mar 10, 2022
ddd623e
Revert "Merge pull request #1853 from LedgerHQ/release/v22.0.0"
lambertkevin Apr 8, 2022
66d37c8
Merge branch 'develop' into hotfix/v21.36.2
lambertkevin Apr 8, 2022
adff6b7
Merge pull request #1868 from LedgerHQ/hotfix/v21.36.2
lambertkevin Apr 8, 2022
987c6ba
Revert "Merge pull request #1853 from LedgerHQ/release/v22.0.0"
lambertkevin Apr 8, 2022
905b4bc
(Solana): fix missing superstruct dependency -> making app crashes
LFBarreto Apr 8, 2022
31215f0
v21.36.2
LFBarreto Apr 8, 2022
fe373cf
LL-1589 more robust bitcoin retry (#1835)
hzheng-ledger Mar 29, 2022
822c669
fix bch address convert issue (#1847)
hzheng-ledger Apr 5, 2022
0b46df3
LIVE-1890 Replace all deprecated uses of open(deviceId) with withDevi…
Apr 7, 2022
70170a0
Remove all libcore codebase (#1831)
gre Apr 5, 2022
4b07ff9
Add ETH goerli (#1841)
hzheng-ledger Apr 5, 2022
3094ccb
Cosmos fixes (#1846)
Apr 5, 2022
ef72e99
Family/crypto org (#1852)
Apr 5, 2022
929e8cb
[LIVE-1174] - Feature: Upgrade NFT Architecture (#1805)
lambertkevin Apr 5, 2022
ba8a53b
Add polygon to NFT currencies (#1848)
lambertkevin Apr 5, 2022
e8d573b
[LIVE-1909] - Bugfix: OpenSea Lazy mint quantity fix (#1862)
lambertkevin Apr 6, 2022
61eb4ae
Reset CLI LLC dep
lambertkevin Apr 5, 2022
d6cd75b
Fix nft operations wrong order to infer balance (#1865)
lambertkevin Apr 7, 2022
1605d3c
[LIVE-1911][LIVE-1912] Feature - Add collection name resolver to eth …
lambertkevin Apr 7, 2022
a26ba77
Bump plist from 3.0.4 to 3.0.5 in /mobile-test-app (#1849)
dependabot[bot] Apr 7, 2022
d006282
Bump cocoapods-downloader from 1.3.0 to 1.6.3 in /mobile-test-app (#1…
dependabot[bot] Apr 7, 2022
da6471f
Bump ansi-regex from 3.0.0 to 3.0.1 (#1850)
dependabot[bot] Apr 7, 2022
a0d0916
Solana staking (#1844)
Apr 7, 2022
c7e4186
v22.0.0
lambertkevin Apr 5, 2022
1449ed2
Fix new architecture nftsResolvers in cli (#1872)
lambertkevin Apr 8, 2022
36098a2
Merge branch 'develop' into release/v22.0.0
lambertkevin Apr 8, 2022
036758a
Merge remote-tracking branch 'ledgerhq/release/v22.0.0' into HEAD
hakim-adamik Apr 8, 2022
4737643
Add missing cli-transaction.ts file
hakim-adamik Apr 8, 2022
e3f847f
Update js-getTransactionStatus + test, after merge
hakim-adamik Apr 8, 2022
b19854f
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik Apr 12, 2022
e91a4ff
Update hedera bot to target family/hedera branch
hakim-adamik Apr 12, 2022
598eb57
Use withDevice instead of open/close
hakim-adamik Apr 13, 2022
f0a7d44
Update speculos-deviceActions for bot
hakim-adamik Apr 13, 2022
2c93df5
lint
hakim-adamik Apr 13, 2022
921cc26
For Nano app version to v1.0.8 for the bot
hakim-adamik Apr 14, 2022
792ab80
feat: add amount display to Send—'Transfer All' flow's confirmation s…
jackthta Apr 15, 2022
3174c20
fix: use the same kind of base64 encoding in both optimistic and rece…
mehcode Apr 25, 2022
7622525
fix: optimistic and confirmed operations' hash and account id discrep…
jackthta Apr 28, 2022
a639615
fix: hedera spec bot (#1922)
jackthta May 3, 2022
73a9e69
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik May 3, 2022
74cf436
feat: add edge case for max spendable (#1937)
jackthta May 5, 2022
b62359c
trigger bot
hakim-adamik May 5, 2022
76ba3f3
refactor: revert workaround for calculateAmount (#1942)
jackthta May 5, 2022
801c924
Add & fix dataset tests
hakim-adamik May 5, 2022
eb31f53
Move Hedera tests into their family
hakim-adamik May 5, 2022
418bbe9
Add Hedera to supported currencies for CLI + tests + tools
hakim-adamik May 5, 2022
a56c6a9
Fix lint issues
hakim-adamik May 5, 2022
4f8a0a4
Handle too low balance on the bot
hakim-adamik May 5, 2022
50493d3
trigger bot
hakim-adamik May 6, 2022
9d93b38
Update test dataset snapshot
hakim-adamik May 6, 2022
595fe78
Fix send max + add tests for it
hakim-adamik May 6, 2022
447a1f7
Remove irrelevant test cases
hakim-adamik May 6, 2022
3b8a1f5
Restore auto rounding of amounts in bot tests
hakim-adamik May 6, 2022
2b95ee5
Attempt to match trailing zeros on device amounts
hakim-adamik May 6, 2022
0361705
Remove unused imports
hakim-adamik May 6, 2022
fda1767
trigger bot
hakim-adamik May 6, 2022
327f680
trigger bot
hakim-adamik May 6, 2022
44e10d7
trigger bot
hakim-adamik May 6, 2022
991ff42
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik May 12, 2022
fc6f506
trigger bot
hakim-adamik May 12, 2022
b6a52b9
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik May 12, 2022
ce391a9
trigger bot
hakim-adamik May 13, 2022
b60cb64
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik May 16, 2022
8f980ab
Revert unwanted change in bitcoin tests
hakim-adamik May 16, 2022
5dff783
Merge remote-tracking branch 'ledgerhq/develop' into HEAD
hakim-adamik May 17, 2022
42c554c
Disable Hedera dataset tests because need frozen accounts
hakim-adamik May 17, 2022
3ef12f1
Fix lint issue
hakim-adamik May 17, 2022
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
1 change: 1 addition & 0 deletions .ci.env
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DISABLE_MOCKED_WARNING=true
EXPERIMENTAL_CURRENCIES_JS_BRIDGE=algorand,bitcoin,bitcoin_cash,bsc,litecoin,dash,qtum,zcash,bitcoin_gold,stratis,dogecoin,digibyte,komodo,pivx,zencash,vertcoin,peercoin,viacoin,stakenet,stealthcoin,decred,bitcoin_testnet,tezos,cosmos
EXPERIMENTAL_CURRENCIES=hedera
81 changes: 81 additions & 0 deletions .github/workflows/bot-hedera-mooncake.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Bot 'Hedera'
on:
push:
branches:
- family/hedera

jobs:
start-runner:
name: "start ec2 instance (Linux)"
if: ${{ always() }}
uses: ledgerhq/actions/.github/workflows/start-linux-runner.yml@main
secrets:
CI_BOT_TOKEN: ${{ secrets.CI_BOT_TOKEN }}

stop-runner:
name: "stop ec2 instance (Linux)"
needs: [start-runner, run-bot]
uses: ledgerhq/actions/.github/workflows/stop-linux-runner.yml@main
if: ${{ always() }}
with:
label: ${{ needs.start-runner.outputs.label }}
ec2-instance-id: ${{ needs.start-runner.outputs.ec2-instance-id }}
secrets:
CI_BOT_TOKEN: ${{ secrets.CI_BOT_TOKEN }}

run-bot:
needs: [start-runner]
runs-on: ${{ needs.start-runner.outputs.label }}
steps:
- name: prepare runner
run: |
sudo growpart /dev/nvme0n1 1
sudo resize2fs /dev/nvme0n1p1
- uses: actions/checkout@v2
- name: Retrieving coin apps
uses: actions/checkout@v2
with:
repository: LedgerHQ/coin-apps
token: ${{ secrets.PAT }}
path: coin-apps
- uses: actions/setup-node@master
with:
node-version: 14.x
- name: install yarn
run: npm i -g yarn
- name: pull docker image
run: docker pull ghcr.io/ledgerhq/speculos
- name: kill apt-get
run: sudo killall -w apt-get apt || echo OK
- name: Install linux deps
run: sudo apt-get install -y libusb-1.0-0-dev jq
- name: Install dependencies
run: |
yarn global add yalc
yarn --frozen-lockfile
yarn ci-setup-cli
- name: BOT
env:
SHOW_LEGACY_NEW_ACCOUNT: "1"
DEBUG_HTTP_RESPONSE: "1"
SEED: ${{ secrets.SEED4 }}
VERBOSE_FILE: bot-tests.txt
GITHUB_SHA: ${GITHUB_SHA}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_RUN_ID: ${{ github.run_id }}
GITHUB_WORKFLOW: ${{ github.workflow }}
SLACK_API_TOKEN: ${{ secrets.SLACK_API_TOKEN }}
SLACK_CHANNEL: ci-hbar-ll
BOT_FILTER_FAMILY: hedera
EXPERIMENTAL_CURRENCIES: hedera
run: COINAPPS=$PWD/coin-apps yarn ci-test-bot
timeout-minutes: 120
- name: Run coverage
if: failure() || success()
run: CODECOV_TOKEN=${{ secrets.CODECOV_TOKEN }} npx codecov
- name: upload logs
if: failure() || success()
uses: actions/upload-artifact@v1
with:
name: bot-tests.txt
path: bot-tests.txt
1 change: 1 addition & 0 deletions cli/src/live-common-setup-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ setSupportedCurrencies([
"crypto_org",
"crypto_org_croeseid",
"celo",
"hedera",
]);

for (const k in process.env) setEnvUnsafe(k as EnvName, process.env[k]);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@crypto-com/chain-jslib": "0.0.19",
"@ethereumjs/common": "^2.6.2",
"@ethereumjs/tx": "^3.5.0",
"@hashgraph/sdk": "^2.10.1",
"@ledgerhq/compressjs": "1.3.2",
"@ledgerhq/cryptoassets": "6.28.2",
"@ledgerhq/devices": "6.27.1",
Expand Down
1 change: 1 addition & 0 deletions src/__tests__/test-helpers/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ setSupportedCurrencies([
"filecoin",
"solana",
"celo",
"hedera",
]);
5 changes: 4 additions & 1 deletion src/cross.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export function accountToAccountData({
index,
balance,
}: Account): AccountData {
return {
const res: AccountData = {
id,
name,
seedIdentifier,
Expand All @@ -281,6 +281,8 @@ export function accountToAccountData({
index,
balance: balance.toString(),
};

return res;
}
// reverse the account data to an account.
// this restore the essential data of an account and the result of the fields
Expand Down Expand Up @@ -358,5 +360,6 @@ export const accountDataToAccount = ({
creationDate: new Date(),
balanceHistoryCache: emptyHistoryCache,
};

return account;
};
3 changes: 3 additions & 0 deletions src/data/icons/svg/HBAR.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ const modes = Object.freeze({
solanaSub: {
overridesDerivation: "44'/501'/<account>'",
},
hederaBip44: {
overridesDerivation: "44/3030",
},
});
modes as Record<DerivationMode, ModeSpec>; // eslint-disable-line

Expand All @@ -179,6 +182,7 @@ const legacyDerivations: Record<CryptoCurrencyIds, DerivationMode[]> = {
stellar: ["sep5"],
polkadot: ["polkadotbip44"],
filecoin: ["filecoin"],
hedera: ["hederaBip44"],
};

const legacyDerivationsPerFamily: Record<string, DerivationMode[]> = {
Expand Down Expand Up @@ -338,11 +342,13 @@ const disableBIP44 = {
stellar: true,
polkadot: true,
solana: true,
hedera: true,
};
const seedIdentifierPath = {
neo: ({ purpose, coinType }) => `${purpose}'/${coinType}'/0'/0/0`,
filecoin: ({ purpose, coinType }) => `${purpose}'/${coinType}'/0'/0/0`,
solana: ({ purpose, coinType }) => `${purpose}'/${coinType}'`,
hedera: ({ purpose, coinType }) => `${purpose}/${coinType}`,
_: ({ purpose, coinType }) => `${purpose}'/${coinType}'/0'`,
};
export const getSeedIdentifierDerivation = (
Expand Down
5 changes: 5 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ const envDefinitions = {
def: "https://validators-solana.coin.ledger.com/api/v1/validators",
desc: "base url for validators.app validator list",
},
API_HEDERA_MIRROR: {
def: "https://hedera.coin.ledger.com",
parser: stringParser,
desc: "mirror node API for Hedera",
},
BASE_SOCKET_URL: {
def: "wss://scriptrunner.api.live.ledger.com/update",
parser: stringParser,
Expand Down
151 changes: 151 additions & 0 deletions src/families/hedera/api/mirror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import BigNumber from "bignumber.js";
import network from "../../../network";
import URL from "url";
import { getAccountBalance } from "./network";
import { Operation, OperationType } from "../../../types";
import { encodeOperationId } from "../../../operation";
import { AccountId } from "@hashgraph/sdk";
import { getEnv } from "../../../env";
import { base64ToUrlSafeBase64 } from "../utils";

const getMirrorApiUrl = (): string => getEnv("API_HEDERA_MIRROR");

const fetch = (path, query = {}) =>
network({
type: "get",
url: URL.format({
pathname: `${getMirrorApiUrl()}/api/v1${path}`,
query,
}),
});

export interface Account {
accountId: AccountId;
balance: BigNumber;
}

export async function getAccountsForPublicKey(
publicKey: string
): Promise<Account[]> {
let r;
try {
r = await fetch("/accounts", {
"account.publicKey": publicKey,
balance: false,
});
} catch (e: any) {
if (e.name === "LedgerAPI4xx") return [];
throw e;
}
const rawAccounts = r.data.accounts;
const accounts: Account[] = [];

for (const raw of rawAccounts) {
const accountBalance = await getAccountBalance(raw.account);

accounts.push({
accountId: AccountId.fromString(raw.account),
balance: accountBalance.balance,
});
}

return accounts;
}

interface HederaMirrorTransaction {
transfers: HederaMirrorTransfer[];
charged_tx_fee: string;
transaction_hash: string;
consensus_timestamp: string;
}

interface HederaMirrorTransfer {
account: string;
amount: number;
}

export async function getOperationsForAccount(
ledgerAccountId: string,
address: string,
latestOperationTimestamp: string
): Promise<Operation[]> {
const operations: Operation[] = [];
const r = await fetch("/transactions", {
"account.id": address,
timestamp: `gt:${latestOperationTimestamp}`,
});
const rawOperations = r.data.transactions as HederaMirrorTransaction[];

for (const raw of rawOperations) {
const { consensus_timestamp } = raw;
const timestamp = new Date(
parseInt(consensus_timestamp.split(".")[0], 10) * 1000
);
const senders: string[] = [];
const recipients: string[] = [];
const fee = new BigNumber(raw.charged_tx_fee);
let value = new BigNumber(0);
let type: OperationType = "NONE";

for (let i = raw.transfers.length - 1; i >= 0; i--) {
const transfer = raw.transfers[i];
const amount = new BigNumber(transfer.amount);
const account = AccountId.fromString(transfer.account);

if (transfer.account === address) {
if (amount.isNegative()) {
value = amount.abs();
type = "OUT";
} else {
value = amount;
type = "IN";
}
}

if (amount.isNegative()) {
senders.push(transfer.account);
} else {
if (account.shard.eq(0) && account.realm.eq(0)) {
if (account.num.lt(100)) {
// account is a node, only add to list if we have none
if (recipients.length === 0) {
recipients.push(transfer.account);
}
} else if (account.num.lt(1000)) {
// account is a system account that is not a node
// do NOT add
} else {
recipients.push(transfer.account);
}
} else {
recipients.push(transfer.account);
}
}
}

// NOTE: earlier addresses are the "fee" addresses
recipients.reverse();
senders.reverse();

const hash = base64ToUrlSafeBase64(raw.transaction_hash);

operations.push({
value,
date: timestamp,
// NOTE: there are no "blocks" in hedera
// Set a value just so that it's considered confirmed according to isConfirmedOperation
blockHeight: 5,
blockHash: null,
extra: { consensus_timestamp },
fee,
hash,
recipients,
senders,
accountId: ledgerAccountId,
id: encodeOperationId(ledgerAccountId, hash, type),
type,
});
}

return operations;
}
56 changes: 56 additions & 0 deletions src/families/hedera/api/network.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import BigNumber from "bignumber.js";
import * as hedera from "@hashgraph/sdk";
import { Account } from "../../../types";
import { Transaction } from "../types";
import { AccountId } from "@hashgraph/sdk";

export function broadcastTransaction(
transaction: hedera.Transaction
): Promise<hedera.TransactionResponse> {
return transaction.execute(getClient());
}

export async function buildUnsignedTransaction({
account,
transaction,
}: {
account: Account;
transaction: Transaction;
}): Promise<hedera.TransferTransaction> {
const hbarAmount = hedera.Hbar.fromTinybars(transaction.amount);
const accountId = account.freshAddress;

return new hedera.TransferTransaction()
.setNodeAccountIds([new AccountId(3)])
.setTransactionId(hedera.TransactionId.generate(accountId))
.addHbarTransfer(accountId, hbarAmount.negated())
.addHbarTransfer(transaction.recipient, hbarAmount)
.freeze();
}

export interface AccountBalance {
balance: BigNumber;
}

export async function getAccountBalance(
address: string
): Promise<AccountBalance> {
const accountId = AccountId.fromString(address);
const accountBalance = await new hedera.AccountBalanceQuery({
accountId,
}).execute(getClient());

return {
balance: accountBalance.hbars.to(hedera.HbarUnit.Tinybar),
};
}

let _hederaClient: hedera.Client | null = null;

function getClient(): hedera.Client {
_hederaClient ??= hedera.Client.forMainnet().setMaxNodesPerTransaction(1);

//_hederaClient.setNetwork({ mainnet: "https://hedera.coin.ledger.com" });

return _hederaClient;
}
Loading