@axis-markets/client 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -9
- package/lib/axis-client.cjs +1 -1
- package/lib/axis-client.cjs.map +1 -1
- package/lib/footprint.cjs +2 -0
- package/lib/footprint.cjs.map +1 -0
- package/package.json +9 -2
- package/src/footprint.d.ts +32 -0
- package/src/footprint.js +123 -0
- package/src/index.d.ts +523 -521
- package/src/index.js +20 -1
package/src/footprint.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {xdr, Address, TransactionBuilder, contract} from '@stellar/stellar-sdk'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Upper bound of a serialized `Order` ledger entry in bytes.
|
|
5
|
+
* (u64 id/expires, u32 kind, three 32-byte addresses, three i128 amounts = 428 bytes, rounded up to 512).
|
|
6
|
+
* @type {number}
|
|
7
|
+
*/
|
|
8
|
+
export const ORDER_ENTRY_SIZE = 512
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* CPU instructions added to the declared budget for every order that was not matched at simulation time.
|
|
12
|
+
* @type {number}
|
|
13
|
+
*/
|
|
14
|
+
export const ORDER_INSTRUCTIONS = 2_000_000
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Resource fee (stroops) added for every order that was not matched at simulation time.
|
|
18
|
+
* (write entry ~10k, read entry ~6k, writes ~12k, 2M instructions ~5k, trade/order events ~10k).
|
|
19
|
+
* @type {bigint}
|
|
20
|
+
*/
|
|
21
|
+
export const ORDER_RESOURCE_FEE = 100_000n
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Network-wide per-transaction instructions limit.
|
|
25
|
+
* @type {number}
|
|
26
|
+
*/
|
|
27
|
+
const MAX_TX_INSTRUCTIONS = 100_000_000
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Flatten and deduplicate order ids from several lists, coercing them to bigint.
|
|
31
|
+
* @param {...(Array<bigint|number|string>|undefined|null)} idLists - Lists of order ids
|
|
32
|
+
* @return {bigint[]} - Unique order ids in the order of first appearance
|
|
33
|
+
*/
|
|
34
|
+
export function collectOrderIds(...idLists) {
|
|
35
|
+
const seen = new Set()
|
|
36
|
+
const res = []
|
|
37
|
+
for (const list of idLists) {
|
|
38
|
+
if (!list)
|
|
39
|
+
continue
|
|
40
|
+
for (const id of list) {
|
|
41
|
+
if (id === undefined || id === null)
|
|
42
|
+
continue
|
|
43
|
+
const value = BigInt(id)
|
|
44
|
+
const key = value.toString()
|
|
45
|
+
if (seen.has(key))
|
|
46
|
+
continue
|
|
47
|
+
seen.add(key)
|
|
48
|
+
res.push(value)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return res
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Build persistent contract data ledger key of an order.
|
|
56
|
+
* @param {string} contractId - DEX contract address
|
|
57
|
+
* @param {bigint|number|string} orderId - Order id
|
|
58
|
+
* @return {xdr.LedgerKey}
|
|
59
|
+
*/
|
|
60
|
+
export function orderLedgerKey(contractId, orderId) {
|
|
61
|
+
return xdr.LedgerKey.contractData(new xdr.LedgerKeyContractData({
|
|
62
|
+
contract: Address.fromString(contractId).toScAddress(),
|
|
63
|
+
key: xdr.ScVal.scvU64(new xdr.Uint64(BigInt(orderId))),
|
|
64
|
+
durability: xdr.ContractDataDurability.persistent()
|
|
65
|
+
}))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Make sure every supplied order id declared in the read-write footprint of a simulated transaction.
|
|
70
|
+
* @param {contract.AssembledTransaction} tx - Simulated transaction
|
|
71
|
+
* @param {string} contractId - DEX contract address
|
|
72
|
+
* @param {Array<bigint|number|string>} orderIds - Order ids passed to the contract call
|
|
73
|
+
* @return {number} - Number of order entries added to the read-write footprint
|
|
74
|
+
*/
|
|
75
|
+
export function ensureOrdersFootprint(tx, contractId, orderIds) {
|
|
76
|
+
const simulation = tx.simulation
|
|
77
|
+
if (!simulation || simulation.error || simulation.restorePreamble || !tx.built || !orderIds?.length)
|
|
78
|
+
return 0
|
|
79
|
+
/** @type {SorobanDataBuilder} */
|
|
80
|
+
const data = simulation.transactionData
|
|
81
|
+
const readOnly = data.getReadOnly()
|
|
82
|
+
const readWrite = data.getReadWrite()
|
|
83
|
+
const roIndex = new Map(readOnly.map((key, i) => [key.toXDR('base64'), i]))
|
|
84
|
+
const rwIndex = new Set(readWrite.map(key => key.toXDR('base64')))
|
|
85
|
+
const dropFromReadOnly = new Set()
|
|
86
|
+
let added = 0
|
|
87
|
+
for (const id of collectOrderIds(orderIds)) {
|
|
88
|
+
const key = orderLedgerKey(contractId, id)
|
|
89
|
+
const encoded = key.toXDR('base64')
|
|
90
|
+
if (rwIndex.has(encoded))
|
|
91
|
+
continue
|
|
92
|
+
//a key can't be listed in both parts of the footprint
|
|
93
|
+
if (roIndex.has(encoded)) {
|
|
94
|
+
dropFromReadOnly.add(roIndex.get(encoded))
|
|
95
|
+
}
|
|
96
|
+
readWrite.push(key)
|
|
97
|
+
rwIndex.add(encoded)
|
|
98
|
+
added++
|
|
99
|
+
}
|
|
100
|
+
if (added === 0)
|
|
101
|
+
return 0
|
|
102
|
+
const newReadOnly = readOnly.filter((_, i) => !dropFromReadOnly.has(i))
|
|
103
|
+
data.setFootprint(newReadOnly, readWrite)
|
|
104
|
+
|
|
105
|
+
//declare resources for the orders that may get matched at execution time
|
|
106
|
+
const resources = data.build().resources()
|
|
107
|
+
const instructions = Math.min(resources.instructions() + added * ORDER_INSTRUCTIONS, MAX_TX_INSTRUCTIONS)
|
|
108
|
+
data.setResources(instructions, resources.diskReadBytes(), resources.writeBytes() + added * ORDER_ENTRY_SIZE)
|
|
109
|
+
const resourceFee = data.build().resourceFee().toBigInt() + BigInt(added) * ORDER_RESOURCE_FEE
|
|
110
|
+
data.setResourceFee(resourceFee.toString())
|
|
111
|
+
|
|
112
|
+
//drop cached simulation data so the SDK picks up the patched builder when signing
|
|
113
|
+
delete tx.simulationTransactionData
|
|
114
|
+
//keep the built transaction consistent with the patched footprint and fee
|
|
115
|
+
const timeout = tx.options?.timeoutInSeconds ?? 300
|
|
116
|
+
tx.built = TransactionBuilder.cloneFrom(tx.built, {
|
|
117
|
+
sorobanData: data.build(),
|
|
118
|
+
timebounds: undefined
|
|
119
|
+
})
|
|
120
|
+
.setTimeout(timeout)
|
|
121
|
+
.build()
|
|
122
|
+
return added
|
|
123
|
+
}
|