Skip to content

Commit 2bb866b

Browse files
authored
Properly encode u/i128 numbers in TypeScript generator (#976)
1 parent d11aa74 commit 2bb866b

7 files changed

Lines changed: 42 additions & 16 deletions

File tree

soroban-spec/fixtures/ts/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
"@stellar/freighter-api": "1.4.0",
66
"buffer": "6.0.3",
77
"soroban-client": "0.8.0",
8-
"bigint-conversion": "2.4.1"
8+
"bigint-conversion": "2.4.1",
9+
"stellar-base": "9.0.0-soroban.3"
910
},
1011
"scripts": {
1112
"build": "node ./scripts/build.mjs"
1213
},
1314
"exports": {
14-
"require": "./dist/cjs/index.js",
15-
"import": "./dist/esm/index.js"
15+
"require": "./dist/cjs/index.js",
16+
"import": "./dist/esm/index.js"
1617
},
1718
"typings": "dist/types/index.d.ts",
1819
"devDependencies": {
1920
"typescript": "5.0.4"
2021
}
21-
}
22+
}

soroban-spec/fixtures/ts/src/convert.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,17 @@ export function addressToScVal(addr: string): xdr.ScVal {
130130
let addrObj = Address.fromString(addr);
131131
return addrObj.toScVal();
132132
}
133+
134+
export function i128ToScVal(i: bigint): xdr.ScVal {
135+
return xdr.ScVal.scvI128(new xdr.Int128Parts({
136+
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
137+
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
138+
}))
139+
}
140+
141+
export function u128ToScVal(i: bigint): xdr.ScVal {
142+
return xdr.ScVal.scvU128(new xdr.UInt128Parts({
143+
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
144+
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
145+
}))
146+
}

soroban-spec/fixtures/ts/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as SorobanClient from 'soroban-client';
22
import { xdr } from 'soroban-client';
33
import { Buffer } from "buffer";
4-
import { scValStrToJs, scValToJs, addressToScVal } from './convert.js';
4+
import { scValStrToJs, scValToJs, addressToScVal, u128ToScVal, i128ToScVal } from './convert.js';
55
import { invoke, InvokeArgs } from './invoke.js';
66

77
declare const Errors: { message: string }[]

soroban-spec/src/gen/typescript/project_template/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@
55
"@stellar/freighter-api": "1.4.0",
66
"buffer": "6.0.3",
77
"soroban-client": "0.8.0",
8-
"bigint-conversion": "2.4.1"
8+
"bigint-conversion": "2.4.1",
9+
"stellar-base": "9.0.0-soroban.3"
910
},
1011
"scripts": {
1112
"build": "node ./scripts/build.mjs"
1213
},
1314
"exports": {
14-
"require": "./dist/cjs/index.js",
15-
"import": "./dist/esm/index.js"
15+
"require": "./dist/cjs/index.js",
16+
"import": "./dist/esm/index.js"
1617
},
1718
"typings": "dist/types/index.d.ts",
1819
"devDependencies": {
1920
"typescript": "5.0.4"
2021
}
21-
}
22+
}

soroban-spec/src/gen/typescript/project_template/src/convert.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,17 @@ export function addressToScVal(addr: string): xdr.ScVal {
130130
let addrObj = Address.fromString(addr);
131131
return addrObj.toScVal();
132132
}
133+
134+
export function i128ToScVal(i: bigint): xdr.ScVal {
135+
return xdr.ScVal.scvI128(new xdr.Int128Parts({
136+
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
137+
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
138+
}))
139+
}
140+
141+
export function u128ToScVal(i: bigint): xdr.ScVal {
142+
return xdr.ScVal.scvU128(new xdr.UInt128Parts({
143+
lo: xdr.Uint64.fromString((i & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
144+
hi: xdr.Int64.fromString(((i >> BigInt(64)) & BigInt(0xFFFFFFFFFFFFFFFFn)).toString()),
145+
}))
146+
}

soroban-spec/src/gen/typescript/project_template/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as SorobanClient from 'soroban-client';
22
import { xdr } from 'soroban-client';
33
import { Buffer } from "buffer";
4-
import { scValStrToJs, scValToJs, addressToScVal } from './convert.js';
4+
import { scValStrToJs, scValToJs, addressToScVal, u128ToScVal, i128ToScVal } from './convert.js';
55
import { invoke, InvokeArgs } from './invoke.js';
66

77
declare const Errors: { message: string }[]

soroban-spec/src/gen/typescript/wrapper.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ pub fn type_to_js_xdr(value: &types::Type) -> String {
4545
types::Type::Bytes => "xdr.ScVal.scvBytes(i)".to_owned(),
4646
types::Type::Address => "addressToScVal(i)".to_owned(),
4747
types::Type::Void => "xdr.ScVal.scvVoid()".to_owned(),
48-
types::Type::U128 => {
49-
"xdr.ScVal.scvU128(xdr.Int128Parts.fromXDR(i.toString(16), 'hex'))".to_owned()
50-
}
51-
types::Type::I128 => {
52-
"xdr.ScVal.scvI128(xdr.Int128Parts.fromXDR(i.toString(16), 'hex'))".to_owned()
53-
}
48+
types::Type::U128 => "u128ToScVal(i)".to_owned(),
49+
types::Type::I128 => "i128ToScVal(i)".to_owned(),
5450
types::Type::I256 => todo!(),
5551
types::Type::U256 => todo!(),
5652
types::Type::String => "xdr.ScVal.scvString(i)".to_owned(),

0 commit comments

Comments
 (0)