Skip to content

Commit

Permalink
feat: upgrade molecule types, unit precision changed
Browse files Browse the repository at this point in the history
Why: godwokenrises/godwoken#675 change the
precision of units.

How: Adjust the molecule types

New types definition: https://github.com/zeroqn/godwoken/blob/e5add779d0f5a5b460e3154859880fab8906f21f/crates/types/schemas/godwoken.mol

```
moleculec --language - --schema-file godwoken.mol  --format json
moleculec-es -inputFile 1.json -outputFile generated.js -generateTypeScriptDefinition
./node_modules/.bin/babel --plugins @babel/plugin-transform-modules-commonjs src/schema/generated.js
```

Tags:
  • Loading branch information
keroro520 committed May 10, 2022
1 parent 09332f0 commit f84fa41
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/cli/src/schema/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export class RawWithdrawalRequest {
getAccountScriptHash(): Byte32;
getRegistryId(): Uint32;
getOwnerLockHash(): Byte32;
getFee(): Uint64;
getFee(): Uint128;
}

export function SerializeWithdrawalRequestVec(
Expand Down Expand Up @@ -365,7 +365,7 @@ export class Fee {
validate(compatible?: boolean): void;
static size(): Number;
getRegistryId(): Uint32;
getAmount(): Uint64;
getAmount(): Uint128;
}

export function SerializeCreateAccount(value: object): ArrayBuffer;
Expand Down Expand Up @@ -396,7 +396,7 @@ export class SUDTTransfer {
constructor(reader: CanCastToArrayBuffer, options?: CreateOptions);
validate(compatible?: boolean): void;
getToAddress(): Bytes;
getAmount(): Uint128;
getAmount(): Uint256;
getFee(): Fee;
}

Expand Down
26 changes: 13 additions & 13 deletions packages/cli/src/schema/generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -2000,7 +2000,7 @@ class RawWithdrawalRequest {
}

getFee() {
return new Uint64(
return new Uint128(
this.view.buffer.slice(
0 +
Uint32.size() +
Expand All @@ -2020,7 +2020,7 @@ class RawWithdrawalRequest {
Byte32.size() +
Uint32.size() +
Byte32.size() +
Uint64.size()
Uint128.size()
),
{
validate: false,
Expand Down Expand Up @@ -2052,7 +2052,7 @@ class RawWithdrawalRequest {
Byte32.size() +
Uint32.size() +
Byte32.size() +
Uint64.size()
Uint128.size()
);
}
}
Expand All @@ -2070,7 +2070,7 @@ function SerializeRawWithdrawalRequest(value) {
Byte32.size() +
Uint32.size() +
Byte32.size() +
Uint64.size()
Uint128.size()
);
const view = new DataView(array.buffer);
array.set(new Uint8Array(SerializeUint32(value.nonce)), 0);
Expand Down Expand Up @@ -2118,7 +2118,7 @@ function SerializeRawWithdrawalRequest(value) {
Uint32.size()
);
array.set(
new Uint8Array(SerializeUint64(value.fee)),
new Uint8Array(SerializeUint128(value.fee)),
0 +
Uint32.size() +
Uint64.size() +
Expand Down Expand Up @@ -2990,10 +2990,10 @@ class Fee {
}

getAmount() {
return new Uint64(
return new Uint128(
this.view.buffer.slice(
0 + Uint32.size(),
0 + Uint32.size() + Uint64.size()
0 + Uint32.size() + Uint128.size()
),
{
validate: false,
Expand All @@ -3008,17 +3008,17 @@ class Fee {
}

static size() {
return 0 + Uint32.size() + Uint64.size();
return 0 + Uint32.size() + Uint128.size();
}
}

exports.Fee = Fee;

function SerializeFee(value) {
const array = new Uint8Array(0 + Uint32.size() + Uint64.size());
const array = new Uint8Array(0 + Uint32.size() + Uint128.size());
const view = new DataView(array.buffer);
array.set(new Uint8Array(SerializeUint32(value.registry_id)), 0);
array.set(new Uint8Array(SerializeUint64(value.amount)), 0 + Uint32.size());
array.set(new Uint8Array(SerializeUint128(value.amount)), 0 + Uint32.size());
return array.buffer;
}

Expand Down Expand Up @@ -3213,7 +3213,7 @@ class SUDTTransfer {
new Bytes(this.view.buffer.slice(offsets[0], offsets[1]), {
validate: false,
}).validate();
new Uint128(this.view.buffer.slice(offsets[1], offsets[2]), {
new Uint256(this.view.buffer.slice(offsets[1], offsets[2]), {
validate: false,
}).validate();
new Fee(this.view.buffer.slice(offsets[2], offsets[3]), {
Expand All @@ -3234,7 +3234,7 @@ class SUDTTransfer {
const start = 8;
const offset = this.view.getUint32(start, true);
const offset_end = this.view.getUint32(start + 4, true);
return new Uint128(this.view.buffer.slice(offset, offset_end), {
return new Uint256(this.view.buffer.slice(offset, offset_end), {
validate: false,
});
}
Expand All @@ -3254,7 +3254,7 @@ exports.SUDTTransfer = SUDTTransfer;
function SerializeSUDTTransfer(value) {
const buffers = [];
buffers.push(SerializeBytes(value.to_address));
buffers.push(SerializeUint128(value.amount));
buffers.push(SerializeUint256(value.amount));
buffers.push(SerializeFee(value.fee));
return serializeTable(buffers);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/schema/godwoken.mol
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ struct RawWithdrawalRequest {
// layer1 lock to withdraw after challenge period
owner_lock_hash: Byte32,
// withdrawal fee, paid to block producer
fee: Uint64,
fee: Uint128,
}

vector WithdrawalRequestVec <WithdrawalRequest>;
Expand Down Expand Up @@ -242,7 +242,7 @@ struct Fee {
// registry id
registry_id: Uint32,
// amount in CKB
amount: Uint64,
amount: Uint128,
}

table CreateAccount {
Expand All @@ -265,7 +265,7 @@ table SUDTQuery {
table SUDTTransfer {
// Godwoken registry address: (registry_id (4 bytes) | address len(4 bytes) | address)
to_address: Bytes,
amount: Uint128,
amount: Uint256,
// paid fee(ckb)
fee: Fee,
}
Expand Down
23 changes: 4 additions & 19 deletions packages/cli/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface RawWithdrawalRequest {
// uint32
registry_id: HexNumber;
owner_lock_hash: Hash;
// uint64
// uint128
fee: HexNumber;
}

Expand Down Expand Up @@ -96,7 +96,7 @@ export class WithdrawalRequestExtraCodec {
account_script_hash: normalizers.normalizeRawData(32),
registry_id: normalizers.normalizeHexNumber(4),
owner_lock_hash: normalizers.normalizeRawData(32),
fee: normalizers.normalizeHexNumber(8),
fee: normalizers.normalizeHexNumber(16),
})
),
signature: normalizers.normalizeRawData(65),
Expand Down Expand Up @@ -132,7 +132,7 @@ export class WithdrawalRequestCodec {
account_script_hash: normalizers.normalizeRawData(32),
registry_id: normalizers.normalizeHexNumber(4),
owner_lock_hash: normalizers.normalizeRawData(32),
fee: normalizers.normalizeHexNumber(8),
fee: normalizers.normalizeHexNumber(16),
})
),
signature: normalizers.normalizeRawData(65),
Expand Down Expand Up @@ -163,26 +163,11 @@ export class RawWithdrawalRequestCodec {
account_script_hash: normalizers.normalizeRawData(32),
registry_id: normalizers.normalizeHexNumber(4),
owner_lock_hash: normalizers.normalizeRawData(32),
fee: normalizers.normalizeHexNumber(8),
fee: normalizers.normalizeHexNumber(16),
}
);
return Reader.from(
molecule.SerializeRawWithdrawalRequest(normalized)
).serializeJson();
}
}

export class FeeCodec {
readonly fee__: Fee;
constructor(fee: Fee) {
this.fee__ = fee;
}

HexSerialize(): HexString {
const normalized = normalizers.normalizeObject("Fee", this.fee__, {
registry_id: normalizers.normalizeHexNumber(4),
amount: normalizers.normalizeHexNumber(8),
});
return Reader.from(molecule.SerializeFee(normalized)).serializeJson();
}
}

0 comments on commit f84fa41

Please sign in to comment.