user5261: I have found this open source project that aims to create a typescript library to write smart contracts.
I had a look at the code but was not able to understand a couple of main pieces useful to understand the whole picture.
Of course the author is following the Plutus Core Specificiation in order to create the library, but to me is not clear where the actual serialization happen.
Also, when the serialization is done, how will be possible to push it to the computational layer of the blockchain? Should we crate a particular transaction containing the obtained binary code?
Thanks

user6763: Searching in the existing folders I ended up finding the UPLCEncoder implementation.
in particular, the class exposes a compile method that has the following signature:
compile( program: UPLCProgram ): BitStream

so it is something that given a UPLCProgram instance returns a BitStream; the implementation of the BitStream class suggest it is a wrapper ona bigint js primitive value used to store the compiled program.
back to the compile method; the library already supports hoisting; so the first part of the method is about inlining hoisted terms;
the actual compilation is forwarded to the encodeTerm method as we can see from
result.append(
    this.encodeTerm(
        progrTerm
    )
);

the implementation of encodeTerm is just an intermediate step to choose the encoding to use based on the actual UPLCTerm instance passed.
once encodeTerm understands the type of the terms calls the specific encoding function that translates 1:1 the UPLCTerm to binary (using BitStream) and calls again encodeTerm (mutually recursive) when necessary as in the case of Application encoding
const result = Application.UPLCTag;
this._ctx.incrementLengthBy( result.length );

result.append(
    this.encodeTerm( app.funcTerm ) 
);

result.append(
    this.encodeTerm( app.argTerm )
);

once compile returns the BitStream currently the solution is to use the toBuffer method to get a Buffer from it and convert it to the serialized hexadecimal string that can be submitted in a transaction, just like any other smart contract.
it has to be said that the project is quite new so I'm sure there will be a more direct way to obtain the hexadecimal string.

5261: <p>I have found <a href="https://github.com/HarmonicPool/plu-ts" rel="nofollow noreferrer">this</a> open source project that aims to create a typescript library to write smart contracts.</p>
<p>I had a look at the code but was not able to understand a couple of main pieces useful to understand the whole picture.</p>
<p>Of course the author is following the <a href="https://github.com/input-output-hk/plutus/blob/master/plutus-core-spec/draft-new-specification/plutus-core-specification.pdf" rel="nofollow noreferrer">Plutus Core Specificiation</a> in order to create the library, but to me is not clear where the actual serialization happen.</p>
<p>Also, when the serialization is done, how will be possible to push it to the computational layer of the blockchain? Should we crate a particular transaction containing the obtained binary code?</p>
<p>Thanks</p>
 6763: <p>Searching in the existing folders I ended up finding the <a href="https://github.com/HarmonicPool/plu-ts/blob/main/src/onchain/UPLC/UPLCEncoder/index.ts" rel="nofollow noreferrer"><code>UPLCEncoder</code></a> implementation.</p>
<p>in particular, the class exposes a <a href="https://github.com/HarmonicPool/plu-ts/blob/f9b0b81056b98b7a0bd95daec5d0bcddbe32ce2f/src/onchain/UPLC/UPLCEncoder/index.ts#L283" rel="nofollow noreferrer"><code>compile method</code></a> that has the following signature:</p>
<pre><code>compile( program: UPLCProgram ): BitStream
</code></pre>
<p>so it is something that given a <code>UPLCProgram</code> instance returns a <code>BitStream</code>; the implementation of the <a href="https://github.com/HarmonicPool/plu-ts/blob/main/src/types/bits/BitStream/index.ts" rel="nofollow noreferrer"><code>BitStream</code> class</a> suggest it is a wrapper ona <code>bigint</code> js primitive value used to store the compiled program.</p>
<p>back to the <code>compile</code> method; the library already supports <a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting" rel="nofollow noreferrer">hoisting</a>; so the first part of the method is about inlining hoisted terms;</p>
<p>the actual compilation is forwarded to the <code>encodeTerm</code> method as we can see from</p>
<pre class="lang-typescript prettyprint-override"><code>result.append(
    this.encodeTerm(
        progrTerm
    )
);
</code></pre>
<p>the <a href="https://github.com/HarmonicPool/plu-ts/blob/f9b0b81056b98b7a0bd95daec5d0bcddbe32ce2f/src/onchain/UPLC/UPLCEncoder/index.ts#L324" rel="nofollow noreferrer">implementation of <code>encodeTerm</code></a> is just an intermediate step to choose the encoding to use based on the actual <code>UPLCTerm</code> instance passed.</p>
<p>once <code>encodeTerm</code> understands the type of the terms calls the specific encoding function that translates 1:1 the <code>UPLCTerm</code> to binary (using <code>BitStream</code>) and calls again <code>encodeTerm</code> (mutually recursive) when necessary as in the case of <code>Application</code> encoding</p>
<pre class="lang-typescript prettyprint-override"><code>const result = Application.UPLCTag;
this._ctx.incrementLengthBy( result.length );

result.append(
    this.encodeTerm( app.funcTerm ) 
);

result.append(
    this.encodeTerm( app.argTerm )
);
</code></pre>
<p>once <code>compile</code> returns the <code>BitStream</code> currently the solution is to use the <code>toBuffer</code> method to get a <code>Buffer</code> from it and convert it to the serialized hexadecimal string that can be submitted in a transaction, just like any other smart contract.</p>
<p>it has to be said that the project is quite new so I'm sure there will be a more direct way to obtain the hexadecimal string.</p>
