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

[ts-sdk] Allow enums with no value, add a test for TransactionData BCS #1484

Merged
merged 20 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
added support for u128
  • Loading branch information
damirka committed Apr 19, 2022
commit 53973d8bd070fd5f27b4643a891d4a2b88c0488d
53 changes: 35 additions & 18 deletions sdk/typescript/src/bcs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class BcsReader {
}
/**
* Read U8 value from the buffer and shift cursor by 1.
* @returns {BN}
* @returns
*/
read8(): BN {
let value = this.dataView.getUint8(this.bytePosition);
Expand All @@ -72,7 +72,7 @@ export class BcsReader {
}
/**
* Read U16 value from the buffer and shift cursor by 2.
* @returns {BN}
* @returns
*/
read16(): BN {
let value = this.dataView.getUint16(this.bytePosition, true);
Expand All @@ -81,7 +81,7 @@ export class BcsReader {
}
/**
* Read U32 value from the buffer and shift cursor by 4.
* @returns {BN}
* @returns
*/
read32(): BN {
let value = this.dataView.getUint32(this.bytePosition, true);
Expand All @@ -90,7 +90,7 @@ export class BcsReader {
}
/**
* Read U64 value from the buffer and shift cursor by 8.
* @returns {BN}
* @returns
*/
read64(): BN {
let value1 = this.read32();
Expand All @@ -101,7 +101,7 @@ export class BcsReader {
}
/**
* Read U128 value from the buffer and shift cursor by 16.
* @returns {BN}
* @returns
*/
read128(): BN {
let value1 = this.read64();
Expand All @@ -112,8 +112,8 @@ export class BcsReader {
}
/**
* Read `num` number of bytes from the buffer and shift cursor by `num`.
* @param {Number} num Number of bytes to read.
* @returns {Uint8Array} Selected Buffer.
* @param num Number of bytes to read.
* @returns Selected Buffer.
*/
readBytes(num: number): Uint8Array {
let start = this.bytePosition + this.dataView.byteOffset;
Expand Down Expand Up @@ -192,36 +192,39 @@ export class BcsWriter {
* @param {Number} value Value to write.
* @returns {this}
*/
write8(value: number): this {
this.dataView.setUint8(this.bytePosition, value);
write8(value: number | bigint | BN): this {
this.dataView.setUint8(this.bytePosition, +this.toBN(value));
return this.shift(1);
}
/**
* Write a U16 value into a buffer and shift cursor position by 2.
* @param {Number} value Value to write.
* @returns {this}
*/
write16(value: number): this {
this.dataView.setUint16(this.bytePosition, value, true);
write16(value: number | bigint | BN): this {
this.dataView.setUint16(this.bytePosition, +this.toBN(value), true);
return this.shift(2);
}
/**
* Write a U32 value into a buffer and shift cursor position by 4.
* @param {Number} value Value to write.
* @returns {this}
*/
write32(value: number): this {
this.dataView.setUint32(this.bytePosition, value, true);
write32(value: number | bigint | BN): this {
this.dataView.setUint32(this.bytePosition, +this.toBN(value), true);
return this.shift(4);
}
/**
* Write a U64 value into a buffer and shift cursor position by 8.
* @param {bigint} value Value to write.
* @returns {this}
*/
write64(value: bigint): this {
this.dataView.setBigUint64(this.bytePosition, value, true);
return this.shift(8);
write64(value: bigint | BN): this {
this.toBN(value)
.toArray('le', 8)
.forEach((el) => this.write8(el));

return this;
}
/**
* Write a U128 value into a buffer and shift cursor position by 16.
Expand All @@ -230,8 +233,12 @@ export class BcsWriter {
* @param {bigint} value Value to write.
* @returns {this}
*/
write128(_value: bigint): this {
throw 'Writing u128 values is not yet implemented';
write128(value: bigint | BN): this {
this.toBN(value)
.toArray('le', 16)
.forEach((el) => this.write8(el));

return this;
}
/**
* Write a ULEB value into a buffer and shift cursor position by number of bytes
Expand Down Expand Up @@ -263,6 +270,16 @@ export class BcsWriter {
toBytes() {
return new Uint8Array(this.dataView.buffer.slice(0, this.bytePosition));
}

/**
* Unify argument types by converting them to BN.
*/
toBN(number: number | BN | bigint | string): BN {
switch (typeof number) {
case 'bigint': return new BN.BN(number.toString());
default: return new BN.BN(number);
}
}
}

// Helper utility: write number as an ULEB array.
Expand Down
17 changes: 17 additions & 0 deletions sdk/typescript/test/bcs/bcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ describe('Move BCS', () => {
expect(BCS.de('u8', new B64('AA==').getData())).toEqual(new BN(0));;
});

it('should ser/de u64', () => {
const exp = 'AO/Nq3hWNBI=';
const num = BigInt('1311768467750121216');
const ser = BCS.ser('u64', num).toBytes();

expect(new B64(ser).toString()).toEqual(exp);
expect(BCS.de('u64', new B64(exp).getData())).toEqual(new BN('1311768467750121216'));
});

it('should ser/de u128', () => {
const sample = new B64('AO9ld3CFjD48AAAAAAAAAA==');
const num = BigInt('1111311768467750121216');

expect(BCS.de('u128', sample.getData()).toString(10)).toEqual('1111311768467750121216');
expect(new B64(BCS.ser('u128', num).toBytes()).toString()).toEqual(sample.toString())
});

it('should de/ser custom objects', () => {
BCS.registerStructType('Coin', {
value: BCS.U64,
Expand Down