-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9111414
commit 97f0676
Showing
19 changed files
with
745 additions
and
712 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
export * from "./initializeToolkit"; | ||
export * from "./networks"; | ||
export * from "./sorobanToolkit"; | ||
// Config | ||
export * from "./config/loader"; | ||
export * from "./config/defaultNetworks"; | ||
export * from "./config/toolkit"; | ||
|
||
// Utils | ||
export * from "./utils/accountUtils"; | ||
|
||
// Managers | ||
export * from "./managers/contract"; | ||
export * from "./managers/transaction"; | ||
export * from "./managers/token"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { | ||
Address, | ||
Contract, | ||
Keypair, | ||
Operation, | ||
StrKey, | ||
hash, | ||
xdr, | ||
} from "@stellar/stellar-sdk"; | ||
import { SorobanToolkit } from "../config/toolkit"; | ||
import { resolvePath } from "../utils/utils"; | ||
import { createTransaction, createTransactionBuilder, sendTransaction } from "./transaction"; | ||
import { randomBytes } from "crypto"; | ||
|
||
export async function installContract( | ||
toolkit: SorobanToolkit, | ||
contractKey: string, | ||
customBuffer?: Buffer, | ||
source?: Keypair | ||
) { | ||
let contractWasm, wasmHash; | ||
|
||
if(!customBuffer) { | ||
const wasmPath = toolkit.getContractPath(contractKey); | ||
contractWasm = resolvePath(wasmPath); | ||
wasmHash = hash(contractWasm); | ||
} else { | ||
wasmHash = hash(customBuffer); | ||
contractWasm = customBuffer; | ||
} | ||
|
||
toolkit.logVerbose("full", `Installing contract: ${contractKey}`); | ||
toolkit.logVerbose("full", `WASM hash: ${wasmHash.toString("hex")}`); | ||
|
||
toolkit.addressBook.setWasmHash(contractKey, wasmHash.toString("hex")); | ||
toolkit.addressBook.writeToFile(); | ||
|
||
const op = Operation.invokeHostFunction({ | ||
func: xdr.HostFunction.hostFunctionTypeUploadContractWasm(contractWasm), | ||
auth: [], | ||
}); | ||
|
||
await createTransaction(toolkit, op, false, source); | ||
} | ||
|
||
export async function deployContract( | ||
toolkit: SorobanToolkit, | ||
contractKey: string, | ||
args: xdr.ScVal[], | ||
source?: Keypair, | ||
) { | ||
const contractIdSalt = randomBytes(32); | ||
const networkId = hash(Buffer.from(toolkit.passphrase)); | ||
|
||
const contractIdPreimage = xdr.ContractIdPreimage.contractIdPreimageFromAddress( | ||
new xdr.ContractIdPreimageFromAddress({ | ||
address: Address.fromString(source?.publicKey() ?? toolkit.admin.publicKey()).toScAddress(), | ||
salt: contractIdSalt, | ||
}) | ||
); | ||
|
||
const hashIdPreimage = xdr.HashIdPreimage.envelopeTypeContractId( | ||
new xdr.HashIdPreimageContractId({ | ||
networkId, | ||
contractIdPreimage, | ||
}) | ||
); | ||
|
||
const contractId = StrKey.encodeContract(hash(hashIdPreimage.toXDR())); | ||
toolkit.addressBook.setContractId(contractKey, contractId); | ||
toolkit.addressBook.writeToFile(); | ||
|
||
const wasmHash = Buffer.from(toolkit.addressBook.getWasmHash(contractKey), "hex"); | ||
|
||
toolkit.logVerbose("some", `Deploying contract: ${contractKey}`); | ||
const deployOp = Operation.invokeHostFunction({ | ||
func: xdr.HostFunction.hostFunctionTypeCreateContractV2( | ||
new xdr.CreateContractArgsV2({ | ||
contractIdPreimage, | ||
executable: xdr.ContractExecutable.contractExecutableWasm(wasmHash), | ||
constructorArgs: args, | ||
}) | ||
), | ||
auth: [], | ||
}); | ||
|
||
await createTransaction(toolkit, deployOp, false, source); | ||
|
||
return contractId; | ||
} | ||
|
||
export async function invokeContract( | ||
toolkit: SorobanToolkit, | ||
contractKey: string, | ||
method: string, | ||
params: xdr.ScVal[], | ||
simulate: boolean = false, | ||
source?: Keypair, | ||
) { | ||
const contractId = toolkit.addressBook.getContractId(contractKey); | ||
const contract = new Contract(contractId); | ||
|
||
const operation = contract.call(method, ...params); | ||
|
||
toolkit.logVerbose("some", `Invoking contract ${contractKey}: ${method}`); | ||
return await createTransaction(toolkit, operation, simulate, source); | ||
} | ||
|
||
export async function invokeCustomContract( | ||
toolkit: SorobanToolkit, | ||
contractId: string, | ||
method: string, | ||
params: xdr.ScVal[], | ||
simulate: boolean = false, | ||
source?: Keypair, | ||
) { | ||
const contract = new Contract(contractId); | ||
|
||
const operation = contract.call(method, ...params); | ||
|
||
toolkit.logVerbose("some", `Invoking contract ${contractId}: ${method}`); | ||
return await createTransaction(toolkit, operation, simulate, source); | ||
} | ||
|
||
export async function bumpContractInstance( | ||
toolkit: SorobanToolkit, | ||
contractId: string, | ||
source?: Keypair | ||
) { | ||
const address = Address.fromString(contractId); | ||
toolkit.logVerbose("some", `Bumping contract instance: ${contractId}`); | ||
const contractInstanceXDR = xdr.LedgerKey.contractData( | ||
new xdr.LedgerKeyContractData({ | ||
contract: address.toScAddress(), | ||
key: xdr.ScVal.scvLedgerKeyContractInstance(), | ||
durability: xdr.ContractDataDurability.persistent(), | ||
}) | ||
); | ||
const bumpTransactionData = new xdr.SorobanTransactionData({ | ||
resources: new xdr.SorobanResources({ | ||
footprint: new xdr.LedgerFootprint({ | ||
readOnly: [contractInstanceXDR], | ||
readWrite: [], | ||
}), | ||
instructions: 0, | ||
readBytes: 0, | ||
writeBytes: 0, | ||
}), | ||
resourceFee: xdr.Int64.fromString("0"), | ||
// @ts-ignore | ||
ext: new xdr.ExtensionPoint(0), | ||
}); | ||
|
||
const txBuilder = await createTransactionBuilder(toolkit, source); | ||
txBuilder.addOperation(Operation.extendFootprintTtl({ extendTo: 535670 })); // 1 year | ||
txBuilder.setSorobanData(bumpTransactionData); | ||
const result = await sendTransaction(toolkit, txBuilder.build(), false, source); | ||
return result; | ||
} | ||
|
||
export async function bumpContractCode( | ||
toolkit: SorobanToolkit, | ||
wasmHash: string, | ||
source?: Keypair | ||
) { | ||
const wasmHashBuffer = Buffer.from(wasmHash, "hex"); | ||
const contractCodeXDR = xdr.LedgerKey.contractCode( | ||
new xdr.LedgerKeyContractCode({ | ||
hash: wasmHashBuffer, | ||
}) | ||
); | ||
const bumpTransactionData = new xdr.SorobanTransactionData({ | ||
resources: new xdr.SorobanResources({ | ||
footprint: new xdr.LedgerFootprint({ | ||
readOnly: [contractCodeXDR], | ||
readWrite: [], | ||
}), | ||
instructions: 0, | ||
readBytes: 0, | ||
writeBytes: 0, | ||
}), | ||
resourceFee: xdr.Int64.fromString("0"), | ||
// @ts-ignore | ||
ext: new xdr.ExtensionPoint(0), | ||
}); | ||
|
||
const txBuilder = await createTransactionBuilder(toolkit, source); | ||
txBuilder.addOperation(Operation.extendFootprintTtl({ extendTo: 535670 })); // 1 year | ||
txBuilder.setSorobanData(bumpTransactionData); | ||
const result = await sendTransaction(toolkit, txBuilder.build(), false, source); | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Address, Keypair, nativeToScVal, xdr } from "@stellar/stellar-sdk"; | ||
import { resolveInternalPath } from "../utils/utils"; | ||
import { SorobanToolkit } from "../config/toolkit"; | ||
import { deployContract, installContract } from "./contract"; | ||
|
||
/** | ||
* Deploy the Soroban Token Contract. | ||
* @param toolkit - The SorobanToolkit instance. | ||
* @param source - The Keypair to use as the source for the transaction. | ||
* @returns The WASM hash of the deployed token contract. | ||
*/ | ||
export async function deploySorobanToken( | ||
toolkit: SorobanToolkit, | ||
name: string, | ||
symbol: string, | ||
decimals: number, | ||
source?: Keypair | ||
) { | ||
toolkit.logVerbose("some", `Deploying Token: ${name} ${symbol}`); | ||
const wasmKey = "soroban_token"; | ||
const wasmBuffer = resolveInternalPath("./soroban_token.wasm"); | ||
|
||
// Check if the WASM hash is already stored in the AddressBook | ||
try { | ||
const existingWasmHash = toolkit.addressBook.getWasmHash(wasmKey); | ||
if (existingWasmHash) { | ||
toolkit.logVerbose("full", `WASM is already installed`); | ||
} | ||
} catch { | ||
// WASM not found in AddressBook, proceed with installation | ||
toolkit.logVerbose("full", "WASM not found in AddressBook, proceeding with installation"); | ||
await installContract(toolkit, wasmKey, wasmBuffer, source); | ||
} | ||
|
||
const args: xdr.ScVal[] = [ | ||
new Address(source?.publicKey() ?? toolkit.admin.publicKey()).toScVal(), | ||
nativeToScVal(decimals, {type: "u32"}), | ||
nativeToScVal(name, {type: "string"}), | ||
nativeToScVal(symbol, {type: "string"}), | ||
]; | ||
|
||
return await deployContract(toolkit, wasmKey, args, source); | ||
} |
Oops, something went wrong.