@indigo-labs/indigo-sdk 0.1.2 → 0.1.3
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/.github/workflows/ci.yml +62 -0
- package/.github/workflows/test.yml +44 -0
- package/.husky/pre-commit +1 -0
- package/.prettierrc +1 -1
- package/README.md +52 -7
- package/dist/index.d.mts +1064 -182
- package/dist/index.d.ts +1064 -182
- package/dist/index.js +2565 -561
- package/dist/index.mjs +2546 -593
- package/eslint.config.mjs +35 -0
- package/package.json +61 -43
- package/src/contracts/cdp.ts +208 -359
- package/src/contracts/collector.ts +13 -6
- package/src/contracts/gov.ts +1 -58
- package/src/contracts/interest-oracle.ts +139 -39
- package/src/contracts/lrp.ts +223 -0
- package/src/contracts/one-shot.ts +67 -0
- package/src/contracts/stability-pool.ts +684 -0
- package/src/contracts/staking.ts +348 -0
- package/src/contracts/treasury.ts +30 -13
- package/src/helpers/asset-helpers.ts +51 -22
- package/src/helpers/helper-txs.ts +30 -0
- package/src/helpers/helpers.ts +29 -8
- package/src/helpers/indigo-helpers.ts +19 -0
- package/src/helpers/interest-oracle.ts +57 -0
- package/src/helpers/lucid-utils.ts +62 -16
- package/src/helpers/price-oracle-helpers.ts +36 -0
- package/src/helpers/stability-pool-helpers.ts +207 -0
- package/src/helpers/staking-helpers.ts +94 -0
- package/src/helpers/time-helpers.ts +4 -3
- package/src/helpers/value-helpers.ts +16 -0
- package/src/index.ts +15 -4
- package/src/scripts/always-fail-validator.ts +7 -0
- package/src/scripts/auth-token-policy.ts +23 -0
- package/src/scripts/cdp-creator-validator.ts +46 -2
- package/src/scripts/collector-validator.ts +2 -2
- package/src/scripts/execute-validator.ts +52 -0
- package/src/scripts/gov-validator.ts +44 -0
- package/src/scripts/iasset-policy.ts +22 -0
- package/src/scripts/interest-oracle-validator.ts +18 -3
- package/src/scripts/lrp-validator.ts +23 -0
- package/src/scripts/one-shot-policy.ts +62 -0
- package/src/scripts/poll-manager-validator.ts +52 -0
- package/src/scripts/poll-shard-validator.ts +47 -0
- package/src/scripts/price-oracle-validator.ts +26 -0
- package/src/scripts/stability-pool-validator.ts +60 -0
- package/src/scripts/staking-validator.ts +8 -0
- package/src/scripts/version-record-policy.ts +26 -0
- package/src/scripts/version-registry.ts +15 -0
- package/src/types/generic.ts +99 -6
- package/src/types/indigo/cdp-creator.ts +46 -0
- package/src/types/indigo/cdp.ts +86 -31
- package/src/types/indigo/execute.ts +21 -0
- package/src/types/indigo/gov.ts +50 -20
- package/src/types/indigo/interest-oracle.ts +55 -5
- package/src/types/indigo/lrp.ts +54 -0
- package/src/types/indigo/poll-manager.ts +21 -0
- package/src/types/indigo/poll-shard.ts +16 -0
- package/src/types/indigo/price-oracle.ts +38 -4
- package/src/types/indigo/stability-pool.ts +233 -0
- package/src/types/indigo/staking.ts +99 -0
- package/src/types/indigo/version-record.ts +17 -0
- package/src/types/on-chain-decimal.ts +23 -0
- package/src/types/one-shot.ts +22 -0
- package/src/types/system-params.ts +95 -77
- package/tests/data/system-params.json +972 -972
- package/tests/datums.test.ts +279 -44
- package/tests/endpoints/initialize.ts +1013 -0
- package/tests/hash-checks.test.ts +71 -15
- package/tests/indigo-test-helpers.ts +115 -0
- package/tests/initialize.test.ts +27 -0
- package/tests/interest-calculations.test.ts +110 -133
- package/tests/interest-oracle.test.ts +90 -0
- package/tests/lrp.test.ts +149 -0
- package/tests/queries/governance-queries.ts +31 -0
- package/tests/queries/iasset-queries.ts +39 -0
- package/tests/queries/interest-oracle-queries.ts +13 -0
- package/tests/queries/lrp-queries.ts +39 -0
- package/tests/queries/price-oracle-queries.ts +27 -0
- package/tests/queries/stability-pool-queries.ts +75 -0
- package/tests/queries/staking-queries.ts +43 -0
- package/tests/stability-pool.test.ts +364 -0
- package/tests/staking.test.ts +105 -0
- package/tests/test-helpers.ts +38 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +8 -27
- package/vitest.config.ts +9 -0
- package/babel.config.cjs +0 -13
- package/examples/sample-cdp.ts +0 -43
- package/jest.config.js +0 -13
- package/src/contracts/cdp-creator.ts +0 -86
- package/src/contracts/price-oracle.ts +0 -24
- package/src/helpers/cdp-helpers.ts +0 -9
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { describe, it } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
Emulator,
|
|
4
|
+
fromText,
|
|
5
|
+
generateEmulatorAccount,
|
|
6
|
+
Lucid,
|
|
7
|
+
Network,
|
|
8
|
+
validatorToScriptHash,
|
|
9
|
+
} from '@lucid-evolution/lucid';
|
|
10
|
+
import { LRPParams } from '../src/types/indigo/lrp';
|
|
11
|
+
import { mkLrpValidator } from '../src/scripts/lrp-validator';
|
|
12
|
+
import { runCreateScriptRefTx } from '../src/helpers/helper-txs';
|
|
13
|
+
import { runOneShotMintTx } from '../src/contracts/one-shot';
|
|
14
|
+
import { cancelLrp, openLrp } from '../src/contracts/lrp';
|
|
15
|
+
import { findLrp } from './queries/lrp-queries';
|
|
16
|
+
import { addrDetails } from '../src/helpers/lucid-utils';
|
|
17
|
+
import { runAndAwaitTx } from './test-helpers';
|
|
18
|
+
import { matchSingle } from '../src/helpers/helpers';
|
|
19
|
+
import { runCreateIAsset, runStartPriceOracle } from './indigo-test-helpers';
|
|
20
|
+
import { mkPriceOracleValidator } from '../src/scripts/price-oracle-validator';
|
|
21
|
+
import { AssetClass, PriceOracleParams } from '../src';
|
|
22
|
+
import { alwaysFailValidator } from '../src/scripts/always-fail-validator';
|
|
23
|
+
import { OCD_ONE, OCD_ZERO } from '../src/types/on-chain-decimal';
|
|
24
|
+
|
|
25
|
+
describe('LRP', () => {
|
|
26
|
+
it('case 1', async () => {
|
|
27
|
+
const network: Network = 'Custom';
|
|
28
|
+
const account1 = generateEmulatorAccount({
|
|
29
|
+
lovelace: 80_000_000_000n, // 80,000 ADA
|
|
30
|
+
});
|
|
31
|
+
const emulator = new Emulator([account1]);
|
|
32
|
+
|
|
33
|
+
const lucid = await Lucid(emulator, network);
|
|
34
|
+
lucid.selectWallet.fromSeed(account1.seedPhrase);
|
|
35
|
+
|
|
36
|
+
const iassetTokenName = fromText('TEST_IBTC');
|
|
37
|
+
const utxos = await lucid.wallet().getUtxos();
|
|
38
|
+
const iassetPolicyId = await runOneShotMintTx(lucid, {
|
|
39
|
+
referenceOutRef: {
|
|
40
|
+
txHash: utxos[0].txHash,
|
|
41
|
+
outputIdx: BigInt(utxos[0].outputIndex),
|
|
42
|
+
},
|
|
43
|
+
mintAmounts: [{ tokenName: iassetTokenName, amount: 10_000_000n }],
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const [ownPkh, _] = await addrDetails(lucid);
|
|
47
|
+
|
|
48
|
+
const priceOracleParams: PriceOracleParams = {
|
|
49
|
+
owner: ownPkh.hash,
|
|
50
|
+
// 1 minute
|
|
51
|
+
biasTime: 1n * 60n * 1000n,
|
|
52
|
+
// 10 minutes
|
|
53
|
+
expiration: 10n * 60n * 1000n,
|
|
54
|
+
};
|
|
55
|
+
const oracleValidator = mkPriceOracleValidator(priceOracleParams);
|
|
56
|
+
const oracleValidatorHash = validatorToScriptHash(oracleValidator);
|
|
57
|
+
const oracleNft = await runStartPriceOracle(
|
|
58
|
+
lucid,
|
|
59
|
+
oracleValidatorHash,
|
|
60
|
+
priceOracleParams,
|
|
61
|
+
network,
|
|
62
|
+
fromText('ORACLE_IBTC'),
|
|
63
|
+
OCD_ONE,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const iassetValHash = validatorToScriptHash(alwaysFailValidator);
|
|
67
|
+
const dummyAc: AssetClass = { currencySymbol: '', tokenName: '' };
|
|
68
|
+
const iassetNft = await runCreateIAsset(lucid, network, iassetValHash, {
|
|
69
|
+
assetName: iassetTokenName,
|
|
70
|
+
price: { Oracle: oracleNft },
|
|
71
|
+
interestOracleNft: dummyAc,
|
|
72
|
+
redemptionRatio: OCD_ONE,
|
|
73
|
+
maintenanceRatio: OCD_ONE,
|
|
74
|
+
liquidationRatio: OCD_ONE,
|
|
75
|
+
debtMintingFeePercentage: OCD_ZERO,
|
|
76
|
+
liquidationProcessingFeePercentage: OCD_ZERO,
|
|
77
|
+
stabilityPoolWithdrawalFeePercentage: OCD_ZERO,
|
|
78
|
+
redemptionReimbursementPercentage: OCD_ONE,
|
|
79
|
+
redemptionProcessingFeePercentage: OCD_ZERO,
|
|
80
|
+
interestCollectorPortionPercentage: OCD_ZERO,
|
|
81
|
+
firstIAsset: true,
|
|
82
|
+
nextIAsset: null,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const lrpParams: LRPParams = {
|
|
86
|
+
versionRecordToken: {
|
|
87
|
+
currencySymbol: fromText('smth'),
|
|
88
|
+
tokenName: fromText('version_record'),
|
|
89
|
+
},
|
|
90
|
+
iassetNft: iassetNft,
|
|
91
|
+
iassetPolicyId: iassetPolicyId,
|
|
92
|
+
minRedemptionLovelacesAmt: 1_000_000n,
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const lrpValidator = mkLrpValidator(lrpParams);
|
|
96
|
+
const lrpValidatorHash = validatorToScriptHash(lrpValidator);
|
|
97
|
+
|
|
98
|
+
const lrpRefScriptOutRef = await runCreateScriptRefTx(
|
|
99
|
+
lucid,
|
|
100
|
+
lrpValidator,
|
|
101
|
+
network,
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
await runAndAwaitTx(
|
|
105
|
+
lucid,
|
|
106
|
+
openLrp(
|
|
107
|
+
iassetTokenName,
|
|
108
|
+
20_000_000n,
|
|
109
|
+
{ getOnChainInt: 1_000_000n },
|
|
110
|
+
lucid,
|
|
111
|
+
lrpValidatorHash,
|
|
112
|
+
network,
|
|
113
|
+
),
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const lrpOutRef = matchSingle(
|
|
117
|
+
await findLrp(
|
|
118
|
+
lucid,
|
|
119
|
+
network,
|
|
120
|
+
lrpValidatorHash,
|
|
121
|
+
ownPkh.hash,
|
|
122
|
+
iassetTokenName,
|
|
123
|
+
),
|
|
124
|
+
(res) => new Error('Expected a single LRP UTXO.: ' + JSON.stringify(res)),
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
await runAndAwaitTx(lucid, cancelLrp(lrpOutRef, lrpRefScriptOutRef, lucid));
|
|
128
|
+
|
|
129
|
+
// await runAndAwaitTx(
|
|
130
|
+
// lucid,
|
|
131
|
+
// redeemLrp(
|
|
132
|
+
// [[lrpOutRef, 5_000_000n]],
|
|
133
|
+
// lrpRefScriptOutRef,
|
|
134
|
+
// await findPriceOracle(lucid, network, oracleValidatorHash, oracleNft),
|
|
135
|
+
// await findIAsset(
|
|
136
|
+
// lucid,
|
|
137
|
+
// network,
|
|
138
|
+
// iassetValHash,
|
|
139
|
+
// iassetNft,
|
|
140
|
+
// iassetTokenName,
|
|
141
|
+
// ),
|
|
142
|
+
// lucid,
|
|
143
|
+
// lrpParams,
|
|
144
|
+
// priceOracleParams,
|
|
145
|
+
// network,
|
|
146
|
+
// ),
|
|
147
|
+
// );
|
|
148
|
+
});
|
|
149
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { LucidEvolution, ScriptHash, UTxO } from '@lucid-evolution/lucid';
|
|
2
|
+
import { createScriptAddress } from '../../src/helpers/lucid-utils';
|
|
3
|
+
import { AssetClass } from '../../src/types/generic';
|
|
4
|
+
import { assetClassToUnit } from '../../src/helpers/value-helpers';
|
|
5
|
+
import { matchSingle, parseGovDatum } from '../../src';
|
|
6
|
+
|
|
7
|
+
export async function findGov(
|
|
8
|
+
lucid: LucidEvolution,
|
|
9
|
+
govScriptHash: ScriptHash,
|
|
10
|
+
govNft: AssetClass,
|
|
11
|
+
): Promise<UTxO> {
|
|
12
|
+
const govUtxos = await lucid.utxosAtWithUnit(
|
|
13
|
+
createScriptAddress(lucid.config().network, govScriptHash),
|
|
14
|
+
assetClassToUnit(govNft),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
return matchSingle(
|
|
18
|
+
govUtxos.filter((utxo) => {
|
|
19
|
+
if (utxo.datum != null) {
|
|
20
|
+
try {
|
|
21
|
+
parseGovDatum(utxo.datum);
|
|
22
|
+
return true; // TODO: implement Gov Datum
|
|
23
|
+
} catch (_) {
|
|
24
|
+
// when incompatible datum
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}),
|
|
29
|
+
(res) => new Error('Expected a single Gov UTXO.: ' + JSON.stringify(res)),
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromText,
|
|
3
|
+
LucidEvolution,
|
|
4
|
+
ScriptHash,
|
|
5
|
+
UTxO,
|
|
6
|
+
} from '@lucid-evolution/lucid';
|
|
7
|
+
import { createScriptAddress } from '../../src/helpers/lucid-utils';
|
|
8
|
+
import { AssetClass } from '../../src/types/generic';
|
|
9
|
+
import { assetClassToUnit } from '../../src/helpers/value-helpers';
|
|
10
|
+
import { matchSingle, parseIAssetDatum } from '../../src';
|
|
11
|
+
|
|
12
|
+
export async function findIAsset(
|
|
13
|
+
lucid: LucidEvolution,
|
|
14
|
+
iassetScriptHash: ScriptHash,
|
|
15
|
+
iassetNft: AssetClass,
|
|
16
|
+
iassetName: string,
|
|
17
|
+
): Promise<UTxO> {
|
|
18
|
+
const iassetUtxos = await lucid.utxosAtWithUnit(
|
|
19
|
+
createScriptAddress(lucid.config().network, iassetScriptHash),
|
|
20
|
+
assetClassToUnit(iassetNft),
|
|
21
|
+
);
|
|
22
|
+
|
|
23
|
+
return matchSingle(
|
|
24
|
+
iassetUtxos.filter((utxo) => {
|
|
25
|
+
if (utxo.datum != null) {
|
|
26
|
+
try {
|
|
27
|
+
const iassetDatum = parseIAssetDatum(utxo.datum);
|
|
28
|
+
|
|
29
|
+
return iassetDatum.assetName == fromText(iassetName);
|
|
30
|
+
} catch (_) {
|
|
31
|
+
// when incompatible datum
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}),
|
|
36
|
+
(res) =>
|
|
37
|
+
new Error('Expected a single IAsset UTXO.: ' + JSON.stringify(res)),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LucidEvolution, UTxO } from '@lucid-evolution/lucid';
|
|
2
|
+
import { AssetClass } from '../../src/types/generic';
|
|
3
|
+
import { assetClassToUnit } from '../../src/helpers/value-helpers';
|
|
4
|
+
import { InterestOracleDatum, parseInterestOracleDatum } from '../../src';
|
|
5
|
+
|
|
6
|
+
export async function findInterestOracle(
|
|
7
|
+
lucid: LucidEvolution,
|
|
8
|
+
interestNft: AssetClass,
|
|
9
|
+
): Promise<[UTxO, InterestOracleDatum]> {
|
|
10
|
+
const interestUtxo = await lucid.utxoByUnit(assetClassToUnit(interestNft));
|
|
11
|
+
if (!interestUtxo.datum) throw new Error('No interest oracle utxo found');
|
|
12
|
+
return [interestUtxo, parseInterestOracleDatum(interestUtxo.datum)];
|
|
13
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Credential,
|
|
3
|
+
LucidEvolution,
|
|
4
|
+
Network,
|
|
5
|
+
OutRef,
|
|
6
|
+
ScriptHash,
|
|
7
|
+
} from '@lucid-evolution/lucid';
|
|
8
|
+
import { createScriptAddress } from '../../src/helpers/lucid-utils';
|
|
9
|
+
import { parseLrpDatum } from '../../src/types/indigo/lrp';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Beware, this shouldn't be used in production since it queries all the UTXOs
|
|
13
|
+
* at an address and does the filtering in this function.
|
|
14
|
+
*/
|
|
15
|
+
export async function findLrp(
|
|
16
|
+
lucid: LucidEvolution,
|
|
17
|
+
network: Network,
|
|
18
|
+
lrpScriptHash: ScriptHash,
|
|
19
|
+
owner: string,
|
|
20
|
+
assetTokenName: string,
|
|
21
|
+
stakeCredential?: Credential,
|
|
22
|
+
): Promise<OutRef[]> {
|
|
23
|
+
const lrpUtxos = await lucid.utxosAt(
|
|
24
|
+
createScriptAddress(network, lrpScriptHash, stakeCredential),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
return lrpUtxos.filter((utxo) => {
|
|
28
|
+
if (utxo.datum != null) {
|
|
29
|
+
try {
|
|
30
|
+
const lrpDatum = parseLrpDatum(utxo.datum);
|
|
31
|
+
|
|
32
|
+
return lrpDatum.owner == owner && lrpDatum.iasset == assetTokenName;
|
|
33
|
+
} catch (_) {
|
|
34
|
+
// when incompatible datum
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LucidEvolution,
|
|
3
|
+
Network,
|
|
4
|
+
OutRef,
|
|
5
|
+
ScriptHash,
|
|
6
|
+
toUnit,
|
|
7
|
+
} from '@lucid-evolution/lucid';
|
|
8
|
+
import { createScriptAddress, matchSingle, OracleAssetNft } from '../../src';
|
|
9
|
+
|
|
10
|
+
export async function findPriceOracle(
|
|
11
|
+
lucid: LucidEvolution,
|
|
12
|
+
network: Network,
|
|
13
|
+
oracleScriptHash: ScriptHash,
|
|
14
|
+
oracleNft: OracleAssetNft,
|
|
15
|
+
): Promise<OutRef> {
|
|
16
|
+
return matchSingle(
|
|
17
|
+
await lucid.utxosAtWithUnit(
|
|
18
|
+
createScriptAddress(network, oracleScriptHash),
|
|
19
|
+
toUnit(
|
|
20
|
+
oracleNft.oracleNft.asset.currencySymbol,
|
|
21
|
+
oracleNft.oracleNft.asset.tokenName,
|
|
22
|
+
),
|
|
23
|
+
),
|
|
24
|
+
(res) =>
|
|
25
|
+
new Error('Expected a single Oracle UTXO.: ' + JSON.stringify(res)),
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fromText,
|
|
3
|
+
LucidEvolution,
|
|
4
|
+
ScriptHash,
|
|
5
|
+
UTxO,
|
|
6
|
+
} from '@lucid-evolution/lucid';
|
|
7
|
+
import { createScriptAddress } from '../../src/helpers/lucid-utils';
|
|
8
|
+
import { AssetClass } from '../../src/types/generic';
|
|
9
|
+
import { assetClassToUnit } from '../../src/helpers/value-helpers';
|
|
10
|
+
import {
|
|
11
|
+
matchSingle,
|
|
12
|
+
parseAccountDatum,
|
|
13
|
+
parseStabilityPoolDatum,
|
|
14
|
+
} from '../../src';
|
|
15
|
+
|
|
16
|
+
export async function findStabilityPool(
|
|
17
|
+
lucid: LucidEvolution,
|
|
18
|
+
stabilityPoolHash: ScriptHash,
|
|
19
|
+
stabilityPoolToken: AssetClass,
|
|
20
|
+
asset: string,
|
|
21
|
+
): Promise<UTxO> {
|
|
22
|
+
const stakingUtxos = await lucid.utxosAtWithUnit(
|
|
23
|
+
createScriptAddress(lucid.config().network, stabilityPoolHash),
|
|
24
|
+
assetClassToUnit(stabilityPoolToken),
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
return matchSingle(
|
|
28
|
+
stakingUtxos.filter((utxo) => {
|
|
29
|
+
if (utxo.datum != null) {
|
|
30
|
+
try {
|
|
31
|
+
const stabilityPoolDatum = parseStabilityPoolDatum(utxo.datum);
|
|
32
|
+
|
|
33
|
+
return stabilityPoolDatum.asset == fromText(asset);
|
|
34
|
+
} catch (_) {
|
|
35
|
+
// when incompatible datum
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}),
|
|
40
|
+
(res) =>
|
|
41
|
+
new Error(
|
|
42
|
+
'Expected a single Stability Pool UTXO.: ' + JSON.stringify(res),
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export async function findStabilityPoolAccount(
|
|
48
|
+
lucid: LucidEvolution,
|
|
49
|
+
stabilityPoolHash: ScriptHash,
|
|
50
|
+
owner: string,
|
|
51
|
+
asset: string,
|
|
52
|
+
): Promise<UTxO> {
|
|
53
|
+
const accountUtxos = await lucid.utxosAt(
|
|
54
|
+
createScriptAddress(lucid.config().network, stabilityPoolHash),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
return matchSingle(
|
|
58
|
+
accountUtxos.filter((utxo) => {
|
|
59
|
+
if (utxo.datum != null) {
|
|
60
|
+
try {
|
|
61
|
+
const accountDatum = parseAccountDatum(utxo.datum);
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
accountDatum.asset == fromText(asset) && accountDatum.owner == owner
|
|
65
|
+
);
|
|
66
|
+
} catch (_) {
|
|
67
|
+
// when incompatible datum
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}),
|
|
72
|
+
(res) =>
|
|
73
|
+
new Error('Expected a single Account UTXO.: ' + JSON.stringify(res)),
|
|
74
|
+
);
|
|
75
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LucidEvolution,
|
|
3
|
+
Network,
|
|
4
|
+
OutRef,
|
|
5
|
+
ScriptHash,
|
|
6
|
+
} from '@lucid-evolution/lucid';
|
|
7
|
+
import { createScriptAddress } from '../../src/helpers/lucid-utils';
|
|
8
|
+
import { AssetClass } from '../../src/types/generic';
|
|
9
|
+
import { assetClassToUnit } from '../../src/helpers/value-helpers';
|
|
10
|
+
import { matchSingle } from '../../src';
|
|
11
|
+
import { parseStakingPositionDatum } from '../../src/types/indigo/staking';
|
|
12
|
+
|
|
13
|
+
export async function findStakingPosition(
|
|
14
|
+
lucid: LucidEvolution,
|
|
15
|
+
network: Network,
|
|
16
|
+
stakingScriptHash: ScriptHash,
|
|
17
|
+
stakingPositionNft: AssetClass,
|
|
18
|
+
owner: string,
|
|
19
|
+
): Promise<OutRef> {
|
|
20
|
+
const stakingUtxos = await lucid.utxosAtWithUnit(
|
|
21
|
+
createScriptAddress(network, stakingScriptHash),
|
|
22
|
+
assetClassToUnit(stakingPositionNft),
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
return matchSingle(
|
|
26
|
+
stakingUtxos.filter((utxo) => {
|
|
27
|
+
if (utxo.datum != null) {
|
|
28
|
+
try {
|
|
29
|
+
const stakingDatum = parseStakingPositionDatum(utxo.datum);
|
|
30
|
+
|
|
31
|
+
return stakingDatum.owner == owner;
|
|
32
|
+
} catch (_) {
|
|
33
|
+
// when incompatible datum
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}),
|
|
38
|
+
(res) =>
|
|
39
|
+
new Error(
|
|
40
|
+
'Expected a single Staking Position UTXO.: ' + JSON.stringify(res),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
}
|