-
Notifications
You must be signed in to change notification settings - Fork 31
/
txBuilder.ts
164 lines (140 loc) · 5.27 KB
/
txBuilder.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { BabelSwapPlugin } from "@fleet-sdk/babel-fees-plugin";
import { OutputBuilder, TransactionBuilder } from "@fleet-sdk/core";
import { CherryPickSelectionStrategy } from "@fleet-sdk/core";
import BigNumber from "bignumber.js";
import { isEmpty } from "lodash-es";
import { fetchBabelBoxes, getNanoErgsPerTokenRate, selectBestBabelBox } from "../babelFees";
import { fetchBoxes } from "../boxFetcher";
import { graphQLService } from "@/api/explorer/graphQlService";
import { ERG_DECIMALS, ERG_TOKEN_ID, MIN_BOX_VALUE, SAFE_MIN_FEE_VALUE } from "@/constants/ergo";
import { ACTIONS } from "@/constants/store";
import store from "@/store";
import { UnsignedTx } from "@/types/connector";
import { AddressState, BigNumberType, FeeSettings, StateAsset, WalletType } from "@/types/internal";
import { undecimalize } from "@/utils/bigNumbers";
import { bip32Pool } from "@/utils/objectPool";
const SAFE_MAX_CHANGE_TOKEN_LIMIT = 100;
export type TxAssetAmount = {
asset: StateAsset;
amount?: BigNumberType;
};
export async function createP2PTransaction({
recipientAddress,
assets,
fee,
walletType
}: {
recipientAddress: string;
assets: TxAssetAmount[];
fee: FeeSettings;
walletType: WalletType;
}): Promise<UnsignedTx> {
const [inputs, currentHeight] = await Promise.all([
fetchBoxes(store.state.currentWallet.id),
graphQLService.getCurrentHeight()
]);
if (isEmpty(inputs)) {
throw Error("Unable to fetch inputs, please check your connection.");
}
if (!currentHeight) {
throw Error("Unable to fetch current height, please check your connection.");
}
const isBabelFee = fee.tokenId !== ERG_TOKEN_ID;
const sendingNanoErgs = getSendingNanoErgs(assets);
const unsigned = new TransactionBuilder(currentHeight)
.from(inputs)
.to(
new OutputBuilder(
sendingNanoErgs.eq(0) && isBabelFee ? BigInt(MIN_BOX_VALUE) : sendingNanoErgs.toString(),
recipientAddress
).addTokens(
assets
.filter((a) => a.asset.tokenId !== ERG_TOKEN_ID && a.amount && !a.amount.isZero())
.map((token) => ({
tokenId: token.asset.tokenId,
amount: undecimalize(
token.amount || BigNumber(0),
token.asset.info?.decimals
).toString()
}))
)
)
.sendChangeTo(await safeGetChangeAddress(recipientAddress));
await setFee(unsigned, fee);
setSelectionAndChangeStrategy(unsigned, walletType);
return unsigned.build().toEIP12Object() as UnsignedTx;
}
export function setSelectionAndChangeStrategy(
builder: TransactionBuilder,
walletType: WalletType
): TransactionBuilder {
if (walletType === WalletType.Ledger) {
return builder
.configure((settings) => settings.isolateErgOnChange().setMaxTokensPerChangeBox(1))
.configureSelector((selector) => selector.defineStrategy(new CherryPickSelectionStrategy()));
}
return builder
.configure((settings) => settings.setMaxTokensPerChangeBox(SAFE_MAX_CHANGE_TOKEN_LIMIT))
.configureSelector((selector) => selector.orderBy((input) => input.creationHeight));
}
export async function setFee(
builder: TransactionBuilder,
fee: FeeSettings
): Promise<TransactionBuilder> {
const isBabelFee = fee.tokenId !== ERG_TOKEN_ID;
let feeNanoErgs = undecimalize(fee.value, ERG_DECIMALS);
let sendingNanoErgs = BigNumber(builder.outputs.sum().nanoErgs.toString());
if (isBabelFee) {
const tokenUnits = undecimalize(fee.value, fee.assetInfo?.decimals);
const selectedBox = selectBestBabelBox(
await fetchBabelBoxes(fee.tokenId, fee.nanoErgsPerToken),
tokenUnits
);
if (!selectedBox) {
throw Error(
"There is not enough liquidity in the Babel Boxes for the selected fee asset in the selected price range."
);
} else {
fee.box = selectedBox;
}
feeNanoErgs = tokenUnits.multipliedBy(getNanoErgsPerTokenRate(selectedBox));
if (
sendingNanoErgs.gt(0) &&
sendingNanoErgs.lte(MIN_BOX_VALUE) &&
sendingNanoErgs.lte(feeNanoErgs.minus(SAFE_MIN_FEE_VALUE))
) {
sendingNanoErgs = BigNumber(MIN_BOX_VALUE);
feeNanoErgs = feeNanoErgs.minus(sendingNanoErgs);
}
builder.extend(
BabelSwapPlugin(fee.box, { tokenId: fee.tokenId, amount: tokenUnits.toString() })
);
}
builder.payFee(feeNanoErgs.toString());
return builder;
}
function getSendingNanoErgs(assets: TxAssetAmount[]): BigNumber {
const erg = assets.find((a) => a.asset.tokenId === ERG_TOKEN_ID);
if (erg && erg.amount) {
return undecimalize(erg.amount, ERG_DECIMALS);
} else {
return new BigNumber(0);
}
}
export async function safeGetChangeAddress(recipientAddress = ""): Promise<string> {
const wallet = store.state.currentWallet;
const addresses = store.state.currentAddresses;
if (wallet.settings.avoidAddressReuse) {
const unused = addresses.find(
(a) => a.state === AddressState.Unused && a.script !== recipientAddress
);
if (isEmpty(unused)) {
await store.dispatch(ACTIONS.NEW_ADDRESS);
}
}
const index = wallet.settings.avoidAddressReuse
? addresses.find((a) => a.state === AddressState.Unused && a.script !== recipientAddress)
?.index ?? wallet.settings.defaultChangeIndex
: wallet.settings.defaultChangeIndex;
return bip32Pool.get(wallet.publicKey).deriveAddress(index || 0).script;
}