Skip to content

Commit

Permalink
Added fromU256/fromU32/fromI32/fromI256 for compact int
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Mar 23, 2024
1 parent b9a9a11 commit 5788edc
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/web3/src/codec/array-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class ArrayCodec<T> implements Codec<T[]> {

fromArray(inputs: T[]): DecodedArray<T> {
return {
length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(inputs.length)),
length: compactUnsignedIntCodec.fromU32(inputs.length),
value: inputs
}
}
Expand Down
11 changes: 5 additions & 6 deletions packages/web3/src/codec/asset-output-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,24 +115,23 @@ export class AssetOutputCodec implements Codec<AssetOutput> {
}

static fromFixedAssetOutput(fixedOutput: FixedAssetOutput): AssetOutput {
const amount: DecodedCompactInt = compactUnsignedIntCodec.decode(
compactUnsignedIntCodec.encodeU256(BigInt(fixedOutput.attoAlphAmount))
)
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(fixedOutput.attoAlphAmount))

const lockTime: Buffer = longCodec.encode(BigInt(fixedOutput.lockTime))
const lockupScript: LockupScript = lockupScriptCodec.decode(Buffer.from(bs58.decode(fixedOutput.address)))
const tokensValue = fixedOutput.tokens.map((token) => {
return {
tokenId: Buffer.from(token.id, 'hex'),
amount: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(BigInt(token.amount)))
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
}
})
const tokens: DecodedArray<Token> = {
length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(tokensValue.length)),
length: compactUnsignedIntCodec.fromU32(tokensValue.length),
value: tokensValue
}
const additionalDataValue = Buffer.from(fixedOutput.message, 'hex')
const additionalData: ByteString = {
length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(additionalDataValue.length)),
length: compactUnsignedIntCodec.fromU32(additionalDataValue.length),
value: additionalDataValue
}

Expand Down
16 changes: 16 additions & 0 deletions packages/web3/src/codec/compact-int-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
return decodePositiveInt(value.mode, body)
}

fromU32(value: number): DecodedCompactInt {
return this.decode(this.encodeU32(value))
}

toU256(value: DecodedCompactInt): bigint {
const mode = value.mode & maskRest
if (fixedSize(mode)) {
Expand All @@ -139,6 +143,10 @@ export class CompactUnsignedIntCodec implements Codec<DecodedCompactInt> {
return BigIntCodec.decode(Buffer.from(value.rest), false)
}
}

fromU256(value: bigint): DecodedCompactInt {
return this.decode(this.encodeU256(value))
}
}

export const compactUnsignedIntCodec = new CompactUnsignedIntCodec()
Expand Down Expand Up @@ -244,6 +252,10 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
}
}

fromI32(value: number): DecodedCompactInt {
return this.decode(this.encodeI32(value))
}

toI256(value: DecodedCompactInt): bigint {
const mode = value.mode & maskRest

Expand All @@ -254,6 +266,10 @@ export class CompactSignedIntCodec implements Codec<DecodedCompactInt> {
return BigIntCodec.decode(Buffer.from(value.rest), true)
}
}

fromI256(value: bigint): DecodedCompactInt {
return this.decode(this.encodeI256(value))
}
}

export const compactSignedIntCodec = new CompactSignedIntCodec()
Expand Down
8 changes: 3 additions & 5 deletions packages/web3/src/codec/contract-output-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,18 @@ export class ContractOutputCodec implements Codec<ContractOutput> {
}

static convertToOutput(apiContractOutput: ApiContractOutput): ContractOutput {
const amount: DecodedCompactInt = compactUnsignedIntCodec.decode(
compactUnsignedIntCodec.encodeU256(BigInt(apiContractOutput.attoAlphAmount))
)
const amount: DecodedCompactInt = compactUnsignedIntCodec.fromU256(BigInt(apiContractOutput.attoAlphAmount))
const lockupScript: P2C = lockupScriptCodec.decode(Buffer.from(bs58.decode(apiContractOutput.address)))
.script as P2C

const tokensValue = apiContractOutput.tokens.map((token) => {
return {
tokenId: Buffer.from(token.id, 'hex'),
amount: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(BigInt(token.amount)))
amount: compactUnsignedIntCodec.fromU256(BigInt(token.amount))
}
})
const tokens: DecodedArray<Token> = {
length: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU32(tokensValue.length)),
length: compactUnsignedIntCodec.fromU32(tokensValue.length),
value: tokensValue
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web3/src/codec/token-codec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Encode & decode tokens', function () {
function success(tokenIn: { id: string; amount: bigint }) {
const token = {
tokenId: Buffer.from(tokenIn.id, 'hex'),
amount: compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(tokenIn.amount))
amount: compactUnsignedIntCodec.fromU256(tokenIn.amount)
}
const encoded = tokenCodec.encode(token)
const decoded = tokenCodec.decode(encoded)
Expand Down
4 changes: 2 additions & 2 deletions packages/web3/src/codec/unsigned-tx-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ export class UnsignedTxCodec implements Codec<UnsignedTx> {
static fromApiUnsignedTx(unsignedTx: ApiUnsignedTx): UnsignedTx {
const version = unsignedTx.version
const networkId = unsignedTx.networkId
const gasAmount = compactSignedIntCodec.decode(compactSignedIntCodec.encodeI32(unsignedTx.gasAmount))
const gasPrice = compactUnsignedIntCodec.decode(compactUnsignedIntCodec.encodeU256(BigInt(unsignedTx.gasPrice)))
const gasAmount = compactSignedIntCodec.fromI32(unsignedTx.gasAmount)
const gasPrice = compactUnsignedIntCodec.fromU256(BigInt(unsignedTx.gasPrice))
const inputsValue = InputCodec.fromAssetInputs(unsignedTx.inputs)
const inputs = inputsCodec.fromArray(inputsValue)
const fixedOutputsValue = AssetOutputCodec.fromFixedAssetOutputs(unsignedTx.fixedOutputs)
Expand Down

0 comments on commit 5788edc

Please sign in to comment.