# Deploy contract with init arguments

**URL:** <https://forum.flow.com/t/deploy-contract-with-init-arguments/729>\
**Category:** 🏄🏻‍♀️ Cadence\
**Created:** [January 4, 2021, 3:47pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729 "2021-01-04T15:47:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [January 4, 2021, 3:47pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/1 "2021-01-04T15:47:29Z")

</div>

Assuming we have a smart contract that requires some parameters at initialisation:

```auto
pub contract TestContract{
  ...
  init(a: Int8, b Int8) {
    ...
  }
}

```

This contract can be deployed by using the cadence `AuthAccount.contracts.add` method described here [https://docs.onflow.org/cadence/language/contracts/#deploying-a-new-contract](https://docs.onflow.org/cadence/language/contracts/#deploying-a-new-contract)

```auto
fun add(
    name: String,
    code: [UInt8],
    ... contractInitializerArguments
): DeployedContract

```

Using the contractInitializerArgument.

Currently to do this through the skds you have to write your own create account transaction. The following is an example how that might look like.

The default template to deploy a contract in _flow-go-sdk_ (and similarly in _flow-js-skd_) looks like this ([https://github.com/onflow/flow-go-sdk/blob/9cd830cd9e3be79f3ad5c3a8d7872165766f658b/templates/accounts.go#L49](https://github.com/onflow/flow-go-sdk/blob/9cd830cd9e3be79f3ad5c3a8d7872165766f658b/templates/accounts.go#L49)).

```auto
transaction(publicKeys: [String], contracts: {String: String}) {
	prepare(signer: AuthAccount) {
		let acct = AuthAccount(payer: signer)
		for key in publicKeys {
			acct.addPublicKey(key.decodeHex())
		}
		for contract in contracts.keys {
			acct.contracts.add(name: contract, code: contracts[contract]!.decodeHex())
		}
	}
}

```

Taking this as a starting point and assuming we are only going to deploy one contract we can write the transaction to deploy our _TestContract_ like this:

```auto
transaction(publicKeys: [String], contractName: String, contractCode: String, a: Int8, b: Int8) {
	prepare(signer: AuthAccount) {
		let acct = AuthAccount(payer: signer)
		for key in publicKeys {
			acct.addPublicKey(key.decodeHex())
		}
		acct.contracts.add(name: contractName, code: contractCode.decodeHex(), a, b)
	}
}

```

---

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [January 4, 2021, 3:55pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/2 "2021-01-04T15:55:40Z")

</div>

I should also note that a similar approach can be used if you already have an account but then the contract deployment transaction would look like:

```auto
transaction(contractName: String, contractCode: String, a: Int8, b: Int8) {
	prepare(signer: AuthAccount) {
		signer.contracts.add(name: contractName, code: contractCode.decodeHex(), a, b)
	}
}

```

---

<div class="post-metadata">

**Author:** ![stwh](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@stwh](https://forum.flow.com/u/stwh)\
**Post date:** [January 4, 2021, 4:03pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/3 "2021-01-04T16:03:28Z")

</div>

It is very clear. Thanks.  
I have another question about passing the argument into a script.  
I have 2 argument with type UFix64, which alphabet should I use instead of %d?  
` let minter <- self.tokenAdmin.createNewMinter(allowedAmount: %d) let mintedVault <- minter.mintTokens(amount: %d)`

---

<div class="post-metadata">

**Author:** ![stwh](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@stwh](https://forum.flow.com/u/stwh)\
**Post date:** [January 4, 2021, 4:10pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/4 "2021-01-04T16:10:04Z")

</div>

And also what is the proper way to pass argument into a transaction?

```
	transaction(recipient: Address, amount: UFix64) {
		let tokenAdmin: &FlowToken.Administrator
		let tokenReceiver: &{FungibleToken.Receiver}
	
		prepare(signer: AuthAccount) {
			self.tokenAdmin = signer
			.borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin) 
			?? panic("Signer is not the token admin")
	
			self.tokenReceiver = getAccount(0x%s)
			.getCapability(/public/flowTokenReceiver)!
			.borrow<&{FungibleToken.Receiver}>()
			?? panic("Unable to borrow receiver reference")
		}
		execute {
			let minter <- self.tokenAdmin.createNewMinter(allowedAmount: %d)
			let mintedVault <- minter.mintTokens(amount: %d)
	
			self.tokenReceiver.deposit(from: <-mintedVault)
	
			destroy minter
		}
	}
```

---

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [January 4, 2021, 4:16pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/5 "2021-01-04T16:16:23Z")

</div>

I assume you are trying to do this:

```auto
	transaction(recipient: Address, amount: UFix64) {
		let tokenAdmin: &FlowToken.Administrator
		let tokenReceiver: &{FungibleToken.Receiver}
	
		prepare(signer: AuthAccount) {
			self.tokenAdmin = signer
			.borrow<&FlowToken.Administrator>(from: /storage/flowTokenAdmin) 
			?? panic("Signer is not the token admin")
	
			self.tokenReceiver = getAccount(recipient)
			.getCapability(/public/flowTokenReceiver)!
			.borrow<&{FungibleToken.Receiver}>()
			?? panic("Unable to borrow receiver reference")
		}
		execute {
			let minter <- self.tokenAdmin.createNewMinter(allowedAmount: amount)
			let mintedVault <- minter.mintTokens(amount: amount)
	
			self.tokenReceiver.deposit(from: <-mintedVault)
	
			destroy minter
		}
	}

```

as for how to pass arguments to transactions, here is a flow-go-sdk example: [https://github.com/onflow/flow-go-sdk/blob/9cd830cd9e3be79f3ad5c3a8d7872165766f658b/examples/transaction\_arguments/main.go#L38](https://github.com/onflow/flow-go-sdk/blob/9cd830cd9e3be79f3ad5c3a8d7872165766f658b/examples/transaction_arguments/main.go#L38)  
and here is the flow-js-sdk example: [https://github.com/onflow/flow-js-sdk/blob/master/docs/examples/introducing-fcl/Quickstart.md#transaction-inputs](https://github.com/onflow/flow-js-sdk/blob/master/docs/examples/introducing-fcl/Quickstart.md#transaction-inputs)

---

<div class="post-metadata">

**Author:** ![stwh](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@stwh](https://forum.flow.com/u/stwh)\
**Post date:** [January 4, 2021, 4:21pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/6 "2021-01-04T16:21:39Z")

</div>

so if i have 2 arguments, i need to add twice and in a sequence?  
eg.

err = tx.AddArgument(recipient)  
err = tx.AddArgument(amount)

---

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [January 4, 2021, 4:22pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/7 "2021-01-04T16:22:12Z")

</div>

Yes, and the order matters.

---

<div class="post-metadata">

**Author:** ![stwh](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@stwh](https://forum.flow.com/u/stwh)\
**Post date:** [January 4, 2021, 4:52pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/8 "2021-01-04T16:52:45Z")

</div>

![image](https://us1.discourse-cdn.com/flex022/uploads/flow/original/1X/bb0dc43232a14d0460d2e8a105f47f31ddcc4ef5.png)  
i have run this script and look like the argument problem has been solved. But the emulator come out with this error, I am not clear about this error. I set  
`amount,_:= cadence.NewUFix64("50000")`  
before adding the argument to the transaction.

---

<div class="post-metadata">

**Author:** ![TheOneSock](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.flow.com/theonesock/32/185_2.png) [@TheOneSock](https://forum.flow.com/u/TheOneSock)\
**Post date:** [January 4, 2021, 4:59pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/9 "2021-01-04T16:59:32Z")

</div>

`amount, err := cadence.NewUFix64("50000")` you are missing this error. The value string should have a decimal dot “5000.0”.

---

<div class="post-metadata">

**Author:** ![stwh](https://avatars.discourse-cdn.com/v4/letter/s/a9adbd/32.png) [@stwh](https://forum.flow.com/u/stwh)\
**Post date:** [January 4, 2021, 5:17pm UTC](https://forum.flow.com/t/deploy-contract-with-init-arguments/729/10 "2021-01-04T17:17:28Z")

</div>

Oh, thanks for your reminder. Now everything looks good. 😀
