Skip to content

Commit

Permalink
Properly encode u/i128 numbers in TypeScript generator (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored Jun 14, 2023
1 parent d11aa74 commit 2bb866b
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 16 deletions.
9 changes: 5 additions & 4 deletions soroban-spec/fixtures/ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
"@stellar/freighter-api": "1.4.0",
"buffer": "6.0.3",
"soroban-client": "0.8.0",
"bigint-conversion": "2.4.1"
"bigint-conversion": "2.4.1",
"stellar-base": "9.0.0-soroban.3"
},
"scripts": {
"build": "node ./scripts/build.mjs"
},
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"typings": "dist/types/index.d.ts",
"devDependencies": {
"typescript": "5.0.4"
}
}
}
14 changes: 14 additions & 0 deletions soroban-spec/fixtures/ts/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,17 @@ export function addressToScVal(addr: string): xdr.ScVal {
let addrObj = Address.fromString(addr);
return addrObj.toScVal();
}

export function i128ToScVal(i: bigint): xdr.ScVal {
return xdr.ScVal.scvI128(new xdr.Int128Parts({
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
}))
}

export function u128ToScVal(i: bigint): xdr.ScVal {
return xdr.ScVal.scvU128(new xdr.UInt128Parts({
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
}))
}
2 changes: 1 addition & 1 deletion soroban-spec/fixtures/ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as SorobanClient from 'soroban-client';
import { xdr } from 'soroban-client';
import { Buffer } from "buffer";
import { scValStrToJs, scValToJs, addressToScVal } from './convert.js';
import { scValStrToJs, scValToJs, addressToScVal, u128ToScVal, i128ToScVal } from './convert.js';
import { invoke, InvokeArgs } from './invoke.js';

declare const Errors: { message: string }[]
Expand Down
9 changes: 5 additions & 4 deletions soroban-spec/src/gen/typescript/project_template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@
"@stellar/freighter-api": "1.4.0",
"buffer": "6.0.3",
"soroban-client": "0.8.0",
"bigint-conversion": "2.4.1"
"bigint-conversion": "2.4.1",
"stellar-base": "9.0.0-soroban.3"
},
"scripts": {
"build": "node ./scripts/build.mjs"
},
"exports": {
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
"require": "./dist/cjs/index.js",
"import": "./dist/esm/index.js"
},
"typings": "dist/types/index.d.ts",
"devDependencies": {
"typescript": "5.0.4"
}
}
}
14 changes: 14 additions & 0 deletions soroban-spec/src/gen/typescript/project_template/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,17 @@ export function addressToScVal(addr: string): xdr.ScVal {
let addrObj = Address.fromString(addr);
return addrObj.toScVal();
}

export function i128ToScVal(i: bigint): xdr.ScVal {
return xdr.ScVal.scvI128(new xdr.Int128Parts({
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
}))
}

export function u128ToScVal(i: bigint): xdr.ScVal {
return xdr.ScVal.scvU128(new xdr.UInt128Parts({
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
}))
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as SorobanClient from 'soroban-client';
import { xdr } from 'soroban-client';
import { Buffer } from "buffer";
import { scValStrToJs, scValToJs, addressToScVal } from './convert.js';
import { scValStrToJs, scValToJs, addressToScVal, u128ToScVal, i128ToScVal } from './convert.js';
import { invoke, InvokeArgs } from './invoke.js';

declare const Errors: { message: string }[]
Expand Down
8 changes: 2 additions & 6 deletions soroban-spec/src/gen/typescript/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,8 @@ pub fn type_to_js_xdr(value: &types::Type) -> String {
types::Type::Bytes => "xdr.ScVal.scvBytes(i)".to_owned(),
types::Type::Address => "addressToScVal(i)".to_owned(),
types::Type::Void => "xdr.ScVal.scvVoid()".to_owned(),
types::Type::U128 => {
"xdr.ScVal.scvU128(xdr.Int128Parts.fromXDR(i.toString(16), 'hex'))".to_owned()
}
types::Type::I128 => {
"xdr.ScVal.scvI128(xdr.Int128Parts.fromXDR(i.toString(16), 'hex'))".to_owned()
}
types::Type::U128 => "u128ToScVal(i)".to_owned(),
types::Type::I128 => "i128ToScVal(i)".to_owned(),
types::Type::I256 => todo!(),
types::Type::U256 => todo!(),
types::Type::String => "xdr.ScVal.scvString(i)".to_owned(),
Expand Down

0 comments on commit 2bb866b

Please sign in to comment.