From 1b7576ad84806475aa488012e47232e44457eba8 Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Tue, 9 Jan 2024 11:35:24 -0500 Subject: [PATCH 1/3] Add native bigint support for select fields in JS --- scripts/generate_typescript.sh | 2 +- typescript_templates/algod_config.properties | 41 ++++++++++++ .../indexer_config.properties | 64 +++++++++++++++++++ typescript_templates/model.vm | 49 +++++++++++--- .../parameter_order_overrides.properties | 10 --- 5 files changed, 145 insertions(+), 21 deletions(-) delete mode 100644 typescript_templates/parameter_order_overrides.properties diff --git a/scripts/generate_typescript.sh b/scripts/generate_typescript.sh index d4b4fb8..becab8a 100755 --- a/scripts/generate_typescript.sh +++ b/scripts/generate_typescript.sh @@ -85,7 +85,7 @@ java -jar target/generator-*-jar-with-dependencies.jar \ -s "$ALGOD_SPEC" \ -t "$TEMPLATE_DIR" \ -m "$SDK_DIR/src/client/v2/algod/models" \ - -p "$TEMPLATE_DIR/algod_config.properties,$TEMPLATE_DIR/parameter_order_overrides.properties" \ + -p "$TEMPLATE_DIR/algod_config.properties" \ java -jar target/generator-*-jar-with-dependencies.jar \ template \ diff --git a/typescript_templates/algod_config.properties b/typescript_templates/algod_config.properties index 338a772..c64feed 100644 --- a/typescript_templates/algod_config.properties +++ b/typescript_templates/algod_config.properties @@ -7,3 +7,44 @@ # changes. use_object_params_if_greater_than=0 indexer=false +type_override_Account_amount=bigint +type_override_Account_amountWithoutPendingRewards=bigint +type_override_Account_minBalance=bigint +type_override_Account_pendingRewards=bigint +type_override_Account_rewards=bigint +type_override_Account_round=bigint +type_override_Account_rewardBase=bigint +type_override_AccountApplicationResponse_round=bigint +type_override_AccountAssetResponse_round=bigint +type_override_AccountParticipation_voteFirstValid=bigint +type_override_AccountParticipation_voteKeyDilution=bigint +type_override_AccountParticipation_voteLastValid=bigint +type_override_Application_id=bigint +type_override_ApplicationLocalState_id=bigint +type_override_Asset_index=bigint +type_override_AssetHolding_assetId=bigint +type_override_Box_round=bigint +type_override_BoxReference_app=bigint +type_override_GetSyncRoundResponse_round=bigint +type_override_NodeStatusResponse_catchupTime=bigint +type_override_NodeStatusResponse_lastRound=bigint +type_override_NodeStatusResponse_nextVersionRound=bigint +type_override_NodeStatusResponse_timeSinceLastRound=bigint +type_override_NodeStatusResponse_upgradeDelay=bigint +type_override_NodeStatusResponse_upgradeNextProtocolVoteBefore=bigint +type_override_PendingTransactionResponse_applicationIndex=bigint +type_override_PendingTransactionResponse_assetClosingAmount=bigint +type_override_PendingTransactionResponse_assetIndex=bigint +type_override_PendingTransactionResponse_closeRewards=bigint +type_override_PendingTransactionResponse_closingAmount=bigint +type_override_PendingTransactionResponse_confirmedRound=bigint +type_override_PendingTransactionResponse_receiverRewards=bigint +type_override_PendingTransactionResponse_senderRewards=bigint +type_override_SimulateRequest_round=bigint +type_override_SimulateResponse_lastRound=bigint +type_override_SupplyResponse_currentRound=bigint +type_override_SupplyResponse_onlineMoney=bigint +type_override_SupplyResponse_totalMoney=bigint +type_override_TransactionParametersResponse_fee=bigint +type_override_TransactionParametersResponse_lastRound=bigint +type_override_TransactionParametersResponse_minFee=bigint diff --git a/typescript_templates/indexer_config.properties b/typescript_templates/indexer_config.properties index 2a0d48c..c558fc1 100644 --- a/typescript_templates/indexer_config.properties +++ b/typescript_templates/indexer_config.properties @@ -1,2 +1,66 @@ use_object_params_if_greater_than=0 indexer=true +type_override_Account_amount=bigint +type_override_Account_amountWithoutPendingRewards=bigint +type_override_Account_pendingRewards=bigint +type_override_Account_rewards=bigint +type_override_Account_round=bigint +type_override_Account_rewardBase=bigint +type_override_AccountParticipation_voteFirstValid=bigint +type_override_AccountParticipation_voteKeyDilution=bigint +type_override_AccountParticipation_voteLastValid=bigint +type_override_AccountResponse_currentRound=bigint +type_override_AccountsResponse_currentRound=bigint +type_override_Application_id=bigint +type_override_ApplicationLocalState_id=bigint +type_override_ApplicationLocalStatesResponse_currentRound=bigint +type_override_ApplicationLogsResponse_applicationId=bigint +type_override_ApplicationLogsResponse_currentRound=bigint +type_override_ApplicationResponse_currentRound=bigint +type_override_ApplicationsResponse_currentRound=bigint +type_override_Asset_index=bigint +type_override_AssetBalancesResponse_currentRound=bigint +type_override_AssetHolding_assetId=bigint +type_override_AssetHoldingsResponse_currentRound=bigint +type_override_AssetResponse_currentRound=bigint +type_override_AssetsResponse_currentRound=bigint +type_override_Block_round=bigint +type_override_BlockRewards_rewardsCalculationRound=bigint +type_override_BlockRewards_rewardsLevel=bigint +type_override_BlockRewards_rewardsRate=bigint +type_override_BlockRewards_rewardsResidue=bigint +type_override_BlockUpgradeState_nextProtocolSwitchOn=bigint +type_override_BlockUpgradeState_nextProtocolVoteBefore=bigint +type_override_BlockUpgradeVote_upgradeDelay=bigint +type_override_Box_round=bigint +type_override_BoxesResponse_applicationId=bigint +type_override_HealthCheck_round=bigint +type_override_StateProofTracking_nextRound=bigint +type_override_StateProofTracking_onlineTotalWeight=bigint +type_override_StateProofTracking_type=number +type_override_StateSchema_numByteSlice=number +type_override_StateSchema_numUint=number +type_override_Transaction_fee=bigint +type_override_Transaction_firstValid=bigint +type_override_Transaction_lastValid=bigint +type_override_Transaction_closeRewards=bigint +type_override_Transaction_closingAmount=bigint +type_override_Transaction_confirmedRound=bigint +type_override_Transaction_createdApplicationIndex=bigint +type_override_Transaction_createdAssetIndex=bigint +type_override_Transaction_receiverRewards=bigint +type_override_Transaction_senderRewards=bigint +type_override_TransactionApplication_applicationId=bigint +type_override_TransactionApplication_foreignApps=bigint +type_override_TransactionApplication_foreignAssets=bigint +type_override_TransactionAssetConfig_assetId=bigint +type_override_TransactionAssetFreeze_assetId=bigint +type_override_TransactionAssetTransfer_assetId=bigint +type_override_TransactionKeyreg_voteFirstValid=bigint +type_override_TransactionKeyreg_voteKeyDilution=bigint +type_override_TransactionKeyreg_voteLastValid=bigint +type_override_TransactionPayment_amount=bigint +type_override_TransactionPayment_closeAmount=bigint +type_override_TransactionResponse_currentRound=bigint +type_override_TransactionStateProof_stateProofType=number +type_override_TransactionsResponse_currentRound=bigint diff --git a/typescript_templates/model.vm b/typescript_templates/model.vm index f5390f5..cc348fd 100644 --- a/typescript_templates/model.vm +++ b/typescript_templates/model.vm @@ -3,19 +3,31 @@ $str.kebabToCamel($param.propertyName.replaceAll("_", "-"))## #end ## Converts a parameter type into the SDK specific type. -#macro ( toSdkType $param $isArgType ) +#macro ( toSdkType $className $param $isArgType ) +#set( $d = "$" ) +#set( $e = "!" ) +#set( $type_override_variable = "${d}${e}propFile.type_override_${className}_#paramName(${param})" ) +#set( $type_override = "#evaluate($type_override_variable)" ) #if ( $param.algorandFormat == "SignedTransaction" ) EncodedSignedTransaction## #elseif ( $param.algorandFormat == "Address" ) ## No special handling for Address in go SDK string## #elseif ( $param.algorandFormat == "BlockHeader" ) BlockHeader## -#elseif ( $param.algorandFormat == "uint64" ) +#elseif ( $type_override == "bigint" || ( $param.algorandFormat == "uint64" && $type_override.length() == 0 ) ) +#if ( $isArgType ) (number | bigint)## +#else +bigint## +#end #elseif ( $param.type == "object" ) Record## -#elseif ( $param.type == "integer" || $param.arrayType == "integer" ) +#elseif ( $type_override == "number" || ( ( $param.type == "integer" || $param.arrayType == "integer" ) && $type_override.length() == 0 ) ) +#if ( $isArgType ) (number | bigint)## +#else +number## +#end #elseif ( $param.type == "boolean" ) boolean## #elseif( $param.type == "address" ) @@ -78,14 +90,30 @@ true## #end #end ## Create an expression to assign a field in a constructor -#macro ( constructorAssignType $prop ) -#set( $argType = "#toSdkType($prop, true)" ) -#set( $fieldType = "#toSdkType($prop, false)" ) +#macro ( constructorAssignType $className $prop ) +#set( $argType = "#toSdkType($className, $prop, true)" ) +#set( $fieldType = "#toSdkType($className, $prop, false)" ) #set( $name = "#paramName($prop)" ) #if ( $argType == $fieldType ) $name## #elseif ( $argType == "string | Uint8Array" && $fieldType == "Uint8Array" ) typeof $name === 'string' ? base64ToBytes($name) : $name## +#elseif ( $argType == "(number | bigint)" && $fieldType == "bigint" ) +#if ( $prop.required ) +ensureBigInt($name)## +#else +typeof $name === 'undefined' ? undefined : ensureBigInt($name)## +#end +#elseif ( $argType == "(number | bigint)[]" && $fieldType == "bigint[]" ) +${name}.map(ensureBigInt)## +#elseif ( $argType == "(number | bigint)" && $fieldType == "number" ) +#if ( $prop.required ) +ensureSafeInteger($name)## +#else +typeof $name === 'undefined' ? undefined : ensureSafeInteger($name)## +#end +#elseif ( $argType == "(number | bigint)[]" && $fieldType == "number[]" ) +${name}.map(ensureSafeInteger)## #else UNHANDLED CONSTRUCTOR TYPE CONVERSION - property: $prop @@ -129,6 +157,7 @@ typeof $value !== 'undefined' ? $assignment : undefined## */ /* eslint-disable no-use-before-define */ +import { ensureBigInt, ensureSafeInteger } from '../../../../utils/utils.js'; import { base64ToBytes } from '../../../../encoding/binarydata.js'; #if ( $propFile.indexer == "false" ) import BlockHeader from '../../../../types/blockHeader.js'; @@ -160,7 +189,7 @@ export class $def.name extends BaseModel { * $str.formatDoc($prop.doc, " * ") */ #end - public #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($prop, false); + public #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($def.name, $prop, false); #end /** @@ -178,13 +207,13 @@ export class $def.name extends BaseModel { #if ($use_object_params) #paramName($prop), #else - #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($prop, true), + #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($def.name, $prop, true), #end #end #if ($use_object_params) }: { #foreach ( $prop in $props ) - #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($prop, true) + #paramName($prop)#questionMarkIfOptional($prop): #toSdkType($def.name, $prop, true) #end }) { @@ -194,7 +223,7 @@ export class $def.name extends BaseModel { super(); #foreach( $prop in $props ) #set( $var = "#paramName($prop)" ) - this.$var = #constructorAssignType($prop); + this.$var = #constructorAssignType($def.name, $prop); #end this.attribute_map = { diff --git a/typescript_templates/parameter_order_overrides.properties b/typescript_templates/parameter_order_overrides.properties deleted file mode 100644 index a1c8aff..0000000 --- a/typescript_templates/parameter_order_overrides.properties +++ /dev/null @@ -1,10 +0,0 @@ -# List of the new order for each respective override -override_DryrunSource_order = field-name,source,txn-index,app-index -override_Application_order = id,params -override_ApplicationStateSchema_order = num-uint,num-byte-slice -override_ApplicationLocalStates_order = id,state -override_ApplicationLocalState_order = id,schema,key-value -override_TealKeyValue_order = key,value -override_TealValue_order = type,bytes,uint -override_AssetHolding_order = amount,asset-id,is-frozen -override_Asset_order = index,params From ecdc276368c047ba744df9d28c1cd8b35d86232a Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Fri, 12 Jan 2024 16:42:33 -0500 Subject: [PATCH 2/3] Properly handle optional int arrays --- typescript_templates/model.vm | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/typescript_templates/model.vm b/typescript_templates/model.vm index cc348fd..4c761ce 100644 --- a/typescript_templates/model.vm +++ b/typescript_templates/model.vm @@ -105,7 +105,11 @@ ensureBigInt($name)## typeof $name === 'undefined' ? undefined : ensureBigInt($name)## #end #elseif ( $argType == "(number | bigint)[]" && $fieldType == "bigint[]" ) +#if ( $prop.required ) ${name}.map(ensureBigInt)## +#else +typeof ${name} === 'undefined' ? undefined : ${name}.map(ensureBigInt)## +#end #elseif ( $argType == "(number | bigint)" && $fieldType == "number" ) #if ( $prop.required ) ensureSafeInteger($name)## @@ -113,8 +117,12 @@ ensureSafeInteger($name)## typeof $name === 'undefined' ? undefined : ensureSafeInteger($name)## #end #elseif ( $argType == "(number | bigint)[]" && $fieldType == "number[]" ) +#if ( $prop.required ) ${name}.map(ensureSafeInteger)## #else +typeof ${name} === 'undefined' ? undefined : ${name}.map(ensureSafeInteger)## +#end +#else UNHANDLED CONSTRUCTOR TYPE CONVERSION - property: $prop - propertyName: $name From 4b0120a3850e3fd58c0412e767e4f59f684c27ef Mon Sep 17 00:00:00 2001 From: Jason Paulos Date: Wed, 14 Feb 2024 11:40:36 -0500 Subject: [PATCH 3/3] Add docs for type overrides --- typescript_templates/algod_config.properties | 10 ++++++++++ typescript_templates/indexer_config.properties | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/typescript_templates/algod_config.properties b/typescript_templates/algod_config.properties index c64feed..b38df6d 100644 --- a/typescript_templates/algod_config.properties +++ b/typescript_templates/algod_config.properties @@ -7,6 +7,16 @@ # changes. use_object_params_if_greater_than=0 indexer=false +# Type override conventions: +# * All Algo and asset amounts should be bigint (including fees) +# * All round numbers should be bigint, including any quantities you would add +# to a round number (e.g. offset or duration) +# * All application and asset IDs should be bigint +# * All timestamps in nanoseconds should be bigint +# * If there is a quantity whose purpose is to be added to, subtracted from, or +# otherwise operated on with a bigint, it should be a bigint for ease of use +# * Other quantities outside of the above and which will never exceed the maximum +# safe javascript integer should be number type_override_Account_amount=bigint type_override_Account_amountWithoutPendingRewards=bigint type_override_Account_minBalance=bigint diff --git a/typescript_templates/indexer_config.properties b/typescript_templates/indexer_config.properties index c558fc1..ceaf708 100644 --- a/typescript_templates/indexer_config.properties +++ b/typescript_templates/indexer_config.properties @@ -1,5 +1,15 @@ use_object_params_if_greater_than=0 indexer=true +# Type override conventions: +# * All Algo and asset amounts should be bigint (including fees) +# * All round numbers should be bigint, including any quantities you would add +# to a round number (e.g. offset or duration) +# * All application and asset IDs should be bigint +# * All timestamps in nanoseconds should be bigint +# * If there is a quantity whose purpose is to be added to, subtracted from, or +# otherwise operated on with a bigint, it should be a bigint for ease of use +# * Other quantities outside of the above and which will never exceed the maximum +# safe javascript integer should be number type_override_Account_amount=bigint type_override_Account_amountWithoutPendingRewards=bigint type_override_Account_pendingRewards=bigint