Skip to content

Commit

Permalink
Int32ArrayConstant support. (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
oskin1 committed Nov 17, 2022
1 parent e7a3344 commit dbdd341
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "@ergolabs/ergo-sdk",
"version": "0.4.5",
"version": "0.5.0",
"description": "Ergo SDK",
"main": "build/main/index.js",
"typings": "build/main/index.d.ts",
Expand Down
15 changes: 12 additions & 3 deletions src/entities/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@ export class ByteaConstant {
constructor(public readonly value: Uint8Array) {}
}

export class Int32ArrayConstant {
constructor(public readonly value: Int32Array) {}
}

export class SigmaPropConstant {
constructor(public readonly value: PublicKey) {}
}

export type Constant = Int32Constant | Int64Constant | ByteaConstant | SigmaPropConstant
export type Constant = Int32Constant | Int64Constant | ByteaConstant | Int32ArrayConstant | SigmaPropConstant

export function serializeConstant(c: Constant): HexString {
let constant: WasmConstant
if (c instanceof Int32Constant) constant = RustModule.SigmaRust.Constant.from_i32(c.value)
else if (c instanceof Int64Constant)
constant = RustModule.SigmaRust.Constant.from_i64(RustModule.SigmaRust.I64.from_str(c.value.toString()))
else if (c instanceof Int32ArrayConstant) constant = RustModule.SigmaRust.Constant.from_i32_array(c.value)
else if (c instanceof SigmaPropConstant)
constant = RustModule.SigmaRust.Constant.decode_from_base16(SigmaPropConstPrefixHex + c.value)
else constant = RustModule.SigmaRust.Constant.from_byte_array(c.value)
Expand All @@ -47,9 +52,13 @@ export function deserializeConstant(r: HexString): Constant | undefined {
return new SigmaPropConstant(hex.slice(SigmaPropConstPrefixHex.length))
} else {
try {
return new ByteaConstant(constant.to_byte_array())
return new Int32ArrayConstant(constant.to_i32_array())
} catch (e) {
return undefined
try {
return new ByteaConstant(constant.to_byte_array())
} catch (e) {
return undefined
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ergoWasmInterop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export function txRequestToWasmTransaction(req: TxRequest, ctx: NetworkContext):
}

export function ergoBoxesFromWasmUtx(wutx: UnsignedTransaction): ErgoBox[] {
const mackTxInputs = Array(wutx.inputs().len()).fill([]);
const unsignedTxId = wutx.id().to_str();
const mackTxInputs = Array(wutx.inputs().len()).fill([])
const unsignedTxId = wutx.id().to_str()
const mockTx = RustModule.SigmaRust.Transaction.from_unsigned_tx(wutx, mackTxInputs)
const outputs = []
for (let i = 0; i < mockTx.outputs().len(); i++) {
Expand Down
55 changes: 29 additions & 26 deletions src/network/ergoNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ export interface ErgoNetwork {

/** Get UTXo by address
*/
searchUnspentBoxesByAddress(address: Address): Promise<ErgoBoxProxy[]>;
searchUnspentBoxesByAddress(address: Address): Promise<ErgoBoxProxy[]>

/** Get UTXo by addresses
*/
searchUnspentBoxesByAddresses(addresses: Address[]): Promise<ErgoBoxProxy[]>;
searchUnspentBoxesByAddresses(addresses: Address[]): Promise<ErgoBoxProxy[]>
}

export class Explorer implements ErgoNetwork {
Expand Down Expand Up @@ -238,38 +238,41 @@ export class Explorer implements ErgoNetwork {
}

async searchUnspentBoxesByAddress(address: Address): Promise<ErgoBoxProxy[]> {
return this.backend
return this.backend
.request<network.Items<network.ExplorerErgoBox>>({
url: `/api/v1/boxes/unspent/byAddress/${address}`,
transformResponse: data => JSONBI.parse(data)
})
.then(res => res.data.items)
.then(items => items.map(item => ({
boxId: item.boxId,
transactionId: item.transactionId,
index: item.index,
ergoTree: item.ergoTree,
creationHeight: item.creationHeight,
value: item.value.toString(),
assets: item.assets.map(a => ({
tokenId: a.tokenId,
amount: a.amount.toString(),
name: a.name,
decimals: a.decimals
})),
additionalRegisters: Object
.entries(item.additionalRegisters)
.reduce<Registers>((acc, [key, value]) => ({
...acc,
[key]: value.serializedValue
}), {})
})))
.then(items =>
items.map(item => ({
boxId: item.boxId,
transactionId: item.transactionId,
index: item.index,
ergoTree: item.ergoTree,
creationHeight: item.creationHeight,
value: item.value.toString(),
assets: item.assets.map(a => ({
tokenId: a.tokenId,
amount: a.amount.toString(),
name: a.name,
decimals: a.decimals
})),
additionalRegisters: Object.entries(item.additionalRegisters).reduce<Registers>(
(acc, [key, value]) => ({
...acc,
[key]: value.serializedValue
}),
{}
)
}))
)
}

async searchUnspentBoxesByAddresses(addresses: Address[]): Promise<ErgoBoxProxy[]> {
return Promise
.all(addresses.map(a => this.searchUnspentBoxesByAddress(a)))
.then(boxesSets => boxesSets.flatMap(bs => bs))
return Promise.all(addresses.map(a => this.searchUnspentBoxesByAddress(a))).then(boxesSets =>
boxesSets.flatMap(bs => bs)
)
}

async getFullTokenInfo(tokenId: TokenId): Promise<AugAssetInfo | undefined> {
Expand Down

0 comments on commit dbdd341

Please sign in to comment.