Skip to content

Commit

Permalink
Update uintXtoNumber/BigInt
Browse files Browse the repository at this point in the history
  • Loading branch information
tequdev committed Aug 23, 2024
1 parent 412b4c6 commit 99c3fd9
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions contracts/utils/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export const uint16FromHex = (hex: string) => {
}
export const uint16FromNumber = (value: number) => {
if (value < 0 || 0xffff < value) rollback('uint16FromNumber: value out of range', -1)
return [value & 0xff, (value >> 8) & 0xff]
return [(value >> 8) & 0xff, value & 0xff]
}
export const uint16ToNumber = (value: number[]) => {
const view = new DataView(new Uint8Array(value).buffer)
Expand All @@ -105,7 +105,7 @@ export const uint32FromHex = (hex: string) => {
}
export const uint32FromNumber = (value: number) => {
if (value < 0 || 0xffffffff < value) rollback('uint32FromNumber: value out of range', -1)
return [value & 0xff, (value >> 8) & 0xff, (value >> 16) & 0xff, (value >> 24) & 0xff]
return [(value >> 24) & 0xff, (value >> 16) & 0xff, (value >> 8) & 0xff, value & 0xff]
}
export const uint32ToNumber = (value: number[]) => {
const view = new DataView(new Uint8Array(value).buffer)
Expand Down Expand Up @@ -141,19 +141,19 @@ export const uint64BigFromHex = (hex: string) => {
export const uint64FromBigInt = (value: bigint) => {
if (value < 0n || 0xffffffffffffffffn < value) rollback('uint64FromBigInt: value out of range', -1)
return [
Number(value & 0xffn),
Number((value >> 8n) & 0xffn),
Number((value >> 16n) & 0xffn),
Number((value >> 24n) & 0xffn),
Number((value >> 32n) & 0xffn),
Number((value >> 40n) & 0xffn),
Number((value >> 48n) & 0xffn),
Number((value >> 56n) & 0xffn),
Number((value >> 48n) & 0xffn),
Number((value >> 40n) & 0xffn),
Number((value >> 32n) & 0xffn),
Number((value >> 24n) & 0xffn),
Number((value >> 16n) & 0xffn),
Number((value >> 8n) & 0xffn),
Number(value & 0xffn),
]
}
export const uint64ToBigInt = (value: number[]) => {
const view = new DataView(new Uint8Array(value).buffer)
return Number(view.getBigUint64(0, true))
return Number(view.getBigUint64(0))
}

type FieldType = 'uint8' | 'uint16' | 'uint32' | 'uint64' | 'account' | 'hash256'
Expand Down

0 comments on commit 99c3fd9

Please sign in to comment.