Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add models for assets create and transfer transactions #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions lib/algorand.asset.create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { AlgorandEncoder } from "./algorand.encoder"

export class AssetCreate {
type: string
snd: Uint8Array
fee: number
fv: number
lv: number
gen: string
gh: Uint8Array

apar: {
dc: bigint
t: number
df?: boolean
am?: Uint8Array
an?: string
au?: string
c?: Uint8Array
f?: Uint8Array
m?: Uint8Array
r?: Uint8Array
un?: string
}

// encode the transaction
// return the encoded transaction
encode(): Uint8Array {
const encoded: Uint8Array = new AlgorandEncoder().encodeTransaction(this)
return encoded
}
}

export interface IAssetCreateTxBuilder {
addSender(sender: string): IAssetCreateTxBuilder
addFee(fee: number): IAssetCreateTxBuilder
addFirstValidRound(firstValid: number): IAssetCreateTxBuilder
addLastValidRound(lastValid: number): IAssetCreateTxBuilder
addUrl(url: string): IAssetCreateTxBuilder
addName(name: string): IAssetCreateTxBuilder
addUnit(unit: string): IAssetCreateTxBuilder
addManagerAddress(manager: string): IAssetCreateTxBuilder
addReserveAddress(reserve: string): IAssetCreateTxBuilder
addFreezeAddress(freeze: string): IAssetCreateTxBuilder
addClawbackAddress(clawback: string): IAssetCreateTxBuilder

get(): AssetCreate
}

export class AssetCreateTxBuilder implements IAssetCreateTxBuilder {
private tx: AssetCreate

constructor(genesisId: string, genesisHash: string, decimals: bigint, totalTokens: number, defaultFreeze: boolean = false) {
this.tx = new AssetCreate()
this.tx.gh = new Uint8Array(Buffer.from(genesisHash, "base64"))
this.tx.gen = genesisId
this.tx.type = "acfg"
this.tx.fee = 1000
this.tx.apar = {
dc: decimals,
t: totalTokens
}

defaultFreeze ? this.tx.apar.df = defaultFreeze : null
}

addSender(sender: string): IAssetCreateTxBuilder {
this.tx.snd = new AlgorandEncoder().decodeAddress(sender)
return this
}

addFee(fee: number): IAssetCreateTxBuilder {
this.tx.fee = fee
return this
}

addFirstValidRound(firstValid: number): IAssetCreateTxBuilder {
this.tx.fv = firstValid
return this
}

addLastValidRound(lastValid: number): IAssetCreateTxBuilder {
this.tx.lv = lastValid
return this
}

addUrl(url: string): IAssetCreateTxBuilder {
this.tx.apar.au = url
return this
}

addName(name: string): IAssetCreateTxBuilder {
this.tx.apar.an = name
return this
}

addUnit(unit: string): IAssetCreateTxBuilder {
this.tx.apar.un = unit
return this
}

addManagerAddress(manager: string): IAssetCreateTxBuilder {
this.tx.apar.m = new AlgorandEncoder().decodeAddress(manager)
return this
}

addReserveAddress(reserve: string): IAssetCreateTxBuilder {
this.tx.apar.r = new AlgorandEncoder().decodeAddress(reserve)
return this
}

addFreezeAddress(freeze: string): IAssetCreateTxBuilder {
this.tx.apar.f = new AlgorandEncoder().decodeAddress(freeze)
return this
}

addClawbackAddress(clawback: string): IAssetCreateTxBuilder {
this.tx.apar.c = new AlgorandEncoder().decodeAddress(clawback)
return this
}

get(): AssetCreate {
return this.tx
}
}
75 changes: 75 additions & 0 deletions lib/algorand.asset.transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { AlgorandEncoder } from "./algorand.encoder"

export class AssetTransfer {
type: string
aamt: number | bigint
arcv: Uint8Array
fee: number
fv: number
lv: number
snd: Uint8Array
xaid: number
gen: string
gh: Uint8Array


// encode the transaction
// return the encoded transaction
encode(): Uint8Array {
const encoded: Uint8Array = new AlgorandEncoder().encodeTransaction(this)
return encoded
}
}

export interface IAssetTransferTxBuilder {
addSender(sender: string): IAssetTransferTxBuilder
addFee(fee: number): IAssetTransferTxBuilder


get(): AssetTransfer
}

export class AssetTransferTxBuilder implements IAssetTransferTxBuilder {
private tx: AssetTransfer

constructor(genesisId: string, genesisHash: string, assetId: number, from: string, to: string, amount: number | bigint) {
this.tx = new AssetTransfer()
this.tx.gh = new Uint8Array(Buffer.from(genesisHash, "base64"))
this.tx.gen = genesisId
this.tx.type = "axfer"
this.tx.fee = 1000
this.tx.xaid = assetId
this.tx.aamt = amount
this.tx.snd = new AlgorandEncoder().decodeAddress(from)
this.tx.arcv = new AlgorandEncoder().decodeAddress(to)
}

addSender(sender: string): IAssetTransferTxBuilder {
this.tx.snd = new AlgorandEncoder().decodeAddress(sender)
return this
}

addFee(fee: number): IAssetTransferTxBuilder {
this.tx.fee = fee
return this
}

addFirstValidRound(firstValid: number): IAssetTransferTxBuilder {
this.tx.fv = firstValid
return this
}

addLastValidRound(lastValid: number): IAssetTransferTxBuilder {
this.tx.lv = lastValid
return this
}

addReceiver(receiver: string): IAssetTransferTxBuilder {
this.tx.arcv = new AlgorandEncoder().decodeAddress(receiver)
return this
}

get(): AssetTransfer {
return this.tx
}
}
1 change: 1 addition & 0 deletions lib/algorand.encoder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe("Algorand Encoding", () => {
const encodedAddress: string = algoEncoder.encodeAddress(Buffer.from(keyPair.publicKey))
// match addresses
expect(encodedAddress).toBe(addr)
expect(encodedAddress.length).toBe(58)

// decode back to public key
const decodedPublicKey: Uint8Array = algoEncoder.decodeAddress(encodedAddress)
Expand Down
23 changes: 23 additions & 0 deletions lib/algorand.transaction.crafter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { AlgorandEncoder } from "./algorand.encoder.js"
import { type IPayTxBuilder, PayTxBuilder } from "./algorand.transaction.pay.js"
import { type IKeyregTxBuilder, KeyregTxBuilder } from "./algorand.transaction.keyreg.js"
import * as msgpack from "algo-msgpack-with-bigint"
import { AssetTransferTxBuilder, IAssetTransferTxBuilder } from "./algorand.asset.transfer.js"
import { AssetCreateTxBuilder, IAssetCreateTxBuilder } from "./algorand.asset.create.js"

export class AlgorandTransactionCrafter extends Crafter {

Expand Down Expand Up @@ -64,6 +66,27 @@ export class AlgorandTransactionCrafter extends Crafter {
.addNonParticipation(true)
}

/**
*
*/
assetTransfer(assetId: number, from: string, to: string, amount: number | bigint): IAssetTransferTxBuilder {
return new AssetTransferTxBuilder(this.genesisId, this.genesisHash, assetId, from, to, amount)
.addFee(1000)

}

/**
*
*/
asset(from: string, unit: string, decimals: bigint, totalTokens: number): IAssetCreateTxBuilder {
return new AssetCreateTxBuilder(this.genesisId, this.genesisHash, decimals, totalTokens, false)
.addSender(from)
.addFee(1000)
.addFirstValidRound(1000)
.addLastValidRound(2000)
.addUnit(unit)
}

/**
*
* @param encodedTransaction
Expand Down
Loading