Skip to content

Commit 85faa30

Browse files
authored
Refactor meastro lbe (#64)
* update maestro * fix eslint
1 parent de40fe0 commit 85faa30

File tree

2 files changed

+43
-54
lines changed

2 files changed

+43
-54
lines changed

examples/build-tx-example.ts

+21-28
Original file line numberDiff line numberDiff line change
@@ -1104,12 +1104,11 @@ const _ONE_DAY_IN_MS = 1000 * 60 * 60 * 24;
11041104
export async function _createLbeV2EventExample(
11051105
lucid: Lucid,
11061106
address: string,
1107-
adapter: Adapter
1107+
adapter: Adapter,
1108+
asset: string
11081109
): Promise<TxComplete> {
11091110
// Replace with your asset here
1110-
const baseAsset = Asset.fromString(
1111-
"d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0bfdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc"
1112-
);
1111+
const baseAsset = Asset.fromString(asset);
11131112
const curSlot = lucid.utils.unixTimeToSlots(Date.now());
11141113
const curDate = lucid.utils.slotsToUnixTime(curSlot);
11151114
const lbeV2Parameters: LbeV2Types.LbeV2Parameters = {
@@ -1175,12 +1174,11 @@ export async function _createLbeV2EventExample(
11751174
export async function _updateLbeV2EventExample(
11761175
lucid: Lucid,
11771176
address: string,
1178-
adapter: Adapter
1177+
adapter: Adapter,
1178+
asset: string
11791179
): Promise<TxComplete> {
11801180
// Replace with your asset here
1181-
const baseAsset = Asset.fromString(
1182-
"d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0ba547d1ae595c49041570991a1c33729106e635f20643b99e3ddb1e77dc439586"
1183-
);
1181+
const baseAsset = Asset.fromString(asset);
11841182
const curSlot = lucid.utils.unixTimeToSlots(Date.now());
11851183
const curDate = lucid.utils.slotsToUnixTime(curSlot);
11861184
const lbeV2Parameters: LbeV2Types.LbeV2Parameters = {
@@ -1245,12 +1243,11 @@ export async function _updateLbeV2EventExample(
12451243
export async function _lbeV2AddMoreSellersExample(
12461244
lucid: Lucid,
12471245
address: string,
1248-
adapter: Adapter
1246+
adapter: Adapter,
1247+
asset: string
12491248
): Promise<TxComplete> {
12501249
// Replace with your asset here
1251-
const baseAsset = Asset.fromString(
1252-
"e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed7243414b45"
1253-
);
1250+
const baseAsset = Asset.fromString(asset);
12541251
const raiseAsset = Asset.fromString("lovelace");
12551252

12561253
const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);
@@ -1281,12 +1278,11 @@ export async function _lbeV2AddMoreSellersExample(
12811278
export async function _cancelLbeV2EventByOwnerExample(
12821279
lucid: Lucid,
12831280
address: string,
1284-
adapter: Adapter
1281+
adapter: Adapter,
1282+
asset: string
12851283
): Promise<TxComplete> {
12861284
// Replace with your asset here
1287-
const baseAsset = Asset.fromString(
1288-
"e4214b7cce62ac6fbba385d164df48e157eae5863521b4b67ca71d865190718981e4e7fab3eb80963f14148714d7a7847652d4017d0fb744db075027"
1289-
);
1285+
const baseAsset = Asset.fromString(asset);
12901286
const raiseAsset = Asset.fromString("lovelace");
12911287

12921288
const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);
@@ -1308,12 +1304,11 @@ export async function _cancelLbeV2EventByOwnerExample(
13081304
export async function _lbeV2DepositOrderExample(
13091305
lucid: Lucid,
13101306
address: string,
1311-
adapter: Adapter
1307+
adapter: Adapter,
1308+
asset: string
13121309
): Promise<TxComplete> {
13131310
// Replace with your asset here
1314-
const baseAsset = Asset.fromString(
1315-
"e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed7243414b45"
1316-
);
1311+
const baseAsset = Asset.fromString(asset);
13171312
const raiseAsset = Asset.fromString("lovelace");
13181313

13191314
const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);
@@ -1362,12 +1357,11 @@ export async function _lbeV2DepositOrderExample(
13621357
export async function _lbeV2WithdrawOrderExample(
13631358
lucid: Lucid,
13641359
address: string,
1365-
adapter: Adapter
1360+
adapter: Adapter,
1361+
asset: string
13661362
): Promise<TxComplete> {
13671363
// Replace with your asset here
1368-
const baseAsset = Asset.fromString(
1369-
"e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed7243414b45"
1370-
);
1364+
const baseAsset = Asset.fromString(asset);
13711365
const raiseAsset = Asset.fromString("lovelace");
13721366

13731367
const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);
@@ -1416,12 +1410,11 @@ export async function _lbeV2WithdrawOrderExample(
14161410
export async function _lbeV2CloseEventExample(
14171411
lucid: Lucid,
14181412
address: string,
1419-
adapter: Adapter
1413+
adapter: Adapter,
1414+
asset: string
14201415
): Promise<TxComplete> {
14211416
// Replace with your asset here
1422-
const baseAsset = Asset.fromString(
1423-
"d6aae2059baee188f74917493cf7637e679cd219bdfbbf4dcbeb1d0bfdfc61f25b3065a310ba3e352159125910b947b7aee704728318949933127cdc"
1424-
);
1417+
const baseAsset = Asset.fromString(asset);
14251418
const raiseAsset = Asset.fromString("lovelace");
14261419

14271420
const lbeId = PoolV2.computeLPAssetName(baseAsset, raiseAsset);

src/adapters/maestro.ts

+22-26
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class MaestroAdapter implements Adapter {
6161
}));
6262
}
6363

64-
private async getAllUtxosDataByAddress(address: string, queryParams?: UtxosByAddressQueryParams): Promise<UtxoWithSlot[]> {
64+
private async getAllUtxosDataByPaymentCred(credential: string, queryParams?: UtxosByAddressQueryParams): Promise<UtxoWithSlot[]> {
6565
let cursor: string | null | undefined = null;
6666
const utxosData: UtxoWithSlot[] = [];
6767

@@ -71,8 +71,8 @@ export class MaestroAdapter implements Adapter {
7171

7272
do {
7373
queryParams.cursor = cursor;
74-
const utxos = await this.maestroClient.addresses.utxosByAddress(
75-
address,
74+
const utxos = await this.maestroClient.addresses.utxosByPaymentCred(
75+
credential,
7676
queryParams
7777
);
7878
utxosData.push(...utxos.data);
@@ -375,8 +375,8 @@ export class MaestroAdapter implements Adapter {
375375
errors: unknown[];
376376
}> {
377377
const v2Config = DexV2Constant.CONFIG[this.networkId];
378-
const utxosData = await this.getAllUtxosDataByAddress(
379-
v2Config.factoryAddress,
378+
const utxosData = await this.getAllUtxosDataByPaymentCred(
379+
v2Config.factoryScriptHashBech32,
380380
{
381381
asset: v2Config.factoryAsset,
382382
}
@@ -598,24 +598,24 @@ export class MaestroAdapter implements Adapter {
598598
return Big(priceNum.toString()).div(priceDen.toString());
599599
}
600600

601+
// MARK: LBE V2
601602
public async getAllLbeV2Factories(): Promise<{
602603
factories: LbeV2Types.FactoryState[];
603604
errors: unknown[];
604605
}> {
605606
const config = LbeV2Constant.CONFIG[this.networkId];
606-
const utxos = await this.maestroClient.addresses.utxosByAddress(
607+
const utxosData = await this.getAllUtxosDataByPaymentCred(
607608
config.factoryHashBech32,
608609
{
609610
asset: config.factoryAsset,
610611
}
611-
);
612-
const utxosData = utxos.data;
612+
)
613613

614614
const factories: LbeV2Types.FactoryState[] = [];
615615
const errors: unknown[] = [];
616616
for (const utxo of utxosData) {
617617
try {
618-
if (utxo.datum?.type != "inline") {
618+
if (utxo.datum?.type != "inline" || !utxo.datum?.bytes) {
619619
throw new Error(
620620
`Cannot find datum of LBE V2 Factory, tx: ${utxo.tx_hash}`
621621
);
@@ -626,7 +626,7 @@ export class MaestroAdapter implements Adapter {
626626
utxo.address,
627627
{ txHash: utxo.tx_hash, index: utxo.index },
628628
this.mapMaestroAssetToValue(utxo.assets),
629-
utxo.datum.hash
629+
utxo.datum?.bytes
630630
);
631631
factories.push(factory);
632632
} catch (err) {
@@ -684,19 +684,18 @@ export class MaestroAdapter implements Adapter {
684684
}> {
685685
const config = LbeV2Constant.CONFIG[this.networkId];
686686

687-
const utxos = await this.maestroClient.addresses.utxosByAddress(
687+
const utxosData = await this.getAllUtxosDataByPaymentCred(
688688
config.treasuryHashBech32,
689689
{
690690
asset: config.treasuryAsset,
691691
}
692692
);
693-
const utxosData = utxos.data;
694693

695694
const treasuries: LbeV2Types.TreasuryState[] = [];
696695
const errors: unknown[] = [];
697696
for (const utxo of utxosData) {
698697
try {
699-
if (utxo.datum?.type != "inline") {
698+
if (utxo.datum?.type != "inline" || !utxo.datum?.bytes) {
700699
throw new Error(
701700
`Cannot find datum of LBE V2 Treasury, tx: ${utxo.tx_hash}`
702701
);
@@ -707,7 +706,7 @@ export class MaestroAdapter implements Adapter {
707706
utxo.address,
708707
{ txHash: utxo.tx_hash, index: utxo.index },
709708
this.mapMaestroAssetToValue(utxo.assets),
710-
utxo.datum.hash
709+
utxo.datum?.bytes
711710
);
712711
treasuries.push(treasury);
713712
} catch (err) {
@@ -738,19 +737,18 @@ export class MaestroAdapter implements Adapter {
738737
}> {
739738
const config = LbeV2Constant.CONFIG[this.networkId];
740739

741-
const utxos = await this.maestroClient.addresses.utxosByAddress(
740+
const utxosData = await this.getAllUtxosDataByPaymentCred(
742741
config.managerHashBech32,
743742
{
744743
asset: config.managerAsset,
745744
}
746745
);
747-
const utxosData = utxos.data;
748746

749747
const managers: LbeV2Types.ManagerState[] = [];
750748
const errors: unknown[] = [];
751749
for (const utxo of utxosData) {
752750
try {
753-
if (utxo.datum?.type != "inline") {
751+
if (utxo.datum?.type != "inline" || !utxo.datum?.bytes) {
754752
throw new Error(
755753
`Cannot find datum of Lbe V2 Manager, tx: ${utxo.tx_hash}`
756754
);
@@ -761,7 +759,7 @@ export class MaestroAdapter implements Adapter {
761759
utxo.address,
762760
{ txHash: utxo.tx_hash, index: utxo.index },
763761
this.mapMaestroAssetToValue(utxo.assets),
764-
utxo.datum.hash
762+
utxo.datum?.bytes
765763
);
766764
managers.push(manager);
767765
} catch (err) {
@@ -792,19 +790,18 @@ export class MaestroAdapter implements Adapter {
792790
}> {
793791
const config = LbeV2Constant.CONFIG[this.networkId];
794792

795-
const utxos = await this.maestroClient.addresses.utxosByAddress(
793+
const utxosData = await this.getAllUtxosDataByPaymentCred(
796794
config.sellerHashBech32,
797795
{
798796
asset: config.sellerAsset,
799797
}
800798
);
801-
const utxosData = utxos.data;
802799

803800
const sellers: LbeV2Types.SellerState[] = [];
804801
const errors: unknown[] = [];
805802
for (const utxo of utxosData) {
806803
try {
807-
if (utxo.datum?.type != "inline") {
804+
if (utxo.datum?.type != "inline" || !utxo.datum?.bytes) {
808805
throw new Error(
809806
`Cannot find datum of Lbe V2 Seller, tx: ${utxo.tx_hash}`
810807
);
@@ -815,7 +812,7 @@ export class MaestroAdapter implements Adapter {
815812
utxo.address,
816813
{ txHash: utxo.tx_hash, index: utxo.index },
817814
this.mapMaestroAssetToValue(utxo.assets),
818-
utxo.datum.hash
815+
utxo.datum?.bytes
819816
);
820817
sellers.push(seller);
821818
} catch (err) {
@@ -846,19 +843,18 @@ export class MaestroAdapter implements Adapter {
846843
}> {
847844
const config = LbeV2Constant.CONFIG[this.networkId];
848845

849-
const utxos = await this.maestroClient.addresses.utxosByAddress(
846+
const utxosData = await this.getAllUtxosDataByPaymentCred(
850847
config.orderHashBech32,
851848
{
852849
asset: config.orderAsset,
853850
}
854851
);
855-
const utxosData = utxos.data;
856852

857853
const orders: LbeV2Types.OrderState[] = [];
858854
const errors: unknown[] = [];
859855
for (const utxo of utxosData) {
860856
try {
861-
if (utxo.datum?.type != "inline") {
857+
if (utxo.datum?.type != "inline" || !utxo.datum?.bytes) {
862858
throw new Error(
863859
`Cannot find datum of Lbe V2 Order, tx: ${utxo.tx_hash}`
864860
);
@@ -869,7 +865,7 @@ export class MaestroAdapter implements Adapter {
869865
utxo.address,
870866
{ txHash: utxo.tx_hash, index: utxo.index },
871867
this.mapMaestroAssetToValue(utxo.assets),
872-
utxo.datum.hash
868+
utxo.datum?.bytes
873869
);
874870
orders.push(order);
875871
} catch (err) {

0 commit comments

Comments
 (0)