Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
94 changes: 94 additions & 0 deletions build/constants.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export declare enum MetadataKey {
Message = 674
}
export declare enum DatumParameterKey {
/**
* Generics.
*/
Action = "Action",
TokenPolicyId = "TokenPolicyId",
TokenAssetName = "TokenAssetName",
ReserveA = "ReserveA",
ReserveB = "ReserveB",
CancelDatum = "CancelDatum",
AScale = "AScale",
BScale = "BScale",
/**
* Swap/wallet info.
*/
Address = "Address",
SenderPubKeyHash = "SenderPubKeyHash",
SenderStakingKeyHash = "SenderStakingKeyHash",
SenderKeyHashes = "SenderKeyHashes",
ReceiverPubKeyHash = "ReceiverPubKeyHash",
ReceiverStakingKeyHash = "ReceiverStakingKeyHash",
SwapInAmount = "SwapInAmount",
SwapInTokenPolicyId = "SwapInTokenPolicyId",
SwapInTokenAssetName = "SwapInTokenAssetName",
SwapOutTokenPolicyId = "SwapOutTokenPolicyId",
SwapOutTokenAssetName = "SwapOutTokenAssetName",
MinReceive = "MinReceive",
Expiration = "Expiration",
AllowPartialFill = "AllowPartialFill",
Direction = "Direction",
FeePaymentKeyHash = "FeePaymentKeyHash",
Beacon = "Beacon",
Batcher = "Batcher",
InToken = "InToken",
/**
* Trading fees.
*/
TotalFees = "TotalFees",
BatcherFee = "BatcherFee",
DepositFee = "DepositFee",
ScooperFee = "ScooperFee",
BaseFee = "BaseFee",
ExecutionFee = "ExecutionFee",
FeeSharingNumerator = "FeeSharingNumerator",
OpeningFee = "OpeningFee",
FinalFee = "FinalFee",
FeesFinalized = "FeesFinalized",
MarketOpen = "MarketOpen",
ProtocolFee = "ProtocolFee",
SwapFee = "SwapFee",
ProjectFeeInBasis = "ProjectFeeInBasis",
ReserveFeeInBasis = "ReserveFeeInBasis",
FeeBasis = "FeeBasis",
AgentFee = "AgentFee",
/**
* LP info.
*/
PoolIdentifier = "PoolIdentifier",
TotalLpTokens = "TotalLpTokens",
LpTokenPolicyId = "LpTokenPolicyId",
LpTokenAssetName = "LpTokenAssetName",
LpFee = "LpFee",
LpFeeNumerator = "LpFeeNumerator",
LpFeeDenominator = "LpFeeDenominator",
PoolAssetAPolicyId = "PoolAssetAPolicyId",
PoolAssetAAssetName = "PoolAssetAAssetName",
PoolAssetATreasury = "PoolAssetATreasury",
PoolAssetABarFee = "PoolAssetABarFee",
PoolAssetBPolicyId = "PoolAssetBPolicyId",
PoolAssetBAssetName = "PoolAssetBAssetName",
PoolAssetBTreasury = "PoolAssetBTreasury",
PoolAssetBBarFee = "PoolAssetBBarFee",
RootKLast = "RootKLast",
LastInteraction = "LastInteraction",
RequestScriptHash = "RequestScriptHash",
StakeAdminPolicy = "StakeAdminPolicy",
LqBound = "LqBound",
Unknown = "Unknown"
}
export declare enum TransactionStatus {
Building = 0,
Signing = 1,
Submitting = 2,
Submitted = 3,
Errored = 4
}
export declare enum AddressType {
Contract = 0,
Base = 1,
Enterprise = 2
}
98 changes: 98 additions & 0 deletions build/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
export var MetadataKey;
(function (MetadataKey) {
MetadataKey[MetadataKey["Message"] = 674] = "Message";
})(MetadataKey || (MetadataKey = {}));
export var DatumParameterKey;
(function (DatumParameterKey) {
/**
* Generics.
*/
DatumParameterKey["Action"] = "Action";
DatumParameterKey["TokenPolicyId"] = "TokenPolicyId";
DatumParameterKey["TokenAssetName"] = "TokenAssetName";
DatumParameterKey["ReserveA"] = "ReserveA";
DatumParameterKey["ReserveB"] = "ReserveB";
DatumParameterKey["CancelDatum"] = "CancelDatum";
DatumParameterKey["AScale"] = "AScale";
DatumParameterKey["BScale"] = "BScale";
/**
* Swap/wallet info.
*/
DatumParameterKey["Address"] = "Address";
DatumParameterKey["SenderPubKeyHash"] = "SenderPubKeyHash";
DatumParameterKey["SenderStakingKeyHash"] = "SenderStakingKeyHash";
DatumParameterKey["SenderKeyHashes"] = "SenderKeyHashes";
DatumParameterKey["ReceiverPubKeyHash"] = "ReceiverPubKeyHash";
DatumParameterKey["ReceiverStakingKeyHash"] = "ReceiverStakingKeyHash";
DatumParameterKey["SwapInAmount"] = "SwapInAmount";
DatumParameterKey["SwapInTokenPolicyId"] = "SwapInTokenPolicyId";
DatumParameterKey["SwapInTokenAssetName"] = "SwapInTokenAssetName";
DatumParameterKey["SwapOutTokenPolicyId"] = "SwapOutTokenPolicyId";
DatumParameterKey["SwapOutTokenAssetName"] = "SwapOutTokenAssetName";
DatumParameterKey["MinReceive"] = "MinReceive";
DatumParameterKey["Expiration"] = "Expiration";
DatumParameterKey["AllowPartialFill"] = "AllowPartialFill";
DatumParameterKey["Direction"] = "Direction";
DatumParameterKey["FeePaymentKeyHash"] = "FeePaymentKeyHash";
DatumParameterKey["Beacon"] = "Beacon";
DatumParameterKey["Batcher"] = "Batcher";
DatumParameterKey["InToken"] = "InToken";
/**
* Trading fees.
*/
DatumParameterKey["TotalFees"] = "TotalFees";
DatumParameterKey["BatcherFee"] = "BatcherFee";
DatumParameterKey["DepositFee"] = "DepositFee";
DatumParameterKey["ScooperFee"] = "ScooperFee";
DatumParameterKey["BaseFee"] = "BaseFee";
DatumParameterKey["ExecutionFee"] = "ExecutionFee";
DatumParameterKey["FeeSharingNumerator"] = "FeeSharingNumerator";
DatumParameterKey["OpeningFee"] = "OpeningFee";
DatumParameterKey["FinalFee"] = "FinalFee";
DatumParameterKey["FeesFinalized"] = "FeesFinalized";
DatumParameterKey["MarketOpen"] = "MarketOpen";
DatumParameterKey["ProtocolFee"] = "ProtocolFee";
DatumParameterKey["SwapFee"] = "SwapFee";
DatumParameterKey["ProjectFeeInBasis"] = "ProjectFeeInBasis";
DatumParameterKey["ReserveFeeInBasis"] = "ReserveFeeInBasis";
DatumParameterKey["FeeBasis"] = "FeeBasis";
DatumParameterKey["AgentFee"] = "AgentFee";
/**
* LP info.
*/
DatumParameterKey["PoolIdentifier"] = "PoolIdentifier";
DatumParameterKey["TotalLpTokens"] = "TotalLpTokens";
DatumParameterKey["LpTokenPolicyId"] = "LpTokenPolicyId";
DatumParameterKey["LpTokenAssetName"] = "LpTokenAssetName";
DatumParameterKey["LpFee"] = "LpFee";
DatumParameterKey["LpFeeNumerator"] = "LpFeeNumerator";
DatumParameterKey["LpFeeDenominator"] = "LpFeeDenominator";
DatumParameterKey["PoolAssetAPolicyId"] = "PoolAssetAPolicyId";
DatumParameterKey["PoolAssetAAssetName"] = "PoolAssetAAssetName";
DatumParameterKey["PoolAssetATreasury"] = "PoolAssetATreasury";
DatumParameterKey["PoolAssetABarFee"] = "PoolAssetABarFee";
DatumParameterKey["PoolAssetBPolicyId"] = "PoolAssetBPolicyId";
DatumParameterKey["PoolAssetBAssetName"] = "PoolAssetBAssetName";
DatumParameterKey["PoolAssetBTreasury"] = "PoolAssetBTreasury";
DatumParameterKey["PoolAssetBBarFee"] = "PoolAssetBBarFee";
DatumParameterKey["RootKLast"] = "RootKLast";
DatumParameterKey["LastInteraction"] = "LastInteraction";
DatumParameterKey["RequestScriptHash"] = "RequestScriptHash";
DatumParameterKey["StakeAdminPolicy"] = "StakeAdminPolicy";
DatumParameterKey["LqBound"] = "LqBound";
DatumParameterKey["Unknown"] = "Unknown";
})(DatumParameterKey || (DatumParameterKey = {}));
export var TransactionStatus;
(function (TransactionStatus) {
TransactionStatus[TransactionStatus["Building"] = 0] = "Building";
TransactionStatus[TransactionStatus["Signing"] = 1] = "Signing";
TransactionStatus[TransactionStatus["Submitting"] = 2] = "Submitting";
TransactionStatus[TransactionStatus["Submitted"] = 3] = "Submitted";
TransactionStatus[TransactionStatus["Errored"] = 4] = "Errored";
})(TransactionStatus || (TransactionStatus = {}));
export var AddressType;
(function (AddressType) {
AddressType[AddressType["Contract"] = 0] = "Contract";
AddressType[AddressType["Base"] = 1] = "Base";
AddressType[AddressType["Enterprise"] = 2] = "Enterprise";
})(AddressType || (AddressType = {}));
28 changes: 28 additions & 0 deletions build/definition-builder.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { DatumParameters, DefinitionConstr } from './types.js';
export declare class DefinitionBuilder {
private _definition;
/**
* Load a DEX definition file as a template for this builder.
*/
loadDefinition(definition: DefinitionConstr): Promise<DefinitionBuilder>;
/**
* Push specified parameters to the definition template.
*/
pushParameters(parameters: DatumParameters): DefinitionBuilder;
/**
* Pull parameters of a datum using a definition template.
*/
pullParameters(definedDefinition: DefinitionConstr): DatumParameters;
/**
* Retrieve the CBOR for the builder.
*/
getCbor(): string;
/**
* Recursively set specified parameters.
*/
private applyParameters;
/**
* Recursively pull parameters from datum using definition template.
*/
private extractParameters;
}
163 changes: 163 additions & 0 deletions build/definition-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import { DatumParameterKey } from './constants.js';
import _ from 'lodash';
import { datumJsonToCbor } from './utils.js';
export class DefinitionBuilder {
/**
* Load a DEX definition file as a template for this builder.
*/
async loadDefinition(definition) {
this._definition = _.cloneDeepWith(definition, (value) => {
if (value instanceof Function) {
return value;
}
});
return this;
}
/**
* Push specified parameters to the definition template.
*/
pushParameters(parameters) {
if (!this._definition) {
throw new Error(`Definition file must be loaded before applying parameters`);
}
this._definition = this.applyParameters(this._definition, parameters);
return this;
}
/**
* Pull parameters of a datum using a definition template.
*/
pullParameters(definedDefinition) {
if (!this._definition) {
throw new Error(`Definition file must be loaded before pulling parameters`);
}
return this.extractParameters(definedDefinition, this._definition);
}
/**
* Retrieve the CBOR for the builder.
*/
getCbor() {
return datumJsonToCbor(JSON.parse(JSON.stringify(this._definition)));
}
/**
* Recursively set specified parameters.
*/
applyParameters(field, mappedParameters) {
if (field instanceof Function) {
return field(field, mappedParameters, false);
}
if ('fields' in field) {
if (typeof field.constructor === 'string') {
const parameterValue = mappedParameters[field.constructor];
if (typeof parameterValue !== 'number') {
throw new Error(`Invalid parameter value '${parameterValue}' for constructor value`);
}
field.constructor = parameterValue;
}
field.fields = field.fields.map((fieldParameter) => {
return this.applyParameters(fieldParameter, mappedParameters);
});
}
if ('list' in field) {
field.list = field.list?.map((fieldParameter) => {
return this.applyParameters(fieldParameter, mappedParameters);
});
}
if ('int' in field) {
let parameterValue = mappedParameters[field.int];
if (typeof parameterValue === 'bigint') {
parameterValue = Number(parameterValue);
}
if (typeof parameterValue !== 'number') {
throw new Error(`Invalid parameter value '${parameterValue}' for type 'int'`);
}
field.int = parameterValue;
}
if ('bytes' in field) {
const parameterValue = mappedParameters[field.bytes] ?? '';
if (typeof parameterValue !== 'string') {
throw new Error(`Invalid parameter value '${parameterValue}' for type 'bytes'`);
}
field.bytes = parameterValue;
}
if (Array.isArray(field) && field.every((item) => typeof item === 'object' && Object.keys(item).length === 1)) {
field.forEach((value) => {
return this.applyParameters(value, mappedParameters);
});
}
return field;
}
/**
* Recursively pull parameters from datum using definition template.
*/
extractParameters(definedDefinition, templateDefinition, foundParameters = {}) {
if (!templateDefinition)
return foundParameters;
if (templateDefinition instanceof Function) {
templateDefinition(definedDefinition, foundParameters);
return foundParameters;
}
if (templateDefinition instanceof Array) {
templateDefinition
.map((fieldParameter, index) => {
return this.extractParameters(fieldParameter, templateDefinition[index], foundParameters);
})
.forEach((parameters) => {
foundParameters = { ...foundParameters, ...parameters };
});
}
if ('fields' in definedDefinition) {
if (!('fields' in templateDefinition)) {
throw new Error("Template definition does not match with 'fields'");
}
if (templateDefinition.constructor && typeof templateDefinition.constructor !== 'number') {
foundParameters[templateDefinition.constructor] = definedDefinition.constructor;
}
else if (templateDefinition.constructor !== definedDefinition.constructor) {
throw new Error('Template definition does not match with constructor value');
}
definedDefinition.fields
.map((fieldParameter, index) => {
return this.extractParameters(fieldParameter, templateDefinition.fields[index], foundParameters);
})
.forEach((parameters) => {
foundParameters = { ...foundParameters, ...parameters };
});
}
if ('list' in definedDefinition) {
if (!('list' in templateDefinition) || !Array.isArray(definedDefinition.list)) {
throw new Error("Template definition does not match or 'list' is not an array");
}
definedDefinition.list
.map((fieldParameter, index) => {
// Ensure the template list at index exists before attempting to access it
if (templateDefinition.list) {
return this.extractParameters(fieldParameter, templateDefinition.list[index], foundParameters);
}
else {
throw new Error(`Template definition at index ${index} is undefined`);
}
})
.forEach((parameters) => {
foundParameters = { ...foundParameters, ...parameters };
});
}
if ('int' in definedDefinition) {
if (!('int' in templateDefinition)) {
throw new Error("Template definition does not match with 'int'");
}
if (typeof templateDefinition.int !== 'number') {
foundParameters[templateDefinition.int] = definedDefinition.int;
}
}
if ('bytes' in definedDefinition) {
if (!('bytes' in templateDefinition)) {
throw new Error("Template definition does not match with 'bytes'");
}
const datumKeys = Object.values(DatumParameterKey);
if (datumKeys.includes(templateDefinition.bytes)) {
foundParameters[templateDefinition.bytes] = definedDefinition.bytes;
}
}
return foundParameters;
}
}
Loading