-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #201 from MeshJS/aiken-lib
Aiken lib
- Loading branch information
Showing
48 changed files
with
2,374 additions
and
1,268 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import { MeshTxBuilder, IFetcher, UTxO, BrowserWallet } from '@meshsdk/core'; | ||
|
||
export type MeshTxInitiatorInput = { | ||
mesh: MeshTxBuilder; | ||
fetcher?: IFetcher; | ||
wallet?: BrowserWallet; | ||
}; | ||
|
||
export class MeshTxInitiator { | ||
mesh: MeshTxBuilder; | ||
fetcher?: IFetcher; | ||
wallet?: BrowserWallet; | ||
|
||
constructor({ mesh, fetcher, wallet }: MeshTxInitiatorInput) { | ||
this.mesh = mesh; | ||
if (fetcher) { | ||
this.fetcher = fetcher; | ||
} | ||
if (wallet) { | ||
this.wallet = wallet; | ||
} | ||
} | ||
|
||
protected signSubmitReset = async () => { | ||
const signedTx = this.mesh.completeSigning(); | ||
const txHash = await this.mesh.submitTx(signedTx); | ||
this.mesh.meshTxBuilderBody = this.mesh.emptyTxBuilderBody(); | ||
return txHash; | ||
}; | ||
|
||
protected queryUtxos = async (walletAddress: string): Promise<UTxO[]> => { | ||
if (this.fetcher) { | ||
const utxos = await this.fetcher.fetchAddressUTxOs(walletAddress); | ||
return utxos; | ||
} | ||
return []; | ||
}; | ||
|
||
protected getWalletDappAddress = async () => { | ||
if (this.wallet) { | ||
const usedAddresses = await this.wallet.getUsedAddresses(); | ||
if (usedAddresses.length > 0) { | ||
return usedAddresses[0]; | ||
} | ||
const unusedAddresses = await this.wallet.getUnusedAddresses(); | ||
if (unusedAddresses.length > 0) { | ||
return unusedAddresses[0]; | ||
} | ||
} | ||
return ''; | ||
}; | ||
|
||
protected getWalletCollateral = async (): Promise<UTxO | undefined> => { | ||
if (this.wallet) { | ||
const utxos = await this.wallet.getCollateral(); | ||
return utxos[0]; | ||
} | ||
return undefined; | ||
}; | ||
|
||
protected getWalletUtxosWithMinLovelace = async ( | ||
lovelace: number, | ||
providedUtxos: UTxO[] = [] | ||
) => { | ||
let utxos: UTxO[] = providedUtxos; | ||
if (this.wallet && (!providedUtxos || providedUtxos.length === 0)) { | ||
utxos = await this.wallet.getUtxos(); | ||
} | ||
return utxos.filter((u) => { | ||
const lovelaceAmount = u.output.amount.find( | ||
(a: any) => a.unit === 'lovelace' | ||
)?.quantity; | ||
return Number(lovelaceAmount) > lovelace; | ||
}); | ||
}; | ||
|
||
protected getWalletUtxosWithToken = async ( | ||
assetHex: string, | ||
userUtxos: UTxO[] = [] | ||
) => { | ||
let utxos: UTxO[] = userUtxos; | ||
if (this.wallet && userUtxos.length === 0) { | ||
utxos = await this.wallet.getUtxos(); | ||
} | ||
return utxos.filter((u) => { | ||
const assetAmount = u.output.amount.find( | ||
(a: any) => a.unit === assetHex | ||
)?.quantity; | ||
return Number(assetAmount) >= 1; | ||
}); | ||
}; | ||
|
||
protected getAddressUtxosWithMinLovelace = async ( | ||
walletAddress: string, | ||
lovelace: number, | ||
providedUtxos: UTxO[] = [] | ||
) => { | ||
let utxos: UTxO[] = providedUtxos; | ||
if (this.fetcher && (!providedUtxos || providedUtxos.length === 0)) { | ||
utxos = await this.fetcher.fetchAddressUTxOs(walletAddress); | ||
} | ||
return utxos.filter((u) => { | ||
const lovelaceAmount = u.output.amount.find( | ||
(a: any) => a.unit === 'lovelace' | ||
)?.quantity; | ||
return Number(lovelaceAmount) > lovelace; | ||
}); | ||
}; | ||
|
||
protected getAddressUtxosWithToken = async ( | ||
walletAddress: string, | ||
assetHex: string, | ||
userUtxos: UTxO[] = [] | ||
) => { | ||
let utxos: UTxO[] = userUtxos; | ||
if (this.fetcher && userUtxos.length === 0) { | ||
utxos = await this.fetcher.fetchAddressUTxOs(walletAddress); | ||
} | ||
return utxos.filter((u) => { | ||
const assetAmount = u.output.amount.find( | ||
(a: any) => a.unit === assetHex | ||
)?.quantity; | ||
return Number(assetAmount) >= 1; | ||
}); | ||
}; | ||
|
||
protected getWalletInfoForTx = async () => { | ||
const utxos = await this.wallet?.getUtxos(); | ||
const collateral = await this.getWalletCollateral(); | ||
const walletAddress = await this.getWalletDappAddress(); | ||
if (!utxos || utxos?.length === 0) { | ||
throw new Error('No utxos found'); | ||
} | ||
if (!collateral) { | ||
throw new Error('No collateral found'); | ||
} | ||
if (!walletAddress) { | ||
throw new Error('No wallet address found'); | ||
} | ||
return { utxos, collateral, walletAddress }; | ||
}; | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Escrow | ||
|
||
The escrow contract is a simple contract that allows two parties to agree on a transaction and then have the funds locked until the transaction is complete. The contract is designed to be used in a trustless manner, so that neither party can cheat the other. | ||
|
||
Actors: | ||
- initiator | ||
- initiator is the party that starts the escrow | ||
- initiator deposit assets into the escrow | ||
- recipient | ||
- recipient deposit assets into the escrow | ||
- both parties | ||
- either party can cancel the trade and all assets are returned to the original owner | ||
- both parties must agree to the trade before the assets are released | ||
|
||
Story: | ||
When two parties agree on a transaction, they can deposit assets into the escrow and the recipient can withdraw the assets if the transaction is complete. if the transaction is not complete or canceled, the assets are returned to the original owner. | ||
|
||
Endpoints: | ||
- initiator deposit assets | ||
- recipient deposit assets | ||
- cancel trade | ||
- complete trade | ||
list, pay, report, dispute, claim? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Aiken compilation artifacts | ||
artifacts/ | ||
# Aiken's project working directory | ||
build/ | ||
# Aiken's default documentation export | ||
docs/ |
26 changes: 26 additions & 0 deletions
26
packages/contracts/src/giftcard/aiken-workspace/aiken.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# This file was generated by Aiken | ||
# You typically do not need to edit this file | ||
|
||
[[requirements]] | ||
name = "aiken-lang/stdlib" | ||
version = "1.7.0" | ||
source = "github" | ||
|
||
[[requirements]] | ||
name = "sidan-lab/aiken-utils" | ||
version = "0.0.1-beta" | ||
source = "github" | ||
|
||
[[packages]] | ||
name = "aiken-lang/stdlib" | ||
version = "1.7.0" | ||
requirements = [] | ||
source = "github" | ||
|
||
[[packages]] | ||
name = "sidan-lab/aiken-utils" | ||
version = "0.0.1-beta" | ||
requirements = [] | ||
source = "github" | ||
|
||
[etags] |
19 changes: 19 additions & 0 deletions
19
packages/contracts/src/giftcard/aiken-workspace/aiken.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name = "meshjs/giftcard" | ||
version = "0.0.0" | ||
license = "Apache-2.0" | ||
description = "Aiken contracts for project 'meshjs/giftcard'" | ||
|
||
[repository] | ||
user = "meshjs" | ||
project = "giftcard" | ||
platform = "github" | ||
|
||
[[dependencies]] | ||
name = "aiken-lang/stdlib" | ||
version = "1.7.0" | ||
source = "github" | ||
|
||
[[dependencies]] | ||
name = "sidan-lab/aiken-utils" | ||
version = "0.0.1-beta" | ||
source = "github" |
Oops, something went wrong.