Skip to content

Commit

Permalink
Merge pull request #25 from xray-network/develop
Browse files Browse the repository at this point in the history
Final edits / TX tests
  • Loading branch information
xray-robot authored Jun 23, 2024
2 parents 539345e + 3925ea1 commit 1b593e4
Show file tree
Hide file tree
Showing 9 changed files with 454 additions and 129 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"cardano-koios-client": "^1.0.4",
"cardano-nftcdn-client": "^1.0.4",
"cardano-pricing-client": "^1.0.4",
"cborg": "^4.2.1",
"crypto-browserify": "^3.12.0",
"js-sha256": "^0.11.0",
"uplc-node": "^0.0.3",
Expand Down
32 changes: 14 additions & 18 deletions src/core/txBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class TxBuilder {
*/
validFrom = (unixTime: number) => {
this.queue.push(async () => {
const slot = this.cw3.utils.time.unixTimeToSlot(unixTime, this.cw3.__config.slotConfig)
const slot = this.cw3.utils.time.unixTimeToSlot(unixTime)
this.__txBuilder.set_validity_start_interval(BigInt(slot))
})
return this
Expand All @@ -214,7 +214,7 @@ export class TxBuilder {
*/
validTo = (unixTime: number) => {
this.queue.push(async () => {
const slot = this.cw3.utils.time.unixTimeToSlot(unixTime, this.cw3.__config.slotConfig)
const slot = this.cw3.utils.time.unixTimeToSlot(unixTime)
this.__txBuilder.set_ttl(BigInt(slot))
})
return this
Expand All @@ -227,7 +227,7 @@ export class TxBuilder {
*/
setTtl = (slotsOffset: number) => {
this.queue.push(async () => {
const slot = this.cw3.utils.time.unixTimeToSlot(Date.now() + slotsOffset * 1000, this.cw3.__config.slotConfig)
const slot = this.cw3.utils.time.unixTimeToSlot(Date.now() + slotsOffset * 1000)
this.__txBuilder.set_ttl(BigInt(slot))
})
return this
Expand Down Expand Up @@ -312,7 +312,6 @@ export class TxBuilder {
"Redeemer is required for Plutus scripts. Use Data.void() if script doesn't require a redeemer"
)
}
// this.shouldAddCollaterals = true
this.__txBuilder.add_mint(
mintBuilder.plutus_script(
this.cw3.utils.script.partialPlutusWitness(this.cw3.utils.script.scriptToPlutusScript(script), redeemer),
Expand Down Expand Up @@ -407,7 +406,6 @@ export class TxBuilder {
"Redeemer is required for Plutus scripts. Use Data.void() if script doesn't require a redeemer"
)
}
// this.shouldAddCollaterals = true
this.__txBuilder.add_withdrawal(
withdrawBuilder.plutus_script(
this.cw3.utils.script.partialPlutusWitness(
Expand Down Expand Up @@ -475,7 +473,6 @@ export class TxBuilder {
"Redeemer is required for Plutus scripts. Use Data.void() if script doesn't require a redeemer"
)
}
// this.shouldAddCollaterals = true
this.__txBuilder.add_cert(
certificateBuilder.plutus_script(
this.cw3.utils.script.partialPlutusWitness(
Expand Down Expand Up @@ -563,7 +560,6 @@ export class TxBuilder {
"Redeemer is required for Plutus scripts. Use Data.void() if script doesn't require a redeemer"
)
}
// this.shouldAddCollaterals = true
this.__txBuilder.add_cert(
certificateBuilder.plutus_script(
this.cw3.utils.script.partialPlutusWitness(
Expand Down Expand Up @@ -648,18 +644,25 @@ export class TxBuilder {
: this.cw3.CML.NetworkId.testnet()
)
// Set default TTL
this.__txBuilder.set_ttl(
BigInt(this.cw3.utils.time.unixTimeToSlot(Date.now() + TTL * 1000, this.cw3.__config.slotConfig))
)
this.__txBuilder.set_ttl(BigInt(this.cw3.utils.time.unixTimeToSlot(Date.now() + TTL * 1000)))
}

// Execute queue tasks
for (const task of this.queue) {
await task()
}

// Prepare draft TX
const draftTx = this.__txBuilder
.build_for_evaluation(
this.cw3.CML.ChangeSelectionAlgo.Default,
this.cw3.CML.Address.from_bech32(this.changeAddress)
)
.draft_tx()
const draftTxRedeemers = draftTx.witness_set().redeemers()

// Set Collateral
if (this.scripts.size > 0) {
if (draftTxRedeemers) {
// TODO: Select up to maxCollateralInputs collaterals
const collateral = [...this.inputs.values()].find((utxo) => utxo.value > 5_000_000)
if (!collateral) throw new Error("Suitable collateral > 5 ADA not found")
Expand All @@ -684,13 +687,6 @@ export class TxBuilder {
if (this.coinSelection !== -1) this.__txBuilder.select_utxos(this.coinSelection)

// Evaluate TX phase two execution cost and set ex_units
const draftTx = this.__txBuilder
.build_for_evaluation(
this.cw3.CML.ChangeSelectionAlgo.Default,
this.cw3.CML.Address.from_bech32(this.changeAddress)
)
.draft_tx()
const draftTxRedeemers = draftTx.witness_set().redeemers()
if (draftTxRedeemers) {
if (!this.remoteTxEvaluate) {
const costModels = this.cw3.utils.tx.createCostModels(this.protocolParams.costModels)
Expand Down
2 changes: 1 addition & 1 deletion src/provider/koios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ const koiosUtxoToUtxo = (utxo: KoiosTypes.components["schemas"]["utxo_infos"][nu
}
}

const koiosUtxosToUtxos = (utxos: KoiosTypes.components["schemas"]["utxo_infos"]) => {
const koiosUtxosToUtxos = (utxos: KoiosTypes.components["schemas"]["utxo_infos"]): T.Utxo[] => {
return utxos.map((utxo) => koiosUtxoToUtxo(utxo))
}

Expand Down
2 changes: 1 addition & 1 deletion src/provider/kupmios/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ const kupoUtxoToUtxo = (utxo: K.Utxo): T.Utxo => {
}
}

const kupoUtxosToUtxos = (utxos: K.Utxo[]) => {
const kupoUtxosToUtxos = (utxos: K.Utxo[]): T.Utxo[] => {
return utxos.map((utxo) => kupoUtxoToUtxo(utxo))
}

Expand Down
4 changes: 2 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ export type Credential = {
}
export type AddressPublicCredentials = {
type: AddressType
paymentCred: Credential
stakingCred: Credential
paymentCred?: Credential
stakingCred?: Credential
}
export type Redeemer = string
export type RedeemerCost = {
Expand Down
Loading

0 comments on commit 1b593e4

Please sign in to comment.