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

New soroban xdr updates #566

Merged
merged 3 commits into from
Dec 8, 2022
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stellar-base",
"version": "8.0.1-soroban.5",
"version": "8.0.1-soroban.6",
"description": "Low level stellar support library",
"main": "./lib/index.js",
"types": "./types/index.d.ts",
Expand Down
19 changes: 10 additions & 9 deletions src/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Contract {
// TODO: Figure out contract owner/id stuff here. How should we represent that?
constructor(contractId) {
// TODO: Add methods based on the contractSpec (or do that elsewhere?)
this._id = contractId;
this._id = Buffer.from(contractId, 'hex');
}

/**
Expand All @@ -27,7 +27,7 @@ export class Contract {
* @returns {string}
*/
contractId() {
return this._id;
return this._id.toString('hex');
}

/**
Expand All @@ -36,21 +36,22 @@ export class Contract {
* @returns {xdr.Operation} Build a InvokeHostFunctionOp operation to call the contract.
*/
call(method, ...params) {
const contractId = Buffer.from(this._id, 'hex');
return Operation.invokeHostFunction({
function: xdr.HostFunction.hostFunctionTypeInvokeContract([
xdr.ScVal.scvObject(
xdr.ScObject.scoBytes(Buffer.from(this._id, 'hex'))
),
xdr.ScVal.scvObject(xdr.ScObject.scoBytes(contractId)),
xdr.ScVal.scvSymbol(method),
...params
]),
// TODO: Figure out how to calculate this or get it from the user?
footprint: new xdr.LedgerFootprint({
readOnly: [
new xdr.LedgerKeyContractData({
contractId: this._id,
key: xdr.ScVal.scvStatic(xdr.ScStatic.scsLedgerKeyContractCode())
})
xdr.LedgerKey.contractData(
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
new xdr.LedgerKeyContractData({
contractId,
key: xdr.ScVal.scvStatic(xdr.ScStatic.scsLedgerKeyContractCode())
})
)
],
readWrite: []
})
Expand Down
1 change: 0 additions & 1 deletion src/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ export class Operation {
case 'invokeHostFunction': {
result.type = 'invokeHostFunction';
result.function = attrs.function();
result.parameters = attrs.parameters();
result.footprint = attrs.footprint();
break;
}
Expand Down
18 changes: 11 additions & 7 deletions test/unit/contract_test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
describe('Contract.call', function() {
it('includes the contract code footprint', function() {
let contractId = '00000000000000000000000000000001';
let contractId =
'0000000000000000000000000000000000000000000000000000000000000001';
let contract = new StellarBase.Contract(contractId);
expect(contract.contractId()).to.equal(contractId);
let call = contract.call('foo');
let op = call.body().invokeHostFunctionOp();
let readOnly = op.footprint().readOnly();
expect(readOnly.length).to.equal(1);
let expected = new StellarBase.xdr.LedgerKeyContractData({
contractId,
key: StellarBase.xdr.ScVal.scvStatic(
StellarBase.xdr.ScStatic.scsLedgerKeyContractCode()
)
})
let expected = new StellarBase.xdr.LedgerKey.contractData(
new StellarBase.xdr.LedgerKeyContractData({
contractId: Buffer.from(contractId, 'hex'),
key: StellarBase.xdr.ScVal.scvStatic(
StellarBase.xdr.ScStatic.scsLedgerKeyContractCode()
)
})
)
.toXDR()
.toString('base64');
expect(readOnly[0].toXDR().toString('base64')).to.equal(expected);
Expand Down