Skip to content

Commit

Permalink
Expand contract footprint with contract code ledger key. (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic authored Aug 10, 2023
1 parent e36abc0 commit 9d54b36
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 34 deletions.
43 changes: 27 additions & 16 deletions src/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export class Contract {
return StrKey.encodeContract(this._id);
}

/**
* Returns the address of this contract as an Address type.
* @returns {Address}
*/
/** @returns {string} the ID as a strkey (C...) */
toString() {
return this.contractId();
}

/** @returns {Address} the wrapped address of this contract */
address() {
return Address.contract(this._id);
}
Expand All @@ -64,20 +66,29 @@ export class Contract {
}

/**
* Returns the read-only footprint entry necessary for any invocations to this
* contract, for convenience when adding it to your transaction's overall
* footprint.
* Returns the read-only footprint entries necessary for any invocations to
* this contract, for convenience when adding it to your transaction's overall
* footprint or doing bump/restore operations.
*
* @returns {xdr.LedgerKey} the contract's executable data ledger key
* @returns {xdr.LedgerKey[]} the ledger keys containing the contract's code
* (first) and its deployed contract instance (second)
*/
getFootprint() {
return xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: this.address().toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
);
return [
xdr.LedgerKey.contractCode(
new xdr.LedgerKeyContractCode({
hash: this._id,
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
),
xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: this.address().toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
)
];
}
}
40 changes: 24 additions & 16 deletions test/unit/contract_test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const xdr = StellarBase.xdr;

const NULL_ADDRESS = 'CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM';

describe('Contract', function () {
Expand Down Expand Up @@ -30,30 +32,36 @@ describe('Contract', function () {
});

describe('getFootprint', function () {
it('includes the correct contract code footprint', function () {
it('includes the correct contract ledger keys', function () {
let contract = new StellarBase.Contract(NULL_ADDRESS);
expect(contract.contractId()).to.equal(NULL_ADDRESS);

const fp = contract.getFootprint();
const actual = contract.getFootprint();
const expected = [
new xdr.LedgerKey.contractCode(
new xdr.LedgerKeyContractCode({
hash: StellarBase.StrKey.decodeContract(contract.contractId()),
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
),
new xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: contract.address().toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent(),
bodyType: xdr.ContractEntryBodyType.dataEntry()
})
)
];

let expected = new StellarBase.xdr.LedgerKey.contractData(
new StellarBase.xdr.LedgerKeyContractData({
contract: contract.address().toScAddress(),
key: StellarBase.xdr.ScVal.scvLedgerKeyContractInstance(),
durability: StellarBase.xdr.ContractDataDurability.persistent(),
bodyType: StellarBase.xdr.ContractEntryBodyType.dataEntry()
})
)
.toXDR()
.toString('base64');
expect(fp.toXDR().toString('base64')).to.equal(expected);
expect(actual).to.eql(expected);
});
});

describe('call', function () {
let call = new StellarBase.Contract(NULL_ADDRESS).call(
'method',
StellarBase.xdr.ScVal.scvU32(123)
xdr.ScVal.scvU32(123)
);
let args = call
.body()
Expand All @@ -68,11 +76,11 @@ describe('Contract', function () {
});

it('passes the method name as the second arg', function () {
expect(args[1]).to.deep.equal(StellarBase.xdr.ScVal.scvSymbol('method'));
expect(args[1]).to.deep.equal(xdr.ScVal.scvSymbol('method'));
});

it('passes all params after that', function () {
expect(args[2]).to.deep.equal(StellarBase.xdr.ScVal.scvU32(123));
expect(args[2]).to.deep.equal(xdr.ScVal.scvU32(123));
});
});
});
6 changes: 4 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ export class Address {

export class Contract {
constructor(contractId: string);
call(method: string, ...params: xdr.ScVal[]): xdr.Operation<Operation.InvokeHostFunction>;
contractId(): string;
address(): Address;
call(method: string, ...params: xdr.ScVal[]): xdr.Operation<Operation.InvokeHostFunction>;
getFootprint(): xdr.LedgerKey;
getFootprint(): xdr.LedgerKey[];

toString(): string;
}

export class MuxedAccount {
Expand Down

0 comments on commit 9d54b36

Please sign in to comment.