Skip to content

Commit

Permalink
Remove approve field from SignExecuteContractMethodParams<T
Browse files Browse the repository at this point in the history
  • Loading branch information
h0ngcha0 committed Apr 19, 2024
1 parent 800c526 commit b091366
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
30 changes: 15 additions & 15 deletions packages/web3/src/contract/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1778,10 +1778,6 @@ export interface SignExecuteContractMethodParams<T extends Arguments = Arguments
signer: SignerProvider
attoAlphAmount?: Number256
tokens?: Token[]
approve?: {
attoAlphAmount?: Number256
tokens?: Token[]
}
gasAmount?: number
gasPrice?: Number256
}
Expand Down Expand Up @@ -2297,7 +2293,13 @@ export async function signExecuteMethod<I extends ContractInstance, F extends Fi
const methodIndex = contract.contract.getMethodIndex(methodName)
const functionSig = contract.contract.functions[methodIndex]

const bytecodeTemplate = getBytecodeTemplate(methodIndex, functionSig, contract.contract.structs, params.approve)
const bytecodeTemplate = getBytecodeTemplate(
methodIndex,
functionSig,
contract.contract.structs,
params.attoAlphAmount,
params.tokens
)

const fieldsSig = toFieldsSig(contract.contract.name, functionSig)
const bytecode = ralph.buildScriptByteCode(
Expand Down Expand Up @@ -2326,10 +2328,8 @@ function getBytecodeTemplate(
methodIndex: number,
functionSig: FunctionSig,
structs: Struct[],
approve?: {
attoAlphAmount?: Number256
tokens?: Token[]
}
attoAlphAmount?: Number256,
tokens?: Token[]
): string {
// For the default TxScript main function
const numberOfMethods = '01'
Expand All @@ -2340,8 +2340,10 @@ function getBytecodeTemplate(

const [templateVarStoreLocalInstrs, templateVarsLength] = getTemplateVarStoreLocalInstrs(functionSig, structs)

const approveAlphInstrs: string[] = getApproveAlphInstrs(approve?.attoAlphAmount)
const approveTokensInstrs: string[] = getApproveTokensInstrs(approve?.tokens)
const approveAlphInstrs: string[] = getApproveAlphInstrs(
functionSig.usePreapprovedAssets ? attoAlphAmount : undefined
)
const approveTokensInstrs: string[] = getApproveTokensInstrs(functionSig.usePreapprovedAssets ? tokens : undefined)
const callerInstrs: string[] = getCallAddressInstrs(approveAlphInstrs.length / 2 + approveTokensInstrs.length / 3)

// First template var is the contract
Expand Down Expand Up @@ -2407,10 +2409,8 @@ function getApproveTokensInstrs(tokens?: Token[]): string[] {
const approveTokensInstrs: string[] = []
if (tokens) {
tokens.forEach((token) => {
const tokenId = byteStringCodec.encodeBuffer(Buffer.from(token.id, 'hex'))
const tokenAmount = encodeU256Const(BigInt(token.amount))
approveTokensInstrs.push('14' + tokenId.toString('hex'))
approveTokensInstrs.push(tokenAmount)
approveTokensInstrs.push('14' + byteStringCodec.encodeBuffer(Buffer.from(token.id, 'hex')).toString('hex'))
approveTokensInstrs.push(encodeU256Const(BigInt(token.amount)))
approveTokensInstrs.push(encodeInstr(ApproveToken))
})
}
Expand Down
6 changes: 1 addition & 5 deletions test/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -609,11 +609,7 @@ describe('contract', function () {
},
signer,
attoAlphAmount: ONE_ALPH * 2n,
tokens: [{ id: sub.contractInstance.contractId, amount: 200n }],
approve: {
attoAlphAmount: ONE_ALPH,
tokens: [{ id: sub.contractInstance.contractId, amount: 200n }]
}
tokens: [{ id: sub.contractInstance.contractId, amount: 200n }]
})

const afterBalances = await signer.nodeProvider.addresses.getAddressesAddressBalance(signerAddress)
Expand Down

0 comments on commit b091366

Please sign in to comment.