Skip to content

Commit

Permalink
feat: building offchain code for swap
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed May 29, 2024
1 parent eddd8b9 commit 49f8c16
Show file tree
Hide file tree
Showing 4 changed files with 4,291 additions and 4,219 deletions.
1 change: 1 addition & 0 deletions packages/contracts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './vesting';
export * from './escrow';
export * from './giftcard';
export * from './payment-splitter';
export * from './swap';
1 change: 1 addition & 0 deletions packages/contracts/src/swap/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './offchain';
44 changes: 17 additions & 27 deletions packages/contracts/src/swap/offchain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { MeshTxInitiator, MeshTxInitiatorInput } from '@mesh/common';
import {
List,
Asset,
UTxO,
mConStr0,
Expand All @@ -10,6 +9,8 @@ import {
PubKeyAddress,
Value,
parsePlutusValueToAssets,
conStr0,
ConStr0,
} from '@meshsdk/common';
import {
applyParamsToScript,
Expand All @@ -20,24 +21,14 @@ import {
} from '@meshsdk/core-csl';
import blueprint from './aiken-workspace/plutus.json';

export type SwapDatum = ConStr0<[PubKeyAddress, Value, Value]>;

export class MeshSwapContract extends MeshTxInitiator {
tokenNameHex: string = '';
paramUtxo: UTxO['input'] = { outputIndex: 0, txHash: '' };
scriptCbor = applyParamsToScript(blueprint.validators[0].compiledCode, []);
scriptAddress: string;

constructor(
inputs: MeshTxInitiatorInput,
tokenNameHex?: string,
paramUtxo?: UTxO['input']
) {
constructor(inputs: MeshTxInitiatorInput) {
super(inputs);
if (tokenNameHex) {
this.tokenNameHex = tokenNameHex;
}
if (paramUtxo) {
this.paramUtxo = paramUtxo;
}
this.scriptAddress = v2ScriptToBech32(
this.scriptCbor,
undefined,
Expand All @@ -53,16 +44,15 @@ export class MeshSwapContract extends MeshTxInitiator {
await this.getWalletInfoForTx();
const { pubKeyHash, stakeCredential } =
serializeBech32Address(walletAddress);
const swapDatum: SwapDatum = conStr0([
pubKeyAddress(pubKeyHash, stakeCredential),
value(toProvide),
value(toReceive),
]);

await this.mesh
.txOut(this.scriptAddress, toProvide)
.txOutInlineDatumValue(
[
pubKeyAddress(pubKeyHash, stakeCredential),
value(toProvide),
value(toReceive),
],
'JSON'
)
.txOutInlineDatumValue(swapDatum, 'JSON')
.changeAddress(walletAddress)
.txInCollateral(
collateral.input.txHash,
Expand All @@ -79,11 +69,11 @@ export class MeshSwapContract extends MeshTxInitiator {
acceptSwap = async (swapUtxo: UTxO): Promise<string> => {
const { utxos, walletAddress, collateral } =
await this.getWalletInfoForTx();
const inlineDatum = parseDatumCbor<List>(swapUtxo.output.plutusData!).list;
const inlineDatum = parseDatumCbor<SwapDatum>(swapUtxo.output.plutusData!);
const initiatorAddress = parsePlutusAddressObjToBech32(
inlineDatum[0] as PubKeyAddress
inlineDatum.fields[0]
);
const initiatorToReceive = inlineDatum[2] as Value;
const initiatorToReceive = inlineDatum.fields[2];

await this.mesh
.spendingPlutusScriptV2()
Expand Down Expand Up @@ -112,9 +102,9 @@ export class MeshSwapContract extends MeshTxInitiator {
cancelSwap = async (swapUtxo: UTxO): Promise<string> => {
const { utxos, walletAddress, collateral } =
await this.getWalletInfoForTx();
const inlineDatum = parseDatumCbor<List>(swapUtxo.output.plutusData!).list;
const inlineDatum = parseDatumCbor<SwapDatum>(swapUtxo.output.plutusData!);
const initiatorAddress = parsePlutusAddressObjToBech32(
inlineDatum[0] as PubKeyAddress
inlineDatum.fields[0]
);
await this.mesh
.spendingPlutusScriptV2()
Expand Down
Loading

0 comments on commit 49f8c16

Please sign in to comment.