Skip to content

Commit

Permalink
v3
Browse files Browse the repository at this point in the history
  • Loading branch information
michele-nuzzi committed Jul 15, 2024
1 parent dd7e6eb commit ee2fe29
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 81 deletions.
19 changes: 5 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"@harmoniclabs/cardano-costmodels-ts": "^1.1.0",
"@harmoniclabs/plutus-machine": "^2.0.0-dev2",
"@harmoniclabs/uplc": "^1.2.3",
"@harmoniclabs/cardano-ledger-ts": "^0.2.0-dev5",
"@harmoniclabs/cardano-ledger-ts": "^0.2.1",
"@harmoniclabs/plu-ts-offchain": "0.1.13-dev4",
"@harmoniclabs/plu-ts-onchain": "^0.3.0-dev2"
},
Expand Down
20 changes: 5 additions & 15 deletions packages/offchain/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/offchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@harmoniclabs/cardano-costmodels-ts": "^1.1.0",
"@harmoniclabs/plutus-machine": "^2.0.0-dev2",
"@harmoniclabs/uplc": "^1.2.3",
"@harmoniclabs/cardano-ledger-ts": "^0.2.0-dev6"
"@harmoniclabs/cardano-ledger-ts": "^0.2.1"
},
"devDependencies": {
"@babel/preset-env": "^7.18.6",
Expand Down
58 changes: 40 additions & 18 deletions packages/offchain/src/TxBuilder/TxBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { freezeAll, defineReadOnlyProperty, definePropertyIfNotPresent, hasOwn,
import { TxBuilderRunner } from "./TxBuilderRunner/TxBuilderRunner";
import { ITxRunnerProvider } from "./IProvider";
import { CanBeData, canBeData, forceData } from "../utils/CanBeData";
import { getSpendingPurposeData } from "../toOnChain/getSpendingPurposeData";
import { getScriptInfoData, getSpendingPurposeData } from "../toOnChain/getSpendingPurposeData";
import { TxBuilderProtocolParams, ValidatedTxBuilderProtocolParams, completeTxBuilderProtocolParams } from "./TxBuilderProtocolParams";
import { ChangeInfos } from "../txBuild/ChangeInfos/ChangeInfos";
import { scriptTypeToDataVersion } from "./utils";
Expand Down Expand Up @@ -363,12 +363,19 @@ export class TxBuilder
const ctxData = getCtx(
script.type,
getSpendingPurposeData( rdmr, tx.body, expectedVersion ),
getScriptInfoData( rdmr, tx.body, expectedVersion ),
rdmrData,
txInfosV1,
txInfosV2,
txInfosV3,
);

const { result, budgetSpent, logs } = cek.eval(
script.type === ScriptType.PlutusV3 ?
new Application(
getScriptLikeUplc( script ),
UPLCConst.data( ctxData )
) :
new Application(
new Application(
getScriptLikeUplc( script ),
Expand Down Expand Up @@ -408,7 +415,9 @@ export class TxBuilder

const { script, datum } = entry;

if( datum === undefined )
const isV2OrLess = script.type === ScriptType.PlutusV1 || script.type === ScriptType.PlutusV2;

if( datum === undefined && isV2OrLess )
throw new Error(
"missing datum for spend redeemer " + index
);
Expand All @@ -421,23 +430,30 @@ export class TxBuilder
const ctxData = getCtx(
script.type,
getSpendingPurposeData( rdmr, tx.body, expectedVersion ),
getScriptInfoData( rdmr, tx.body, expectedVersion, datum ),
rdmrData,
txInfosV1,
txInfosV2,
txInfosV3
);

const { result, budgetSpent, logs } = cek.eval(
isV2OrLess ?
new Application(
new Application(
new Application(
getScriptLikeUplc( script ),
UPLCConst.data( datum )
UPLCConst.data( datum! )
),
UPLCConst.data( rdmrData )
),
UPLCConst.data(
ctxData
)
) :
new Application(
getScriptLikeUplc( script ),
UPLCConst.data( ctxData )
)
);

Expand All @@ -448,11 +464,11 @@ export class TxBuilder
result,
budgetSpent,
logs,
[
datum,
isV2OrLess ? [
datum!,
rdmrData,
ctxData
],
] : [ ctxData ],
rdmrs,
onScriptResult,
onScriptInvalid
Expand All @@ -474,7 +490,7 @@ export class TxBuilder
// bigint division truncates always towards 0;
// we don't like that so we add `1n` for both divisions ( + 2n )
BigInt(2);

if( fee === prevFee ) break; // return last transaciton

// reset for next loop
Expand Down Expand Up @@ -1021,9 +1037,10 @@ export class TxBuilder
return withdrawal;
});

let i = 0;
const _votingProcedures = Array.isArray( votingProcedures ) ?
new VotingProcedures(
votingProcedures?.map(({ votingProcedure, script }, i) => {
votingProcedures?.map(({ votingProcedure, script }) => {

if( script !== undef )
{
Expand All @@ -1039,18 +1056,21 @@ export class TxBuilder
const toExec = checkScriptAndPushIfInline( script );

pushScriptToExec( i, TxRedeemerTag.Voting, toExec );

i++;
}

return votingProcedure;
})
) : undef;

i = 0;
const _proposalProcedures = Array.isArray( proposalProcedures ) ?
proposalProcedures.map(({ proposalProcedure, script }, i) => {
proposalProcedures.map(({ proposalProcedure, script }) => {

if( script !== undef )
{
voteRedeemers.push(
proposeRedeemers.push(
new TxRedeemer({
data: forceData( script.redeemer ),
index: i,
Expand All @@ -1062,11 +1082,15 @@ export class TxBuilder
const toExec = checkScriptAndPushIfInline( script );

pushScriptToExec( i, TxRedeemerTag.Proposing, toExec );

i++;
}

return new ProposalProcedure( proposalProcedure );
}) : undef;

i = 0;

const auxData = metadata !== undefined? new AuxiliaryData({ metadata }) : undefined;

const redeemers =
Expand Down Expand Up @@ -1365,6 +1389,8 @@ function pushUniqueScript<T extends ScriptType>( arr: Script<T>[], toPush: Scrip
function getCtx(
scriptType: ScriptType,
spendingPurpose: DataConstr,
scriptInfo: DataConstr,
redeemerData: Data,
txInfosV1: Data | undefined,
txInfosV2: Data | undefined,
txInfosV3: Data
Expand All @@ -1375,15 +1401,16 @@ function getCtx(
return new DataConstr(
0, [
txInfosV3,
spendingPurpose
redeemerData,
scriptInfo
]
)
}
else if( scriptType === ScriptType.PlutusV2 )
{
if( txInfosV2 === undefined )
throw new Error(
"plutus script v1 included in a v2 transaction"
"plutus script v2 included in a v3 transaction"
);

return new DataConstr(
Expand Down Expand Up @@ -1464,12 +1491,7 @@ function onEvaluationResult(
`called with data arguments:\n${
callArgs
.map( (d, i) =>
(
i === 0 ? ( rdmr.tag === TxRedeemerTag.Spend ? "datum" : "redeemer" ) :
i === 1 ? ( rdmr.tag === TxRedeemerTag.Spend ? "redeemer" : "script context" ) :
i === 2 ? ( rdmr.tag === TxRedeemerTag.Spend ? "script context" : i.toString() ) :
i.toString()
) + ": " + dataToCbor( d ).toString()
i.toString() + ": " + dataToCbor( d ).toString()
)
.join("\n")
}\n\n` +
Expand Down
66 changes: 66 additions & 0 deletions packages/offchain/src/__tests__/docs/docs.v3.hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Address, Credential, defaultProtocolParameters, forceTxOutRef, Script, ScriptType, TxOutRef, UTxO, Value } from "@harmoniclabs/cardano-ledger-ts";
import { DataB } from "@harmoniclabs/plutus-data";
import { fromAscii, fromHex } from "@harmoniclabs/uint8array-utils";
import { defaultMainnetGenesisInfos, TxBuilder } from "../..";
import { defaultV3Costs } from "@harmoniclabs/cardano-costmodels-ts"

test("Hello plu-ts",() => {

const script = new Script(
ScriptType.PlutusV3,
fromHex("01010032323232323232322323259800800c5268b2ae686644b3001002800c5282ae693001329800991aba1357446ae8800400a003001800c0064601800300148888888c8cc966002600290004400e2b300130014800a20091598009800a400910058acc004c005200688034566002600290044401e2b300130014802a20111655cd1573455cd1573455cd15734370e6aae74004d55cf0009baa007134928ca6002e3e664664600e46ae80c0080040052f588eb8dd6191aba1357446ae88d5d11aba2357446ae88d5d11aba2001300435742005375c6ae84005222330093003001002244464664b300130014800220071598009800a400510048b2ae68ab9a1b8735573a0026aae78004dd500184cdc79bae300a0014890c48656c6c6f20706c752d7473003002001235573c6ea800488cc01484008888cc014008c00c0048cc00c852811119802980200109801800912cc00400a2946002ab9a18011111194c004c0100066006003002401866008006004444b3001001801c4cc008d5d08009aba2001555cf88c8c0088cc0080080048c0088cc0080080048d5d09aba200101")
);

const scriptAddr = Address.mainnet(
Credential.script( script.hash )
);

const myAddr = Address.fake;
const myPkh = myAddr.paymentCreds.hash;

const scriptInput = new UTxO({
utxoRef: { id: "aa".repeat( 32 ), index: 0 },
resolved: {
address: scriptAddr,
value: Value.lovelaces( 5_000_000 ),
datum: new DataB( myPkh.toBuffer() )
},
});

const myInput = new UTxO({
utxoRef: { id: "bb".repeat( 32 ), index: 0 },
resolved: {
address: myAddr,
value: Value.lovelaces( 5_000_000_000_000_000_000_000 ),
}
})

// defaultProtocolParameters.costModels.PlutusScriptV3 = defaultV3Costs;

const pps = {
...defaultProtocolParameters,
costModels: {
...defaultProtocolParameters.costModels,
PlutusScriptV3: defaultV3Costs
}
};

const txBuilder = new TxBuilder( pps, defaultMainnetGenesisInfos );

const tx = txBuilder.buildSync({
inputs: [
{
utxo: scriptInput,
inputScript: {
script: script,
datum: "inline",
redeemer: new DataB( fromAscii("Hello plu-ts") )
}
},
{ utxo: myInput }
],
requiredSigners: [ myPkh ],
changeAddress: myAddr
});

})
Loading

0 comments on commit ee2fe29

Please sign in to comment.