Skip to content

Commit

Permalink
feat(contracts): contractInvoke improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
abstract829 committed Nov 10, 2023
1 parent 49f7136 commit 7462a96
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions packages/contracts/src/contractInvoke.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
import { SorobanContextType } from '@soroban-react/core';
import { SorobanContextType } from '@soroban-react/core'

import * as SorobanClient from 'soroban-client';
import { SorobanRpc } from 'soroban-client';
import * as SorobanClient from 'soroban-client'
import { SorobanRpc } from 'soroban-client'

import { contractTransaction } from './contractTransaction';
import { signAndSendTransaction } from './transaction';
import { TxResponse } from './types';
import { contractTransaction } from './contractTransaction'
import { signAndSendTransaction } from './transaction'
import { TxResponse } from './types'

let xdr = SorobanClient.xdr;
let xdr = SorobanClient.xdr

export type InvokeArgs = {
contractAddress: string;
method: string;
args?: SorobanClient.xdr.ScVal[] | undefined;
signAndSend?: boolean;
fee?: number;
skipAddingFootprint?: boolean;
secretKey?: string;
sorobanContext: SorobanContextType;
reconnectAfterTx?: boolean;
};
contractAddress: string
method: string
args?: SorobanClient.xdr.ScVal[] | undefined
signAndSend?: boolean
fee?: number
skipAddingFootprint?: boolean
secretKey?: string
sorobanContext: SorobanContextType
reconnectAfterTx?: boolean
}

// Dummy source account for simulation. The public key for this is all 0-bytes.
const defaultAddress = 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF';
const defaultAddress =
'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF'

export async function contractInvoke({
contractAddress,
Expand All @@ -35,30 +36,34 @@ export async function contractInvoke({
sorobanContext,
reconnectAfterTx = true,
}: InvokeArgs): Promise<TxResponse | SorobanClient.xdr.ScVal> {
const { server, address, activeChain } = sorobanContext;
const { server, address, activeChain } = sorobanContext

if (!activeChain) {
throw new Error('No active Chain');
throw new Error('No active Chain')
}
if (!server) {
throw new Error('No connected to a Server');
throw new Error('No connected to a Server')
}
if (signAndSend && !secretKey && !sorobanContext.activeConnector) {
throw new Error(
'contractInvoke: You are trying to sign a txn without providing a source, secretKey or active connector',
);
'contractInvoke: You are trying to sign a txn without providing a source, secretKey or active connector'
)
}

const networkPassphrase = activeChain?.networkPassphrase;
let source = null;
const networkPassphrase = activeChain?.networkPassphrase
let source = null

if (secretKey) {
source = await server.getAccount(SorobanClient.Keypair.fromSecret(secretKey).publicKey());
source = await server.getAccount(
SorobanClient.Keypair.fromSecret(secretKey).publicKey()
)
} else {
try {
source = await server?.getAccount(address);
if (!address) throw new Error('No address')

source = await server.getAccount(address)
} catch (error) {
source = new SorobanClient.Account(defaultAddress, '0');
source = new SorobanClient.Account(defaultAddress, '0')
}
}

Expand All @@ -69,31 +74,32 @@ export async function contractInvoke({
contractAddress,
method,
args,
});
})

const simulated: SorobanRpc.SimulateTransactionResponse = await server?.simulateTransaction(txn);
const simulated: SorobanRpc.SimulateTransactionResponse =
await server?.simulateTransaction(txn)

if (SorobanRpc.isSimulationError(simulated)) {
throw new Error(simulated.error);
throw new Error(simulated.error)
} else if (!simulated.result) {
throw new Error(`invalid simulation: no result in ${simulated}`);
throw new Error(`invalid simulation: no result in ${simulated}`)
}

if (!signAndSend && simulated) {
return simulated.result.retval;
return simulated.result.retval
} else {
// If signAndSend
const res = await signAndSendTransaction({
txn,
skipAddingFootprint,
secretKey,
sorobanContext,
});
})

if (reconnectAfterTx) {
sorobanContext.connect();
sorobanContext.connect()
}

return res;
return res
}
}

0 comments on commit 7462a96

Please sign in to comment.