Skip to content

Commit def82f1

Browse files
authored
Add support for buildInvocationTree to parse V2 of creation calls (#784)
* Release v13.0.1
1 parent ca27152 commit def82f1

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
## Unreleased
44

55

6+
## [`v13.0.1`](https://github.com/stellar/js-stellar-base/compare/v13.0.0...v13.0.1)
7+
8+
### Fixed
9+
* `buildInvocationTree` will now successfully walk creation invocations with constructor arguments ([#784](https://github.com/stellar/js-stellar-base/pull/784)).
10+
11+
612
## [`v13.0.0`](https://github.com/stellar/js-stellar-base/compare/v12.1.1...v13.0.0)
713

814
**This release supports Protocol 22.** While the network has not upgraded yet, you can start integrating the new features into your codebase if you want a head start. Keep in mind that while the binary XDR is backwards-compatible, the naming and layout of structures is not. In other words, this build will continue to work on Protocol 21, but you may have to update code that references XDR directly.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stellar/stellar-base",
3-
"version": "13.0.0",
3+
"version": "13.0.1",
44
"description": "Low-level support library for the Stellar network.",
55
"main": "./lib/index.js",
66
"browser": {

src/invocation.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { scValToNative } from './scval';
1515
* @prop {string} wasm.address contract address of this deployment
1616
* @prop {string} wasm.salt hex salt that the user consumed when creating
1717
* this contract (encoded in the resulting address)
18+
* @prop {any[]} [wasm.constructorArgs] a list of natively-represented values
19+
* (see {@link scValToNative}) that are passed to the constructor when
20+
* creating this contract
1821
*/
1922

2023
/**
@@ -23,8 +26,8 @@ import { scValToNative } from './scval';
2326
* @prop {string} source the strkey of the contract (C...) being invoked
2427
* @prop {string} function the name of the function being invoked
2528
* @prop {any[]} args the natively-represented parameters to the function
26-
* invocation (see {@link scValToNative}) for rules on how they're
27-
* represented a JS types
29+
* invocation (see {@link scValToNative} for rules on how they're
30+
* represented a JS types)
2831
*/
2932

3033
/**
@@ -85,7 +88,7 @@ export function buildInvocationTree(root) {
8588
/** @type {InvocationTree} */
8689
const output = {};
8790

88-
/** @type {xdr.CreateContractArgs | xdr.InvokeContractArgs} */
91+
/** @type {xdr.CreateContractArgs|xdr.CreateContractArgsV2|xdr.InvokeContractArgs} */
8992
const inner = fn.value();
9093

9194
switch (fn.switch().value) {
@@ -100,7 +103,10 @@ export function buildInvocationTree(root) {
100103
break;
101104

102105
// sorobanAuthorizedFunctionTypeCreateContractHostFn
103-
case 1: {
106+
// sorobanAuthorizedFunctionTypeCreateContractV2HostFn
107+
case 1: // fallthrough: just no ctor args in V1
108+
case 2: {
109+
const createV2 = fn.switch().value === 2;
104110
output.type = 'create';
105111
output.args = {};
106112

@@ -130,7 +136,13 @@ export function buildInvocationTree(root) {
130136
output.args.wasm = {
131137
salt: details.salt().toString('hex'),
132138
hash: exec.wasmHash().toString('hex'),
133-
address: Address.fromScAddress(details.address()).toString()
139+
address: Address.fromScAddress(details.address()).toString(),
140+
// only apply constructor args for WASM+CreateV2 scenario
141+
...(createV2 && {
142+
constructorArgs: inner
143+
.constructorArgs()
144+
.map((arg) => scValToNative(arg))
145+
}) // empty indicates V2 and no ctor, undefined indicates V1
134146
};
135147
break;
136148
}

test/unit/invocation_test.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const {
3030
// |
3131
// +--- someNft.transfer(invoker, someNft, 2)
3232
// |
33-
// +--- create(custom wasm contract)
33+
// +--- createV2(custom wasm contract)
3434
function rk() {
3535
return Keypair.random().publicKey();
3636
}
@@ -116,15 +116,20 @@ describe('parsing invocation trees', function () {
116116
}),
117117
new xdr.SorobanAuthorizedInvocation({
118118
function:
119-
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeCreateContractHostFn(
120-
new xdr.CreateContractArgs({
119+
xdr.SorobanAuthorizedFunction.sorobanAuthorizedFunctionTypeCreateContractV2HostFn(
120+
new xdr.CreateContractArgsV2({
121121
contractIdPreimage:
122122
xdr.ContractIdPreimage.contractIdPreimageFromAddress(
123123
new xdr.ContractIdPreimageFromAddress({
124124
address: nftContract.address().toScAddress(),
125125
salt: Buffer.alloc(32, 0)
126126
})
127127
),
128+
constructorArgs: [1, '2', 3].map((arg, i) => {
129+
return nativeToScVal(arg, {
130+
type: ['u32', 'string', 'i32'][i]
131+
});
132+
}),
128133
executable: xdr.ContractExecutable.contractExecutableWasm(
129134
Buffer.alloc(32, '\x20')
130135
)
@@ -196,7 +201,8 @@ describe('parsing invocation trees', function () {
196201
wasm: {
197202
salt: '00'.repeat(32),
198203
hash: '20'.repeat(32),
199-
address: nftContract.contractId()
204+
address: nftContract.contractId(),
205+
constructorArgs: [1, '2', 3]
200206
}
201207
},
202208
invocations: []

0 commit comments

Comments
 (0)