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

Fix transaction payload JSON serialization #382

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Yarn stuff
# Yarn stuff
.pnp.*
.yarn/*
!.yarn/patches
Expand Down Expand Up @@ -45,3 +45,5 @@ package-lock.json

# MacOS
.DS_Store

.vscode
1 change: 1 addition & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixed

- Update `@concordium/rust-bindings` to `3.2.1` which fixes an issue causing runtime error `unreachable` for the internal WebAssembly module.
- Update JSON serialization of `AccountTransactionPayload` through `AccountTransactionPayloadHandler` to correctly serialize `CcdAmount` as `string`

## 7.5.0

Expand Down
103 changes: 68 additions & 35 deletions packages/sdk/src/accountTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
ConfigureBakerPayload,
ConfigureDelegationPayload,
DelegationTarget,
DelegationTargetType,
DeployModulePayload,
HexString,
InitContractPayload,
Expand Down Expand Up @@ -84,7 +85,7 @@ export interface AccountTransactionHandler<

export interface SimpleTransferPayloadJSON {
toAddress: Base58String;
amount: bigint;
amount: string;
}

export class SimpleTransferHandler
Expand All @@ -111,15 +112,15 @@ export class SimpleTransferHandler

toJSON(transfer: SimpleTransferPayload): SimpleTransferPayloadJSON {
return {
toAddress: AccountAddress.toBase58(transfer.toAddress),
amount: transfer.amount.microCcdAmount,
toAddress: transfer.toAddress.toJSON(),
amount: transfer.amount.toJSON(),
};
}

fromJSON(json: SimpleTransferPayloadJSON): SimpleTransferPayload {
return {
toAddress: AccountAddress.fromBase58(json.toAddress),
amount: CcdAmount.fromMicroCcd(json.amount),
toAddress: AccountAddress.fromJSON(json.toAddress),
amount: CcdAmount.fromJSON(json.amount),
};
}
}
Expand Down Expand Up @@ -153,17 +154,17 @@ export class SimpleTransferWithMemoHandler

toJSON(transfer: SimpleTransferWithMemoPayload): SimpleTransferWithMemoPayloadJSON {
return {
toAddress: AccountAddress.toBase58(transfer.toAddress),
toAddress: transfer.toAddress.toJSON(),
memo: transfer.memo.toJSON(),
amount: transfer.amount.microCcdAmount,
amount: transfer.amount.toJSON(),
};
}

fromJSON(json: SimpleTransferWithMemoPayloadJSON): SimpleTransferWithMemoPayload {
return {
toAddress: AccountAddress.fromBase58(json.toAddress),
toAddress: AccountAddress.fromJSON(json.toAddress),
memo: DataBlob.fromJSON(json.memo),
amount: CcdAmount.fromMicroCcd(json.amount),
amount: CcdAmount.fromJSON(json.amount),
};
}
}
Expand Down Expand Up @@ -210,13 +211,13 @@ export class DeployModuleHandler implements AccountTransactionHandler<DeployModu
fromJSON(json: DeployModulePayloadJSON): DeployModulePayload {
return {
source: Buffer.from(json.source, 'hex'),
version: json.version,
version: json.version !== undefined ? Number(json.version) : undefined,
};
}
}

export interface InitContractPayloadJSON {
amount: bigint;
amount: string;
moduleRef: HexString;
initName: string;
param: HexString;
Expand Down Expand Up @@ -244,27 +245,27 @@ export class InitContractHandler implements AccountTransactionHandler<InitContra

toJSON(payload: InitContractPayload): InitContractPayloadJSON {
return {
amount: payload.amount.microCcdAmount,
moduleRef: ModuleReference.toHexString(payload.moduleRef),
initName: ContractName.toString(payload.initName),
param: Parameter.toHexString(payload.param),
amount: payload.amount.toJSON(),
moduleRef: payload.moduleRef.toJSON(),
initName: payload.initName.toJSON(),
param: payload.param.toJSON(),
maxContractExecutionEnergy: payload.maxContractExecutionEnergy.value,
};
}

fromJSON(json: InitContractPayloadJSON): InitContractPayload {
return {
amount: CcdAmount.fromMicroCcd(json.amount),
moduleRef: ModuleReference.fromHexString(json.moduleRef),
initName: ContractName.fromString(json.initName),
param: Parameter.fromHexString(json.param),
amount: CcdAmount.fromJSON(json.amount),
moduleRef: ModuleReference.fromJSON(json.moduleRef),
initName: ContractName.fromJSON(json.initName),
param: Parameter.fromJSON(json.param),
maxContractExecutionEnergy: Energy.create(json.maxContractExecutionEnergy),
};
}
}

export interface UpdateContractPayloadJSON {
amount: bigint;
amount: string;
address: ContractAddress.SchemaValue;
receiveName: string;
message: HexString;
Expand Down Expand Up @@ -301,20 +302,20 @@ export class UpdateContractHandler

toJSON(payload: UpdateContractPayload): UpdateContractPayloadJSON {
return {
amount: payload.amount.microCcdAmount,
amount: payload.amount.toJSON(),
address: ContractAddress.toSchemaValue(payload.address),
receiveName: ReceiveName.toString(payload.receiveName),
message: Parameter.toHexString(payload.message),
receiveName: payload.receiveName.toJSON(),
message: payload.message.toJSON(),
maxContractExecutionEnergy: payload.maxContractExecutionEnergy.value,
};
}

fromJSON(json: UpdateContractPayloadJSON): UpdateContractPayload {
return {
amount: CcdAmount.fromMicroCcd(json.amount),
amount: CcdAmount.fromJSON(json.amount),
address: ContractAddress.fromSchemaValue(json.address),
receiveName: ReceiveName.fromString(json.receiveName),
message: Parameter.fromHexString(json.message),
receiveName: ReceiveName.fromJSON(json.receiveName),
message: Parameter.fromJSON(json.message),
maxContractExecutionEnergy: Energy.create(json.maxContractExecutionEnergy),
};
}
Expand Down Expand Up @@ -355,12 +356,27 @@ export class UpdateCredentialsHandler implements AccountTransactionHandler<Updat
}

toJSON(updateCredentials: UpdateCredentialsPayload): UpdateCredentialsPayload {
// UpdateCredentialsPayload is already fully JSON serializable.
return updateCredentials;
}

fromJSON(json: UpdateCredentialsPayload): UpdateCredentialsPayload {
return json;
return {
...json,
currentNumberOfCredentials: BigInt(json.currentNumberOfCredentials),
threshold: Number(json.threshold),
newCredentials: json.newCredentials.map((nc) => ({
index: Number(nc.index),
cdi: {
...nc.cdi,
credentialPublicKeys: {
...nc.cdi.credentialPublicKeys,
threshold: Number(nc.cdi.credentialPublicKeys.threshold),
},
ipIdentity: Number(nc.cdi.ipIdentity),
revocationThreshold: Number(nc.cdi.revocationThreshold),
},
})),
};
}
}

Expand Down Expand Up @@ -399,7 +415,7 @@ export class RegisterDataHandler implements AccountTransactionHandler<RegisterDa
}

export interface ConfigureBakerPayloadJSON {
stake?: bigint;
stake?: string;
restakeEarnings?: boolean;
openForDelegation?: OpenStatus;
keys?: BakerKeysWithProofs;
Expand Down Expand Up @@ -431,20 +447,27 @@ export class ConfigureBakerHandler
toJSON(payload: ConfigureBakerPayload): ConfigureBakerPayloadJSON {
return {
...payload,
stake: payload.stake?.microCcdAmount,
stake: payload.stake?.toJSON(),
};
}

fromJSON(json: ConfigureBakerPayloadJSON): ConfigureBakerPayload {
return {
...json,
stake: json.stake ? CcdAmount.fromMicroCcd(json.stake) : undefined,
stake: json.stake ? CcdAmount.fromJSON(json.stake) : undefined,
openForDelegation: json.openForDelegation !== undefined ? Number(json.openForDelegation) : undefined,
transactionFeeCommission:
json.transactionFeeCommission !== undefined ? Number(json.transactionFeeCommission) : undefined,
bakingRewardCommission:
json.bakingRewardCommission !== undefined ? Number(json.bakingRewardCommission) : undefined,
finalizationRewardCommission:
json.finalizationRewardCommission !== undefined ? Number(json.finalizationRewardCommission) : undefined,
};
}
}

export interface ConfigureDelegationPayloadJSON {
stake?: bigint;
stake?: string;
restakeEarnings?: boolean;
delegationTarget?: DelegationTarget;
}
Expand All @@ -467,15 +490,25 @@ export class ConfigureDelegationHandler
toJSON(payload: ConfigureDelegationPayload): ConfigureDelegationPayloadJSON {
return {
...payload,
stake: payload.stake?.microCcdAmount,
stake: payload.stake?.toJSON(),
};
}

fromJSON(json: ConfigureDelegationPayloadJSON): ConfigureDelegationPayload {
return {
let result: ConfigureDelegationPayload = {
...json,
stake: json.stake ? CcdAmount.fromMicroCcd(json.stake) : undefined,
stake: json.stake ? CcdAmount.fromJSON(json.stake) : undefined,
};

if (
json.delegationTarget === undefined ||
json.delegationTarget.delegateType === DelegationTargetType.PassiveDelegation
) {
return result;
}

result.delegationTarget = { ...json.delegationTarget, bakerId: BigInt(json.delegationTarget.bakerId) };
return result;
}
}

Expand Down
Loading
Loading