-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathinvokeContract.ts
More file actions
102 lines (93 loc) · 3.34 KB
/
invokeContract.ts
File metadata and controls
102 lines (93 loc) · 3.34 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Stellar Invoke Contract Example
*
* This example demonstrates how to use the OpenZeppelin Relayer SDK to invoke contract functions on Stellar.
*
* IMPORTANT: This is provided as a demonstration only. For production use:
* - Use proper error handling and transaction confirmation checks
* - Implement appropriate security measures for API keys and tokens
* - Consider rate limiting and monitoring for production applications
* - Test thoroughly on testnet before using on mainnet
* - Use https connection for production applications
*
* Usage:
* ts-node invokeContract.ts mint # Mint tokens (owner only)
* ts-node invokeContract.ts transfer # Transfer tokens
*/
import {
Configuration,
RelayersApi,
StellarTransactionRequest,
OperationSpecOneOf1TypeEnum,
AuthSpecOneOf1TypeEnum,
} from '../../src';
import { ScVal } from '../../custom-models/stellar-transaction-request';
// example dev config
const config = new Configuration({
basePath: 'http://localhost:8080',
accessToken: '', // replace with your actual api key
});
const relayersApi = new RelayersApi(config);
// replace with your actual ids
const relayer_id = 'stellar-testnet';
// Replace with your actual addresses
const SOURCE_ACCOUNT = 'GCRID3RFJXOBEB73FWRYJJ4II5E5UQ423F7LTM4W5KI54NBHQDRUXVLY';
const RECIPIENT_ADDRESS = 'GCRID3RFJXOBEB73FWRYJJ4II5E5UQ423F7LTM4W5KI54NBHQDRUXVLY';
const CONTRACT_ADDRESS = 'CBC4BKTDLYXSKP76SFWY7IQWWOLF6QSWEMUUEOHBWEXHCSTL6SOIRCRX';
// Helper function to convert string to i128 for token amounts
function stringToI128(value: string): ScVal {
const bigValue = BigInt(value);
const hi = (bigValue >> BigInt(64)).toString();
const lo = (bigValue & ((BigInt(1) << BigInt(64)) - BigInt(1))).toString();
return { i128: { hi, lo } };
}
// Parse command line arguments
const command = process.argv[2] || 'mint';
// Define transactions based on command
let transaction: StellarTransactionRequest;
if (command === 'mint') {
transaction = {
source_account: SOURCE_ACCOUNT,
network: 'testnet',
operations: [
{
type: OperationSpecOneOf1TypeEnum.INVOKE_CONTRACT,
contract_address: CONTRACT_ADDRESS,
function_name: 'mint',
args: [
{ address: RECIPIENT_ADDRESS },
stringToI128('1000000000000000000000'), // 1000 tokens (18 decimals)
],
auth: { type: AuthSpecOneOf1TypeEnum.SOURCE_ACCOUNT },
},
],
};
} else if (command === 'transfer') {
transaction = {
source_account: SOURCE_ACCOUNT,
network: 'testnet',
operations: [
{
type: OperationSpecOneOf1TypeEnum.INVOKE_CONTRACT,
contract_address: CONTRACT_ADDRESS,
function_name: 'transfer',
args: [
{ address: SOURCE_ACCOUNT }, // From
{ address: RECIPIENT_ADDRESS }, // To
stringToI128('100000000000000000000'), // 100 tokens (18 decimals)
],
auth: { type: AuthSpecOneOf1TypeEnum.SOURCE_ACCOUNT },
},
],
};
} else {
console.error(`Unknown command: ${command}`);
console.error('Usage:');
console.error(' ts-node invokeContract.ts mint # Mint tokens (owner only)');
console.error(' ts-node invokeContract.ts transfer # Transfer tokens');
process.exit(1);
}
relayersApi
.sendTransaction(relayer_id, transaction)
.then((response) => console.log(JSON.stringify(response.data, null, 2)))
.catch(console.error);