-
Notifications
You must be signed in to change notification settings - Fork 137
/
contract.js
87 lines (81 loc) · 2.55 KB
/
contract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Address } from './address';
import { Operation } from './operation';
import xdr from './xdr';
import { StrKey } from './strkey';
/**
* Create a new Contract object.
*
* `Contract` represents a single contract in the Stellar network, embodying the
* interface of the contract. See
* [Contracts](https://soroban.stellar.org/docs/learn/interacting-with-contracts)
* for more information about how contracts work in Stellar.
*
* @constructor
*
* @param {string} contractId - ID of the contract (ex.
* `CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE`).
*/
export class Contract {
constructor(contractId) {
try {
// First, try it as a strkey
this._id = StrKey.decodeContract(contractId);
} catch (_) {
throw new Error(`Invalid contract ID: ${contractId}`);
}
}
/**
* Returns Stellar contract ID as a strkey, ex.
* `CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE`.
* @returns {string}
*/
contractId() {
return StrKey.encodeContract(this._id);
}
/** @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);
}
/**
* Returns an operation that will invoke this contract call.
*
* @param {string} method name of the method to call
* @param {...xdr.ScVal} params arguments to pass to the function call
*
* @returns {xdr.Operation} an InvokeHostFunctionOp operation to call the
* contract with the given method and parameters
*
* @see Operation.invokeHostFunction
* @see Operation.invokeContractFunction
* @see Operation.createCustomContract
* @see Operation.createStellarAssetContract
* @see Operation.uploadContractWasm
*/
call(method, ...params) {
return Operation.invokeContractFunction({
contract: this.address().toString(),
function: method,
args: params
});
}
/**
* Returns the read-only footprint entries necessary for any invocations to
* this contract, for convenience when manually adding it to your
* transaction's overall footprint or doing bump/restore operations.
*
* @returns {xdr.LedgerKey} the ledger key for the deployed contract instance
*/
getFootprint() {
return xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contract: this.address().toScAddress(),
key: xdr.ScVal.scvLedgerKeyContractInstance(),
durability: xdr.ContractDataDurability.persistent()
})
);
}
}